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

[action][spm] add simulator flag for swift compiler #21707

Merged
merged 17 commits into from Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion fastlane/lib/fastlane/actions/spm.rb
Expand Up @@ -10,6 +10,15 @@ def self.run(params)
cmd << "--configuration #{params[:configuration]}" if params[:configuration]
cmd << "--disable-sandbox" if params[:disable_sandbox]
cmd << "--verbose" if params[:verbose]
if params[:simulator]
simulator_flags = [
"-Xswiftc", "-sdk",
"-Xswiftc", "$(xcrun --sdk #{params[:simulator]} --show-sdk-path)",
"-Xswiftc", "-target",
"-Xswiftc", "#{params[:simulator_arch] || "arm64"}-apple-#{params[:simulator] == "iphonesimulator" ? 'ios' : 'macosx'}$(xcrun --sdk #{params[:simulator]} --show-sdk-version | cut -d '.' -f 1)#{"-simulator" if params[:simulator] == "iphonesimulator"}"
gharary marked this conversation as resolved.
Show resolved Hide resolved
]
cmd += simulator_flags
end
cmd << params[:command] if package_commands.include?(params[:command])
cmd << "--enable-code-coverage" if params[:enable_code_coverage] && (params[:command] == 'generate-xcodeproj' || params[:command] == 'test')
if params[:xcconfig]
Expand Down Expand Up @@ -93,7 +102,23 @@ def self.available_options
env_name: "FL_SPM_VERBOSE",
description: "Increase verbosity of informational output",
type: Boolean,
default_value: false)
default_value: false),
FastlaneCore::ConfigItem.new(key: :simulator,
env_name: "FL_SPM_SIMULATOR",
description: "Specifies the simulator to pass for Swift Compiler (one of: #{valid_simulators.join(', ')})",
type: String,
optional: true,
verify_block: proc do |value|
UI.user_error!("Please pass a valid simulator. Use one of the following: #{valid_simulators.join(', ')}") unless valid_simulators.include?(value)
end),
FastlaneCore::ConfigItem.new(key: :simulator_arch,
env_name: "FL_SPM_SIMULATOR_ARCH",
description: "Specifies the architecture of the simulator to pass for Swift Compiler (one of: #{valid_architectures.join(', ')}), requires simulator to be specified also",
gharary marked this conversation as resolved.
Show resolved Hide resolved
type: String,
optional: true,
gharary marked this conversation as resolved.
Show resolved Hide resolved
verify_block: proc do |value|
UI.user_error!("Please pass a valid simulator architecrure. Use one of the following: #{valid_architectures.join(', ')}") unless valid_architectures.include?(value)
gharary marked this conversation as resolved.
Show resolved Hide resolved
end)
]
end

Expand Down Expand Up @@ -139,6 +164,14 @@ def self.valid_configurations
def self.xcpretty_output_types
%w(simple test knock tap)
end

def self.valid_simulators
%w(iphonesimulator macosx)
gharary marked this conversation as resolved.
Show resolved Hide resolved
end

def self.valid_architectures
%w(x86_64 arm64)
end
end
end
end
66 changes: 66 additions & 0 deletions fastlane/spec/actions_specs/spm_spec.rb
Copy link
Member

Choose a reason for hiding this comment

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

Since it looks like the macOS simulator command wasn't working (? can't really tell for sure ATM), is there any way we can add a unit test that makes sure not only the output command is the expected string, but that the command actually works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to find a way to actually test the code in unit tests. I was thinking of using open3 to catch the result of executing command, but then testing the swift build command needs an actual SPM. So wasn't making sense to me. Any suggestion?

Copy link
Member

Choose a reason for hiding this comment

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

Uhm, if we wanted, we could create a sample SPM just for testing purposes. But, on a second thought, I think we actually might not need this, as long as we test it manually and make sure it works with all configurations, this should be fine. The reason is that we'd be validating whether "swift build" works, and that's not really the intent of what fastlane's unit tests should be :)

So this is good as is 🙏 thank you!

Expand Up @@ -339,6 +339,72 @@
expect(result).to eq("set -o pipefail && swift package --verbose generate-xcodeproj --xcconfig-overrides Package.xcconfig 2>&1 | xcpretty --simple")
end
end

context "when simulator is specified" do
it "adds simulator flags to the build command" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'iphonesimulator'
)
end").runner.execute(:test)

expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk iphonesimulator --show-sdk-path) -Xswiftc -target -Xswiftc arm64-apple-ios$(xcrun --sdk iphonesimulator --show-sdk-version | cut -d '.' -f 1)-simulator")
end

it "raises an error if simulator syntax is invalid" do
expect do
Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'invalid_simulator'
)
end").runner.execute(:test)
end.to raise_error("Please pass a valid simulator. Use one of the following: iphonesimulator, macosx")
end

it "sets arm64 as the default architecture when simulator is specified without architecture" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'iphonesimulator'
)
end").runner.execute(:test)
expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk iphonesimulator --show-sdk-path) -Xswiftc -target -Xswiftc arm64-apple-ios$(xcrun --sdk iphonesimulator --show-sdk-version | cut -d '.' -f 1)-simulator")
end

it "sets x86-64 as the architecture parameter when simulator is specified" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'iphonesimulator',
simulator_arch: 'x86_64'
)
end").runner.execute(:test)
expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk iphonesimulator --show-sdk-path) -Xswiftc -target -Xswiftc x86_64-apple-ios$(xcrun --sdk iphonesimulator --show-sdk-version | cut -d '.' -f 1)-simulator")
end

it "sets macosx as the simulator parameter without architecture being specified" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'macosx'
)
end").runner.execute(:test)
expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk macosx --show-sdk-path) -Xswiftc -target -Xswiftc arm64-apple-macosx$(xcrun --sdk macosx --show-sdk-version | cut -d '.' -f 1)")
end

it "sets macosx as the simulator parameter with x86_64 passed as architecture" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'macosx',
simulator_arch: 'x86_64'
)
end").runner.execute(:test)
expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk macosx --show-sdk-path) -Xswiftc -target -Xswiftc x86_64-apple-macosx$(xcrun --sdk macosx --show-sdk-version | cut -d '.' -f 1)")
end
end
end
end
end