diff --git a/Package.resolved b/Package.resolved index d9ce4af..601c8cc 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/tadija/AEXML", "state": { "branch": null, - "revision": "54bb8ea6fb693dd3f92a89e5fcc19e199fdeedd0", - "version": "4.3.3" + "revision": "e4d517844dd03dac557e35d77a8e9ab438de91a6", + "version": "4.4.0" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/kylef/PathKit", "state": { "branch": null, - "revision": "e2f5be30e4c8f531c9c1e8765aa7b71c0a45d7a0", - "version": "0.9.2" + "revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511", + "version": "1.0.0" } }, { @@ -29,21 +29,21 @@ } }, { - "package": "SwiftShell", - "repositoryURL": "https://github.com/kareman/SwiftShell", + "package": "XcodeProj", + "repositoryURL": "https://github.com/tuist/xcodeproj.git", "state": { "branch": null, - "revision": "beebe43c986d89ea5359ac3adcb42dac94e5e08a", - "version": "4.1.2" + "revision": "912d40cc2ea4a300eff6dd7c6a10b4f4dedbcbec", + "version": "7.10.0" } }, { - "package": "xcodeproj", - "repositoryURL": "https://github.com/tuist/xcodeproj.git", + "package": "XcodeProjCExt", + "repositoryURL": "https://github.com/tuist/XcodeProjCExt", "state": { "branch": null, - "revision": "4d31d2be2532e9213d58cd4e0b15588c5dfae42d", - "version": "6.5.0" + "revision": "21a510c225ff2bc83d5920a21d902af4b1e7e218", + "version": "0.1.0" } } ] diff --git a/Package.swift b/Package.swift index 58ff29b..a6dfaf9 100644 --- a/Package.swift +++ b/Package.swift @@ -12,12 +12,12 @@ let package = Package( ) ], dependencies: [ - .package(url: "https://github.com/tuist/xcodeproj.git", from: "6.2.0"), + .package(url: "https://github.com/tuist/xcodeproj.git", .upToNextMajor(from: "7.10.0")), ], targets: [ .target( name: "xcodeproj-modify", - dependencies: ["xcodeproj"]), + dependencies: ["XcodeProj"]), .testTarget( name: "xcodeproj-modifyTests", dependencies: ["xcodeproj-modify"]), diff --git a/Sources/xcodeproj-modify/Arguments.swift b/Sources/xcodeproj-modify/Arguments.swift index e0c8fbf..ff9f51b 100644 --- a/Sources/xcodeproj-modify/Arguments.swift +++ b/Sources/xcodeproj-modify/Arguments.swift @@ -9,6 +9,8 @@ struct Arguments { case missingCommand case unknownCommand(String) case missingTarget + case missingName + case missingPosition case missingContents var errorDescription: String? { @@ -21,6 +23,10 @@ struct Arguments { return "Unknown command: \(command)" case .missingTarget: return "Please specify a target as the first argument after add-run-script-phase" + case .missingPosition: + return "Please specify the position the build script shall be injected at" + case .missingName: + return "Please specify a build phase name" case .missingContents: return "Please specify shell script contents as the second argument after add-run-script-phase" } @@ -50,10 +56,16 @@ struct Arguments { guard let target = iterator.next() else { throw Error.missingTarget } + guard let position = Int(iterator.next() ?? "-1") else { + throw Error.missingPosition + } + guard let name = iterator.next() else { + throw Error.missingName + } guard let contents = iterator.next() else { throw Error.missingContents } - return Command.addRunScriptPhase(target: target, contents: contents) + return Command.addRunScriptPhase(target: target, position: position, name: name, contents: contents) default: throw Error.unknownCommand(commandName) diff --git a/Sources/xcodeproj-modify/Command.swift b/Sources/xcodeproj-modify/Command.swift index 62544b3..a4a3d2c 100644 --- a/Sources/xcodeproj-modify/Command.swift +++ b/Sources/xcodeproj-modify/Command.swift @@ -1,5 +1,5 @@ import Foundation enum Command { - case addRunScriptPhase(target: String, contents: String) + case addRunScriptPhase(target: String, position: Int, name: String, contents: String) } diff --git a/Sources/xcodeproj-modify/XcodeprojModify.swift b/Sources/xcodeproj-modify/XcodeprojModify.swift index 43d6fa8..0092407 100644 --- a/Sources/xcodeproj-modify/XcodeprojModify.swift +++ b/Sources/xcodeproj-modify/XcodeprojModify.swift @@ -1,15 +1,18 @@ import Foundation import PathKit -import xcodeproj +import XcodeProj struct XcodeprojModify { enum Error: LocalizedError { case unknownTarget(String) + case invalidPosition(Int) var errorDescription: String? { switch self { case .unknownTarget(let string): return "Couldn't find target named \(string)" + case .invalidPosition(let int): + return "Unable to place build script at position \(int)" } } } @@ -39,12 +42,12 @@ struct XcodeprojModify { private func runCommand(_ command: Command, with arguments: Arguments) throws { switch command { - case .addRunScriptPhase(let target, let contents): - try addRunScriptPhase(target: target, contents: contents, xcodeprojPath: arguments.xcodeprojPath) + case .addRunScriptPhase(let target, let position, let name, let contents): + try addRunScriptPhase(target: target, position: position, name: name, contents: contents, xcodeprojPath: arguments.xcodeprojPath) } } - private func addRunScriptPhase(target: String, contents: String, xcodeprojPath: String) throws { + private func addRunScriptPhase(target: String, position: Int, name: String, contents: String, xcodeprojPath: String) throws { let path = Path(xcodeprojPath) let xcodeproj = try XcodeProj(path: path) let targets = xcodeproj.pbxproj.targets(named: target) @@ -52,10 +55,29 @@ struct XcodeprojModify { throw Error.unknownTarget(target) } - let phase = PBXShellScriptBuildPhase(shellScript: contents) + let phase = PBXShellScriptBuildPhase(name: name, shellScript: contents) for target in targets { - target.buildPhases = target.buildPhases + [phase] + var buildPhases = target.buildPhases.filter { + ($0 as? PBXShellScriptBuildPhase)?.name != phase.name + } + + if position < 0 { + buildPhases.append(phase) + } else { + guard position < buildPhases.count else { + throw Error.invalidPosition(position) + } + buildPhases.insert(phase, at: position) + } + target.buildPhases = buildPhases + print("Added build phase") } - try xcodeproj.write(path: path) + xcodeproj.pbxproj.add(object: phase) + try xcodeproj.writePBXProj( + path: path, + override: true, + outputSettings: PBXOutputSettings() + ) + print("Successfully wrote data") } }