Skip to content

Commit

Permalink
Added option to specify name and position.
Browse files Browse the repository at this point in the history
  • Loading branch information
PKizzle committed Jun 1, 2020
1 parent ffc73d0 commit 5710514
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
24 changes: 12 additions & 12 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/tadija/AEXML",
"state": {
"branch": null,
"revision": "54bb8ea6fb693dd3f92a89e5fcc19e199fdeedd0",
"version": "4.3.3"
"revision": "e4d517844dd03dac557e35d77a8e9ab438de91a6",
"version": "4.4.0"
}
},
{
"package": "PathKit",
"repositoryURL": "https://github.com/kylef/PathKit",
"state": {
"branch": null,
"revision": "e2f5be30e4c8f531c9c1e8765aa7b71c0a45d7a0",
"version": "0.9.2"
"revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511",
"version": "1.0.0"
}
},
{
Expand All @@ -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"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
14 changes: 13 additions & 1 deletion Sources/xcodeproj-modify/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct Arguments {
case missingCommand
case unknownCommand(String)
case missingTarget
case missingName
case missingPosition
case missingContents

var errorDescription: String? {
Expand All @@ -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"
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/xcodeproj-modify/Command.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation

enum Command {
case addRunScriptPhase(target: String, contents: String)
case addRunScriptPhase(target: String, position: Int, name: String, contents: String)
}
36 changes: 29 additions & 7 deletions Sources/xcodeproj-modify/XcodeprojModify.swift
Original file line number Diff line number Diff line change
@@ -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)"
}
}
}
Expand Down Expand Up @@ -39,23 +42,42 @@ 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)
guard !targets.isEmpty else {
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")
}
}

0 comments on commit 5710514

Please sign in to comment.