Class: XcodeInstall::Installer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstaller

Returns a new instance of Installer.



34
35
36
# File 'lib/xcode/install.rb', line 34

def initialize
  FileUtils.mkdir_p(CACHE_DIR)
end

Instance Attribute Details

#xcodesObject (readonly)

Returns the value of attribute xcodes.



32
33
34
# File 'lib/xcode/install.rb', line 32

def xcodes
  @xcodes
end

Instance Method Details

#cache_dirObject



38
39
40
# File 'lib/xcode/install.rb', line 38

def cache_dir
  CACHE_DIR
end


42
43
44
# File 'lib/xcode/install.rb', line 42

def current_symlink
  File.symlink?(SYMLINK_PATH) ? SYMLINK_PATH : nil
end

#download(version) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/xcode/install.rb', line 46

def download(version)
  return unless exist?(version)
  xcode = seedlist.find { |x| x.name == version }
  dmg_file = Pathname.new(File.basename(xcode.path))

  result = Curl.new.fetch(xcode.url, CACHE_DIR, spaceship.cookie, dmg_file)
  result ? CACHE_DIR + dmg_file : nil
end

#exist?(version) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/xcode/install.rb', line 55

def exist?(version)
  list_versions.include?(version)
end

#install_components(xcode_path) ⇒ Object



59
60
61
# File 'lib/xcode/install.rb', line 59

def install_components(xcode_path)
  `#{xcode_path}/Contents/MacOS/Xcode -installComponents`
end

#install_dmg(dmgPath, suffix = '', switch = true, clean = true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xcode/install.rb', line 73

def install_dmg(dmgPath, suffix = '', switch = true, clean = true)
  xcode_path = "/Applications/Xcode#{suffix}.app"

  `hdiutil mount -nobrowse -noverify #{dmgPath}`
  puts 'Please authenticate for Xcode installation...'
  source =  Dir.glob('/Volumes/Xcode/Xcode*.app').first

  if source.nil?
    $stderr.puts <<-HELP
No `Xcode.app` found in DMG. Please remove #{dmgPath} if you suspect a corrupted
download or run `xcode-install update` to see if the version you tried to install
has been pulled by Apple. If none of this is true, please open a new GH issue.
HELP
    return
  end

  `sudo ditto "#{source}" "#{xcode_path}"`
  `umount "/Volumes/Xcode"`

  enable_developer_mode
  `sudo xcodebuild -license` unless xcode_license_approved?
  install_components(xcode_path)

  if switch
    `sudo rm -f #{SYMLINK_PATH}` unless current_symlink.nil?
    `sudo ln -sf #{xcode_path} #{SYMLINK_PATH}` unless SYMLINK_PATH.exist?

    `sudo xcode-select --switch #{xcode_path}`
    puts `xcodebuild -version`
  end

  FileUtils.rm_f(dmgPath) if clean
end

#install_version(version, switch = true, clean = true, install = true) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/xcode/install.rb', line 107

def install_version(version, switch = true, clean = true, install = true)
  return if version.nil?
  dmg_path = get_dmg(version)
  fail Informative, "Failed to download Xcode #{version}." if dmg_path.nil?

  install_dmg(dmg_path, "-#{version.split(' ')[0]}", switch, clean) if install
end

#installed?(version) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/xcode/install.rb', line 63

def installed?(version)
  installed_versions.map(&:version).include?(version)
end

#installed_versionsObject



67
68
69
70
71
# File 'lib/xcode/install.rb', line 67

def installed_versions
  @installed ||= installed.map { |x| InstalledXcode.new(x) }.sort do |a, b|
    Gem::Version.new(a.version) <=> Gem::Version.new(b.version)
  end
end

#listObject



121
122
123
# File 'lib/xcode/install.rb', line 121

def list
  list_versions.join("\n")
end

#list_currentObject



115
116
117
118
119
# File 'lib/xcode/install.rb', line 115

def list_current
  stable_majors = list_versions.reject { |v| /beta/i =~ v }.map { |v| v.split('.')[0] }.map { |v| v.split(' ')[0] }
  latest_stable_major = stable_majors.select { |v| v.length == 1 }.uniq.sort.last.to_i
  list_versions.select { |v| v.split('.')[0].to_i >= latest_stable_major }.sort.join("\n")
end

#rm_list_cacheObject



125
126
127
# File 'lib/xcode/install.rb', line 125

def rm_list_cache
  FileUtils.rm_f(LIST_FILE)
end


129
130
131
132
133
# File 'lib/xcode/install.rb', line 129

def symlink(version)
  xcode = installed_versions.find { |x| x.version == version }
  `sudo rm -f #{SYMLINK_PATH}` unless current_symlink.nil?
  `sudo ln -sf #{xcode.path} #{SYMLINK_PATH}` unless xcode.nil? || SYMLINK_PATH.exist?
end


135
136
137
# File 'lib/xcode/install.rb', line 135

def symlinks_to
  File.absolute_path(File.readlink(current_symlink), SYMLINK_PATH.dirname) if current_symlink
end