移动开发之Fastlane自动化

2018-09-10  本文已影响51人  TitanCoder
fastlane_text

Fastlane介绍

简介

安装

  1. 确保ruby为最新版本
brew update
brew install ruby
  1. 安装fastlane
sudo gem install -n /usr/local/bin fastlane
  1. fastlane相关操作命令
// 查看当前fastlane版本
fastlane --version

// 查看所有action
fastlane actions

// fastlane初始化
fastlane init
  1. 如果出现类似的错误, 则可能是ruby源问题, 可更换ruby源后重新安装
ERROR:  Could not find a valid gem 'fastlane' (>= 0), here is why:
          Unable to download data from https://gems.ruby-china.org/ - bad response Not Found 404 (https://gems.ruby-china.org/specs.4.8.gz)

<div class="note info"><p>更换ruby源</p></div>

// 官方https://gems.ruby-china.com/把org改成了com, 使用时注意替换
// 查看gem源
gem sources

// 删除默认的gem源
gem sources --remove https://gems.ruby-china.org/

// 增加gem源
gem sources -a https://gems.ruby-china.com/

// 查看当前的gem源
gem sources

// 清空源缓存
gem sources -c

// 更新源缓存
gem sources -u

fastlane提交私有库

初始化fastlane

cd 到当前目录, 执行fastlane init命令

但是对于提交私有库来说这个过程会创建一些无用的文件, 包括双穿需要的appleID等, 有雨我们不涉及这些, 所以我们还可以直接创建所需文件

cd 根目录

// 创建一个fastlane文件夹
mkdir fastlane

// 进入fastlane目录
cd xxx/xxx/fastlane

// 创建一个Fastfile文件
touch Fastfile

接下来就是编写Fastfile文件内容

配置Fastfile`文件

语法解析

fastlane init创建好文件后, 打开Fastfile文件, 默认内容如下

default_platform(:ios)

platform :ios do
  desc "Description of what the lane does"
  lane :custom_lane do
    # add actions here: https://docs.fastlane.tools/actions
  end
end
tagName = options[:tag]

外部使用命令是, 即可通过xxx tag: 0.0.1进行传值

<div class="note warning"><p>Fastfile文件</p></div>

Fastfile文件配置

提交私有库时, 需要执行的命令如下

// 将本地库更像到测试项目
pod install

// 将本地代码加入本地仓库里
git add .

// 提交修改到本地仓库
git commit -m '你的修改记录'

// 添加名称为origin的远程连接
git remote add origin '你的github项目地址'

// 在push之前, 查看spec是否配置有问题
// 验证本地spec文件是否有误
pod lib lint

// 推送master分支的代码到名称为origin的远程仓库
git push origin master

// 设置tag值
git tag "0.0.1"  

// 上传提交tag
git push --tags
fastlanepod

Fastfile文件配置内容如下, 可做响应的参考

default_platform(:ios)

platform :ios do
  desc "用于对私有库的升级维护和提交"
  lane :DownloadLane do |options|

    # 0. 需要外界传入的参数
    tagName = options[:tag]
    targetName = options[:target]


    # 1. 将本地库更像到测试项目
    # pod install
    cocoapods(
        clean: true,
        # 该目录为执行Podfile未见的相对路径
        podfile: "./Example/Podfile"
    )

    # 2. 将本地代码加入本地仓库里
    # git add .
    # path为需要提交的文件的路径, 这里是所有文件
    git_add(path: ".")

    # 3. 提交修改到本地仓库
    # git commit -m '你的修改记录'
    # message: 提交信息
    git_commit(path: ".", message: "这里是提交信息")

    # 4. 在push之前, 查看spec是否配置有问题
    #  验证本地spec文件是否有误
    # pod lib lint
    # allow_warnings: 允许警告的存在
    pod_lib_lint(allow_warnings: true)

    # 5. 推送master分支的代码到名称为origin的远程仓库
    # git push origin master
    push_to_git_remote  # 更多的信息可查看官网

    # 6. 判断标签是否存在, 重复添加标签会报错
    # if-else-end和if-end判断语句
    if git_tag_exists(tag: tagName)
        # UI.message: 打印信息
        UI.message("发现tag:#{tagName} 该标签已经存在")
    end

    # 7. 设置tag值
    # git tag "0.0.1"  
    add_git_tag(
        tag: tagName
    )

    # 8. 上传提交tag
    # git push --tags
    push_git_tags

    # 9. 更新索引库
    # pod repo push XXXX xxx.podspec
    pod_push(path: "#{targetName}.podspec", repo: "TitanjunSpec", allow_warnings: true)

  end
end

自定义Action

创建ruby文件

fastlaneaction

之后fastlane文件内会多一个actions的文件件, 内有delete_tag.rb文件, 如下

actionsName

下面把我自定义的actions的相关代码贴出来

module Fastlane
  module Actions
    module SharedValues
      DELETE_TAG_CUSTOM_VALUE = :DELETE_TAG_CUSTOM_VALUE
    end

    class DeleteTagAction < Action
      def self.run(params)
        # 运行该action最终需要执行的代码, 在这里写
        tagName = params[:tag]
        isRemoveLocalTag = params[:rL]
        isRemoveRemoteTag = params[:rR]
        
        # 1. 先定义一个数组, 用来存储所有需要执行的命令
        cmds = []
        
        # 2. 往数组里面, 添加相应的命令
        # 删除本地标签
        # git tag -d 标签名称
        if isRemoveLocalTag
          cmds << "git tag -d #{tagName} "
        end
      
        # 删除远程标签
        # git push origin :标签名称
        if isRemoveRemoteTag
          cmds << " git push origin :#{tagName}"
        end

        #3. 执行数组里面的所有命令
        result = Actions.sh(cmds.join('&'));
        return result

      end

      def self.description
        # 间断的说明该Action的作用是什么, 不超过80个字符
        "删除标签"
      end

      def self.details
        # 详细的描述当前的Action
        "使用该action, 可删除本地或远程标签"
      end

      def self.available_options
        # 该action需要的参数, 使用同构数组进行分割的, 可以根据每一个参数, 确定其作用
        [
          # key: 参数名称, description: 参数描述或作用, optional: 是否是可选, is_string: 是否是string类型, default_value: 默认值, verify_block: 验证的block
          FastlaneCore::ConfigItem.new(key: :tag,
                                             description: "需要被删除的标签名称",
                                             optional: false,
                                             is_string: true),
          FastlaneCore::ConfigItem.new(key: :rL,
                                       description: "是否需要删除本地标签",
                                       optional: true,
                                       is_string: false,
                                       default_value: true),
          FastlaneCore::ConfigItem.new(key: :rR,
                                       description: "是否需要删除远程标签",
                                       optional: true,
                                       is_string: false,
                                       default_value: true)
        ]
      end

      def self.output
        # 表示输出的内容
      end

      def self.return_value
        # 返回值
        nil
      end

      def self.authors
        # 作者
        ["CoderTitan"]
      end

      def self.is_supported?(platform)
        # 支持的平台
        platform == :ios
      end
    end
  end
end

最后需要验证rb文件是否格式正确, 终端输入fastlane action delete_tag

fastlaneTest
上一篇 下一篇

猜你喜欢

热点阅读