From 03b212c51742124c171136b199928c69e0783b1d Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Tue, 1 Aug 2017 18:08:43 +0200 Subject: [PATCH 1/5] support absolute paths --- Sources/XcodeGenKit/PBXProjGenerator.swift | 13 +++++++++++-- Sources/XcodeGenKit/ProjectGenerator.swift | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index ef47ebc3..d42d7220 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -42,6 +42,15 @@ public class PBXProjGenerator { projectReference = generateUUID(PBXProject.self, spec.name) } + func getPath(_ path: String) -> Path { + let currentPath = Path(path) + if currentPath.isRelative { + return basePath + currentPath + } else { + return currentPath + } + } + public func generateUUID(_ element: T.Type, _ id: String) -> String { var uuid: String = "" var counter: UInt = 0 @@ -132,7 +141,7 @@ public class PBXProjGenerator { func generateTarget(_ target: Target) throws -> PBXNativeTarget { - let sourcePaths = target.sources.map { basePath + $0 } + let sourcePaths = target.sources.map { getPath($0) } var sourceFilePaths: [Path] = [] for source in sourcePaths { @@ -145,7 +154,7 @@ public class PBXProjGenerator { var baseConfigurationReference: String? if let configPath = target.configFiles[config.name] { - let path = basePath + configPath + let path = getPath(configPath) baseConfigurationReference = fileReferencesByPath[path] } return XCBuildConfiguration(reference: generateUUID(XCBuildConfiguration.self, config.name), name: config.name, baseConfigurationReference: baseConfigurationReference, buildSettings: buildSettings) diff --git a/Sources/XcodeGenKit/ProjectGenerator.swift b/Sources/XcodeGenKit/ProjectGenerator.swift index 59c4587b..7f69d9a2 100644 --- a/Sources/XcodeGenKit/ProjectGenerator.swift +++ b/Sources/XcodeGenKit/ProjectGenerator.swift @@ -33,7 +33,16 @@ public class ProjectGenerator { return spec.configs.first { $0.type == .release }! } - func validate() throws { + func getPath(_ path: String) -> Path { + let currentPath = Path(path) + if currentPath.isRelative { + return self.path + currentPath + } else { + return currentPath + } + } + + public func validate() throws { if spec.configs.isEmpty { spec.configs = [Config(name: "Debug", type: .debug), Config(name: "Release", type: .release)] @@ -71,7 +80,7 @@ public class ProjectGenerator { } for source in target.sources { - let sourcePath = path + source + let sourcePath = getPath(source) if !sourcePath.exists { errors.append(.missingTargetSource(target: target.name, source: sourcePath.string)) } From 9cc1e5082496c4aa08e772f12ede9da36f764b5e Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Tue, 1 Aug 2017 18:40:40 +0200 Subject: [PATCH 2/5] add run script support --- .../project.pbxproj | 33 ++++++++++ .../TestProject.xcodeproj/project.pbxproj | 34 ++++++++++ Fixtures/TestProject/scripts/swiftlint.sh | 5 ++ Fixtures/TestProject/spec.yml | 11 ++++ Sources/ProjectSpec/RunScript.swift | 66 +++++++++++++++++++ Sources/ProjectSpec/Target.swift | 6 +- Sources/XcodeGenKit/PBXProjGenerator.swift | 28 ++++++++ Sources/XcodeGenKit/ProjectGenerator.swift | 11 ++++ .../ProjectGeneratorTests.swift | 20 ++++++ Tests/XcodeGenKitTests/SpecLoadingTests.swift | 19 ++++++ docs/ProjectSpec.md | 36 +++++++++- 11 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 Fixtures/TestProject/scripts/swiftlint.sh create mode 100644 Sources/ProjectSpec/RunScript.swift diff --git a/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj b/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj index 0875d8a6..29435aad 100644 --- a/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj +++ b/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj @@ -156,6 +156,7 @@ HBP299349701 /* Headers */, FBP299349701 /* Frameworks */, CFBP29934901 /* Copy Files */, + SSBP25860701 /* Run Script */, ); buildRules = ( ); @@ -174,6 +175,7 @@ HBP652326301 /* Headers */, FBP652326301 /* Frameworks */, CFBP65232601 /* Copy Files */, + SSBP25860701 /* Run Script */, ); buildRules = ( ); @@ -224,6 +226,37 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + SSBP25860701 /* Run Script */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = Swiftlint; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n"; + }; + SSBP25860701 /* Run Script */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = Swiftlint; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ SBP299349701 /* Sources */ = { isa = PBXSourcesBuildPhase; diff --git a/Fixtures/TestProject/TestProject.xcodeproj/project.pbxproj b/Fixtures/TestProject/TestProject.xcodeproj/project.pbxproj index 9afe5e52..0d7bab28 100644 --- a/Fixtures/TestProject/TestProject.xcodeproj/project.pbxproj +++ b/Fixtures/TestProject/TestProject.xcodeproj/project.pbxproj @@ -138,6 +138,7 @@ CCA4B6071F1FC00500DF34A1 /* Frameworks */, CCA4B6081F1FC00500DF34A1 /* Resources */, CCA71FF01F225E4C00F772C1 /* Embed Frameworks */, + CC2DB89A1F30E29600B4B0FA /* Swiftlint */, ); buildRules = ( ); @@ -157,6 +158,7 @@ CCA71FE41F225E4C00F772C1 /* Frameworks */, CCA71FE51F225E4C00F772C1 /* Headers */, CCA71FE61F225E4C00F772C1 /* Resources */, + CC2DB89B1F30E2AC00B4B0FA /* Swiftlint */, ); buildRules = ( ); @@ -229,6 +231,37 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + CC2DB89A1F30E29600B4B0FA /* Swiftlint */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = Swiftlint; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi"; + }; + CC2DB89B1F30E2AC00B4B0FA /* Swiftlint */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = Swiftlint; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ CCA4B6061F1FC00500DF34A1 /* Sources */ = { isa = PBXSourcesBuildPhase; @@ -475,6 +508,7 @@ CCA71FF31F225E4C00F772C1 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Fixtures/TestProject/scripts/swiftlint.sh b/Fixtures/TestProject/scripts/swiftlint.sh new file mode 100644 index 00000000..ee2d2178 --- /dev/null +++ b/Fixtures/TestProject/scripts/swiftlint.sh @@ -0,0 +1,5 @@ +if which swiftlint >/dev/null; then + swiftlint +else + echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" +fi diff --git a/Fixtures/TestProject/spec.yml b/Fixtures/TestProject/spec.yml index d3af7ff2..12b57d37 100644 --- a/Fixtures/TestProject/spec.yml +++ b/Fixtures/TestProject/spec.yml @@ -9,9 +9,20 @@ targets: PRODUCT_BUNDLE_IDENTIFIER: com.test dependencies: - target: MyFramework + postbuildScripts: + - name: Swiftlint + script: | + if which swiftlint >/dev/null; then + swiftlint + else + echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" + fi - name: MyFramework type: framework platform: iOS sources: MyFramework settings: INFOPLIST_FILE: MyFramework/Info.plist + postbuildScripts: + - name: Swiftlint + path: scripts/swiftlint.sh diff --git a/Sources/ProjectSpec/RunScript.swift b/Sources/ProjectSpec/RunScript.swift new file mode 100644 index 00000000..84d78104 --- /dev/null +++ b/Sources/ProjectSpec/RunScript.swift @@ -0,0 +1,66 @@ +// +// RunScript.swift +// XcodeGen +// +// Created by Yonas Kolb on 1/8/17. +// +// + +import Foundation +import JSONUtilities + +public struct RunScript: Equatable { + + public var script: ScriptType + public var name: String? + public var shell: String? + public var inputFiles: [String] + public var outputFiles: [String] + + public enum ScriptType: Equatable { + case path(String) + case script(String) + + public static func ==(lhs: ScriptType, rhs: ScriptType) -> Bool { + switch (lhs, rhs) { + case let (.path(lhs), .path(rhs)): return lhs == rhs + case let (.script(lhs), .script(rhs)): return lhs == rhs + default: return false + } + } + } + + public init(script: ScriptType, name: String? = nil, inputFiles: [String] = [], outputFiles: [String] = [], shell: String? = nil) { + self.script = script + self.name = name + self.inputFiles = inputFiles + self.outputFiles = outputFiles + self.shell = shell + } + + public static func ==(lhs: RunScript, rhs: RunScript) -> Bool { + return lhs.script == rhs.script && + lhs.name == rhs.name && + lhs.script == rhs.script && + lhs.inputFiles == rhs.inputFiles && + lhs.outputFiles == rhs.outputFiles && + lhs.shell == rhs.shell + } +} + +extension RunScript: JSONObjectConvertible { + + public init(jsonDictionary: JSONDictionary) throws { + name = jsonDictionary.json(atKeyPath: "name") + inputFiles = jsonDictionary.json(atKeyPath: "inputFiles") ?? [] + outputFiles = jsonDictionary.json(atKeyPath: "outputFiles") ?? [] + + if let string: String = jsonDictionary.json(atKeyPath: "script") { + script = .script(string) + } else { + let path: String = try jsonDictionary.json(atKeyPath: "path") + script = .path(path) + } + shell = jsonDictionary.json(atKeyPath: "shell") + } +} diff --git a/Sources/ProjectSpec/Target.swift b/Sources/ProjectSpec/Target.swift index eb4755e8..44e67314 100644 --- a/Sources/ProjectSpec/Target.swift +++ b/Sources/ProjectSpec/Target.swift @@ -18,8 +18,8 @@ public struct Target { public var sources: [String] public var sourceExludes: [String] public var dependencies: [Dependency] - public var prebuildScripts: [String] - public var postbuildScripts: [String] + public var prebuildScripts: [RunScript] + public var postbuildScripts: [RunScript] public var configFiles: [String: String] public var generateSchemes: [String] @@ -31,7 +31,7 @@ public struct Target { return name } - public init(name: String, type: PBXProductType, platform: Platform, settings: Settings = .empty, configFiles: [String: String] = [:], sources: [String] = [], sourceExludes: [String] = [], dependencies: [Dependency] = [], prebuildScripts: [String] = [], postbuildScripts: [String] = [], generateSchemes: [String] = []) { + public init(name: String, type: PBXProductType, platform: Platform, settings: Settings = .empty, configFiles: [String: String] = [:], sources: [String] = [], sourceExludes: [String] = [], dependencies: [Dependency] = [], prebuildScripts: [RunScript] = [], postbuildScripts: [RunScript] = [], generateSchemes: [String] = []) { self.name = name self.type = type self.platform = platform diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index d42d7220..4f50df13 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -232,6 +232,32 @@ public class PBXProjGenerator { return Set(files.map { $0.buildFile.reference }) } + func getRunScript(runScript: RunScript) throws -> PBXShellScriptBuildPhase { + + let shellScript: String + switch runScript.script { + case let .path(path): + shellScript = try getPath(path).read() + case let .script(script): + shellScript = script + } + let escapedScript = shellScript.replacingOccurrences(of: "\"", with: "\\\"").replacingOccurrences(of: "\n", with: "\\n") + let shellScriptPhase = PBXShellScriptBuildPhase( + reference: generateUUID(PBXShellScriptBuildPhase.self, String(describing: runScript.name) + shellScript), + files: [], + name: runScript.name ?? "Run Script", + inputPaths: Set(runScript.inputFiles), + outputPaths: Set(runScript.outputFiles), + shellPath: runScript.shell ?? "/bin/sh", + shellScript: escapedScript) + + objects.append(.pbxShellScriptBuildPhase(shellScriptPhase)) + buildPhases.append(shellScriptPhase.reference) + return shellScriptPhase + } + + _ = try target.prebuildScripts.map(getRunScript) + let sourcesBuildPhase = PBXSourcesBuildPhase(reference: generateUUID(PBXSourcesBuildPhase.self, target.name), files: getBuildFilesForPhase(.sources)) objects.append(.pbxSourcesBuildPhase(sourcesBuildPhase)) buildPhases.append(sourcesBuildPhase.reference) @@ -277,6 +303,8 @@ public class PBXProjGenerator { } } + _ = try target.postbuildScripts.map(getRunScript) + let nativeTarget = PBXNativeTarget( reference: targetNativeReferences[target.name]!, buildConfigurationList: buildConfigList.reference, diff --git a/Sources/XcodeGenKit/ProjectGenerator.swift b/Sources/XcodeGenKit/ProjectGenerator.swift index 7f69d9a2..c0c99026 100644 --- a/Sources/XcodeGenKit/ProjectGenerator.swift +++ b/Sources/XcodeGenKit/ProjectGenerator.swift @@ -95,6 +95,15 @@ public class ProjectGenerator { } } + let scripts = target.prebuildScripts + target.postbuildScripts + for script in scripts { + if case let .path(path) = script.script { + if !getPath(path).exists { + errors.append(.invalidRunScriptPath(target: target.name, path: path)) + } + } + } + errors += validateSettings(target.settings) } @@ -217,6 +226,7 @@ public struct SpecValidationError: Error, CustomStringConvertible { case invalidSettingsPreset(String) case missingTargetSource(target: String, source: String) case invalidTargetGeneratedSchema(target: String, scheme: String, configType: ConfigType) + case invalidRunScriptPath(target: String, path: String) public var description: String { switch self { @@ -226,6 +236,7 @@ public struct SpecValidationError: Error, CustomStringConvertible { case let .invalidBuildSettingConfig(config): return "Build setting has invalid build configuration \(config.quoted)" case let .missingTargetSource(target, source): return "Target \(target.quoted) has a missing source directory \(source.quoted)" case let .invalidSettingsPreset(preset): return "Invalid settings preset \(preset.quoted)" + case let .invalidRunScriptPath(target, path): return "Target \(target.quoted) has a script path that doesn't exist \(path.quoted)" case let .invalidTargetGeneratedSchema(target, scheme, configType): return "Target \(target.quoted) has an invalid schema generation name which requires a config that has a \(configType.rawValue.quoted) type and contains the name \(scheme.quoted)" } } diff --git a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift index ca558b55..5556d4b0 100644 --- a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift @@ -91,6 +91,26 @@ func projectGeneratorTests() { try expect(dependencies.count) == 1 try expect(dependencies.first!.target) == nativeTargets.first { $0.name == framework.name }!.reference } + + $0.it("generates dependencies") { + let pbxProject = try getPbxProj(spec) + let nativeTargets = pbxProject.objects.nativeTargets + let dependencies = pbxProject.objects.targetDependencies + try expect(dependencies.count) == 1 + try expect(dependencies.first!.target) == nativeTargets.first { $0.name == framework.name }!.reference + } + + $0.it("generates run scripts") { + var scriptSpec = spec + scriptSpec.targets[0].prebuildScripts = [RunScript(script: .script("swiftlint"), name: "Swiftlint")] + let pbxProject = try getPbxProj(scriptSpec) + + let scripts = pbxProject.objects.shellScriptBuildPhases + try expect(scripts.count) == 1 + guard let script = scripts.first else { throw failure("Run script not found") } + + try expect(script.shellScript) == "swiftlint" + } } $0.describe("Schemes") { diff --git a/Tests/XcodeGenKitTests/SpecLoadingTests.swift b/Tests/XcodeGenKitTests/SpecLoadingTests.swift index f5632106..7cacfb15 100644 --- a/Tests/XcodeGenKitTests/SpecLoadingTests.swift +++ b/Tests/XcodeGenKitTests/SpecLoadingTests.swift @@ -100,5 +100,24 @@ func specLoadingTests() { try expect(spec.settingPresets["preset7"]) == preset7 try expect(spec.settingPresets["preset8"]) == preset8 } + + $0.it("parses run scripts") { + var target = validTarget + let scripts: [[String: Any]] = [ + ["path": "script.sh"], + ["script": "shell script\ndo thing", "name": "myscript", "inputFiles": ["file","file2"], "outputFiles": ["file","file2"], "shell": "bin/customshell"], + ] + target["prebuildScripts"] = scripts + target["postbuildScripts"] = scripts + + let expectedScripts = [ + RunScript(script: .path("script.sh")), + RunScript(script: .script("shell script\ndo thing"), name: "myscript", inputFiles: ["file","file2"], outputFiles: ["file","file2"], shell: "bin/customshell"), + ] + + let parsedTarget = try Target(jsonDictionary: target) + try expect(parsedTarget.prebuildScripts) == expectedScripts + try expect(parsedTarget.postbuildScripts) == expectedScripts + } } } diff --git a/docs/ProjectSpec.md b/docs/ProjectSpec.md index 9d1ab65f..4ae0e370 100644 --- a/docs/ProjectSpec.md +++ b/docs/ProjectSpec.md @@ -146,8 +146,42 @@ targets: Release: config_files/release.xcconfig ``` +#### Run Scripts +Run script phases can be added via **prebuildScripts** or **postBuildScripts** which run before or after any other build phases respectively. They run in the order defined. Each script can contain + +- **path:** a relative or absolute path to a shell script +- **script:** an inline shell script +- **name:** optional name of a script. Defaults to `Run Script` +- **inputFiles:** optional list of input files +- **outputFiles:** optional list of output files +- **shell:** optional shell used for the script. Defaults to `/bin/sh` + +Either a **path** or **script** must be defined, the rest are optional. + +A multiline script can be written using the various YAML multiline methods, for example with `|` as below: + +```yaml +targets: + - name: MyTarget + prebuildScripts: + - path: myscripts/my_script.sh + name: My Script + inputFiles: + - $(SRCROOT)/file1 + - $(SRCROOT)/file2 + outputFiles: + - $(DERIVED_FILE_DIR)/file1 + - $(DERIVED_FILE_DIR)/file2 + postbuildScripts: + - script: swiftlint + name: Swiftlint + - script: | + command do + othercommand +``` + #### generateSchemes -This is a conveniance used to automatically generate schemes for a target based on large amount of configs. A list of names is provided, then for each of these names a scheme is created, using configs that contain the name with debug and release variants. This is useful for having different environment schemes. +This is a convenience used to automatically generate schemes for a target based on large amount of configs. A list of names is provided, then for each of these names a scheme is created, using configs that contain the name with debug and release variants. This is useful for having different environment schemes. For example, the following spec would create 3 schemes called: From 63d98adb65a28c75100128fc74989d5517504780 Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Wed, 2 Aug 2017 12:54:07 +0200 Subject: [PATCH 3/5] user PathKit for resolving absolute paths --- Sources/XcodeGenKit/PBXProjGenerator.swift | 15 +++------------ Sources/XcodeGenKit/ProjectGenerator.swift | 18 +++++------------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 4f50df13..6318ab10 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -42,15 +42,6 @@ public class PBXProjGenerator { projectReference = generateUUID(PBXProject.self, spec.name) } - func getPath(_ path: String) -> Path { - let currentPath = Path(path) - if currentPath.isRelative { - return basePath + currentPath - } else { - return currentPath - } - } - public func generateUUID(_ element: T.Type, _ id: String) -> String { var uuid: String = "" var counter: UInt = 0 @@ -141,7 +132,7 @@ public class PBXProjGenerator { func generateTarget(_ target: Target) throws -> PBXNativeTarget { - let sourcePaths = target.sources.map { getPath($0) } + let sourcePaths = target.sources.map { basePath + $0 } var sourceFilePaths: [Path] = [] for source in sourcePaths { @@ -154,7 +145,7 @@ public class PBXProjGenerator { var baseConfigurationReference: String? if let configPath = target.configFiles[config.name] { - let path = getPath(configPath) + let path = basePath + configPath baseConfigurationReference = fileReferencesByPath[path] } return XCBuildConfiguration(reference: generateUUID(XCBuildConfiguration.self, config.name), name: config.name, baseConfigurationReference: baseConfigurationReference, buildSettings: buildSettings) @@ -237,7 +228,7 @@ public class PBXProjGenerator { let shellScript: String switch runScript.script { case let .path(path): - shellScript = try getPath(path).read() + shellScript = try (basePath + path).read() case let .script(script): shellScript = script } diff --git a/Sources/XcodeGenKit/ProjectGenerator.swift b/Sources/XcodeGenKit/ProjectGenerator.swift index c0c99026..72b7e82a 100644 --- a/Sources/XcodeGenKit/ProjectGenerator.swift +++ b/Sources/XcodeGenKit/ProjectGenerator.swift @@ -33,15 +33,6 @@ public class ProjectGenerator { return spec.configs.first { $0.type == .release }! } - func getPath(_ path: String) -> Path { - let currentPath = Path(path) - if currentPath.isRelative { - return self.path + currentPath - } else { - return currentPath - } - } - public func validate() throws { if spec.configs.isEmpty { @@ -80,7 +71,7 @@ public class ProjectGenerator { } for source in target.sources { - let sourcePath = getPath(source) + let sourcePath = path + source if !sourcePath.exists { errors.append(.missingTargetSource(target: target.name, source: sourcePath.string)) } @@ -97,9 +88,10 @@ public class ProjectGenerator { let scripts = target.prebuildScripts + target.postbuildScripts for script in scripts { - if case let .path(path) = script.script { - if !getPath(path).exists { - errors.append(.invalidRunScriptPath(target: target.name, path: path)) + if case let .path(pathString) = script.script { + let scriptPath = path + pathString + if !scriptPath.exists { + errors.append(.invalidRunScriptPath(target: target.name, path: pathString)) } } } From 63bdd95a6bea5b97e6a69bf147e01f1506813c53 Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Wed, 2 Aug 2017 13:27:57 +0200 Subject: [PATCH 4/5] add more run script tests --- .../ProjectGeneratorTests.swift | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift index 5556d4b0..b007f36f 100644 --- a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift @@ -24,6 +24,8 @@ func projectGeneratorTests() { let framework = Target(name: "MyFramework", type: .framework, platform: .iOS, settings: Settings(buildSettings: BuildSettings(dictionary: ["SETTING_2": "VALUE"]))) + let targets = [application, framework] + $0.describe("Config") { $0.it("generates config defaults") { @@ -74,7 +76,7 @@ func projectGeneratorTests() { $0.describe("Targets") { - let spec = ProjectSpec(name: "test", targets: [application, framework]) + let spec = ProjectSpec(name: "test", targets: targets) $0.it("generates targets") { let pbxProject = try getPbxProj(spec) @@ -102,14 +104,21 @@ func projectGeneratorTests() { $0.it("generates run scripts") { var scriptSpec = spec - scriptSpec.targets[0].prebuildScripts = [RunScript(script: .script("swiftlint"), name: "Swiftlint")] + scriptSpec.targets[0].prebuildScripts = [RunScript(script: .script("script1"))] + scriptSpec.targets[0].postbuildScripts = [RunScript(script: .script("script2"))] let pbxProject = try getPbxProj(scriptSpec) - let scripts = pbxProject.objects.shellScriptBuildPhases - try expect(scripts.count) == 1 - guard let script = scripts.first else { throw failure("Run script not found") } + guard let buildPhases = pbxProject.objects.nativeTargets.first?.buildPhases else { throw failure("Build phases not found") } - try expect(script.shellScript) == "swiftlint" + let scripts = pbxProject.objects.shellScriptBuildPhases + let script1 = scripts[0] + let script2 = scripts[1] + try expect(scripts.count) == 2 + try expect(buildPhases.first) == script1.reference + try expect(buildPhases.last) == script2.reference + + try expect(script1.shellScript) == "script1" + try expect(script2.shellScript) == "script2" } } From b71eb02496bcd29335db72f393bf42c70a0f1544 Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Wed, 2 Aug 2017 13:29:45 +0200 Subject: [PATCH 5/5] fix duplicate uuids --- .../project.pbxproj | 32 +++++++++---------- Sources/XcodeGenKit/PBXProjGenerator.swift | 1 + 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj b/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj index 29435aad..7fa0aeae 100644 --- a/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj +++ b/Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj @@ -10,8 +10,8 @@ BF1073850101 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FR1332263601 /* AppDelegate.swift */; settings = {}; }; BF1744565901 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FR6218091901 /* ViewController.swift */; settings = {}; }; BF2753556301 /* MyFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FR2993497801 /* MyFramework.framework */; }; - BF2753556301 /* MyFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FR2993497801 /* MyFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; BF2753556301 /* MyFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FR2993497801 /* MyFramework.framework */; }; + BF2753556302 = {isa = PBXBuildFile; fileRef = FR2993497801 /* MyFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; BF3154421201 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FR5980633301 /* Assets.xcassets */; settings = {}; }; BF3515549501 /* MyFramework.h in Headers */ = {isa = PBXBuildFile; fileRef = FR7740960501 /* MyFramework.h */; settings = {ATTRIBUTES = (Public, ); }; }; BF5986511201 = {isa = PBXBuildFile; fileRef = FR6523263101 /* TestProject.app */; }; @@ -44,7 +44,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - BF2753556301 /* MyFramework.framework in CopyFiles */, + BF2753556302 /* MyFramework.framework in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -53,10 +53,10 @@ /* Begin PBXFileReference section */ FR1332263601 /* AppDelegate.swift */ = {isa = PBXFileReference; path = AppDelegate.swift; sourceTree = ""; }; FR1345298501 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = ""; }; - FR1345298501 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = ""; }; + FR1345298502 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = ""; }; FR2993497801 /* MyFramework.framework */ = {isa = PBXFileReference; explicitFileType = framework; includeInIndex = 0; path = MyFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; }; FR3676338401 /* Base */ = {isa = PBXFileReference; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; - FR3676338401 /* Base */ = {isa = PBXFileReference; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + FR3676338402 /* Base */ = {isa = PBXFileReference; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; FR5980633301 /* Assets.xcassets */ = {isa = PBXFileReference; path = Assets.xcassets; sourceTree = ""; }; FR6218091901 /* ViewController.swift */ = {isa = PBXFileReference; path = ViewController.swift; sourceTree = ""; }; FR6523263101 /* TestProject.app */ = {isa = PBXFileReference; explicitFileType = app; includeInIndex = 0; path = TestProject.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -87,7 +87,7 @@ isa = PBXGroup; children = ( FR7078510801 /* FrameworkFile.swift */, - FR1345298501 /* Info.plist */, + FR1345298502 /* Info.plist */, FR7740960501 /* MyFramework.h */, ); name = MyFramework; @@ -156,7 +156,7 @@ HBP299349701 /* Headers */, FBP299349701 /* Frameworks */, CFBP29934901 /* Copy Files */, - SSBP25860701 /* Run Script */, + SSBP25860702 /* Run Script */, ); buildRules = ( ); @@ -241,7 +241,7 @@ shellPath = /bin/sh; shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n"; }; - SSBP25860701 /* Run Script */ = { + SSBP25860702 /* Run Script */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( @@ -289,7 +289,7 @@ VG1473702401 /* Main.storyboard */ = { isa = PBXVariantGroup; children = ( - FR3676338401 /* Base */, + FR3676338402 /* Base */, ); name = Main.storyboard; sourceTree = ""; @@ -356,7 +356,7 @@ }; name = Debug; }; - XCBC47994501 /* Debug */ = { + XCBC47994502 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; @@ -369,7 +369,7 @@ }; name = Debug; }; - XCBC47994501 /* Debug */ = { + XCBC47994503 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; @@ -430,7 +430,7 @@ }; name = Release; }; - XCBC88111401 /* Release */ = { + XCBC88111402 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; @@ -443,7 +443,7 @@ }; name = Release; }; - XCBC88111401 /* Release */ = { + XCBC88111403 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; @@ -466,8 +466,8 @@ XCCL29934901 /* Build configuration list for PBXNativeTarget "MyFramework" */ = { isa = XCConfigurationList; buildConfigurations = ( - XCBC88111401 /* Release */, - XCBC47994501 /* Debug */, + XCBC88111403 /* Release */, + XCBC47994503 /* Debug */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = ""; @@ -475,8 +475,8 @@ XCCL65232601 /* Build configuration list for PBXNativeTarget "TestProject" */ = { isa = XCConfigurationList; buildConfigurations = ( - XCBC88111401 /* Release */, - XCBC47994501 /* Debug */, + XCBC47994502 /* Debug */, + XCBC88111402 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = ""; diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 6318ab10..957ca443 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -52,6 +52,7 @@ public class PBXProjGenerator { counter += 1 uuid = "\(classAcronym)\(stringID)\(String(format: "%02d", counter))" } while (uuids.contains(uuid)) + uuids.insert(uuid) return uuid }