Skip to content

Commit

Permalink
[action][spm] add simulator flag for swift compiler (fastlane#21707)
Browse files Browse the repository at this point in the history
* Add simulator flag to spm action for swift compiler

* add user entry syntax check

* removing ipadsimulator from syntax check

* Adding verify_block for flag check

* adding simulators list to user_error

* Update fastlane/lib/fastlane/actions/spm.rb

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>

* Update fastlane/lib/fastlane/actions/spm.rb

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>

* Adding unit tests for simulator

* Fix simulator typo

* Update fastlane/spec/actions_specs/spm_spec.rb

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>

* Fix typo.

* Adding support for different simulator architecture

* Adding unit tests for simulator architecture

* Modify code by extracting the logic into methods

* Update fastlane/lib/fastlane/actions/spm.rb

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>

* Update fastlane/lib/fastlane/actions/spm.rb

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>

---------

Co-authored-by: Roger Oba <rogerluan.oba@gmail.com>
  • Loading branch information
2 people authored and SubhrajyotiSen committed Jan 17, 2024
1 parent 347c1a4 commit f632f7d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
60 changes: 59 additions & 1 deletion fastlane/lib/fastlane/actions/spm.rb
Expand Up @@ -11,6 +11,18 @@ 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_platform = simulator_platform(simulator: params[:simulator], simulator_arch: params[:simulator_arch])
simulator_sdk = simulator_sdk(simulator: params[:simulator])
simulator_sdk_suffix = simulator_sdk_suffix(simulator: params[:simulator])
simulator_flags = [
"-Xswiftc", "-sdk",
"-Xswiftc", "$(xcrun --sdk #{params[:simulator]} --show-sdk-path)",
"-Xswiftc", "-target",
"-Xswiftc", "#{simulator_platform}#{simulator_sdk}#{simulator_sdk_suffix}"
]
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')
cmd << "--parallel" if params[:parallel] && params[:command] == 'test'
Expand Down Expand Up @@ -105,7 +117,24 @@ 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 the simulator option to be specified also, otherwise, it's ignored",
type: String,
optional: false,
default_value: "arm64",
verify_block: proc do |value|
UI.user_error!("Please pass a valid simulator architecture. Use one of the following: #{valid_architectures.join(', ')}") unless valid_architectures.include?(value)
end)
]
end

Expand All @@ -132,6 +161,13 @@ def self.example_code
'spm(
command: "test",
parallel: true
),
spm(
simulator: "iphonesimulator"
)',
'spm(
simulator: "macosx",
simulator_arch: "arm64"
)'
]
end
Expand All @@ -155,6 +191,28 @@ def self.valid_configurations
def self.xcpretty_output_types
%w(simple test knock tap)
end

def self.valid_simulators
%w(iphonesimulator macosx)
end

def self.valid_architectures
%w(x86_64 arm64)
end

def self.simulator_platform(params)
platform_suffix = "#{params[:simulator] == "iphonesimulator" ? "ios" : "macosx"}"
"#{params[:simulator_arch]}-apple-#{platform_suffix}"
end

def self.simulator_sdk(params)
"$(xcrun --sdk #{params[:simulator]} --show-sdk-version | cut -d '.' -f 1)"
end

def self.simulator_sdk_suffix(params)
return "" unless params[:simulator] == "iphonesimulator"
"-simulator"
end
end
end
end
66 changes: 66 additions & 0 deletions fastlane/spec/actions_specs/spm_spec.rb
Expand Up @@ -381,6 +381,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

0 comments on commit f632f7d

Please sign in to comment.