Module: Flappy::BuildIpa

Included in:
Util::ClassMethods
Defined in:
lib/flappy/util/build_ipa.rb

Instance Method Summary collapse

Instance Method Details

#build_ipa(*args, options) ⇒ Object



6
7
8
9
10
11
12
13
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/flappy/util/build_ipa.rb', line 6

def build_ipa(*args, options)
  initialize_output_path(options)
  initialize_app_output_archive_info_path(args)
  initialize_iOS_logger(options)
  initialize_work_dir(args, options)

  update_pod_if_needed(args, options)
  writeEnvToPlist(args, options)

  @build_cmd = initialize_ipa_build_cmd(args, options)

  unless @build_cmd.blank?
    remove_build_cache

    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Build......')
    log_iOS("build cmd: #{@build_cmd}")

    log_iOS_build(@build_cmd)

    if $?.to_i != 0
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Build failed')
      exit 1
    else
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Build succeed')
    end
  end


  initialize_ipa_output_path
  @xcrun_cmd = archive_ipa_with_path(@build_dir, @temp_ipa_path)

  unless @xcrun_cmd.blank?
    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Archiving......')
    log_iOS("archive cmd: #{@xcrun_cmd}")

    log_iOS_archive(@xcrun_cmd)
    if $?.to_i != 0
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Archive failed')
      exit 1
    else
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Archive succeed')
    end

    rename_ipa  # 重命名
  end


  log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Saving dSYM file...')
  save_dSYM_file
  log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Saving dSYM file Succeed')

  if options[:open]
    system("open -R \"#{@app_output_archive_info_path}\"") if File.exists?(@app_output_archive_info_path)
  end

  unless options[:firToken].blank?
    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Uploading app to fir...')
    publish(@ipa_path, options)

    if $?.to_i != 0
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Upload app failed')
    else
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Upload app succeed')
    end
  end
end

#git_clone_work_dir(args, options) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/flappy/util/build_ipa.rb', line 120

def git_clone_work_dir(args, options)
  git_url = args.first.to_s
  branch = options[:branch].blank? ? 'master' : options[:branch]  # 分支名不指定则默认为master

  repo_path = File.join(@app_output_archive_info_path, @start_time + "_#{@app_dir_name}")
  git_cmd   = "git clone --depth=50 --branch=#{branch} #{git_url} #{repo_path}"

  log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Cloning......')
  log_iOS("git cmd: #{git_cmd}")

  system(git_cmd)
  if $?.to_i != 0
    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Clone failed')
    exit 1
  else
    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Clone succeed')
    repo_path
  end

end

#initialize_app_output_archive_info_path(args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/flappy/util/build_ipa.rb', line 88

def initialize_app_output_archive_info_path(args)
  @start_time = Time.now.strftime('%Y%m%d%H%M%S')

  work_dir = args.first.to_s
  if File.extname(work_dir) == '.git'             # git地址
    @app_dir_name = File.basename(work_dir, '.git')  # git名字
  elsif work_dir.blank? || !File.exist?(work_dir)       # 为空
    @app_dir_name =  File.basename(Dir.pwd)          # 当前目录
  else
    @app_dir_name = File.basename(work_dir)
  end

  @app_output_archive_info_path = File.join(@output_path, @app_dir_name, @start_time)
  @app_output_archive_log_path = File.join(@app_output_archive_info_path, "#{@start_time}_Log")
  FileUtils.mkdir_p(@app_output_archive_info_path) unless File.exist?(@app_output_archive_info_path)   # 创建本次打包的存储目录
  FileUtils.mkdir_p(@app_output_archive_log_path) unless File.exist?(@app_output_archive_log_path)   # 创建本次打包的logger目录
end

#initialize_ipa_build_cmd(args, options) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/flappy/util/build_ipa.rb', line 184

def initialize_ipa_build_cmd(args, options)
  @workspace_path = ''
  @workspace_name = ''
  @workspace = ''

  @project_path = ''
  @project_name = ''
  @project = ''

  @build_scheme_name   = options[:scheme]
  @build_target_name   = options[:target]
  @build_configuration = options[:configuration]

  @build_dir = ''
  @build_SYMROOT_dir = ''

  @codesign = options[:codesign]
  @provision_name  = options[:provision]

  if check_and_find_ios_xcworkspace(@work_dir)
    check_condition(@build_dir, 'build dir is empty!')

    unless @workspace.blank?
      if @build_scheme_name.blank?
        log_iOS('该工程为workspace,编译workspace必须同时指定一个scheme,可以使用`xcodebuild --list`来查看该workspace的scheme列表')
        exit 1
      end
    end

    build_cmd =  "cd #{@build_dir}; xcodebuild"
    build_cmd += " -workspace '#{@workspace}'" unless @workspace.blank?
    build_cmd += " -scheme '#{@build_scheme_name}'" unless @build_scheme_name.blank?

  elsif check_and_find_ios_xcodeproj(@work_dir)
    check_condition(@build_dir, 'build dir is empty!')

    build_cmd =  "cd #{@build_dir}; xcodebuild"
    build_cmd += " -target '#{@build_target_name}'" unless @build_target_name.blank?
  else
    log_iOS('No xcworkspace or xcodeproj found')
    exit 1
  end

  build_cmd += " -configuration '#{@build_configuration}'" unless @build_configuration.blank?

  check_condition(@build_SYMROOT_dir, 'SYMROOT_dir is empty!')
  build_cmd += " clean build SYMROOT=\"#{@build_SYMROOT_dir}\""

  unless @codesign.blank?
    logger_iOS.info 'Use custom certificate'
    build_cmd += " CODE_SIGN_IDENTITY=\"#{@codesign}\" PROVISIONING_PROFILE=\"#{@provision_name}\""
  else
    log_iOS('Use project certificate')
  end

  build_cmd
end

#initialize_ipa_output_pathObject



142
143
144
# File 'lib/flappy/util/build_ipa.rb', line 142

def initialize_ipa_output_path
  @temp_ipa_path = "#{@app_output_archive_info_path}/#{@start_time}.ipa"  # ipa包路径
end

#initialize_output_path(options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/flappy/util/build_ipa.rb', line 74

def initialize_output_path(options)
  output_path = File.expand_path(options[:output]) unless options[:output].blank?
  default_output_path = File.expand_path('~/Documents/Flappy-Archives/iOS')

  unless output_path.blank?
    @output_path = File.join(output_path, 'Flappy-Archives', 'iOS')
  else
    log_iOS("Output path is empty, use default output path #{default_output_path}")
    @output_path = default_output_path
  end

  FileUtils.mkdir_p(@output_path) unless File.exist?(@output_path)
end

#initialize_work_dir(args, options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/flappy/util/build_ipa.rb', line 107

def initialize_work_dir(args, options)
  work_dir = args.first.to_s

  if File.extname(work_dir) == '.git'         # git地址
    @work_dir = git_clone_work_dir(args, options)  # clone好work dir即为clone后的项目路径
  elsif work_dir.blank? || !File.exist?(work_dir)   # 为空
    @work_dir = Dir.pwd                     # 当前目录
  else
    @work_dir = File.expand_path(work_dir)
  end
end

#ipa_info(ipa_path, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/flappy/util/build_ipa.rb', line 146

def ipa_info(ipa_path, options = {})
  ipa  = Flappy::IpaInfo::Ipa.new(ipa_path)
  app  = ipa.app
  info = app.full_info(options)

  ipa.cleanup
  info
end

#remove_build_cacheObject



179
180
181
# File 'lib/flappy/util/build_ipa.rb', line 179

def remove_build_cache
  FileUtils.rm_rf(@build_SYMROOT_dir) if File.exists?(@build_SYMROOT_dir)
end

#rename_ipaObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/flappy/util/build_ipa.rb', line 168

def rename_ipa
  ipa_info  = ipa_info(@temp_ipa_path)

  @ipa_name = "#{@start_time}_#{ipa_info[:name]}.ipa"
  @ipa_path = File.join(@app_output_archive_info_path, "#{@ipa_name}")

  if File.exist?(@temp_ipa_path)
    FileUtils.mv(@temp_ipa_path, @ipa_path, force: true)
  end
end

#save_dSYM_fileObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/flappy/util/build_ipa.rb', line 156

def save_dSYM_file
  dSYM_file_to_copy = ''
  Dir.glob("#{@build_dir}/**/*.app.dSYM").each do |path|
    dSYM_file_to_copy = path
    basename = File.basename(dSYM_file_to_copy)
    @dSYM_file_path = File.join(@app_output_archive_info_path, "#{@start_time}_#{basename}")
  end

  FileUtils.cp_r(dSYM_file_to_copy, @dSYM_file_path) if File.exists?(dSYM_file_to_copy)
end