Class: Fir::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/fir-cli.chk.rb,
lib/fir-cli.fir.rb,
lib/fir-cli.opt.rb,
lib/fir-cli.core.rb,
lib/fir-cli.utils.rb,
lib/fir-cli.output.rb,
lib/fir-cli/version.rb,
lib/fir-cli-commands/00-info.rb,
lib/fir-cli-commands/00-login.rb,
lib/fir-cli-commands/01-config.rb,
lib/fir-cli-commands/00-profile.rb,
lib/fir-cli-commands/00-upgrade.rb,
lib/fir-cli-commands/00-version.rb,
lib/fir-cli-commands/11-publish.rb,
lib/fir-cli-commands/12-build_ipa.rb

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



4
5
6
7
8
9
10
# File 'lib/fir-cli.core.rb', line 4

def initialize(*args)
  super
  @tmpfiles = []
  _init_config
  _load_config
  _puts_welcome
end

Class Method Details

.build_ipa_optionsObject



4
5
6
7
8
9
10
# File 'lib/fir-cli-commands/12-build_ipa.rb', line 4

def self.build_ipa_options
  option :workspace, :aliases => '-w', :desc => '编译指定路径中的 workspace', :type => :boolean
  option :scheme, :aliases => '-s', :desc => '如果编译 workspace 则必须指定 scheme'
  option :configuration, :aliases => '-C', :desc => '选择编译的配置,如 Release'
  option :output, :aliases => '-o', :desc => '指定 ipa 的输出路径'
  option :publish, :aliases => '-p', :desc => '设置是否发布到 FIR.im', :type => :boolean
end

.find_extendsObject



11
12
13
14
15
16
# File 'lib/fir-cli.utils.rb', line 11

def self.find_extends
  `gem list --local`
    .each_line("\n")
    .map { |gem| /^[^\s]+/.match(gem)[0] }
    .select { |gem| true if gem.start_with? 'fir-cli-' }
end

.output_optionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fir-cli.output.rb', line 4

def self.output_options
  option :verbose,
         :desc => '设置输出辅助信息的详细程度',
         :type => :string,
         :enum => ['v', 'vv', 'vvv']
  option :quiet,
         :aliases => '-q',
         :desc => '安静模式,不输出任何辅助信息',
         :type => 'boolean'
  option :color,
         :desc => '设置输出带有颜色的信息',
         :type => 'boolean'
end

.publish_optionsObject



5
6
7
8
9
10
# File 'lib/fir-cli-commands/11-publish.rb', line 5

def self.publish_options
  # option :resign, :aliases => '-r', :desc => '设置是否进行企业签名', :type => :boolean
  option :token, :aliases => '-t', :desc => '用户 token'
  option :short, :aliases => '-s', :desc => '自定义短地址'
  option :changelog, :aliases => '-c', :desc => '修改纪录,默认为空字符串', :default => ''
end

Instance Method Details

#build_ipa(path, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fir-cli-commands/12-build_ipa.rb', line 15

def build_ipa(path, *args)
  _chk_os! 'mac'
  settings = _convert_settings *args

  path = _path(path).to_s
  project_dir = (File.directory? path) ? path : (File.dirname path)

  ipa_path = _path(options[:output]).to_s if options[:output]
  if !ipa_path && !_opt_publish
    _puts "! #{Paint['如果不发布到 FIR.im,则需要指定 ipa 文件的输出路径', :red]}"
    _exit
  end
  if !ipa_path
    Dir.mktmpdir do |_d|
      ipa_path = _d.to_s
      _build_ipa project_dir, settings, options,
                 :ipa_path => ipa_path
      return _batch_publish ipa_path
    end
  end
  _build_ipa project_dir, settings, options,
             :ipa_path => ipa_path
  return if !_opt_publish
  if File.directory? ipa_path
    _batch_publish ipa_path
  elsif File.file? ipa_path
    publish ipa_path
  end
end

#config(*args) ⇒ Object



11
12
13
14
15
# File 'lib/fir-cli-commands/01-config.rb', line 11

def config(*args)
  _set_config options.merge _convert_settings *args
  _puts '> 设置完成,您现在使用的设置是'
  @config.each { |conf| _puts "> #{Paint[conf[0].to_s.rjust(10), :blue]} => #{conf[1]}" }
end

#info(path) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/fir-cli-commands/00-info.rb', line 8

def info(path)
  app = _info path, options[:all]
  app.each { |i| _puts "#{Paint[i[0].to_s.rjust(18), :blue]}  #{i[1]}" }
  if options[:fir]
    fir_app = _fir_info app[:identifier], app[:type]
    fir_app.each { |i| _puts "#{Paint[i[0].to_s.rjust(18), :blue]}  #{i[1]}" }
  end
end

#loginObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fir-cli-commands/00-login.rb', line 6

def 
  token = _prompt_secret('输入你的用户 token:')
  if token.empty?
    _puts_require_token
    _exit
  end
  user = _user token
  if !user
    _puts_invalid_token
    _exit
  end
  if _opt_token && _opt_token != token
    _puts "> 已登陆用户: #{_opt_token}"
    _puts "> 替换为用户: #{token}"
  end
  if user[:email]
    _puts "> 设置用户邮件地址为: #{user[:email]}"
    @config['email'] = user[:email]
  end
  @config['token'] = token
  @config.save
  _puts "> 当前登陆用户为:#{token}"
end

#profile(prof) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fir-cli-commands/00-profile.rb', line 7

def profile(prof)
  if prof == 'global'
    _puts '! 不能使用 global 作为配置文件名'
    _exit
  end
  prof = "#{prof}.yaml"
  if options[:delete]
    _puts "> 正在删除配置文件:#{prof}"
    @uconfig.delete prof
  else
    if _profile == prof
      _puts "! #{Paint["你正在使用该配置:#{prof}", :red]}"
      _exit
    end
    _puts "> 正在使用配置文件:#{_profile}"
    _puts "> 即将替换为:#{prof}"
    @global_config['profile'] = prof
    @global_config.save
    _load_config
    _puts "> 已替换为:#{prof}"
    config
  end
end

#publish(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fir-cli-commands/11-publish.rb', line 14

def publish(path)
  # if _opt_resign && _is_ipa?(path)
  #   tfile = Tempfile.new ["resign-#{SecureRandom.hex}", '.ipa']
  #   resign path, tfile.path
  #   path = tfile.path
  # end
  app = _info path, true
  fir_app = _fir_info app[:identifier], app[:type]

  id = fir_app[:id]
  bundle_app = fir_app[:bundle][:pkg]
  bundle_icon = fir_app[:bundle][:icon]

  if app[:icons] != nil && app[:icons].length > 0
    icon_path = app[:icons][0][:path]

    if _is_ipa? path
      _puts '> 转换图标...'
      Pngdefry.defry icon_path, icon_path
    end

    _puts "> 上传图标..."
    RestClient.post bundle_icon[:url],
                    :key => bundle_icon[:key],
                    :token => bundle_icon[:token],
                    :file => File.new(icon_path, 'rb')

    _puts '> 上传图标成功'
  end

  _puts '> 上传应用...'
  res = RestClient.post bundle_app[:url],
                        :key => bundle_app[:key],
                        :token => bundle_app[:token],
                        :file => File.new(path, 'rb')
  _puts '> 上传应用成功'
  upload_res = JSON.parse res.body, :symbolize_names => true

  _fir_put fir_app[:id],
          :name => app[:display_name] || app[:name],
          :short => options[:short] || fir_app[:short],
          # :desc => options[:describe] || fir_app[:desc]
          # :show => options[:public] || fir_app[:show]
          :source => 'fir-cli'

  _fir_vput_complete upload_res[:versionOid],
                     :version => app[:version],
                     :versionShort => app[:short_version],
                     :devices => app[:devices],
                     :release_type => app[:release_type]

  _fir_vput upload_res[:versionOid],
           :changelog => options[:changelog]

  # Get updated app info
  fir_app = _fir_info app[:identifier], app[:type]
  _puts "> #{_fir_url}/#{fir_app[:short]}"
end

#upgradeObject



6
7
8
9
10
11
12
13
# File 'lib/fir-cli-commands/00-upgrade.rb', line 6

def upgrade
  _puts '> gem update fir-cli'
  `gem update fir-cli`
  _extends.each do |gem|
    _puts "> gem update #{gem}"
    `gem update #{gem}`
  end
end

#versionObject



6
7
8
# File 'lib/fir-cli-commands/00-version.rb', line 6

def version
  _puts "FIR Cli #{VERSION}"
end