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.



522
523
524
525
526
527
528
# File 'lib/xcode/install.rb', line 522

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.



518
519
520
# File 'lib/xcode/install.rb', line 518

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.



517
518
519
# File 'lib/xcode/install.rb', line 517

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



519
520
521
# File 'lib/xcode/install.rb', line 519

def source
  @source
end

#versionObject (readonly)

Returns the value of attribute version.



516
517
518
# File 'lib/xcode/install.rb', line 516

def version
  @version
end

#xcodeObject (readonly)

Returns the value of attribute xcode.



520
521
522
# File 'lib/xcode/install.rb', line 520

def xcode
  @xcode
end

Instance Method Details

#apply_variables(template) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/xcode/install.rb', line 610

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



602
603
604
# File 'lib/xcode/install.rb', line 602

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

#download(progress, progress_block = nil, retry_download_count = 3) ⇒ Object



551
552
553
554
555
556
557
558
559
560
# File 'lib/xcode/install.rb', line 551

def download(progress, progress_block = nil, retry_download_count = 3)
  result = Curl.new.fetch(
    url: source,
    directory: CACHE_DIR,
    progress: progress,
    progress_block: progress_block,
    retry_download_count: retry_download_count
  )
  result ? dmg_path : nil
end

#install(progress, should_install) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/xcode/install.rb', line 562

def install(progress, should_install)
  dmg_path = download(progress)
  fail Informative, "Failed to download #{@name}." if dmg_path.nil?

  return unless should_install
  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)


530
531
532
533
# File 'lib/xcode/install.rb', line 530

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

#installed_stringObject



535
536
537
# File 'lib/xcode/install.rb', line 535

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

#pkg_pathObject



606
607
608
# File 'lib/xcode/install.rb', line 606

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

#prepare_packageObject



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/xcode/install.rb', line 581

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



539
540
541
# File 'lib/xcode/install.rb', line 539

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