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 exist?(version) || url
  xcode = seedlist.find { |x| x.name == version }
  dmg_file = Pathname.new(File.basename(url || xcode.path))

  result = Curl.new.fetch(url || xcode.url, CACHE_DIR, 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
# File 'lib/xcode/install.rb', line 74

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

  mount_dir = mount(dmg_path)
  puts 'Please authenticate for Xcode installation...'
  source = Dir.glob(File.join(mount_dir, 'Xcode*.app')).first

  if source.nil?
    out = "No `Xcode.app` found in DMG. Please remove \#{dmg_path} if you suspect a corrupted\ndownload or run `xcversion update` to see if the version you tried to install\nhas been pulled by Apple. If none of this is true, please open a new GH issue.\n"
    $stderr.puts out.tr("\n", ' ')
    return
  end

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

  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



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

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)
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



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

def list
  list_annotated(list_versions.sort)
end

#list_annotated(xcodes_list) ⇒ Object



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

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



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

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



160
161
162
163
164
165
166
# File 'lib/xcode/install.rb', line 160

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



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

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



146
147
148
# File 'lib/xcode/install.rb', line 146

def rm_list_cache
  FileUtils.rm_f(LIST_FILE)
end


150
151
152
153
154
# File 'lib/xcode/install.rb', line 150

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


156
157
158
# File 'lib/xcode/install.rb', line 156

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