Class: XcodeInstaller::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/xcode-installer/install.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstall

Returns a new instance of Install.



19
20
21
# File 'lib/xcode-installer/install.rb', line 19

def initialize
  @copied_kb = 0
end

Instance Attribute Details

#copied_file_countObject

Returns the value of attribute copied_file_count.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def copied_file_count
  @copied_file_count
end

#copied_kbObject

Returns the value of attribute copied_kb.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def copied_kb
  @copied_kb
end

#progress_barObject

Returns the value of attribute progress_bar.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def progress_bar
  @progress_bar
end

#releaseObject

Returns the value of attribute release.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def release
  @release
end

#verboseObject

Returns the value of attribute verbose.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def verbose
  @verbose
end

#version_suffixObject

Returns the value of attribute version_suffix.



17
18
19
# File 'lib/xcode-installer/install.rb', line 17

def version_suffix
  @version_suffix
end

Instance Method Details

#accumulate_kbytes(path) ⇒ Object



92
93
94
95
# File 'lib/xcode-installer/install.rb', line 92

def accumulate_kbytes(path)
  @copied_file_count = copied_file_count + 1
  @progress_bar.progress = @copied_file_count.to_i
end

#action(args, options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xcode-installer/install.rb', line 23

def action(args, options)
  install_type = (args.include? 'cli') ? 'cli' : 'gui'

  @verbose = options.verbose
  mgr = XcodeInstaller::ReleaseManager.new
  @release = mgr.get_release(options.release, options.pre_release, install_type)
  @version_suffix = "-#{@release['version']}"

  files = Dir.glob(dmg_file_name)
  if files.length == 0
    puts "#{dmg_file_name} file not found in current directory. Run the download command first."
    return
  elsif files.length > 1
    puts "Multiple #{dmg_file_name} files found in the current directory. Is this partition formatted with a case-insensitive disk format?"
    return
  end
  dmg_file = files[0]
  puts dmg_file if @verbose

  # Mount disk image
  mountpoint = '/Volumes/Xcode'
  # system "hdid '#{dmg_file}' -mountpoint #{mountpoint}"
  system "hdiutil attach -quiet #{dmg_file}"

  #
  # Install
  #
  if (install_type == 'gui')
    app_bundle_name = @release['app_bundle_name'] ||= "Xcode.app"

    # Trash existing install (so command is rerunnable)
    destination = "/Applications/Xcode#{version_suffix}.app"
    Trash.new.throw_out(destination)

    # TODO: Dynamically determine .app file name (DP releases have the version embedded)
    copy("#{mountpoint}/#{app_bundle_name}", destination)
  else
    # CLI install
    mountpoint = '/Volumes/Command\ Line\ Developer\ Tools'
    system "sudo installer -package #{mountpoint}/*.pkg -target /"
  end

  system "hdiutil detach -quiet #{mountpoint}"
end

#copy(source_path, destination_path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xcode-installer/install.rb', line 72

def copy(source_path, destination_path)
  # Copy into /Applications
  # puts 'Copying Xcode.app into Applications directory (this can take a little while)'
  puts "#{source_path} -> #{destination_path}"

  total_files = `find #{source_path} -print | wc -l`
  puts "total_files: #{total_files}" if @verbose
  @progress_bar = ProgressBar.create(:title => "Copying", :starting_at => 0, :total => total_files.to_i)
  @copied_file_count = 0
  cp_r(source_path, destination_path, {})
  @progress_bar.finish
end

#copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/xcode-installer/install.rb', line 114

def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
  Entry_.new(src, nil, dereference_root).traverse do |ent|
    destent = Entry_.new(dest, ent.rel, false)

    # puts "#{dir_size(ent.path)} #{ent.path}"
    accumulate_kbytes(ent.path)

    File.unlink destent.path if remove_destination && File.file?(destent.path)
    ent.copy destent.path
    ent. destent.path if preserve
  end
end

#cp_r(src, dest, options = {}) ⇒ Object

# The following code was copied out of fileutils.rb from ruby 1.9.3-p392 #

#


103
104
105
106
107
108
109
110
111
112
# File 'lib/xcode-installer/install.rb', line 103

def cp_r(src, dest, options = {})
  # fu_check_options options, OPT_TABLE['cp_r']
  # fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  return if options[:noop]
  options = options.dup
  options[:dereference_root] = true unless options.key?(:dereference_root)
  fu_each_src_dest(src, dest) do |s, d|
    copy_entry s, d, options[:preserve], options[:dereference_root], options[:remove_destination]
  end
end

#dir_size(path) ⇒ Object

Exmaple output of the du command: 2359828 /Volumes/Xcode/Xcode.app/



87
88
89
90
# File 'lib/xcode-installer/install.rb', line 87

def dir_size(path)
  output = `du -sk '#{path}' 2> /dev/null`
  return output.split(" ").first.to_i * 1024
end

#dmg_file_nameObject



68
69
70
# File 'lib/xcode-installer/install.rb', line 68

def dmg_file_name
  return File.basename(@release['download_url'])
end

#fu_each_src_dest(src, dest) ⇒ Object



127
128
129
130
131
132
# File 'lib/xcode-installer/install.rb', line 127

def fu_each_src_dest(src, dest)
  fu_each_src_dest0(src, dest) do |s, d|
    raise ArgumentError, "same file: #{s} and #{d}" if File.identical?(s, d)
    yield s, d, File.stat(s)
  end
end

#fu_each_src_dest0(src, dest) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/xcode-installer/install.rb', line 134

def fu_each_src_dest0(src, dest)
  if tmp = Array.try_convert(src)
    tmp.each do |s|
      s = File.path(s)
      yield s, File.join(dest, File.basename(s))
    end
  else
    src = File.path(src)
    if File.directory?(dest)
      yield src, File.join(dest, File.basename(src))
    else
      yield src, File.path(dest)
    end
  end
end