From 3dc5d8f5244b9cd72b52d89572a724ddee4ec331 Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Wed, 15 Mar 2023 07:56:13 -0400 Subject: [PATCH] wip --- .../GenerateVersionBuildPlugin.swift | 2 +- Sources/GitVersion/SwiftBuild.swift | 204 ------------------ Sources/git-version/BuildCommand.swift | 3 +- Sources/git-version/GenerateCommand.swift | 3 +- Sources/git-version/UpdateCommand.swift | 3 +- 5 files changed, 7 insertions(+), 208 deletions(-) delete mode 100644 Sources/GitVersion/SwiftBuild.swift diff --git a/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift b/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift index 0b63dde..3bf6f82 100644 --- a/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift +++ b/Plugins/GenerateVersionBuildPlugin/GenerateVersionBuildPlugin.swift @@ -20,7 +20,7 @@ struct GenerateVersionBuildPlugin: BuildToolPlugin { return [ .buildCommand( - displayName: "Build With Version", + displayName: "Build With Version Plugin", executable: tool.path, arguments: ["build", "--verbose", "--git-directory", gitDirectoryPath.string, outputPath.string], environment: [:], diff --git a/Sources/GitVersion/SwiftBuild.swift b/Sources/GitVersion/SwiftBuild.swift deleted file mode 100644 index 1558344..0000000 --- a/Sources/GitVersion/SwiftBuild.swift +++ /dev/null @@ -1,204 +0,0 @@ -import Dependencies -import Foundation -import ShellClient - -extension ShellCommand { - - /// Create a ``ShellCommand`` instance that's a wrapper for the `swift build` - /// command. - /// - /// - Parameters: - /// - build: The build command to use. - public static func swiftBuild(_ build: SwiftBuild) -> Self { - build.command - } -} - -/// A wrapper around the `swift build` command. This allows you to build a project from -/// swift. -/// -public struct SwiftBuild { - @Dependency(\.logger) var logger - @Dependency(\.shellClient) var shell - - /// Represents the default arguments for the build command. - public static let defaults: [Argument] = [ - .disableSandBox, - .xSwiftC("-cross-module-optimization") - ] - - /// The arguments for the build command. - public let arguments: [Argument] - - /// The configuration for the build command. - public let configuration: Configuration - - /// Create a ``SwiftBuild`` instance for the ``SwiftBuild/Configuration/debug`` configuration. - /// - /// - Parameters: - /// - arguments: The arguments for the `swift build` command. - public static func debug(_ arguments: [Argument] = Self.defaults) -> Self { - .init(configuration: .debug, arguments: arguments) - } - - /// Create a ``SwiftBuild`` instance for the ``SwiftBuild/Configuration/release`` configuration. - /// - /// - Parameters: - /// - arguments: The arguments for the `swift build` command. - public static func release(_ arguments: [Argument] = Self.defaults) -> Self { - .init(configuration: .release, arguments: arguments) - } - - internal init( - configuration: Configuration = .debug, - arguments: [Argument] = Self.defaults - ) { - self.arguments = arguments - self.configuration = configuration - } - - internal var command: ShellCommand { - .init( - shell: .env, - [ - "swift", - "build", - "--configuration", - "\(configuration)" - ] - + arguments.strings - ) - } - - internal func run() throws { - logger.info("\("Building.".blue)") - - try withDependencies { - $0.logger.logLevel = .debug - } operation: { - try shell.foreground(command) - } - } - - /// Represents an argument for the ``SwiftBuild`` command. - public enum Argument { - /// Disable the sandbox. - case disableSandBox - /// Pass a string through to the `swift build` command. - case custom([String]) - /// Pass an `-Xswiftc` compiler flag through to the `swift build` command. - case xSwiftC(String) - } - - /// Represents the configuration for the ``SwiftBuild`` command. - public enum Configuration: String, CustomStringConvertible, CaseIterable { - /// The debug configuration. - case debug - /// The release configuration. - case release - public var description: String { rawValue } - } -} - -//extension ShellClient { -// -// /// Reads contents at the given file path, then allows the caller to act on the -// /// results after "nil" has been replaced with the current version string. -// /// -// /// - Parameters: -// /// - filePath: The file path to replace nil in. -// /// - workingDirectory: Customize the working directory for the command. -// /// - closure: The closure to run with the updated file content string. -// public func replacingNilWithVersionString( -// in filePath: String, -// from workingDirectory: String? = nil, -// _ closure: @escaping (String) throws -> Void -// ) throws { -// @Dependency(\.fileClient) var fileClient: FileClient -// @Dependency(\.gitVersionClient) var gitClient: GitVersionClient -// @Dependency(\.logger) var logger: Logger -// -// let currentVersion = try gitClient.currentVersion(in: workingDirectory) -// let originalContents = try fileClient.readAsString(path: filePath) -// -// logger.debug("Setting version: \(currentVersion)") -// -// let updatedContents = originalContents -// .replacingOccurrences(of: "nil", with: "\"\(currentVersion)\"") -// -// logger.debug("Set version") -// logger.debug("Updated contents:") -// logger.debug("\(updatedContents)") -// -// try closure(updatedContents) -// } -// -// -// /// Replace nil in the given file path and then run the given closure. -// /// -// /// > Note: The file contents will be reset back to nil after the closure operation. -// /// -// /// - Parameters: -// /// - filePath: The file path to replace nil in. -// /// - workingDirectory: Customize the working directory for the command. -// /// - build: The swift builder to use. -// public func replacingNilWithVersionString( -// in filePath: String, -// from workingDirectory: String? = nil, -// _ closure: @escaping () throws -> Void -// ) throws { -// @Dependency(\.fileClient) var fileClient: FileClient -// @Dependency(\.logger) var logger: Logger -// -// // grab the original contents, to set it back when done. -// let originalContents = try fileClient.readAsString(path: filePath) -// -// try self.replacingNilWithVersionString( -// in: filePath, -// from: workingDirectory -// ) { update in -// try fileClient.write(string: update, to: filePath) -// defer { try! fileClient.write(string: originalContents, to: filePath) } -// -// try closure() -// } -// -// } -// -// /// Replace nil in the given file path and then build the project, using the -// /// given builder. -// /// -// /// > Note: The file contents will be reset back to nil after the build operation. -// /// -// /// - Parameters: -// /// - filePath: The file path to replace nil in. -// /// - workingDirectory: Customize the working directory for the command. -// /// - build: The swift builder to use. -// public func replacingNilWithVersionString( -// in filePath: String, -// from workingDirectory: String? = nil, -// build: SwiftBuild -// ) throws { -// try replacingNilWithVersionString( -// in: filePath, -// from: workingDirectory, -// build.run -// ) -// } -//} - -fileprivate extension Array where Element == SwiftBuild.Argument { - - var strings: [String] { - reduce(into: [String]()) { current, argument in - switch argument { - case .disableSandBox: - current.append("--disable-sandbox") - case .custom(let strings): - current += strings - case .xSwiftC(let value): - current += ["-Xswiftc", value] - } - } - } -} diff --git a/Sources/git-version/BuildCommand.swift b/Sources/git-version/BuildCommand.swift index e960dde..741e3ee 100644 --- a/Sources/git-version/BuildCommand.swift +++ b/Sources/git-version/BuildCommand.swift @@ -6,7 +6,8 @@ import ShellClient extension GitVersionCommand { struct Build: ParsableCommand { static var configuration: CommandConfiguration = .init( - abstract: "Used for the build with version plugin." + abstract: "Used for the build with version plugin.", + discussion: "This should generally not be interacted with directly, outside of the build plugin." ) @OptionGroup var shared: SharedOptions diff --git a/Sources/git-version/GenerateCommand.swift b/Sources/git-version/GenerateCommand.swift index 3873d2e..9e7c77d 100644 --- a/Sources/git-version/GenerateCommand.swift +++ b/Sources/git-version/GenerateCommand.swift @@ -8,7 +8,8 @@ extension GitVersionCommand { struct Generate: ParsableCommand { static var configuration: CommandConfiguration = .init( - abstract: "Generates a version file in a command line tool that can be set via the git tag or git sha." + abstract: "Generates a version file in a command line tool that can be set via the git tag or git sha.", + discussion: "This command can be interacted with directly, outside of the plugin usage context." ) @OptionGroup var shared: SharedOptions diff --git a/Sources/git-version/UpdateCommand.swift b/Sources/git-version/UpdateCommand.swift index 296f23d..4cbceb0 100644 --- a/Sources/git-version/UpdateCommand.swift +++ b/Sources/git-version/UpdateCommand.swift @@ -7,7 +7,8 @@ extension GitVersionCommand { struct Update: ParsableCommand { static var configuration: CommandConfiguration = .init( - abstract: "Updates a version string to the git tag or git sha." + abstract: "Updates a version string to the git tag or git sha.", + discussion: "This command can be interacted with directly outside of the plugin context." ) @OptionGroup var shared: SharedOptions