From f13c85ad8ee9c33d0f19641603085da387c3b8d1 Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Mon, 20 May 2024 19:04:53 +1000 Subject: [PATCH] add cache command --- .../XcodeGenCLI/Commands/CacheCommand.swift | 44 +++++++++++++++++++ .../XcodeGenCLI/Commands/DumpCommand.swift | 2 +- .../Commands/GenerateCommand.swift | 23 +--------- .../XcodeGenCLI/Commands/ProjectCommand.swift | 21 +++++++++ Sources/XcodeGenCLI/XcodeGenCLI.swift | 1 + 5 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 Sources/XcodeGenCLI/Commands/CacheCommand.swift diff --git a/Sources/XcodeGenCLI/Commands/CacheCommand.swift b/Sources/XcodeGenCLI/Commands/CacheCommand.swift new file mode 100644 index 000000000..70a14a241 --- /dev/null +++ b/Sources/XcodeGenCLI/Commands/CacheCommand.swift @@ -0,0 +1,44 @@ +import Foundation +import PathKit +import ProjectSpec +import SwiftCLI +import XcodeGenKit +import XcodeProj +import Version + +class CacheCommand: ProjectCommand { + + @Key("--cache-path", description: "Where the cache file will be loaded from and save to. Defaults to ~/.xcodegen/cache/{SPEC_PATH_HASH}") + var cacheFilePath: Path? + + init(version: Version) { + super.init(version: version, + name: "cache", + shortDescription: "Write the project cache") + } + + override func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws { + + let cacheFilePath = self.cacheFilePath ?? Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute() + + var cacheFile: CacheFile? + + // generate cache + do { + cacheFile = try specLoader.generateCacheFile() + } catch { + throw GenerationError.projectSpecParsingError(error) + } + + // write cache + if let cacheFile = cacheFile { + do { + try cacheFilePath.parent().mkpath() + try cacheFilePath.write(cacheFile.string) + success("Wrote cache to \(cacheFilePath)") + } catch { + info("Failed to write cache: \(error.localizedDescription)") + } + } + } +} diff --git a/Sources/XcodeGenCLI/Commands/DumpCommand.swift b/Sources/XcodeGenCLI/Commands/DumpCommand.swift index b3da9dcca..7e054062e 100644 --- a/Sources/XcodeGenCLI/Commands/DumpCommand.swift +++ b/Sources/XcodeGenCLI/Commands/DumpCommand.swift @@ -49,7 +49,7 @@ class DumpCommand: ProjectCommand { try file.parent().mkpath() try file.write(output) } else { - stdout.print(output) + success(output) } } } diff --git a/Sources/XcodeGenCLI/Commands/GenerateCommand.swift b/Sources/XcodeGenCLI/Commands/GenerateCommand.swift index b607b09b3..385846e5d 100644 --- a/Sources/XcodeGenCLI/Commands/GenerateCommand.swift +++ b/Sources/XcodeGenCLI/Commands/GenerateCommand.swift @@ -8,9 +8,6 @@ import Version class GenerateCommand: ProjectCommand { - @Flag("-q", "--quiet", description: "Suppress all informational and success output") - var quiet: Bool - @Flag("-c", "--use-cache", description: "Use a cache for the xcodegen spec. This will prevent unnecessarily generating the project if nothing has changed") var useCache: Bool @@ -46,7 +43,7 @@ class GenerateCommand: ProjectCommand { Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute() var cacheFile: CacheFile? - // read cache + // generate cache if useCache || self.cacheFilePath != nil { do { cacheFile = try specLoader.generateCacheFile() @@ -138,22 +135,4 @@ class GenerateCommand: ProjectCommand { try Task.run(bash: command, directory: projectDirectory.absolute().string) } } - - func info(_ string: String) { - if !quiet { - stdout.print(string) - } - } - - func warning(_ string: String) { - if !quiet { - stdout.print(string.yellow) - } - } - - func success(_ string: String) { - if !quiet { - stdout.print(string.green) - } - } } diff --git a/Sources/XcodeGenCLI/Commands/ProjectCommand.swift b/Sources/XcodeGenCLI/Commands/ProjectCommand.swift index 894dbc5ba..9f3b013c8 100644 --- a/Sources/XcodeGenCLI/Commands/ProjectCommand.swift +++ b/Sources/XcodeGenCLI/Commands/ProjectCommand.swift @@ -12,6 +12,9 @@ class ProjectCommand: Command { let name: String let shortDescription: String + @Flag("-q", "--quiet", description: "Suppress all informational and success output") + var quiet: Bool + @Key("-s", "--spec", description: "The path to the project spec file. Defaults to project.yml. (It is also possible to link to multiple spec files by comma separating them. Note that all other flags will be the same.)") var spec: String? @@ -58,4 +61,22 @@ class ProjectCommand: Command { } func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {} + + func info(_ string: String) { + if !quiet { + stdout.print(string) + } + } + + func warning(_ string: String) { + if !quiet { + stdout.print(string.yellow) + } + } + + func success(_ string: String) { + if !quiet { + stdout.print(string.green) + } + } } diff --git a/Sources/XcodeGenCLI/XcodeGenCLI.swift b/Sources/XcodeGenCLI/XcodeGenCLI.swift index 42afef037..8d6a69c59 100644 --- a/Sources/XcodeGenCLI/XcodeGenCLI.swift +++ b/Sources/XcodeGenCLI/XcodeGenCLI.swift @@ -15,6 +15,7 @@ public class XcodeGenCLI { description: "Generates Xcode projects", commands: [ generateCommand, + CacheCommand(version: version), DumpCommand(version: version), ] )