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.



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

def initialize
  FileUtils.mkdir_p(CACHE_DIR)
end

Instance Attribute Details

#xcodesObject (readonly)

Returns the value of attribute xcodes.



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

def xcodes
  @xcodes
end

Instance Method Details

#cache_dirObject



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

def cache_dir
  CACHE_DIR
end


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

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

#download(version, progress) ⇒ Object



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

def download(version, progress)
  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, progress)
  result ? CACHE_DIR + dmg_file : nil
end

#exist?(version) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#install_components(xcode_path) ⇒ Object



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

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

#install_dmg(dmgPath, suffix = '', switch = true, clean = true, components = 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
# File 'lib/xcode/install.rb', line 74

def install_dmg(dmgPath, suffix = '', switch = true, clean = true, components = 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 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(dmgPath) if clean
end

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



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

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

  install_dmg(dmg_path, "-#{version.split(' ')[0]}", switch, clean, components) if install
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 ||= installed.map { |x| InstalledXcode.new(x) }.sort do |a, b|
    Gem::Version.new(a.version) <=> Gem::Version.new(b.version)
  end
end

#listObject



123
124
125
# File 'lib/xcode/install.rb', line 123

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

#list_currentObject



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

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



127
128
129
# File 'lib/xcode/install.rb', line 127

def rm_list_cache
  FileUtils.rm_f(LIST_FILE)
end


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

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


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

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