Class: XcodeInstall::Installer
- Inherits:
-
Object
- Object
- XcodeInstall::Installer
- Defined in:
- lib/xcode/install.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#xcodes ⇒ Object
readonly
Returns the value of attribute xcodes.
Instance Method Summary collapse
- #cache_dir ⇒ Object
- #current_symlink ⇒ Object
- #download(version, progress, url = nil, progress_block = nil, retry_download_count = 3) ⇒ Object
- #exist?(version) ⇒ Boolean
- #find_xcode_version(version) ⇒ Object
-
#initialize ⇒ Installer
constructor
A new instance of Installer.
- #install_dmg(dmg_path, suffix = '', switch = true, clean = true) ⇒ Object
-
#install_version(version, switch = true, clean = true, install = true, progress = true, url = nil, show_release_notes = true, progress_block = nil, retry_download_count = 3) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #installed?(version) ⇒ Boolean
- #installed_versions ⇒ Object
- #list ⇒ Object
- #list_annotated(xcodes_list) ⇒ Object
- #mount(dmg_path) ⇒ Object
- #open_release_notes_url(version) ⇒ Object
- #rm_list_cache ⇒ Object
-
#seedlist ⇒ Object
Returns an array of ‘XcodeInstall::Xcode` <XcodeInstall::Xcode:0x007fa1d451c390 @date_modified=2015, @name=“6.4”, @path=“/Developer_Tools/Xcode_6.4/Xcode_6.4.dmg”, @url= “developer.apple.com/devcenter/download.action?path=/Developer_Tools/Xcode_6.4/Xcode_6.4.dmg”, @version=Gem::Version.new(“6.4”)>,.
- #symlink(version) ⇒ Object
- #symlinks_to ⇒ Object
Constructor Details
Instance Attribute Details
#xcodes ⇒ Object (readonly)
Returns the value of attribute xcodes.
126 127 128 |
# File 'lib/xcode/install.rb', line 126 def xcodes @xcodes end |
Instance Method Details
#cache_dir ⇒ Object
132 133 134 |
# File 'lib/xcode/install.rb', line 132 def cache_dir CACHE_DIR end |
#current_symlink ⇒ Object
136 137 138 |
# File 'lib/xcode/install.rb', line 136 def current_symlink File.symlink?(SYMLINK_PATH) ? SYMLINK_PATH : nil end |
#download(version, progress, url = nil, progress_block = nil, retry_download_count = 3) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/xcode/install.rb', line 140 def download(version, progress, url = nil, progress_block = nil, retry_download_count = 3) xcode = find_xcode_version(version) if url.nil? return if url.nil? && xcode.nil? dmg_file = Pathname.new(File.basename(url || xcode.path)) result = Curl.new.fetch( url: url || xcode.url, directory: CACHE_DIR, cookies: url ? nil : spaceship., output: dmg_file, progress: progress, progress_block: progress_block, retry_download_count: retry_download_count ) result ? CACHE_DIR + dmg_file : nil end |
#exist?(version) ⇒ Boolean
183 184 185 186 |
# File 'lib/xcode/install.rb', line 183 def exist?(version) return true if find_xcode_version(version) false end |
#find_xcode_version(version) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/xcode/install.rb', line 158 def find_xcode_version(version) # By checking for the name and the version we have the best success rate # Sometimes the user might pass # "4.3 for Lion" # or they might pass an actual Gem::Version # Gem::Version.new("8.0.0") # which should automatically match with "Xcode 8" begin parsed_version = Gem::Version.new(version) rescue ArgumentError nil end seedlist.each do |current_seed| return current_seed if current_seed.name == version end seedlist.each do |current_seed| return current_seed if parsed_version && current_seed.version == parsed_version end nil end |
#install_dmg(dmg_path, suffix = '', switch = true, clean = true) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/xcode/install.rb', line 223 def install_dmg(dmg_path, suffix = '', switch = true, clean = true) prompt = "Please authenticate for Xcode installation.\nPassword: " xcode_path = "/Applications/Xcode#{suffix}.app" if dmg_path.extname == '.xip' `xip -x #{dmg_path}` xcode_orig_path = File.join(Dir.pwd, 'Xcode.app') xcode_beta_path = File.join(Dir.pwd, '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 xcode = InstalledXcode.new(xcode_path) unless xcode.verify_integrity `sudo rm -rf #{xcode_path}` return end enable_developer_mode 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, progress_block = nil, retry_download_count = 3) ⇒ Object
rubocop:disable Metrics/ParameterLists
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/xcode/install.rb', line 286 def install_version(version, switch = true, clean = true, install = true, progress = true, url = nil, show_release_notes = true, progress_block = nil, retry_download_count = 3) dmg_path = get_dmg(version, progress, url, progress_block, retry_download_count) fail Informative, "Failed to download Xcode #{version}." if dmg_path.nil? if install install_dmg(dmg_path, "-#{version.to_s.split(' ').join('.')}", 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
188 189 190 |
# File 'lib/xcode/install.rb', line 188 def installed?(version) installed_versions.map(&:version).include?(version) end |
#installed_versions ⇒ Object
192 193 194 195 196 |
# File 'lib/xcode/install.rb', line 192 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 |
#list ⇒ Object
319 320 321 |
# File 'lib/xcode/install.rb', line 319 def list list_annotated(list_versions.sort { |first, second| compare_versions(first, second) }) end |
#list_annotated(xcodes_list) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/xcode/install.rb', line 305 def list_annotated(xcodes_list) installed = installed_versions.map(&:appname_version) xcodes_list.map do |x| xcode_version = x.split(' ') # split version and "beta N", "for Lion" xcode_version[0] << '.0' unless xcode_version[0].include?('.') # to match InstalledXcode.appname_version format version = Gem::Version.new(xcode_version.join('.')) installed.include?(version) ? "#{x} (installed)" : x end.join("\n") end |
#mount(dmg_path) ⇒ Object
337 338 339 340 341 342 343 |
# File 'lib/xcode/install.rb', line 337 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
299 300 301 302 303 |
# File 'lib/xcode/install.rb', line 299 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_cache ⇒ Object
323 324 325 |
# File 'lib/xcode/install.rb', line 323 def rm_list_cache FileUtils.rm_f(LIST_FILE) end |
#seedlist ⇒ Object
Returns an array of ‘XcodeInstall::Xcode`
<XcodeInstall::Xcode:0x007fa1d451c390
@date_modified=2015,
@name="6.4",
@path="/Developer_Tools/Xcode_6.4/Xcode_6.4.dmg",
@url=
"https://developer.apple.com/devcenter/download.action?path=/Developer_Tools/Xcode_6.4/Xcode_6.4.dmg",
@version=Gem::Version.new("6.4")>,
the resulting list is sorted with the most recent release as first element
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/xcode/install.rb', line 208 def seedlist @xcodes = Marshal.load(File.read(LIST_FILE)) if LIST_FILE.exist? && xcodes.nil? all_xcodes = (xcodes || fetch_seedlist) # We have to set the `installed` value here, as we might still use # the cached list of available Xcode versions, but have a new Xcode # installed in the mean-time cached_installed_versions = installed_versions.map(&:bundle_version) all_xcodes.each do |current_xcode| current_xcode.installed = cached_installed_versions.include?(current_xcode.version) end all_xcodes.sort_by { |seed| [seed.version, -seed.date_modified] }.reverse end |
#symlink(version) ⇒ Object
327 328 329 330 331 |
# File 'lib/xcode/install.rb', line 327 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 |
#symlinks_to ⇒ Object
333 334 335 |
# File 'lib/xcode/install.rb', line 333 def symlinks_to File.absolute_path(File.readlink(current_symlink), SYMLINK_PATH.dirname) if current_symlink end |