diff --git a/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift b/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift index 37d2c1e..5f312bc 100644 --- a/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift +++ b/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift @@ -8,6 +8,12 @@ struct GenerateVersionBuildPlugin: BuildToolPlugin { target: PackagePlugin.Target ) async throws -> [PackagePlugin.Command] { guard let target = target as? SourceModuleTarget else { return [] } + + let gitDirectoryPath = target.directory + .removingLastComponent() + .removingLastComponent() + .removingLastComponent() + let tool = try context.tool(named: "git-version") let outputPath = context.pluginWorkDirectory @@ -17,7 +23,7 @@ struct GenerateVersionBuildPlugin: BuildToolPlugin { .buildCommand( displayName: "Build With Version", executable: tool.path, - arguments: ["generate", "--verbose", outputPath.string], + arguments: ["build", "--git-directory", gitDirectoryPath.string, outputPath.string], environment: [:], inputFiles: target.sourceFiles.map(\.path), outputFiles: [outputFile] diff --git a/Sources/git-version/BuildCommand.swift b/Sources/git-version/BuildCommand.swift new file mode 100644 index 0000000..bcca658 --- /dev/null +++ b/Sources/git-version/BuildCommand.swift @@ -0,0 +1,47 @@ +import ArgumentParser +import Foundation +import GitVersion +import ShellClient + +extension GitVersionCommand { + struct Build: ParsableCommand { + static var configuration: CommandConfiguration = .init( + abstract: "Used for the build with version plugin." + ) + +// @OptionGroup var shared: SharedOptions + + @Argument(help: "The output file path.") + var outputPath: String + + @Option( + name: .customLong("git-directory"), + help: "The git directory for the version." + ) + var gitDirectory: String? = nil + + func run() throws { + try withDependencies { + $0.logger.logLevel = .debug + $0.fileClient = .liveValue + $0.gitVersionClient = .liveValue + } operation: { + @Dependency(\.gitVersionClient) var gitVersion + @Dependency(\.fileClient) var fileClient + @Dependency(\.logger) var logger + + let fileUrl = URL(fileURLWithPath: outputPath) + let fileString = fileUrl.fileString() + + let currentVersion = try gitVersion.currentVersion(in: gitDirectory) + + let fileContents = template + .replacingOccurrences(of: "nil", with: "\"\(currentVersion)\"") + + try fileClient.write(string: fileContents, to: fileUrl) + logger.info("Updated version file: \(fileString)") + } + } + } +} + diff --git a/Sources/git-version/GitVersionCommand.swift b/Sources/git-version/GitVersionCommand.swift index 9b8971d..1bdb318 100644 --- a/Sources/git-version/GitVersionCommand.swift +++ b/Sources/git-version/GitVersionCommand.swift @@ -8,6 +8,7 @@ struct GitVersionCommand: ParsableCommand { commandName: "git-version", version: VERSION ?? "0.0.0", subcommands: [ + Build.self, Generate.self, Update.self ]