class OceanPackage::Command

Attributes

arguments[RW]

命令参数

at_mobiles[RW]

@ 的手机号

change_log[RW]

本次更新内容

custom_ipa_file_path[RW]

自定义的ipa文件路径

ding[RW]

ding ding

fir[RW]

fir 平台

oss[RW]

oss 对象

package[RW]

xcodebuild 打包相关

pgy[RW]

蒲公英平台

Public Class Methods

new(params = []) click to toggle source
# File lib/ocean_package/command.rb, line 28
def initialize(params = [])
  argv = CLAide::ARGV.new(params)

  @arguments = argv.arguments
  Log.info("arguments: #{@arguments}")

  workspace_path = argv.option("workspace-path", "")
  Log.info("workspace_path: #{workspace_path}")

  scheme = argv.option("scheme", "")
  Log.info("scheme: #{scheme}")

  # 默认配置改为空,防止包环境错误
  configuration = argv.option("configuration", "")
  Log.info("configuration: #{configuration}")

  archive_path = argv.option("archive-path", OceanPackage::Constants::DEFAULT_ARCHIVE_PATH)
  Log.info("archive_path: #{archive_path}")

  company_name = argv.option("company-name", OceanPackage::Constants::DEFAULT_COMPANY_NAME)
  Log.info("company_name: #{company_name}")

  export_options_plist = argv.option("export-options-plist", "")
  Log.info("export_options_plist: #{export_options_plist}")

  ##### 自定义的 ipa 文件 #####
  ipa_file_path = argv.option("ipa-file-path", "")
  Log.info("ipa_file_path: #{ipa_file_path}")
  @custom_ipa_file_path = ipa_file_path

  extra_export_params = argv.option("extra-export-params", "")
  Log.info("extra-export-params: #{extra_export_params}")

  open_finder = argv.flag?("open-finder", false )
  Log.info("open-finder: #{open_finder}")

  # 自定义ipa文件,使用该文件作为 archive path
  tmp_archive_path = has_custom_ipa_file ? File.dirname("#{ipa_file_path}") : archive_path
  Log.info("tmp_archive_path: #{tmp_archive_path}")
  @package = OceanPackage::Package.new(workspace_path, scheme, configuration, tmp_archive_path, company_name, export_options_plist, extra_export_params, open_finder)

  fir_token = argv.option("fir-token", "")
  Log.info("fir_token: #{fir_token}")

  change_log = argv.option("change-log", "")
  @change_log = change_log
  Log.info("change_log: #{change_log}")

  fir_log_path = @package.final_archive_path + 'fir.log'
  @fir = OceanPackage::Fir.new(fir_token, final_change_log, final_ipa_file_path, fir_log_path)

  ##### 蒲公英 #####
  pgy_api_key = argv.option("pgy-api-key", "")
  @pgy = OceanPackage::Pgy.new(pgy_api_key, final_change_log, final_ipa_file_path)
  Log.info("pgy_api_key: #{pgy_api_key}")

  ##### oss #####
  oss_bucket_name = argv.option("oss-bucket-name", "")
  Log.info("oss_bucket_name: #{oss_bucket_name}")

  oss_bucket_path = argv.option("oss-bucket-path", "")
  Log.info("oss_bucket_path: #{oss_bucket_path}")

  oss_endpoint = argv.option("oss-endpoint", "")
  Log.info("oss_endpoint: #{oss_endpoint}")

  @oss = OceanPackage::Oss.new(oss_bucket_name, oss_bucket_path, oss_endpoint)

  ding_token = argv.option("ding-token", "")
  Log.info("ding_token: #{ding_token}")

  at_mobiles = argv.option("at-mobiles", "").split(",")
  Log.info("ding_at_mobiles: #{at_mobiles}")
  @at_mobiles = at_mobiles

  @ding = OceanPackage::DingTalk.new(ding_token)
end

Public Instance Methods

compute_total_time() click to toggle source

总共时间,单位 秒 s

# File lib/ocean_package/command.rb, line 175
def compute_total_time
  time1 = package.start_time
  time2 = Time.now
  seconds = time2 - time1

  Log.info("total time: #{seconds}")

  seconds
end
final_change_log() click to toggle source

最终的 change log

# File lib/ocean_package/command.rb, line 107
def final_change_log
  if @change_log.empty?
    syscall('git log --pretty=format:%s -1')
  else
    @change_log
  end
end
final_ipa_file_path() click to toggle source

最终的ipa文件路径

# File lib/ocean_package/command.rb, line 121
def final_ipa_file_path
  has_custom_ipa_file ? "#{@custom_ipa_file_path}" : @package.ipa_file_path
end
finished() click to toggle source

打包完成

# File lib/ocean_package/command.rb, line 225
def finished
  Log.divider
  Log.info("package finished")
  Log.divider
end
has_custom_ipa_file() click to toggle source

是否设置了自定义的ipa文件

# File lib/ocean_package/command.rb, line 116
def has_custom_ipa_file
  "#{@custom_ipa_file_path}".empty? ? false : true
end
make_web_hook_message() click to toggle source

web hook 消息内容

# File lib/ocean_package/command.rb, line 191
def make_web_hook_message
  ipa = OceanPackage::Ipa.new(final_ipa_file_path)
  ipa.run

  # markdown 格式
  content = "# #{ipa.display_name} \n\n"
  content += "当前平台: iOS \n\n"
  content += "APP名称: " + ipa.display_name + "\n\n"
  content += "当前版本: " + ipa.version + "(#{ipa.build_version})" + "\n\n"
  content += "打包耗时: " + "#{compute_total_time}" + "s" + "\n\n"
  content += "发布环境: " + "#{package.configuration}" + "\n\n"
  content += "更新描述: " + final_change_log + "\n\n"
  content += "发布时间: " + Time.new.strftime("%Y年%m月%d日 %H时%M分%S秒") + "\n\n"
  content += "下载链接: [点我](#{@ipa_download_link})" + "\n\n"
  content += "![二维码](#{@qr_code_url})"

  Log.divider
  Log.info("web hook message: \n#{content}")
  Log.divider

  content
end
make_web_hook_message_title() click to toggle source

web hook 消息标题

# File lib/ocean_package/command.rb, line 186
def make_web_hook_message_title
  "iOS 来新包啦~"
end
run() click to toggle source

运行

# File lib/ocean_package/command.rb, line 126
def run
  # 没有自定义ipa文件,需要执行打包命令
  unless has_custom_ipa_file
    package.run
  end
  upload
  send_ding_talk_msg
  finished
end
send_ding_talk_msg() click to toggle source

发送打包信息到钉钉

# File lib/ocean_package/command.rb, line 215
def send_ding_talk_msg
  # 消息卡片,富文本
  title = make_web_hook_message_title
  content = make_web_hook_message

  ding.send_card_message(title, content)
  ding.send_text_message(title, @at_mobiles)
end
upload() click to toggle source

上传 ipa 文件

# File lib/ocean_package/command.rb, line 137
def upload
  can_fir = fir.check
  can_pgy = pgy.check
  if can_fir
    Log.info("publish platform: fir")
    upload_to_fir
  elsif can_pgy
    Log.info("publish platform: pgy")
    upload_to_pgy
  else
    Log.info("publish platform: none, exit")
    exit(1)
  end
end
upload_qr_code(path, name) click to toggle source

上传 二维码 QRCode 图片到 oss 后续其他平台,比如蒲公英也是需要类似的逻辑

# File lib/ocean_package/command.rb, line 163
def upload_qr_code(path, name)
  @qr_code_url = oss.upload(path, name)
end
upload_to_fir() click to toggle source

上传到 fir 平台

# File lib/ocean_package/command.rb, line 155
def upload_to_fir
  fir.run
  upload_qr_code(fir.find_qr_code_path, fir.find_release_id)
  @ipa_download_link = fir.whole_download_link
end
upload_to_pgy() click to toggle source

—— pgy 平台 ——-

# File lib/ocean_package/command.rb, line 168
def upload_to_pgy
  pgy.run
  @qr_code_url = pgy.get_qr_code_url
  @ipa_download_link = pgy.get_download_url
end