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

Permit scheme build stage to be optional #1303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions Sources/ProjectSpec/Scheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public typealias BuildType = XCScheme.BuildAction.Entry.BuildFor
public struct Scheme: Equatable {

public var name: String
public var build: Build
public var build: Build!
Copy link
Author

Choose a reason for hiding this comment

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

This should of course should be Build?, but changing to a full optional led to many type-inference errors in SpecLoadingTests that I wasn't able to easily fix

public var run: Run?
public var archive: Archive?
public var analyze: Analyze?
Expand All @@ -17,7 +17,7 @@ public struct Scheme: Equatable {

public init(
name: String,
build: Build,
build: Build? = nil,
run: Run? = nil,
test: Test? = nil,
profile: Profile? = nil,
Expand Down Expand Up @@ -712,7 +712,7 @@ extension Scheme: NamedJSONDictionaryConvertible {

public init(name: String, jsonDictionary: JSONDictionary) throws {
self.name = name
build = try jsonDictionary.json(atKeyPath: "build")
build = jsonDictionary.json(atKeyPath: "build")
run = jsonDictionary.json(atKeyPath: "run")
test = jsonDictionary.json(atKeyPath: "test")
analyze = jsonDictionary.json(atKeyPath: "analyze")
Expand All @@ -724,7 +724,7 @@ extension Scheme: NamedJSONDictionaryConvertible {
extension Scheme: JSONEncodable {
public func toJSONValue() -> Any {
[
"build": build.toJSONValue(),
"build": build?.toJSONValue(),
"run": run?.toJSONValue(),
"test": test?.toJSONValue(),
"analyze": analyze?.toJSONValue(),
Expand Down
8 changes: 5 additions & 3 deletions Sources/ProjectSpec/SpecValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ extension Project {
}

for scheme in schemes {
errors.append(
contentsOf: scheme.build.targets.compactMap { validationError(for: $0.target, in: scheme, action: "build") }
)
if let action = scheme.build {
errors.append(contentsOf: action.targets.compactMap {
validationError(for: $0.target, in: scheme, action: "build")
})
}
if let action = scheme.run, let config = action.config, getConfig(config) == nil {
errors.append(.invalidSchemeConfig(scheme: scheme.name, config: config))
}
Expand Down
56 changes: 34 additions & 22 deletions Sources/XcodeGenKit/SchemeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class SchemeGenerator {

let testBuildTargetEntries = try testBuildTargets.map(getBuildEntry)

let buildActionEntries: [XCScheme.BuildAction.Entry] = try scheme.build.targets.map(getBuildEntry)
let buildActionEntries: [XCScheme.BuildAction.Entry] = try scheme.build?.targets.map(getBuildEntry) ?? []

func getExecutionAction(_ action: Scheme.ExecutionAction) -> XCScheme.ExecutionAction {
// ExecutionActions can require the use of build settings. Xcode allows the settings to come from a build or test target.
Expand All @@ -183,27 +183,42 @@ public class SchemeGenerator {

if let targetName = scheme.run?.executable {
schemeTarget = project.getTarget(targetName)
} else {
guard let firstTarget = scheme.build.targets.first else {
throw SchemeGenerationError.missingBuildTargets(scheme.name)
}
let name = scheme.build.targets.first { $0.buildTypes.contains(.running) }?.target.name ?? firstTarget.target.name
} else if
let targets = scheme.build?.targets,
let firstTarget = targets.first
{
let name = targets.first { $0.buildTypes.contains(.running) }?.target.name ?? firstTarget.target.name
schemeTarget = target ?? project.getTarget(name)
} else {
schemeTarget = nil
}

let shouldExecuteOnLaunch = schemeTarget?.shouldExecuteOnLaunch == true
let buildableReference: XCScheme.BuildableReference?
let buildAction: XCScheme.BuildAction?
let runnables: (launch: XCScheme.Runnable, profile: XCScheme.BuildableProductRunnable)?

if let buildableReferenceCandidate = buildActionEntries.first(where: { $0.buildableReference.blueprintName == schemeTarget?.name })?.buildableReference ?? buildActionEntries.first?.buildableReference
{
runnables = makeProductRunnables(for: schemeTarget, buildableReference: buildableReferenceCandidate)

buildableReference = buildableReferenceCandidate
buildAction = scheme.build.flatMap { build in
XCScheme.BuildAction(
buildActionEntries: buildActionEntries,
preActions: build.preActions.map(getExecutionAction),
postActions: build.postActions.map(getExecutionAction),
parallelizeBuild: build.parallelizeBuild,
buildImplicitDependencies: build.buildImplicitDependencies,
runPostActionsOnFailure: build.runPostActionsOnFailure
)
}
} else {
buildableReference = nil
buildAction = nil
runnables = nil
}

let buildableReference = buildActionEntries.first(where: { $0.buildableReference.blueprintName == schemeTarget?.name })?.buildableReference ?? buildActionEntries.first!.buildableReference
let runnables = makeProductRunnables(for: schemeTarget, buildableReference: buildableReference)

let buildAction = XCScheme.BuildAction(
buildActionEntries: buildActionEntries,
preActions: scheme.build.preActions.map(getExecutionAction),
postActions: scheme.build.postActions.map(getExecutionAction),
parallelizeBuild: scheme.build.parallelizeBuild,
buildImplicitDependencies: scheme.build.buildImplicitDependencies,
runPostActionsOnFailure: scheme.build.runPostActionsOnFailure
)

let testables: [XCScheme.TestableReference] = zip(testTargets, testBuildTargetEntries).map { testTarget, testBuildEntries in

Expand Down Expand Up @@ -298,7 +313,7 @@ public class SchemeGenerator {
}

let launchAction = XCScheme.LaunchAction(
runnable: shouldExecuteOnLaunch ? runnables.launch : nil,
runnable: shouldExecuteOnLaunch ? runnables?.launch : nil,
buildConfiguration: scheme.run?.config ?? defaultDebugConfig.name,
preActions: scheme.run?.preActions.map(getExecutionAction) ?? [],
postActions: scheme.run?.postActions.map(getExecutionAction) ?? [],
Expand All @@ -321,7 +336,7 @@ public class SchemeGenerator {
)

let profileAction = XCScheme.ProfileAction(
buildableProductRunnable: shouldExecuteOnLaunch ? runnables.profile : nil,
buildableProductRunnable: shouldExecuteOnLaunch ? runnables?.profile : nil,
buildConfiguration: scheme.profile?.config ?? defaultReleaseConfig.name,
preActions: scheme.profile?.preActions.map(getExecutionAction) ?? [],
postActions: scheme.profile?.postActions.map(getExecutionAction) ?? [],
Expand Down Expand Up @@ -403,16 +418,13 @@ enum SchemeGenerationError: Error, CustomStringConvertible {
case missingTarget(TargetReference, projectPath: String)
case missingPackage(String)
case missingProject(String)
case missingBuildTargets(String)

var description: String {
switch self {
case .missingTarget(let target, let projectPath):
return "Unable to find target named \"\(target)\" in \"\(projectPath)\""
case .missingProject(let project):
return "Unable to find project reference named \"\(project)\" in project.yml"
case .missingBuildTargets(let name):
return "Unable to find at least one build target in scheme \"\(name)\""
case .missingPackage(let package):
return "Unable to find swift package named \"\(package)\" in project.yml"
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/Fixtures/test_only.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: TestOnly
targets:
Target:
type: application
platform: iOS
sources:
- Target
schemes:
TestOnly:
test:
targets:
- Target
Comment on lines +10 to +12
Copy link
Author

Choose a reason for hiding this comment

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

Use case: scheme has only a test action, no build

9 changes: 9 additions & 0 deletions Tests/ProjectSpecTests/SpecLoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class SpecLoadingTests: XCTestCase {
]
}

$0.it("accepts schemes that have no build phase") {
let path = fixturePath + "test_only.yml"
let project = try loadSpec(path: path)
try expect(project.name) == "TestOnly"
try expect(project.schemes.count) == 1
try expect(project.schemes.first?.build).to.beNil()
try expect(project.schemes.first?.test?.targets.first?.name) == "Target"
}

$0.it("expands directories") {
let path = fixturePath + "paths_test.yml"
let project = try loadSpec(path: path)
Expand Down