iOS持续集成之自动更新设备

在iOS开发中经常遇到的问题就是要添加测试设备。一般都需要登录开发者网站,操作比较麻烦。这里主要是通过脚本的形式添加设备

这里主要用到的工具是fastlane中的spaceship,不熟悉的可以查看这里

代码如下:

#!/usr/bin/env ruby

require 'rubygems'
require 'commander/import'
require "spaceship"

global_option('-u', '--username USER', 'Username') { |arg| $username = arg unless arg.nil? }
global_option('-p', '--password PASSWORD', 'Password') { |arg| $password = arg unless arg.nil? }

program :name, 'app profile'
program :version, '1.0.0'
program :description, '证书 Create By Jay'


command :'profiles:download:all' do |c|
  c.syntax = 'ios profiles:download:all'
  c.summary = 'Downloads all the active Provisioning Profiles'

  c.option '--id  [ID]', "" ,"程序包名" 

  c.action do |args, options|
    bundleId =  options.id
    say "开始登陆" + bundleId
    Spaceship.login($username, $password)
    say "登陆成功" 
    say "开始下载证书"
    profiles = Spaceship.provisioning_profile.find_by_bundle_id(bundleId)
    profiles.each do |profile|
        say "下载 " + profile.name
        File.write(profile.name + ".mobileprovision", profile.download)
    end
    say "完成"
  end
end

command :'device:add:dict' do |c|
  c.syntax = 'aa device:add'
  c.summary = '添加设备'
  c.option '--dict [DICT]', "", "设备名称:udid键值对"
  c.option '--id  [ID]', "" ,"程序包名" 
  c.action do |args,options|
    deviceDicts = eval(options.dict)
    bundleId =  options.id
    Spaceship.login($username, $password)
    if deviceDicts.class == Hash
        deviceName = deviceDicts[:name]
        deviceUDID = deviceDicts[:udid]
        registerDevice(deviceName,deviceUDID)
        
    elsif deviceDicts.class == Array
        for dict in deviceDicts
            deviceName = deviceDicts[:name]
            deviceUDID = deviceDicts[:udid]
            registerDevice(deviceName,deviceUDID)
        end

    else
        say "格式不正确"
    end
    say "完成注册,更新profile"    
    say "获取profiles"
    profiles = Spaceship.provisioning_profile.find_by_bundle_id(bundleId)
    allDevices =  Spaceship.device.all
    say "开始更新Profile中的Device"
    profiles.each do |profile|
        say "更新" + profile.name + "中的Device"
        profile.devices = allDevices
        profile.update!
    end

  end
end

def registerDevice(deviceName,deviceUDID)
    say "注册设备:" + deviceName + ":" + deviceUDID
    Spaceship.device.create!(name: deviceName, udid: deviceUDID)
end


command :'device:add' do |c|
  c.syntax = 'aa device:add'
  c.summary = '添加设备'
  c.option '--name [NAME]', "", "设备名称"
  c.option '--udid  [UIID]', "" ,"设备" 
  c.option '--id  [ID]', "" ,"程序包名" 
  c.action do |args,options|
    deviceName = options.name
    deviceUDID = options.udid
    bundleId = options.id
    say "获取profiles"
    profiles = Spaceship.provisioning_profile.find_by_bundle_id(bundleId)
    say "注册Device"
    Spaceship.device.create!(name: deviceName, udid: deviceUDID)
    allDevices =  Spaceship.device.all
    profiles.each do |profile|
        say profile.name
        profile.devices = allDevices
        profile.update!
    end
    say "完成"
  end
end




你可能感兴趣的:(iOS持续集成之自动更新设备)