wip
This commit is contained in:
@@ -164,10 +164,14 @@ extension FileClient {
|
||||
|
||||
// MARK: - Private
|
||||
fileprivate func url(for path: String) throws -> URL {
|
||||
if #available(macOS 13.0, *) {
|
||||
return URL(filePath: path)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
#if os(Linux)
|
||||
return URL(fileURLWithPath: path)
|
||||
}
|
||||
#else
|
||||
if #available(macOS 13.0, *) {
|
||||
return URL(filePath: path)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
return URL(fileURLWithPath: path)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -101,6 +101,39 @@ public struct SwiftBuild {
|
||||
}
|
||||
|
||||
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.
|
||||
///
|
||||
/// > 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.
|
||||
/// - 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")
|
||||
|
||||
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.
|
||||
@@ -115,17 +148,21 @@ extension ShellClient {
|
||||
_ closure: @escaping () 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)
|
||||
// grab the original contents, to set it back when done.
|
||||
let originalContents = try fileClient.readAsString(path: filePath)
|
||||
|
||||
let updatedContents = originalContents
|
||||
.replacingOccurrences(of: "nil", with: "\"\(currentVersion)\"")
|
||||
try fileClient.write(string: updatedContents, to: filePath)
|
||||
defer { try! fileClient.write(string: originalContents, to: 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()
|
||||
}
|
||||
|
||||
try closure()
|
||||
}
|
||||
|
||||
/// Replace nil in the given file path and then build the project, using the
|
||||
|
||||
@@ -10,9 +10,20 @@ public struct Build {
|
||||
public static func main() throws {
|
||||
@Dependency(\.shellClient) var shell: ShellClient
|
||||
|
||||
try shell.replacingNilWithVersionString(
|
||||
in: "Sources/example/Version.swift",
|
||||
build: SwiftBuild.release()
|
||||
)
|
||||
let gitDir = URL(fileURLWithPath: #file)
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
|
||||
try withDependencies {
|
||||
$0.gitVersionClient = .liveValue
|
||||
$0.logger.logLevel = .debug
|
||||
} operation: {
|
||||
try shell.replacingNilWithVersionString(
|
||||
in: "Sources/example/Version.swift",
|
||||
from: gitDir.absoluteString,
|
||||
build: SwiftBuild.release()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import ShellClient
|
||||
|
||||
/// An example of using the git version client with a command line tool
|
||||
/// The ``VERSION`` variable get's set during the build process.
|
||||
|
||||
@main
|
||||
public struct Example: ParsableCommand {
|
||||
|
||||
|
||||
34
Sources/git-version-builder/GitVersionBuilder.swift
Normal file
34
Sources/git-version-builder/GitVersionBuilder.swift
Normal file
@@ -0,0 +1,34 @@
|
||||
import ArgumentParser
|
||||
import Dependencies
|
||||
import GitVersion
|
||||
import ShellClient
|
||||
|
||||
@main
|
||||
public struct GitVersionBuilder: AsyncParsableCommand {
|
||||
|
||||
public init() { }
|
||||
|
||||
@Argument
|
||||
var input: String
|
||||
|
||||
@Argument
|
||||
var output: String
|
||||
|
||||
public func run() async throws {
|
||||
@Dependency(\.logger) var logger
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.shellClient) var shell: ShellClient
|
||||
|
||||
logger.debug("Building with input file: \(input)")
|
||||
logger.debug("Output file: \(output)")
|
||||
|
||||
try shell.replacingNilWithVersionString(
|
||||
in: input
|
||||
) { update in
|
||||
logger.debug("Updating with:\n\(update)")
|
||||
try fileClient.write(string: update, to: output)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user