Skip to content

Commit 88e6e07

Browse files
wileykestnerJonathan Schear
authored andcommitted
Enable a custom location for SettingsPresets via environment variable
1 parent a6cfa0e commit 88e6e07

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ Options:
136136
- **--use-cache**: Used to prevent unnecessarily generating the project. If this is set, then a cache file will be written to when a project is generated. If `xcodegen` is later run but the spec and all the files it contains are the same, the project won't be generated.
137137
- **--cache-path**: A custom path to use for your cache file. This defaults to `~/.xcodegen/cache/{PROJECT_SPEC_PATH_HASH}`
138138

139+
Optional Environment Variables:
140+
141+
`XCODEGEN_SETTINGS_PRESETS_PARENT_DIR`: When certain build systems (i.e Bazel) use `xcodegen` as a dependency, the `SettingsPresets` directory can sometimes be placed in surprising locations. Setting this environment variables gives users of these build systems a way to provide a custom path to the parent directory of the `SettingsPresets` directory that overrides the defaults lookup locations if it is found, and proceeds to the default lookup locations if it is not found.
142+
139143
There are other commands as well such as `xcodegen dump` which lets out output the resolved spec in many different formats, or write it to a file. Use `xcodegen help` to see more detailed usage information.
140144

141145
## Dependency Diagrams

Sources/XcodeGenKit/SettingsBuilder.swift

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,41 @@ extension SettingsPresetFile {
186186
if let cached = settingPresetSettings[path] {
187187
return cached.value
188188
}
189+
189190
let bundlePath = Path(Bundle.main.bundlePath)
190-
let relativePath = Path("SettingPresets/\(path).yml")
191-
var possibleSettingsPaths: [Path] = [
192-
relativePath,
193-
bundlePath + relativePath,
194-
bundlePath + "../share/xcodegen/\(relativePath)",
195-
Path(#file).parent().parent().parent() + relativePath,
196-
]
191+
let relativeSettingsPresetsPath = Path("SettingPresets/\(path).yml")
192+
193+
var possibleSettingsPresetsPaths: [Path] = []
194+
195+
// Allow users to optionally specify the location of the `SettingPresets`
196+
// parent directory as an environment variable.
197+
//
198+
// If the user goes out of their way to specify this environment variable,
199+
// we assume they _really_ want to look for it here, so we give it first
200+
// priority over the usual places we look for it. If it is not found,
201+
// we just proceed to look for it in the normal locations as usual.
202+
//
203+
if let settingsPresetsParentDirectory: String = ProcessInfo.processInfo.environment["XCODEGEN_SETTINGS_PRESETS_PARENT_DIR"] {
204+
possibleSettingsPresetsPaths.append(Path(settingsPresetsParentDirectory) + relativeSettingsPresetsPath)
205+
}
206+
207+
// These are the places that we look for the `SettingPresets` directory if
208+
// the user hasn't gone out of their way to look for it in another place
209+
// by specifying an environment variable - or if it can't be found at the
210+
// location the user specified in the environment variable.
211+
//
212+
possibleSettingsPresetsPaths.append(relativeSettingsPresetsPath)
213+
possibleSettingsPresetsPaths.append(bundlePath + relativeSettingsPresetsPath)
214+
possibleSettingsPresetsPaths.append(bundlePath + "../share/xcodegen/\(relativeSettingsPresetsPath)")
215+
possibleSettingsPresetsPaths.append(Path(#file).parent().parent().parent() + relativeSettingsPresetsPath)
197216

198217
if let symlink = try? (bundlePath + "xcodegen").symlinkDestination() {
199-
possibleSettingsPaths = [
200-
symlink.parent() + relativePath,
201-
] + possibleSettingsPaths
218+
possibleSettingsPresetsPaths = [
219+
symlink.parent() + relativeSettingsPresetsPath,
220+
] + possibleSettingsPresetsPaths
202221
}
203222

204-
guard let settingsPath = possibleSettingsPaths.first(where: { $0.exists }) else {
223+
guard let settingsPresetsPath = possibleSettingsPresetsPaths.first(where: { $0.exists }) else {
205224
switch self {
206225
case .base, .config, .platform:
207226
print("No \"\(name)\" settings found")
@@ -212,7 +231,7 @@ extension SettingsPresetFile {
212231
return nil
213232
}
214233

215-
guard let buildSettings = try? loadYamlDictionary(path: settingsPath) else {
234+
guard let buildSettings = try? loadYamlDictionary(path: settingsPresetsPath) else {
216235
print("Error parsing \"\(name)\" settings")
217236
return nil
218237
}

0 commit comments

Comments
 (0)