Skip to content

Commit 576739b

Browse files
authored
Add cache command (#1476)
* add cache command * docs: update git hook info
1 parent aa79a3e commit 576739b

6 files changed

Lines changed: 76 additions & 27 deletions

File tree

Docs/FAQ.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ Absolutely. You will get the most out of XcodeGen by adding your project to your
99
>Note that you can run `xcodegen` as a step in your build process on CI.
1010
1111
## What happens when I switch branches
12-
If files were added or removed in the new checkout you will most likely need to run `xcodegen` again so that your project will reference all your files. Unfortunately this is a manual step at the moment, but in the future this could be automated.
13-
14-
For now you can always add xcodegen as a git `post-checkout` hook.
15-
It's recommended to use `--use-cache` so that the project is not needlessly generated.
12+
If files were added or removed in the new checkout you will most likely need to run `xcodegen` again so that your project will reference all your files.
13+
14+
It's recommended to set up some [git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to automate the process:
15+
- run `xcodegen generate --use-cache` on the following hooks. This will make sure the project is up to date when checking out, merging and rebasing
16+
- post-checkout
17+
- post-rewrite
18+
- post-merge
19+
- run `xcodegen cache` on `post-commit`. This will make sure that when switching branches the cache will be updated in case you made local changes, or are ammending a commit that added a new file.
1620

1721
## Can I use CocoaPods
1822
Yes, you will just need to run `pod install` after the project is generated to integrate Cocoapods changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
import PathKit
3+
import ProjectSpec
4+
import SwiftCLI
5+
import XcodeGenKit
6+
import XcodeProj
7+
import Version
8+
9+
class CacheCommand: ProjectCommand {
10+
11+
@Key("--cache-path", description: "Where the cache file will be loaded from and save to. Defaults to ~/.xcodegen/cache/{SPEC_PATH_HASH}")
12+
var cacheFilePath: Path?
13+
14+
init(version: Version) {
15+
super.init(version: version,
16+
name: "cache",
17+
shortDescription: "Write the project cache")
18+
}
19+
20+
override func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {
21+
22+
let cacheFilePath = self.cacheFilePath ?? Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute()
23+
24+
var cacheFile: CacheFile?
25+
26+
// generate cache
27+
do {
28+
cacheFile = try specLoader.generateCacheFile()
29+
} catch {
30+
throw GenerationError.projectSpecParsingError(error)
31+
}
32+
33+
// write cache
34+
if let cacheFile = cacheFile {
35+
do {
36+
try cacheFilePath.parent().mkpath()
37+
try cacheFilePath.write(cacheFile.string)
38+
success("Wrote cache to \(cacheFilePath)")
39+
} catch {
40+
info("Failed to write cache: \(error.localizedDescription)")
41+
}
42+
}
43+
}
44+
}

Sources/XcodeGenCLI/Commands/DumpCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DumpCommand: ProjectCommand {
4949
try file.parent().mkpath()
5050
try file.write(output)
5151
} else {
52-
stdout.print(output)
52+
success(output)
5353
}
5454
}
5555
}

Sources/XcodeGenCLI/Commands/GenerateCommand.swift

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import Version
88

99
class GenerateCommand: ProjectCommand {
1010

11-
@Flag("-q", "--quiet", description: "Suppress all informational and success output")
12-
var quiet: Bool
13-
1411
@Flag("-c", "--use-cache", description: "Use a cache for the xcodegen spec. This will prevent unnecessarily generating the project if nothing has changed")
1512
var useCache: Bool
1613

@@ -46,7 +43,7 @@ class GenerateCommand: ProjectCommand {
4643
Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute()
4744
var cacheFile: CacheFile?
4845

49-
// read cache
46+
// generate cache
5047
if useCache || self.cacheFilePath != nil {
5148
do {
5249
cacheFile = try specLoader.generateCacheFile()
@@ -138,22 +135,4 @@ class GenerateCommand: ProjectCommand {
138135
try Task.run(bash: command, directory: projectDirectory.absolute().string)
139136
}
140137
}
141-
142-
func info(_ string: String) {
143-
if !quiet {
144-
stdout.print(string)
145-
}
146-
}
147-
148-
func warning(_ string: String) {
149-
if !quiet {
150-
stdout.print(string.yellow)
151-
}
152-
}
153-
154-
func success(_ string: String) {
155-
if !quiet {
156-
stdout.print(string.green)
157-
}
158-
}
159138
}

Sources/XcodeGenCLI/Commands/ProjectCommand.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class ProjectCommand: Command {
1212
let name: String
1313
let shortDescription: String
1414

15+
@Flag("-q", "--quiet", description: "Suppress all informational and success output")
16+
var quiet: Bool
17+
1518
@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.)")
1619
var spec: String?
1720

@@ -58,4 +61,22 @@ class ProjectCommand: Command {
5861
}
5962

6063
func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {}
64+
65+
func info(_ string: String) {
66+
if !quiet {
67+
stdout.print(string)
68+
}
69+
}
70+
71+
func warning(_ string: String) {
72+
if !quiet {
73+
stdout.print(string.yellow)
74+
}
75+
}
76+
77+
func success(_ string: String) {
78+
if !quiet {
79+
stdout.print(string.green)
80+
}
81+
}
6182
}

Sources/XcodeGenCLI/XcodeGenCLI.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class XcodeGenCLI {
1515
description: "Generates Xcode projects",
1616
commands: [
1717
generateCommand,
18+
CacheCommand(version: version),
1819
DumpCommand(version: version),
1920
]
2021
)

0 commit comments

Comments
 (0)