Skip to content

Commit

Permalink
add cache command
Browse files Browse the repository at this point in the history
  • Loading branch information
yonaskolb committed May 20, 2024
1 parent d99e448 commit f13c85a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
44 changes: 44 additions & 0 deletions Sources/XcodeGenCLI/Commands/CacheCommand.swift
Original file line number Diff line number Diff line change
@@ -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)")
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/XcodeGenCLI/Commands/DumpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DumpCommand: ProjectCommand {
try file.parent().mkpath()
try file.write(output)
} else {
stdout.print(output)
success(output)
}
}
}
Expand Down
23 changes: 1 addition & 22 deletions Sources/XcodeGenCLI/Commands/GenerateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
}
21 changes: 21 additions & 0 deletions Sources/XcodeGenCLI/Commands/ProjectCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions Sources/XcodeGenCLI/XcodeGenCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class XcodeGenCLI {
description: "Generates Xcode projects",
commands: [
generateCommand,
CacheCommand(version: version),
DumpCommand(version: version),
]
)
Expand Down

0 comments on commit f13c85a

Please sign in to comment.