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.



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

def initialize
  FileUtils.mkdir_p(CACHE_DIR)
end

Instance Attribute Details

#xcodesObject (readonly)

Returns the value of attribute xcodes.



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

def xcodes
  @xcodes
end

Instance Method Details

#cache_dirObject



52
53
54
# File 'lib/xcode/install.rb', line 52

def cache_dir
  CACHE_DIR
end


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

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

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



60
61
62
63
64
65
66
67
# File 'lib/xcode/install.rb', line 60

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)


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

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

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



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/xcode/install.rb', line 83

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_path = "/Applications/Xcode#{suffix}.app"

  if dmg_path.extname == '.xip'
    `'#{archive_util}' #{dmg_path}`
    xcode_orig_path = dmg_path.dirname + 'Xcode.app'
    xcode_beta_path = dmg_path.dirname + 'Xcode-beta.app'
    if Pathname.new(xcode_orig_path).exist?
      `sudo -p "#{prompt}" mv "#{xcode_orig_path}" "#{xcode_path}"`
    elsif Pathname.new(xcode_beta_path).exist?
      `sudo -p "#{prompt}" mv "#{xcode_beta_path}" "#{xcode_path}"`
    else
      out = <<-HELP
No `Xcode.app(or Xcode-beta.app)` found in XIP. 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
  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 -rf #{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, show_release_notes = true) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/xcode/install.rb', line 145

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

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

  open_release_notes_url(version) if show_release_notes && !url
end

#installed?(version) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/xcode/install.rb', line 73

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

#installed_versionsObject



77
78
79
80
81
# File 'lib/xcode/install.rb', line 77

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



177
178
179
# File 'lib/xcode/install.rb', line 177

def list
  list_annotated(list_versions.sort)
end

#list_annotated(xcodes_list) ⇒ Object



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

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



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

def list_current
  safe_list_versions = list_versions.reject { |v| v.nil? || v.empty? }
  stable_majors = safe_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 = safe_list_versions.select { |v| v.split('.')[0].to_i >= latest_stable_major }.sort
  list_annotated(current_versions)
end

#mount(dmg_path) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/xcode/install.rb', line 195

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



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

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



181
182
183
# File 'lib/xcode/install.rb', line 181

def rm_list_cache
  FileUtils.rm_f(LIST_FILE)
end


185
186
187
188
189
# File 'lib/xcode/install.rb', line 185

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


191
192
193
# File 'lib/xcode/install.rb', line 191

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