美文网首页
iOS-组件化开发- 自动化

iOS-组件化开发- 自动化

作者: 三月望龙 | 来源:发表于2018-08-07 11:12 被阅读0次

上一篇制作私有库(组件)的过程中,使用的命令有很多,以下我们就使用自动化的方式提交推送私有组件。

Fastlane

fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. 🚀 It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application.
简而言之,就是一堆ruby脚本集合,使用Actions机制,实现自动化。

  • 安装

    • $ gem install fastlane -NV
    • $ brew cask install fastlane
  • 创建

    标准创建,是使用$ fastlane init

    此处,我们是简单创建

    • 在私有库(组件)的根目录创建fastlane文件夹和Fastfile文件

      36.png
    • 配置Fastfile文件

      # 描述航道
      desc "pod私有库自动化"
      
      # 定义航道, 航道名称是 repo
      lane :repo do |options|
      target = options[:target]
      tag = options[:tag]
      
      # 定义action
      
      #测试工程install
      # pod install
      cocoapods(
      podfile: "./Example/Podfile"
      )
      
      # 提交本地仓库
      # git add .
      git_add(path: ".")
      # git commit -m "xxx"
      git_commit(path: ".", message: "维护pod私有库")
      
      # 推送远程仓库
      # git push origin master
      push_to_git_remote
      
      # 判断是否有该版本, 如果有就先删除
      if git_tag_exists(tag:tag)
      UI.message("Found tag #{tag} 🚀, remove it")
      remove_tag(tag:tag)
      end
      
      # 打版本标签
      # git tag -a '#{tag}'
      add_git_tag(
      tag: tag
      )
      # 推送到远程库
      # git push --tags
      push_git_tags
      
      # 本地验证私有库
      # pod lib lint
      pod_lib_lint(allow_warnings: true)
      
      # 推送到私有管理库
      # pod repo push HQSpecs "#{targetName}.podspec"
      pod_push(path: "#{target}.podspec", repo: "HQSpecs", allow_warnings: true)
      
      end
      

      其中HQSpecs是私有库管理库

  • 使用

    使用之前需注意以下几点

    1. 必须cd到项目的根目录
    2. 需手动配置.podspec文件信息,例如:增加版本号
    3. 如果是首次创建私有库,则需手动关联远程库,如果是维护升级,则不需要
    • $ fastlane 航道名称 tag:版本号 target:组件库名称
    • 例:$ fastlane repo tag:0.1.1 target:HQMain
      37.png
      38.png

自定义Action

ActionFastlane自动化流程中的最小执行单元,体现在Fastfile脚本中的一个个命令。上述所用的action,社区都有提供,但有时,我们所需的action没有提供,需我们手动创建

  • 场景
    • 在制作私有库(组件)时,在执行手动或自动命令后,本地和远程的标签已经生成,如果此时验证没通过,修改正确后重新执行命令,此时的版本号如何处理?使用之前的版本会报错,增加一个新版本又不合适。
    • 解决方案: 删除本地和远程的版本号
    • $ git tag -d xxx & $ git push origin :xxx
  • 尴尬
    • Fastlane社区中,并没有提供类似删除git版本号的action
    • 此时,需要自己创建所需的action
  • 创建action
    • 执行命令:$ fastlane new_action

    • 输入自定义action的名称

      39.png
      40.png
    • 编辑remove_tag.rb

      • 核心代码如下:
      def self.run(params)
          tag = params[:tag]
          isRemoveRemoteTag = params[:isRemoveRemoteTag]
                    
          cmd = []
          cmd << "git tag -d #{tag} "
              
          if isRemoveRemoteTag
              cmd << " git push origin :#{tag}"
          end
            
          result = Actions.sh(cmd.join('&'));
          return result
      end
               
      def self.available_options
          [
              FastlaneCore::ConfigItem.new(key: :tag,
                                                     description: "被删除的标签",
                                                     optional: false,
                                                     is_string: true
                                                     ),
              FastlaneCore::ConfigItem.new(key: :isRemoveRemoteTag,
                                                     description: "是否删除远程标签",
                                                     optional: true,
                                                     is_string: false,
                                                     default_value: true),
          ]
      end
      
    • 查看remove_tag.rb编辑后的文档

      • $ fastlane action remove_tag
      41.png
  • 使用
    • 重新修改Fastfile文件

      • 在添加版本之前,先判断该版本是否存在,如果存在,则先删除该版本后再添加
      if git_tag_exists(tag:tag)
          UI.message("Found tag #{tag} 🚀, remove it")
          remove_tag(tag:tag)
      end
      
    • 验证

      • $ fastlane repo tag:0.1.1 target:HQMain
      42.png

相关文章

网友评论

      本文标题:iOS-组件化开发- 自动化

      本文链接:https://www.haomeiwen.com/subject/sveovftx.html