Skip to content

Commit

Permalink
Added schemes_arguments to configuration (#699)
Browse files Browse the repository at this point in the history
Co-authored-by: amseddi <amseddi>
  • Loading branch information
amseddi authored May 19, 2024
1 parent 742be59 commit 4bd56a6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions Sources/Shared/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public final class Configuration {
@Setting(key: "build_arguments", defaultValue: [])
public var buildArguments: [String]

@Setting(key: "schemes_arguments", defaultValue: [])
public var schemesArguments: [String]

@Setting(key: "retain_assign_only_property_types", defaultValue: [], valueSanitizer: PropertyTypeSanitizer.sanitize)
public var retainAssignOnlyPropertyTypes: [String]

Expand Down Expand Up @@ -347,6 +350,8 @@ public final class Configuration {
$cleanBuild.assign(value)
case $buildArguments.key:
$buildArguments.assign(value)
case $schemesArguments.key:
$schemesArguments.assign(value)
case $relativeResults.key:
$relativeResults.assign(value)
case $retainCodableProperties.key:
Expand Down
4 changes: 2 additions & 2 deletions Sources/XcodeSupport/XcodeProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ final class XcodeProject: XcodeProjectlike {
}
}

func schemes() throws -> Set<String> {
try xcodebuild.schemes(project: self)
func schemes(additionalArguments: [String]) throws -> Set<String> {
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/XcodeSupport/XcodeProjectDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public final class XcodeProjectDriver {
}

// Ensure schemes exist within the project
let schemes = try project.schemes().filter { configuration.schemes.contains($0) }
let schemes = try project.schemes(
additionalArguments: configuration.schemesArguments
).filter { configuration.schemes.contains($0) }
let validSchemeNames = schemes.mapSet { $0 }

if let scheme = Set(configuration.schemes).subtracting(validSchemeNames).first {
Expand Down
11 changes: 9 additions & 2 deletions Sources/XcodeSupport/XcodeProjectSetupGuide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public final class XcodeProjectSetupGuide: SetupGuideHelpers, ProjectSetupGuide
print(colorize("Select build targets to analyze:", .bold))
configuration.targets = select(multiple: targets, allowAll: true).selectedValues

let schemes = try filter(project.schemes(), project).map { $0 }.sorted()
let schemes = try filter(
project.schemes(additionalArguments: configuration.schemesArguments),
project
).map { $0 }.sorted()

print(colorize("\nSelect the schemes necessary to build your chosen targets:", .bold))
configuration.schemes = select(multiple: schemes, allowAll: false).selectedValues
Expand Down Expand Up @@ -82,7 +85,11 @@ public final class XcodeProjectSetupGuide: SetupGuideHelpers, ProjectSetupGuide
private func getPodSchemes(in project: XcodeProjectlike) throws -> Set<String> {
let path = project.sourceRoot.appending("Pods/Pods.xcodeproj")
guard path.exists else { return [] }
return try xcodebuild.schemes(type: "project", path: path.lexicallyNormalized().string)
return try xcodebuild.schemes(
type: "project",
path: path.lexicallyNormalized().string,
additionalArguments: configuration.schemesArguments
)
}

private func filter(_ schemes: Set<String>, _ project: XcodeProjectlike) throws -> [String] {
Expand Down
2 changes: 1 addition & 1 deletion Sources/XcodeSupport/XcodeProjectlike.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protocol XcodeProjectlike: AnyObject {
var name: String { get }
var sourceRoot: FilePath { get }

func schemes() throws -> Set<String>
func schemes(additionalArguments: [String]) throws -> Set<String>
}

extension XcodeProjectlike {
Expand Down
4 changes: 2 additions & 2 deletions Sources/XcodeSupport/XcodeWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ final class XcodeWorkspace: XcodeProjectlike {
}
}

func schemes() throws -> Set<String> {
try xcodebuild.schemes(project: self)
func schemes(additionalArguments: [String]) throws -> Set<String> {
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
}

// MARK: - Private
Expand Down
26 changes: 22 additions & 4 deletions Sources/XcodeSupport/Xcodebuild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,36 @@ public final class Xcodebuild {
return path
}

func schemes(project: XcodeProjectlike) throws -> Set<String> {
try schemes(type: project.type, path: project.path.lexicallyNormalized().string)
func schemes(project: XcodeProjectlike, additionalArguments: [String]) throws -> Set<String> {
try schemes(
type: project.type,
path: project.path.lexicallyNormalized().string,
additionalArguments: additionalArguments
)
}

func schemes(type: String, path: String) throws -> Set<String> {
func schemes(type: String, path: String, additionalArguments: [String]) throws -> Set<String> {
let args = [
"-\(type)", path,
"-list",
"-json"
]

let lines = try shell.exec(["xcodebuild"] + args, stderr: false).split(separator: "\n").map { String($0).trimmed }
var quotedArguments = additionalArguments

for (i, arg) in additionalArguments.enumerated() {
if arg.hasPrefix("-"),
let value = additionalArguments[safe: i + 1],
!value.hasPrefix("-"),
!value.hasPrefix("\""),
!value.hasPrefix("\'")
{
quotedArguments[i + 1] = "\"\(value)\""
}
}

let xcodebuild = "xcodebuild \((args + quotedArguments).joined(separator: " "))"
let lines = try shell.exec(["/bin/sh", "-c", xcodebuild], stderr: false).split(separator: "\n").map { String($0).trimmed }

// xcodebuild may output unrelated warnings, we need to strip them out otherwise
// JSON parsing will fail.
Expand Down
2 changes: 1 addition & 1 deletion Tests/XcodeTests/XcodebuildTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class XcodebuildSchemesTest: XCTestCase {
func testParseSchemes() {
for output in XcodebuildListOutputs {
shell.output = output
let schemes = try! xcodebuild.schemes(project: project)
let schemes = try! xcodebuild.schemes(project: project, additionalArguments: [])
XCTAssertEqual(schemes, ["SchemeA", "SchemeB"])
}
}
Expand Down

0 comments on commit 4bd56a6

Please sign in to comment.