This commit is contained in:
2023-03-14 16:23:16 -04:00
parent 37f3bfde62
commit 78bfa7863a
18 changed files with 237 additions and 217 deletions

View File

@@ -0,0 +1,49 @@
import ArgumentParser
import Dependencies
import Foundation
import GitVersion
import ShellClient
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."
)
@OptionGroup var shared: SharedOptions
func run() throws {
@Dependency(\.logger) var logger: Logger
@Dependency(\.fileClient) var fileClient
let targetUrl = parseTarget(shared.target)
let fileUrl = targetUrl.appendingPathComponent(shared.fileName)
let fileString = fileUrl.fileString()
guard !FileManager.default.fileExists(atPath: fileUrl.absoluteString) else {
logger.info("File already exists at path.")
throw GenerationError.fileExists(path: fileString)
}
if !shared.dryRun {
try fileClient.write(string: template, to: fileUrl)
logger.info("Generated file at: \(fileString)")
} else {
logger.info("Would generate file at: \(fileString)")
}
}
}
}
fileprivate enum GenerationError: Error {
case fileExists(path: String)
}
fileprivate let template = """
// Do not set this variable, it is set during the build process.
let VERSION: String? = nil
"""

View File

@@ -0,0 +1,30 @@
import ArgumentParser
import Foundation
@main
struct GitVersionCommand: ParsableCommand {
static var configuration: CommandConfiguration = .init(
commandName: "git-version",
version: VERSION ?? "0.0.0",
subcommands: [
Generate.self,
Update.self
]
)
}
struct SharedOptions: ParsableArguments {
@Argument(help: "The target for the version file.")
var target: String
@Option(
name: .customLong("filename"),
help: "Specify the file name for the version file."
)
var fileName: String = "Version.swift"
@Flag(name: .customLong("dry-run"))
var dryRun: Bool = false
}

View File

@@ -0,0 +1,20 @@
import Foundation
func parseTarget(_ target: String) -> URL {
let url = URL(fileURLWithPath: target)
let urlTest = url
.deletingLastPathComponent()
guard urlTest.lastPathComponent == "Sources" else {
return URL(fileURLWithPath: "Sources")
.appendingPathComponent(target)
}
return url
}
extension URL {
func fileString() -> String {
self.absoluteString
.replacingOccurrences(of: "file://", with: "")
}
}

View File

@@ -0,0 +1,66 @@
import ArgumentParser
import Foundation
import GitVersion
import ShellClient
extension GitVersionCommand {
struct Update: ParsableCommand {
static var configuration: CommandConfiguration = .init(
abstract: "Updates a version string to the git tag or git sha."
)
@OptionGroup var shared: SharedOptions
@Option(
name: .customLong("git-directory"),
help: "The git directory for the version."
)
var gitDirectory: String? = nil
func run() throws {
@Dependency(\.logger) var logger: Logger
@Dependency(\.fileClient) var fileClient: FileClient
@Dependency(\.gitVersionClient) var gitVersion
@Dependency(\.shellClient) var shell
let targetUrl = parseTarget(shared.target)
let fileUrl = targetUrl.appendingPathComponent(shared.fileName)
let fileString = fileUrl.fileString()
// guard FileManager.default.fileExists(atPath: fileUrl.absoluteString) else {
// logger.info("Version file does not exist.")
// throw UpdateError.versionFileDoesNotExist(path: fileString)
// }
let currentVersion = try gitVersion.currentVersion()
let cwd = FileManager.default.currentDirectoryPath
logger.info("CWD: \(cwd)")
logger.info("Git version: \(currentVersion)")
var updatedContents: String = ""
try withDependencies({
$0.logger.logLevel = .debug
}, operation: {
try shell.replacingNilWithVersionString(
in: fileString
// from: gitDirectory
) {
updatedContents = $0
}
})
if !shared.dryRun {
try fileClient.write(string: updatedContents, to: fileUrl)
logger.info("Updated version file: \(fileString)")
} else {
logger.info("Would update file contents to:")
logger.info("\(updatedContents)")
}
}
}
}
fileprivate enum UpdateError: Error {
case versionFileDoesNotExist(path: String)
}

View File

@@ -0,0 +1,2 @@
// Do not set this variable, it is set during the build process.
let VERSION: String? = ""