今天看啥  ›  专栏  ›  Fade_VV

Fastlane自动化管理自己的私有库

Fade_VV  · 掘金  ·  · 2021-03-21 15:28
阅读 9

Fastlane自动化管理自己的私有库

私有仓库的创建和优化已经完成了,但是私有库的升级还是很麻烦,还是有通过很多的git指令来完成,要提交代码,打标签,验证pod lib lint 等一系列的繁琐指令。但是现在可以通过一个自动化的工具来完成这些工作。

私有库参考 juejin.cn/post/693645…

Fastlane

  • Fastlane是一个ruby脚本集合

使用概念

  • lane :航道
  • Action机制
    • Action是Fastlane自动化流程中的最小执行单元,体现在Fastfile脚本中的一个个指令
    • 比如:cocoapods,git_add等等,而这些指令背后对应一个用ruby编写的脚本
    • 目前所有的Action
    • 常用action
      • produce 创建可用于 iTunes Connect 和 Apple Developer Portal 的 iOS app。
      • cert 自动创建和维护 iOS 代码签名证书。
      • sigh 创建、更新、下载和修复 provisioning profiles。
      • snapshot 自动将 App 屏幕截图本地化到每种设备上。
      • frameit 将屏幕截图适配到适当的设备屏幕大小。
      • gym 创建和打包 iOS app。
      • deliver 上传屏幕截图、元数据和 App 到 App 商店。
      • PEM 自动创建和更新 Push 通知的 profile。

安装

  • sudo gem install fastlane
  • sudo gem install -n/usr/loacl/bin fastlane 我是用的这个
  • 要求ruby 版本最新 brew update ,brew install ruby

使用

  • 1.进入项目的根目录
  • 比如这个项目 cd 到这个目录下

image.png

  • 2.fastlane init
    • 注意 如果不需要实现上传等操作, 其实我们可以直接在工程目录下, 创建一个文件夹, 在文件夹内部创建"使用文件"
    • fastlane Fastfile
    • 因为我只是练习一下自己的私有库所以就简单一下 创建完一个fastlane的空文件夹后 进入这个文件夹 touch Fastfile 创建一个 Fastfile的文件后用Xcode 打开编辑
  • 3.在Fastfile文件中, 描述不同的"航道"
    • 在航道中, 使用各种action描述自动化流程
    • 航道定义
      • desc "提交一个私有库"
      • lane :blib do |options|
      • 获取传递参数
        • options[:xxx]
          • targetName = options[:project]
          • tagName = options[:tagName]
      • 各种action执行
        • 分支
          • if
          • else
          • end
        • 信息打印 UI.message("xxxx#{xx}")
        • end

end

  • 4.在项目根目录下, 执行某个"航道"

    • fastlane 航道名称 参数1:值1 参数2:值2
      • 如:fastlane ManagerLib tag:0.1.0 target:FastlaneTest
  • 5.实战

    • 1.创建一个远程私有库的索引库
    • 2.创建一个测试用的代码库

    image.png

    • 3.pod repo add XMGFMSpecs 远程索引仓库地址 然后就可以不管他了
    • 4.cd 到空的文件夹 pod lib create WLFBase 创建一个Example工程
    • 前面的步骤具体参考 文章开头的文章参考地址。

    image.png

    • fastfile 内容 如果不了解意思可以去描述连接查看
desc 'ManagerLib 使用这个航道, 可以快速的对自己的私有库, 进行升级维护'
lane :ManagerLib do |options|

tagName = options[:tag]
targetName = options[:target]

# 具体这个巷道上面执行哪些行为

# 1. pod install
cocoapods(
clean: true,
podfile: "./Example/Podfile"
)

# 2. git add .
git_add(path: ".")
#    git commit -m 'xxx'
git_commit(path: ".", message: "版本升级维护")
#    git push origin master
push_to_git_remote

# 3. git tag 标签名称
add_git_tag(
tag: tagName
)
#    git push --tags
push_git_tags

# 4. pod spec lint
pod_lib_lint(allow_warnings: true)
#    pod repo push XXXX xxx.podspec
pod_push(path: "#{targetName}.podspec", repo: "XMGFMSpecs", allow_warnings: true)


end
复制代码
  • 完成之后运行 终端 fastlane ManagerLib tag:0.1.0 target:FastlaneTest
  • 当然你的 podspecs 文件要写对
  • 还要就是 git remote add origin 组件仓库地址.git 这个要在脚本运行前执行一次来管理到你的组件库,还有就是最后弄好项目后 先强制提交一次

image.png

  • 假如你的项目是 git clone 的应该是没有问题的
  • 之后运行就没问题了

image.png

  • 索引库添加成功

image.png

  • 代码提交成功

image.png

优化

之后我们会一直管理升级自己的私有库,有一天我们改了一个小小的bug,需要打个新的标签 0.1.0 升级到 0.1.1 更改podspecs后 终端 $fastlan ManagerLib tag:0.1.1 target:FastlaneTest 但是我就是不要升级成功0.1.1还是想0.1.0因为这个bug实在是微不足道。

  • 方法1
    • git tag -d 0.0.1 //删除本地tag
    • git push origin :0.0.1 //删除远程tag
    • 重新跑脚本
  • 方法2
    • 把自己的fastlane 写的好一些
  • 提示0.1.0 这个tag 已经存在了失败了

image.png

开始优化


desc 'ManagerLib 使用这个航道, 可以快速的对自己的私有库, 进行升级维护'
lane :ManagerLib do |options|

tagName = options[:tag]
targetName = options[:target]

# 具体这个巷道上面执行哪些行为

# 1. pod install
cocoapods(
clean: true,
podfile: "./Example/Podfile"
)

# 2. git add .
git_add(path: ".")
#    git commit -m 'xxx'
git_commit(path: ".", message: "版本升级维护")
#    git push origin master
push_to_git_remote

# 验证tag是否存在,如果存在, 应该删除本地标签和远程标签
#if 判断标签是否存在
#    执行删除本地/远程标签
#end

if git_tag_exists(tag: tagName)
    UI.message("发现tag:#{tagName} 已经存在, 即将执行, 删除动作 🚀")
    remove_tag(tag:tagName)
end


# 3. git tag 标签名称
add_git_tag(
tag: tagName
)
#    git push --tags
push_git_tags

# 4. pod spec lint
pod_lib_lint(allow_warnings: true)
#    pod repo push XXXX xxx.podspec
pod_push(path: "#{targetName}.podspec", repo: "XMGFMSpecs", allow_warnings: true)


end

复制代码
  • 去描述链接地址查找

image.png

  • 复制到脚本中

image.png if git_tag_exists(tag: tagName) UI.message("发现tag:#{tagName} 已经存在, 即将执行, 删除动作 🚀") remove_tag(tag:tagName) end 在加标签前做一个判断

  • remove_tag(tag:tagName) 这个是我们自己定义的 action 作用是删除本地的和远程的tag
  • cd 到根目录中
  • fastlane new_action
  • Name of your action: remove_tag 自定义的命名
  • 多了一个这样的文件 是一个ruby 的脚本

image.png

  • 打开

remove_tag.rb

  • 1.执行内容的编辑地方

image.png

  • 2.定义描述

image.png

  • 3.具体的描述

image.png

  • 4.可能需要的选项接收参数的地方

image.png

  • 5.内容的输出和返回的值 加入没有利用可删除

image.png

  • 6.作者名称和平台

image.png

开始编写

  • 在4的这个位置的代码替换成下面这段
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)
复制代码
  • 不会写的话可以查找源码链接点击查看

image.png

  • 1处修改的代码
    • 取出params里面的变量
    • 创建数组 将指令存入数组
    • 执行数组的所有命令
def self.run(params)

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

复制代码
  • 完整代码
module Fastlane
  module Actions
    module SharedValues
      REMOVE_TAG_CUSTOM_VALUE = :REMOVE_TAG_CUSTOM_VALUE
    end

    class RemoveTagAction < Action
      def self.run(params)
      
      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
        "恩, 牛逼"
      end

      def self.details
        # Optional:
        # this is your chance to provide a more detailed description of this action
        "我们可以使用这个action ,来删除本地或者远程标签"
      end

      def self.available_options
        # Define all options your action supports. 
        
        # Below a few examples
        [

                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
        # So no one will ever forget your contribution to fastlane :) You are awesome btw!
        ["王大顺"]
      end

      def self.is_supported?(platform)
        # you can do things like
        # 
        #  true
        # 
        #  platform == :ios
        # 
        #  [:ios, :mac].include?(platform)
        # 

        platform == :ios
      end
    end
  end
end

复制代码

验证指令是否成功

  • cd 根目录
  • fastlane action remove_tag

image.png

成功

  • 终端运行 fastlane ManagerLib tag:0.1.0 target:FastlaneTest

image.png




原文地址:访问原文地址
快照地址: 访问文章快照