this repo has no description
1import Foundation
2import PackagePlugin
3
4@main
5struct VersionPlugin: BuildToolPlugin {
6 func createBuildCommands(context: PluginContext, target _: Target) async throws -> [Command] {
7 let outputPath = context.pluginWorkDirectory.appending("Version.swift")
8
9 return [
10 .buildCommand(
11 displayName: "Generate Version.swift",
12 executable: .init("/usr/bin/env"),
13 arguments: [
14 "swift", "-e",
15 """
16 import Foundation
17
18 func getVersion() -> String {
19 // Priority 1: Environment variable (CI)
20 if let envVersion = ProcessInfo.processInfo.environment["AXE_VERSION"] {
21 return envVersion
22 }
23
24 // Priority 2: Git describe (local development)
25 let gitProcess = Process()
26 gitProcess.executableURL = URL(fileURLWithPath: "/usr/bin/git")
27 gitProcess.arguments = ["describe", "--tags", "--always", "--dirty"]
28
29 let pipe = Pipe()
30 gitProcess.standardOutput = pipe
31 gitProcess.standardError = Pipe()
32
33 do {
34 try gitProcess.run()
35 gitProcess.waitUntilExit()
36
37 if gitProcess.terminationStatus == 0 {
38 let data = pipe.fileHandleForReading.readDataToEndOfFile()
39 if let version = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) {
40 return version.isEmpty ? "dev" : version
41 }
42 }
43 } catch {
44 // Git command failed
45 }
46
47 // Priority 3: Fallback
48 return "dev"
49 }
50
51 let version = getVersion()
52 let content = "// Auto-generated by VersionPlugin\\nlet VERSION = \\"\\(version)\\""
53
54 try content.write(toFile: "\\(CommandLine.arguments[1])", atomically: true, encoding: .utf8)
55 """,
56 outputPath.string,
57 ],
58 environment: [:],
59 inputFiles: [],
60 outputFiles: [outputPath]
61 ),
62 ]
63 }
64}