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.



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

def initialize
  FileUtils.mkdir_p(CACHE_DIR)
end

Instance Attribute Details

#xcodesObject (readonly)

Returns the value of attribute xcodes.



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

def xcodes
  @xcodes
end

Instance Method Details

#cache_dirObject



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

def cache_dir
  CACHE_DIR
end


47
48
49
# File 'lib/xcode/install.rb', line 47

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

#download(version, progress, url = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/xcode/install.rb', line 51

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

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

#exist?(version) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/xcode/install.rb', line 74

def install_dmg(dmg_path, suffix = '', switch = true, clean = true)
  archive_util = '/System/Library/CoreServices/Applications/Archive Utility.app/Contents/MacOS/Archive Utility'
  prompt = "Please authenticate for Xcode installation.\nPassword: "
  xcode_beta_path = dmg_path.dirname + 'Xcode-beta.app'
  xcode_path = "/Applications/Xcode#{suffix}.app"

  if dmg_path.extname == '.xip'
    `'#{archive_util}' #{dmg_path}`
    `sudo -p "#{prompt}" mv "#{xcode_beta_path}" "#{xcode_path}"`
  else
    mount_dir = mount(dmg_path)
    source = Dir.glob(File.join(mount_dir, 'Xcode*.app')).first

    if source.nil?
      out = <<-HELP
No `Xcode.app` found in DMG. Please remove #{dmg_path} if you suspect a corrupted
download or run `xcversion 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
      $stderr.puts out.tr("\n", ' ')
      return
    end

    `sudo -p "#{prompt}" ditto "#{source}" "#{xcode_path}"`
    `umount "/Volumes/Xcode"`
  end

  unless verify_integrity(xcode_path)
    `sudo rm -f #{xcode_path}`
    return
  end

  enable_developer_mode
  xcode = InstalledXcode.new(xcode_path)
  xcode.approve_license
  xcode.install_components

  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(dmg_path) if clean
end

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



122
123
124
125
126
127
128
129
# File 'lib/xcode/install.rb', line 122

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

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

  open_release_notes_url(version) unless url
end

#installed?(version) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#installed_versionsObject



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

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

#listObject



149
150
151
# File 'lib/xcode/install.rb', line 149

def list
  list_annotated(list_versions.sort)
end

#list_annotated(xcodes_list) ⇒ Object



137
138
139
140
# File 'lib/xcode/install.rb', line 137

def list_annotated(xcodes_list)
  installed = installed_versions.map(&:version)
  xcodes_list.map { |x| installed.include?(x) ? "#{x} (installed)" : x }.join("\n")
end

#list_currentObject



142
143
144
145
146
147
# File 'lib/xcode/install.rb', line 142

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
  current_versions = list_versions.select { |v| v.split('.')[0].to_i >= latest_stable_major }.sort
  list_annotated(current_versions)
end

#mount(dmg_path) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/xcode/install.rb', line 167

def mount(dmg_path)
  plist = hdiutil('mount', '-plist', '-nobrowse', '-noverify', dmg_path.to_s)
  document = REXML::Document.new(plist)
  node = REXML::XPath.first(document, "//key[.='mount-point']/following-sibling::*[1]")
  fail Informative, 'Failed to mount image.' unless node
  node.text
end

#open_release_notes_url(version) ⇒ Object



131
132
133
134
135
# File 'lib/xcode/install.rb', line 131

def open_release_notes_url(version)
  return if version.nil?
  xcode = seedlist.find { |x| x.name == version }
  `open #{xcode.release_notes_url}` unless xcode.nil? || xcode.release_notes_url.nil?
end

#rm_list_cacheObject



153
154
155
# File 'lib/xcode/install.rb', line 153

def rm_list_cache
  FileUtils.rm_f(LIST_FILE)
end


157
158
159
160
161
# File 'lib/xcode/install.rb', line 157

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


163
164
165
# File 'lib/xcode/install.rb', line 163

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