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

#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



110
111
112
113
114
115
116
117
118
# File 'lib/xcode-installer/install.rb', line 110

def accumulate_kbytes(path)
  # @progress_bar.log path
  # @progress_bar.increment
  # @copied_kb += File.stat(path).size if File.exists?(path)
  @copied_file_count++
  if @copied_file_count.modulo(10000) == 0
    @progress_bar.progress = dir_size("/Applications/Xcode-5.app")
  end
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
# File 'lib/xcode-installer/install.rb', line 23

def action(args, options)
  mgr = XcodeInstaller::ReleaseManager.new
  @release = mgr.get_release(options.release, options.pre_release)
  @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]
  # TODO: if verbose...
  # puts dmg_file

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

  # 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}/Xcode.app", destination)

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

#copy(source_path, destination_path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/xcode-installer/install.rb', line 59

def copy(source_path, destination_path)
  # Copy into /Applications
  # puts 'Copying Xcode.app into Applications directory (this can take a little while)'
  # TODO: wrap debug output with verbose checks
  puts "#{source_path} -> #{destination_path}"
  # system "cp -R #{source_path} #{destination_path}"

  total_kb = dir_size(source_path)
  # puts total_kb

  # puts File.stat(source_path).size

  @progress_bar = ProgressBar.create(:title => "Copying", :starting_at => 0, :total => @release['app_size_extracted'])
  @copied_file_count = 0
  cp_r(source_path, destination_path, {})
  @progress_bar.finish

  # block_size = File.stat(source_path).blksize
  # puts @copied_kb

  # in_name     = "src_file.txt"
  # out_name    = "dest_file.txt"

  # in_file     = File.new(in_name, "r")
  # out_file    = File.new(out_name, "w")

  # in_size     = File.size(in_name)
  # batch_bytes = ( in_size / 100 ).ceil
  # total       = 0
  # p_bar       = ProgressBar.new('Copying', 100)

  # buffer      = in_file.sysread(batch_bytes)
  # while total < in_size do
  #  out_file.syswrite(buffer)
  #  p_bar.inc
  #  total += batch_bytes
  #  if (in_size - total) < batch_bytes
  #    batch_bytes = (in_size - total)
  #  end
  #  buffer = in_file.sysread(batch_bytes)
  # end
  # p_bar.finish
end

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



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

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 #

#


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

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/



105
106
107
108
# File 'lib/xcode-installer/install.rb', line 105

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

#dmg_file_nameObject



55
56
57
# File 'lib/xcode-installer/install.rb', line 55

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

#fu_each_src_dest(src, dest) ⇒ Object



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

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/xcode-installer/install.rb', line 157

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