This commit is contained in:
2023-03-14 17:03:24 -04:00
parent 78bfa7863a
commit 8dc72e4a95
11 changed files with 185 additions and 139 deletions

View File

@@ -42,8 +42,3 @@ 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

@@ -14,17 +14,3 @@ struct GitVersionCommand: ParsableCommand {
)
}
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

@@ -1,3 +1,4 @@
import ArgumentParser
import Foundation
func parseTarget(_ target: String) -> URL {
@@ -18,3 +19,27 @@ extension URL {
.replacingOccurrences(of: "file://", with: "")
}
}
let template = """
// Do not set this variable, it is set during the build process.
let VERSION: String? = nil
"""
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
@Flag(name: .long, help: "Increase logging level.")
var verbose: Bool = false
}

View File

@@ -19,43 +19,35 @@ extension GitVersionCommand {
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
try withDependencies {
$0.logger.logLevel = shared.verbose ? .debug : .info
$0.fileClient = .liveValue
$0.gitVersionClient = .liveValue
$0.shellClient = .liveValue
} operation: {
@Dependency(\.gitVersionClient) var gitVersion
@Dependency(\.fileClient) var fileClient
@Dependency(\.logger) var logger
@Dependency(\.shellClient) var shell
let targetUrl = parseTarget(shared.target)
let fileUrl = targetUrl.appendingPathComponent(shared.fileName)
let fileString = fileUrl.fileString()
let targetUrl = parseTarget(shared.target)
let fileUrl = targetUrl
.appendingPathComponent(shared.fileName)
// guard FileManager.default.fileExists(atPath: fileUrl.absoluteString) else {
// logger.info("Version file does not exist.")
// throw UpdateError.versionFileDoesNotExist(path: fileString)
// }
let fileString = fileUrl.fileString()
let currentVersion = try gitVersion.currentVersion()
let cwd = FileManager.default.currentDirectoryPath
logger.info("CWD: \(cwd)")
logger.info("Git version: \(currentVersion)")
let currentVersion = try gitVersion.currentVersion(in: gitDirectory)
var updatedContents: String = ""
try withDependencies({
$0.logger.logLevel = .debug
}, operation: {
try shell.replacingNilWithVersionString(
in: fileString
// from: gitDirectory
) {
updatedContents = $0
let fileContents = template
.replacingOccurrences(of: "nil", with: "\"\(currentVersion)\"")
if !shared.dryRun {
try fileClient.write(string: fileContents, to: fileUrl)
logger.info("Updated version file: \(fileString)")
} else {
logger.info("Would update file contents to:")
logger.info("\(fileContents)")
}
})
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)")
}
}
}

View File

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