前言
在Fastfile文件中,常常需要执行一些其他的脚本及命令,同时还要打印日志
一、在Fastfile中执行Shell命令
参考:
sh("命令")
: 最原始的方式,返回打印的日志
sh("shell路径")
: 最原始的方式,返回打印的日志
Helper.backticks("命令")
:执行耗时比较短的命令,返回打印的日志
FastlaneCore::CommandExecutor.execute(command: "命令")
:建议的方式
system("命令")
: 执行完后返回true/false
exec("命令")
: 执行完后会强制结束,不再往下执行
Fastfile的demo如下:
default_platform(:ios)
platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
result0 = sh( "./../hello.sh")
UI.message "result0:#{result0}"
result1 = sh("pwd")
UI.message "result1:#{result1}"
result2 = Helper.backticks("pwd")
UI.message "result2:#{result2}"
result3 = FastlaneCore::CommandExecutor.execute(command: "pwd")
UI.message "result3:#{result3}"
result4 = system("pwd")
UI.message "result4:#{result4}"
result5 = exec("pwd")
#以下因为exec会强制结束不会被执行
UI.message "result5:#{result5}"
UI.message "end=======end"
end
end
hello.sh的脚本内容:
#! /bin/bash
echo "I am Hello world"
pwd
结果如下:
16:41:28: ------------------------------
16:41:28: --- Step: default_platform ---
16:41:28: ------------------------------
16:41:28: Driving the lane 'ios custom_lane' 🚀
16:41:28: ---------------------------
16:41:28: --- Step: ./../hello.sh ---
16:41:28: ---------------------------
16:41:28: $ ./../hello.sh
16:41:28: ▸ I am Hello world
16:41:28: ▸ /Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
16:41:28: result0:I am Hello world
/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane16:41:28: --- Step: pwd ---
16:41:28: -----------------
16:41:28: $ pwd
16:41:28: ▸ /Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
16:41:28: result1:/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane16:41:28: $ pwd
16:41:28: ▸ /Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
16:41:28: result2:/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane16:41:28: $ pwd
16:41:28: result3:/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
16:41:28: result4:true
/Users/tony/fastlane_workspace/OwnActionPlugin/fastlane
1.2:
UI.input
: 接受输入的文本
UI.password
:接收输入的密码
UI.confirm
:接收y/n的选择
UI.select
:接收从多个选择中选择一个选项
更多参考:fastlane actions 交互
Fastfile的demo如下:
default_platform(:ios)
platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
name = UI.input("What's your name? ")
pwd = UI.password("Your password please: ")
confirm = UI.confirm("Are you '#{name}'?")
UI.message "name:#{name} pwd:#{pwd} confirm:#{confirm}"
end
end
16:15:09: --- Step: default_platform ---
16:15:09: ------------------------------
16:15:09: Driving the lane 'ios custom_lane' 🚀
16:15:09: What's your name? jobs
[16:15:17]: Your password please: ***
[16:15:19]: Are you 'jobs'? (y/n)
y
[16:15:20]: name:jobs pwd:123 confirm:true
二、在Fastfile中执行打印日志
platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
puts "hello---puts----111111"
p "hello---p----2222222222"
UI.message "hello---message----3333333"
UI.important "hello---important----44444444"
UI.error "hello---error----5555555"
UI.success "hello---success----66666666"
UI.header "hello---header----777777"
end
end
17:02:51: --- Step: default_platform ---
17:02:51: ------------------------------
17:02:51: Driving the lane 'ios custom_lane' 🚀
17:02:51: hello---puts----111111
"hello---p----2222222222"
17:02:51: hello---message----3333333
17:02:51: hello---important----44444444
17:02:51: hello---error----5555555
17:02:51: hello---success----66666666
17:02:51: --------------------------------
17:02:51: --- hello---header----777777 ---
17:02:51: --------------------------------