lib/xcode/install.rb in xcode-install-0.0.3 vs lib/xcode/install.rb in xcode-install-0.1.0

- old
+ new

@@ -1,8 +1,9 @@ require "fastlane_core" require "fastlane_core/developer_center/developer_center" require "nokogiri" +require "rubygems/version" require "xcode/install/command" require "xcode/install/version" module FastlaneCore class DeveloperCenter @@ -60,10 +61,14 @@ def initialize FileUtils.mkdir_p(CACHE_DIR) end + def cache_dir + CACHE_DIR + end + def current_symlink File.symlink?(SYMLINK_PATH) ? SYMLINK_PATH : nil end def download(version) @@ -82,24 +87,42 @@ def installed?(version) installed_versions.map { |x| x.version }.include?(version) end def installed_versions - @installed ||= installed.map { |x| InstalledXcode.new(x) } + @installed ||= installed.map { |x| InstalledXcode.new(x) }.sort { + |a,b| Gem::Version.new(a.version) <=> Gem::Version.new(b.version) + } end - def install_dmg(dmgPath, suffix = '') + def install_dmg(dmgPath, suffix = '', switch = true, clean = 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? + puts 'No `Xcode.app` found in DMG.' + return + end + `sudo ditto "#{source}" "#{xcode_path}"` `umount "/Volumes/Xcode"` - `sudo xcode-select -s #{xcode_path}` - puts `xcodebuild -version` + enable_developer_mode + `sudo xcodebuild -license` unless xcode_license_approved? + + 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 def list_current majors = list_versions.map { |v| v.split('.')[0] }.select { |v| v.length == 1 }.uniq list_versions.select { |v| v.start_with?(majors.last) }.join("\n") @@ -125,16 +148,22 @@ :private CACHE_DIR = Pathname.new("#{ENV['HOME']}/Library/Caches/XcodeInstall") LIST_FILE = CACHE_DIR + Pathname.new('xcodes.bin') + MINIMUM_VERSION = Gem::Version.new('4.3') SYMLINK_PATH = Pathname.new('/Applications/Xcode.app') def devcenter @devcenter ||= FastlaneCore::DeveloperCenter.new end + def enable_developer_mode + `sudo /usr/sbin/DevToolsSecurity -enable` + `sudo /usr/sbin/dseditgroup -o edit -t group -a staff _developer` + end + def get_seedlist @xcodes = parse_seedlist(devcenter.download_seedlist) @xcodes += prereleases File.open(LIST_FILE,'w') do |f| @@ -149,11 +178,13 @@ end def parse_seedlist(seedlist) seedlist['data'].select { |t| /^Xcode [0-9]/.match(t['name']) - }.map { |x| Xcode.new(x) }.sort { |a,b| a.dateModified <=> b.dateModified } + }.map { |x| Xcode.new(x) }.reject { |x| x.version < MINIMUM_VERSION }.sort { + |a,b| a.dateModified <=> b.dateModified + } end def list_versions installed = installed_versions.map { |x| x.version } seedlist.map { |x| x.name }.reject { |x| installed.include?(x) } @@ -168,10 +199,14 @@ def seedlist @xcodes = Marshal.load(File.read(LIST_FILE)) if LIST_FILE.exist? && xcodes.nil? xcodes || get_seedlist end + + def xcode_license_approved? + !(`/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?) + end end class InstalledXcode attr_reader :path attr_reader :version @@ -192,20 +227,27 @@ class Xcode attr_reader :dateModified attr_reader :name attr_reader :path attr_reader :url + attr_reader :version def initialize(json) @dateModified = json['dateModified'].to_i @name = json['name'].gsub(/^Xcode /, '') @path = json['files'].first['remotePath'] @url = "https://developer.apple.com/devcenter/download.action?path=#{@path}" + + begin + @version = Gem::Version.new(@name.split(' ')[0]) + rescue + @version = Installer::MINIMUM_VERSION + end end def self.new_prelease(version, url) self.new({'name' => version, 'dateModified' => Time.now.to_i, 'files' => [{'remotePath' => url.split('=').last}]}) end end -end \ No newline at end of file +end