Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run scripts #17

Merged
merged 5 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions Fixtures/TestProject/GeneratedProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -44,7 +44,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
BF2753556301 /* MyFramework.framework in CopyFiles */,
BF2753556302 /* MyFramework.framework in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -53,10 +53,10 @@
/* Begin PBXFileReference section */
FR1332263601 /* AppDelegate.swift */ = {isa = PBXFileReference; path = AppDelegate.swift; sourceTree = "<group>"; };
FR1345298501 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = "<group>"; };
FR1345298501 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = "<group>"; };
FR1345298502 /* Info.plist */ = {isa = PBXFileReference; path = Info.plist; sourceTree = "<group>"; };
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 = "<group>"; };
FR3676338401 /* Base */ = {isa = PBXFileReference; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
FR3676338402 /* Base */ = {isa = PBXFileReference; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
FR5980633301 /* Assets.xcassets */ = {isa = PBXFileReference; path = Assets.xcassets; sourceTree = "<group>"; };
FR6218091901 /* ViewController.swift */ = {isa = PBXFileReference; path = ViewController.swift; sourceTree = "<group>"; };
FR6523263101 /* TestProject.app */ = {isa = PBXFileReference; explicitFileType = app; includeInIndex = 0; path = TestProject.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -87,7 +87,7 @@
isa = PBXGroup;
children = (
FR7078510801 /* FrameworkFile.swift */,
FR1345298501 /* Info.plist */,
FR1345298502 /* Info.plist */,
FR7740960501 /* MyFramework.h */,
);
name = MyFramework;
Expand Down Expand Up @@ -156,6 +156,7 @@
HBP299349701 /* Headers */,
FBP299349701 /* Frameworks */,
CFBP29934901 /* Copy Files */,
SSBP25860702 /* Run Script */,
);
buildRules = (
);
Expand All @@ -174,6 +175,7 @@
HBP652326301 /* Headers */,
FBP652326301 /* Frameworks */,
CFBP65232601 /* Copy Files */,
SSBP25860701 /* Run Script */,
);
buildRules = (
);
Expand Down Expand Up @@ -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";
};
SSBP25860702 /* 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;
Expand Down Expand Up @@ -256,7 +289,7 @@
VG1473702401 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
FR3676338401 /* Base */,
FR3676338402 /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
Expand Down Expand Up @@ -323,7 +356,7 @@
};
name = Debug;
};
XCBC47994501 /* Debug */ = {
XCBC47994502 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
Expand All @@ -336,7 +369,7 @@
};
name = Debug;
};
XCBC47994501 /* Debug */ = {
XCBC47994503 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
Expand Down Expand Up @@ -397,7 +430,7 @@
};
name = Release;
};
XCBC88111401 /* Release */ = {
XCBC88111402 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
Expand All @@ -410,7 +443,7 @@
};
name = Release;
};
XCBC88111401 /* Release */ = {
XCBC88111403 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
Expand All @@ -433,17 +466,17 @@
XCCL29934901 /* Build configuration list for PBXNativeTarget "MyFramework" */ = {
isa = XCConfigurationList;
buildConfigurations = (
XCBC88111401 /* Release */,
XCBC47994501 /* Debug */,
XCBC88111403 /* Release */,
XCBC47994503 /* Debug */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = "";
};
XCCL65232601 /* Build configuration list for PBXNativeTarget "TestProject" */ = {
isa = XCConfigurationList;
buildConfigurations = (
XCBC88111401 /* Release */,
XCBC47994501 /* Debug */,
XCBC47994502 /* Debug */,
XCBC88111402 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = "";
Expand Down
34 changes: 34 additions & 0 deletions Fixtures/TestProject/TestProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
CCA4B6071F1FC00500DF34A1 /* Frameworks */,
CCA4B6081F1FC00500DF34A1 /* Resources */,
CCA71FF01F225E4C00F772C1 /* Embed Frameworks */,
CC2DB89A1F30E29600B4B0FA /* Swiftlint */,
);
buildRules = (
);
Expand All @@ -157,6 +158,7 @@
CCA71FE41F225E4C00F772C1 /* Frameworks */,
CCA71FE51F225E4C00F772C1 /* Headers */,
CCA71FE61F225E4C00F772C1 /* Resources */,
CC2DB89B1F30E2AC00B4B0FA /* Swiftlint */,
);
buildRules = (
);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -475,6 +508,7 @@
CCA71FF31F225E4C00F772C1 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
5 changes: 5 additions & 0 deletions Fixtures/TestProject/scripts/swiftlint.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Fixtures/TestProject/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 66 additions & 0 deletions Sources/ProjectSpec/RunScript.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
6 changes: 3 additions & 3 deletions Sources/ProjectSpec/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -223,6 +224,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 (basePath + 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",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making shell non-optional and using a default value in the RunScript constructor with /bin/sh. Since the shell is required for creating a PBXShellScriptBuildPhase I'd make it non-optional here as well. What do you think?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be an option. I've been trying to keep any specific knowledge out of ProjectSpec though, and just keep it dumb.
Also I think that /bin/sh should be the default value in the xcodeproj constructor, and I didn't want to double up work there. It's something we still need to add to xcodeproj though

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)
Expand Down Expand Up @@ -268,6 +295,8 @@ public class PBXProjGenerator {
}
}

_ = try target.postbuildScripts.map(getRunScript)

let nativeTarget = PBXNativeTarget(
reference: targetNativeReferences[target.name]!,
buildConfigurationList: buildConfigList.reference,
Expand Down
Loading