Skip to content

Commit

Permalink
Add reusable logic for checking whether in xcodebuild context.
Browse files Browse the repository at this point in the history
* Restores the async bundle command to be immutable again, instead of mutable.

Signed-off-by: furby™ <[email protected]>
  • Loading branch information
furby-tm committed Nov 30, 2024
1 parent 71cd250 commit 430934a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Sources/swift-bundler/Bundler/DarwinBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ enum DarwinBundler: Bundler {
return .failure(.missingDarwinPlatformVersion(context.platform))
}

// Whether a universal application binary (arm64 and x86_64) will be created.
let universal = command.arguments.universal || command.arguments.architectures.count > 1

// Whether or not we are building with xcodebuild instead of swiftpm.
let isUsingXcodeBuild = XcodeBuildManager.isUsingXcodeBuild(for: command)

return .success(
Context(
isXcodeBuild: command.builtWithXcode,
isXcodeBuild: command.builtWithXcode || isUsingXcodeBuild,
universal: universal,
standAlone: command.arguments.standAlone,
platform: applePlatform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,21 @@ enum XcodeBuildManager {
)
}
}

/// Whether or not the bundle command utilizes xcodebuild instead of swiftpm.
/// - Parameters:
/// - command: The subcommand for creating app bundles for a package.
/// - Returns: Whether or not xcodebuild is invoked instead of swiftpm.
static func isUsingXcodeBuild(for command: BundleCommand) -> Bool {
var forceUsingXcodeBuild = command.arguments.xcodebuild
// For all apple platforms (not including macOS), we generate xcode
// support, because macOS cannot cross-compile for any of the other
// darwin platforms like it can with linux, and thus we need to use
// xcodebuild to build for these platforms (ex. visionOS, iOS, etc)
if forceUsingXcodeBuild || ![Platform.linux, Platform.macOS].contains(command.arguments.platform) {
forceUsingXcodeBuild = true
}

return command.arguments.noXcodebuild ? !command.arguments.noXcodebuild : forceUsingXcodeBuild
}
}
4 changes: 2 additions & 2 deletions Sources/swift-bundler/Commands/AsyncCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ protocol AsyncCommand: AsyncParsableCommand {
func wrappedValidate() throws

/// Implement this instead of `run()` to get custom Swift Bundler error handling.
mutating func wrappedRun() async throws
func wrappedRun() async throws
}

extension AsyncCommand {
func wrappedValidate() {}
}

extension AsyncCommand {
mutating func run() async {
func run() async {
do {
try await wrappedRun()
} catch {
Expand Down
24 changes: 7 additions & 17 deletions Sources/swift-bundler/Commands/BundleCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ struct BundleCommand: AsyncCommand {
return architectures
}

mutating func wrappedRun() async throws {
func wrappedRun() async throws {
_ = try await doBundling()
}

Expand All @@ -196,7 +196,7 @@ struct BundleCommand: AsyncCommand {
/// `RunCommand` to figure out where the output bundle will end up even
/// when the user instructs it to skip bundling.
/// - Returns: A description of the structure of the bundler's output.
mutating func doBundling(dryRun: Bool = false) async throws -> BundlerOutputStructure {
func doBundling(dryRun: Bool = false) async throws -> BundlerOutputStructure {
// Time execution so that we can report it to the user.
let (elapsed, bundlerOutputStructure) = try await Stopwatch.time {
// Load configuration
Expand All @@ -220,20 +220,10 @@ struct BundleCommand: AsyncCommand {
// Get relevant configuration
let architectures = getArchitectures(platform: arguments.platform)

var forceUsingXcodeBuild = arguments.xcodebuild
// For all apple platforms (not including macOS), we generate xcode
// support, because macOS cannot cross-compile for any of the other
// darwin platforms like it can with linux, and thus we need to use
// xcodebuild to build for these platforms (ex. visionOS, iOS, etc)
if forceUsingXcodeBuild || ![Platform.linux, Platform.macOS].contains(arguments.platform) {
forceUsingXcodeBuild = true
}
forceUsingXcodeBuild = arguments.noXcodebuild ? !arguments.noXcodebuild : forceUsingXcodeBuild

if forceUsingXcodeBuild {
// If building with xcodebuild, be sure to set that here.
self.builtWithXcode = true
// Whether or not we are building with xcodebuild instead of swiftpm.
let isUsingXcodeBuild = XcodeBuildManager.isUsingXcodeBuild(for: self)

if isUsingXcodeBuild {
// Terminate the program if the project is an Xcodeproj based project.
let xcodeprojs = try FileManager.default.contentsOfDirectory(
at: packageDirectory,
Expand Down Expand Up @@ -276,7 +266,7 @@ struct BundleCommand: AsyncCommand {
// Get build output directory
let productsDirectory: URL

if !forceUsingXcodeBuild {
if !isUsingXcodeBuild {
productsDirectory = try arguments.productsDirectory
?? SwiftPackageManager.getProductsDirectory(buildContext).unwrap()
} else {
Expand All @@ -303,7 +293,7 @@ struct BundleCommand: AsyncCommand {

// Create build job
let build: () -> Result<Void, Error> = {
if forceUsingXcodeBuild {
if isUsingXcodeBuild {
XcodeBuildManager.build(
product: appConfiguration.product,
buildContext: buildContext
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-bundler/Commands/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct RunCommand: AsyncCommand {
simulatorSearchTerm: simulatorSearchTerm
)

var bundleCommand = BundleCommand(
let bundleCommand = BundleCommand(
arguments: _arguments,
skipBuild: false,
builtWithXcode: false,
Expand Down

0 comments on commit 430934a

Please sign in to comment.