Class: XcodeInstall::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/xcode/install.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(downloadable) ⇒ Simulator

Returns a new instance of Simulator.



352
353
354
355
356
357
358
# File 'lib/xcode/install.rb', line 352

def initialize(downloadable)
  @version = Gem::Version.new(downloadable['version'])
  @install_prefix = apply_variables(downloadable['userInfo']['InstallPrefix'])
  @name = apply_variables(downloadable['name'])
  @identifier = apply_variables(downloadable['identifier'])
  @source = apply_variables(downloadable['source'])
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



348
349
350
# File 'lib/xcode/install.rb', line 348

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.



347
348
349
# File 'lib/xcode/install.rb', line 347

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



349
350
351
# File 'lib/xcode/install.rb', line 349

def source
  @source
end

#versionObject (readonly)

Returns the value of attribute version.



346
347
348
# File 'lib/xcode/install.rb', line 346

def version
  @version
end

#xcodeObject (readonly)

Returns the value of attribute xcode.



350
351
352
# File 'lib/xcode/install.rb', line 350

def xcode
  @xcode
end

Instance Method Details

#apply_variables(template) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/xcode/install.rb', line 431

def apply_variables(template)
  variable_map = {
    '$(DOWNLOADABLE_VERSION_MAJOR)' => version.to_s.split('.')[0],
    '$(DOWNLOADABLE_VERSION_MINOR)' => version.to_s.split('.')[1],
    '$(DOWNLOADABLE_IDENTIFIER)' => identifier,
    '$(DOWNLOADABLE_VERSION)' => version.to_s
  }.freeze
  variable_map.each do |key, value|
    next unless template.include?(key)
    template.sub!(key, value)
  end
  template
end

#dmg_pathObject



423
424
425
# File 'lib/xcode/install.rb', line 423

def dmg_path
  CACHE_DIR + Pathname.new(source).basename
end

#downloadObject



381
382
383
384
# File 'lib/xcode/install.rb', line 381

def download
  result = Curl.new.fetch(source, CACHE_DIR)
  result ? dmg_path : nil
end

#installObject



386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/xcode/install.rb', line 386

def install
  download unless dmg_path.exist?
  prepare_package unless pkg_path.exist?
  puts "Please authenticate to install #{name}..."
  `sudo installer -pkg #{pkg_path} -target /`
  fail Informative, "Could not install #{name}, please try again" unless installed?
  source_receipts_dir = '/private/var/db/receipts'
  target_receipts_dir = "#{@install_prefix}/System/Library/Receipts"
  FileUtils.mkdir_p(target_receipts_dir)
  FileUtils.cp("#{source_receipts_dir}/#{@identifier}.bom", target_receipts_dir)
  FileUtils.cp("#{source_receipts_dir}/#{@identifier}.plist", target_receipts_dir)
  puts "Successfully installed #{name}"
end

#installed?Boolean

Returns:

  • (Boolean)


360
361
362
363
# File 'lib/xcode/install.rb', line 360

def installed?
  # FIXME: use downloadables' `InstalledIfAllReceiptsArePresentOrNewer` key
  File.directory?(@install_prefix)
end

#installed_stringObject



365
366
367
# File 'lib/xcode/install.rb', line 365

def installed_string
  installed? ? 'installed' : 'not installed'
end

#pkg_pathObject



427
428
429
# File 'lib/xcode/install.rb', line 427

def pkg_path
  CACHE_DIR + "#{identifier}.pkg"
end

#prepare_packageObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/xcode/install.rb', line 402

def prepare_package
  puts 'Mounting DMG'
  mount_location = Installer.new.mount(dmg_path)
  puts 'Expanding pkg'
  expanded_pkg_path = CACHE_DIR + identifier
  FileUtils.rm_rf(expanded_pkg_path)
  `pkgutil --expand #{mount_location}/*.pkg #{expanded_pkg_path}`
  puts "Expanded pkg into #{expanded_pkg_path}"
  puts 'Unmounting DMG'
  `umount #{mount_location}`
  puts 'Setting package installation location'
  package_info_path = expanded_pkg_path + 'PackageInfo'
  package_info_contents = File.read(package_info_path)
  File.open(package_info_path, 'w') do |f|
    f << package_info_contents.sub('pkg-info', %(pkg-info install-location="#{@install_prefix}"))
  end
  puts 'Rebuilding package'
  `pkgutil --flatten #{expanded_pkg_path} #{pkg_path}`
  FileUtils.rm_rf(expanded_pkg_path)
end

#to_sObject



369
370
371
# File 'lib/xcode/install.rb', line 369

def to_s
  "#{name} (#{installed_string})"
end