Class: Xcode::Builder::BaseBuilder

Inherits:
Object
  • Object
show all
Includes:
TerminalOutput
Defined in:
lib/xcode/builder/base_builder.rb

Overview

This class tries to pull various bits of Xcoder together to provide a higher-level API for common project build tasks.

Direct Known Subclasses

ProjectTargetConfigBuilder, SchemeBuilder

Constant Summary

Constants included from TerminalOutput

TerminalOutput::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TerminalOutput

#color_output=, #color_output?, #format_lhs, included, #log_level, log_level=, #print, #print_input, #print_output, #print_system, #print_task, #puts, terminal_supports_colors?

Constructor Details

#initialize(target, config) ⇒ BaseBuilder

Returns a new instance of BaseBuilder.



15
16
17
18
19
20
# File 'lib/xcode/builder/base_builder.rb', line 15

def initialize(target, config)
  @target = target
  @config = config

  @sdk = @target.project.sdk
end

Instance Attribute Details

#build_pathObject

Returns the value of attribute build_path.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def build_path
  @build_path
end

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/xcode/builder/base_builder.rb', line 13

def config
  @config
end

#identityObject

Returns the value of attribute identity.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def identity
  @identity
end

#keychainObject

Returns the value of attribute keychain.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def keychain
  @keychain
end

#objrootObject

Returns the value of attribute objroot.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def objroot
  @objroot
end

#profileObject

Returns the value of attribute profile.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def profile
  @profile
end

#sdkObject

Returns the value of attribute sdk.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def sdk
  @sdk
end

#symrootObject

Returns the value of attribute symroot.



12
13
14
# File 'lib/xcode/builder/base_builder.rb', line 12

def symroot
  @symroot
end

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/xcode/builder/base_builder.rb', line 13

def target
  @target
end

Instance Method Details

#app_pathObject



271
272
273
# File 'lib/xcode/builder/base_builder.rb', line 271

def app_path
  "#{configuration_build_path}/#{@config.product_name}.app"
end

#build(options = {}, &block) ⇒ Object

Build the project



142
143
144
145
146
147
148
149
150
151
# File 'lib/xcode/builder/base_builder.rb', line 142

def build options = {}, &block
  print_task :builder, "Building #{product_name}", :notice
  cmd = prepare_build_command options[:sdk]||@sdk

  with_keychain do
    cmd.execute
  end

  self
end

#bundle_identifierObject



301
302
303
# File 'lib/xcode/builder/base_builder.rb', line 301

def bundle_identifier
  @config.info_plist.identifier
end

#bundle_versionObject



305
306
307
# File 'lib/xcode/builder/base_builder.rb', line 305

def bundle_version
  @config.info_plist.version
end

#clean(options = {:sdk=>@sdk}, &block) ⇒ Object

testflight = Xcode::Deploy::Testflight.new(api_token, team_token)

yield(testflight) if block_given?
testflight.upload(ipa_path, dsym_zip_path)

end



232
233
234
235
236
237
238
239
# File 'lib/xcode/builder/base_builder.rb', line 232

def clean options = {:sdk=>@sdk}, &block
  print_task :builder, "Cleaning #{product_name}", :notice
  prepare_clean_command(options[:sdk]).execute

  @built = false
  @packaged = false
  self
end

#cocoapods_installed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/xcode/builder/base_builder.rb', line 30

def cocoapods_installed?
  system("which pod > /dev/null 2>&1")
end

#configuration_build_pathObject



263
264
265
# File 'lib/xcode/builder/base_builder.rb', line 263

def configuration_build_path
  "#{symroot}/#{@config.name}-#{@sdk}"
end

#dependenciesObject

If a Podfile exists, perform a pod install



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xcode/builder/base_builder.rb', line 42

def dependencies
  if has_dependencies? and cocoapods_installed?
    print_task :builder, "Fetch depencies", :notice
    podfile = File.join(File.dirname(@target.project.path), "Podfile")
   
    print_task :cocoapods, "pod setup", :info
    with_command('pod setup').execute

    print_task :cocoapods, "pod install", :info
    with_command('pod install').execute
  end
end

#deploy(method, options = {}) {|deployer| ... } ⇒ Object

Deploy the package through the chosen method

If a block is given, the deployer is yielded before deploy() is called

TODO: move deployment to Xcode::Deploy module

Parameters:

  • method

    the deployment method (web, ssh, testflight)

  • options (defaults to: {})

    options specific for the chosen deployment method

Yields:

  • (deployer)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/xcode/builder/base_builder.rb', line 194

def deploy method, options = {}
  print_task :builder, "Deploy to #{method}", :notice
  options = {
    :ipa_path => ipa_path,
    :dsym_zip_path => dsym_zip_path,
    :ipa_name => ipa_name,
    :app_path => app_path,
    :configuration_build_path => configuration_build_path,
    :product_name => @config.product_name,
    :info_plist => @config.info_plist
  }.merge options

  require "xcode/deploy/#{method.to_s}.rb"
  deployer = Xcode::Deploy.const_get("#{method.to_s.capitalize}").new(self, options)

  yield(deployer) if block_given?

  deployer.deploy
end

#dsym_pathObject



289
290
291
# File 'lib/xcode/builder/base_builder.rb', line 289

def dsym_path
  "#{app_path}.dSYM"
end

#dsym_zip_pathObject



293
294
295
# File 'lib/xcode/builder/base_builder.rb', line 293

def dsym_zip_path
  "#{product_version_basename}.dSYM.zip"
end

#entitlements_pathObject



267
268
269
# File 'lib/xcode/builder/base_builder.rb', line 267

def entitlements_path
  "#{build_path}/#{@target.name}.build/#{name}-#{@target.project.sdk}/#{@target.name}.build/#{@config.product_name}.xcent"
end

#has_dependencies?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/xcode/builder/base_builder.rb', line 34

def has_dependencies?
  podfile = File.join(File.dirname(@target.project.path), "Podfile")
  File.exists? podfile
end

#ipa_nameObject



297
298
299
# File 'lib/xcode/builder/base_builder.rb', line 297

def ipa_name
  File.basename(ipa_path)
end

#ipa_pathObject



285
286
287
# File 'lib/xcode/builder/base_builder.rb', line 285

def ipa_path
  "#{product_version_basename}.ipa"
end

#package(options = {}, &block) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/xcode/builder/base_builder.rb', line 241

def package options = {}, &block
  options = {:show_output => false}.merge(options)

  raise "Can't find #{app_path}, do you need to call builder.build?" unless File.exists? app_path or File.symlink? app_path

  print_task 'builder', "Packaging #{product_name}", :notice

  print_task :package, "generating IPA: #{ipa_path}", :info
  with_keychain do
    prepare_package_command.execute
  end

  print_task :package, "creating dSYM zip: #{dsym_zip_path}", :info
  prepare_dsym_command.execute

  self
end

#prepare_build_command(sdk = @sdk) ⇒ Object



93
94
95
96
# File 'lib/xcode/builder/base_builder.rb', line 93

def prepare_build_command sdk=@sdk
  cmd = prepare_xcodebuild sdk
  cmd
end

#prepare_clean_command(sdk = @sdk) ⇒ Object



109
110
111
112
113
# File 'lib/xcode/builder/base_builder.rb', line 109

def prepare_clean_command sdk=@sdk
  cmd = prepare_xcodebuild sdk
  cmd << "clean"
  cmd
end

#prepare_dsym_commandObject



129
130
131
132
133
134
135
136
137
# File 'lib/xcode/builder/base_builder.rb', line 129

def prepare_dsym_command
  # package dSYM
  with_command 'zip' do |cmd|
    cmd << "-r"
    cmd << "-T"
    cmd << "-y \"#{dsym_zip_path}\""
    cmd << "\"#{dsym_path}\""
  end
end

#prepare_package_commandObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/xcode/builder/base_builder.rb', line 115

def prepare_package_command
  #package IPA
  with_command 'xcrun' do |cmd|
    cmd << "-sdk #{@sdk}" unless @sdk.nil?
    cmd << "PackageApplication"
    cmd << "-v \"#{app_path}\""
    cmd << "-o \"#{ipa_path}\""

    unless @profile.nil?
      cmd << "--embed \"#{@profile}\""
    end
  end
end

#prepare_test_command(sdk = @sdk) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/xcode/builder/base_builder.rb', line 98

def prepare_test_command sdk=@sdk
  cmd = prepare_xcodebuild sdk
  cmd.env["OBJROOT"]  = "\"#{objroot}/\""
  cmd.env["SYMROOT"]  = "\"#{symroot}/\""
  cmd.env["TEST_AFTER_BUILD"]="YES"
  cmd.env["ONLY_ACTIVE_ARCH"]="NO"
  # cmd.env["TEST_HOST"]=0
  # cmd << "-sdk #{sdk}" unless sdk.nil?
  cmd
end

#prepare_xcodebuild(sdk = @sdk) ⇒ Object

:yield: Xcode::Shell::Command



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xcode/builder/base_builder.rb', line 55

def prepare_xcodebuild sdk=@sdk #:yield: Xcode::Shell::Command
  with_command 'xcodebuild' do |cmd|

    cmd.log_to_file = true
    cmd.attach Xcode::Builder::XcodebuildParser.new
    
    cmd.env["OBJROOT"]  = "\"#{objroot}/\""
    cmd.env["SYMROOT"]  = "\"#{symroot}/\""
      
    unless profile.nil?
      profile.install
      print_task "builder", "Using profile #{profile.install_path}", :debug
      cmd.env["PROVISIONING_PROFILE"]   = "#{profile.uuid}"
    end

    unless @keychain.nil?
      print_task 'builder', "Using keychain #{@keychain.path}", :debug
      cmd.env["OTHER_CODE_SIGN_FLAGS"]  = "'--keychain #{@keychain.path}'"
    end

    unless @identity.nil?
      print_task 'builder', "Using identity #{@identity}", :debug
      cmd.env["CODE_SIGN_IDENTITY"]     = "\"#{@identity}\""
    end

    cmd << "-sdk #{sdk}" unless sdk.nil?

    yield cmd if block_given?
  end
end

#product_nameObject



281
282
283
# File 'lib/xcode/builder/base_builder.rb', line 281

def product_name
  @config.product_name
end

#product_version_basenameObject



275
276
277
278
279
# File 'lib/xcode/builder/base_builder.rb', line 275

def product_version_basename
  version = @config.info_plist.version
  version = "SNAPSHOT" if version.nil? or version==""
  "#{configuration_build_path}/#{@config.product_name}-#{@config.name}-#{version}"
end

#project_rootObject



259
260
261
# File 'lib/xcode/builder/base_builder.rb', line 259

def project_root
  @target.project.path
end

#test(options = {:sdk => 'iphonesimulator', :show_output => false}) ⇒ Object

Invoke the configuration’s test target and parse the resulting output

If a block is provided, the report is yielded for configuration before the test is run

TODO: Move implementation to the Xcode::Test module



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/xcode/builder/base_builder.rb', line 159

def test options = {:sdk => 'iphonesimulator', :show_output => false}
  report = Xcode::Test::Report.new
  print_task :builder, "Testing #{product_name}", :notice

  cmd = prepare_test_command options[:sdk]||@sdk

  if block_given?
    yield(report)
  else
    report.add_formatter :stdout, { :color_output => true }
    report.add_formatter :junit, 'test-reports'
  end

  cmd.attach Xcode::Test::Parsers::OCUnitParser.new(report)
  cmd.show_output = options[:show_output] # override it if user wants output

  begin
    cmd.execute
  rescue Xcode::Shell::ExecutionError => e
    # FIXME: Perhaps we should always raise this?
    raise e if report.suites.count==0
  end

  report
end

#with_command(command_line) {|cmd| ... } ⇒ Object

Yields:

  • (cmd)


86
87
88
89
90
91
# File 'lib/xcode/builder/base_builder.rb', line 86

def with_command command_line
  cmd = Xcode::Shell::Command.new command_line
  cmd.output_dir = objroot
  yield cmd if block_given?
  cmd
end