Class: Pik::Install

Inherits:
Command show all
Defined in:
lib/pik/commands/install_command.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#config, #debug, #options, #output, #version

Instance Method Summary collapse

Methods inherited from Command

#actual_gem_home, #add_sigint_handler, aka, choose_from, #close, #cmd_name, cmd_name, #create, #current_gem_bin_path, #current_version?, #default_gem_home, #delete_old_pik_script, description, #editors, #find_config_from_path, #gem_path, #get_version, hl, inherited, it, names, #parse_options, #pik_version, #sh, summary

Constructor Details

#initialize(args = ARGV, config_ = nil) ⇒ Install

Returns a new instance of Install.



14
15
16
17
18
19
# File 'lib/pik/commands/install_command.rb', line 14

def initialize(args=ARGV, config_=nil)
  super
  @download_dir = config.global[:download_dir] || PIK_HOME + 'downloads'
  @install_root = config.global[:install_dir]  || PIK_HOME + 'rubies'
  FileUtils.mkdir_p @download_dir.to_s
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



12
13
14
# File 'lib/pik/commands/install_command.rb', line 12

def target
  @target
end

Instance Method Details

#command_optionsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pik/commands/install_command.rb', line 30

def command_options
  super
  sep =<<SEP
  Choices are: ruby, jruby, or ironruby
  
  If no version is specified, the latest version will be installed.
  Download and install locations can be configured with 'pik config'.
  
  Examples:

# install the latest version of JRuby (currently 1.4.0RC1)
>pik install jruby

# install the latest 1.8 version of MinGW Ruby 
>pik install ruby 1.8    

SEP
  options.separator sep  
end

#download(package, download_dir = @download_dir) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/pik/commands/install_command.rb', line 50

def download(package, download_dir=@download_dir)   
  target = download_dir + package. split('/').last
  puts "** Downloading:  #{package} \n   to:  #{target.windows}\n\n"
  URI.download(package, target.to_s, {:progress => true})
  puts
  return target
end

#download_seven_zipObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pik/commands/install_command.rb', line 76

def download_seven_zip
  question = "You need the 7zip utility to extract this file.\n"
  question << "Would you like me to download it? (yes/no)"
  if @hl.agree(question){|answer| answer.default = 'yes' }
    uri  = 'http://downloads.sourceforge.net/sevenzip/7za465.zip'
    file = download(uri)
    Zip.fake_unzip(file.to_s, /\.exe|\.dll$/, PIK_SCRIPT.dirname.to_s)
  else
    raise QuitError
  end
end

#executeObject



21
22
23
24
25
26
27
28
# File 'lib/pik/commands/install_command.rb', line 21

def execute
  implementation  = Implementations[@args.shift]
  @target, package = implementation.find(*@args)
  @target          = @install_root + "#{implementation.name}-#{@target.gsub('.','')}"
  file            = download(package)
  extract(@target, file)
  implementation.after_install(self)
end

#extract(target, file) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/pik/commands/install_command.rb', line 58

def extract(target, file)
  if Which::SevenZip.exe
    FileUtils.mkdir_p target
    extract_(file, target)
  else
    download_seven_zip
    extract(target, file)
  end  
end

#extract_(file, target, options = {}) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pik/commands/install_command.rb', line 88

def extract_(file, target, options = {})
  fail unless File.directory?(target)
  
  # create a temporary folder used to extract files there
  tmpdir = File.expand_path(File.join(Dir.tmpdir, "extract_sandbox_#{$$}"))
  FileUtils.mkpath(tmpdir) unless File.exist?(tmpdir)

  # based on filetypes, extract the intermediate file into the temporary folder
  case file
    # tar.z, tar.gz and tar.bz2 contains .tar files inside, extract into 
    # temp first
    when /(^.+\.tar)\.z$/, /(^.+\.tar)\.gz$/, /(^.+\.tar)\.bz2$/
      seven_zip tmpdir, file
      seven_zip target, File.join(tmpdir, File.basename($1))
    when /(^.+)\.tgz$/
      seven_zip tmpdir, file
      seven_zip target, File.join(tmpdir, "#{File.basename($1)}.tar")
    when /(^.+\.zip$)/, /(^.+\.7z$)/
      seven_zip(target, $1)
    else
      raise "Unknown file extension! (for file #{file})"
  end

  # after extraction, lookup for a folder that contains '-' (version number or datestamp)
  folders_in_target = []
  Dir.chdir(target) { folders_in_target = Dir.glob('*') }

  # the package was created inside another folder
  # exclude folders packagename-X.Y.Z or packagename-DATE
  # move all the folders within that into target directly.
  folders_in_target.each do |folder|
    next unless File.directory?(File.join(target, folder)) && folder =~ /\-/

    # take the folder contents out!, now!
    contents = []
    Dir.chdir(File.join(target, folder)) { contents = Dir.glob('*') }

    contents.each do |c|
      #puts "** Moving out #{c} from #{folder} and drop into #{target}" if Rake.application.options.trace
      FileUtils.mv File.join(target, folder, c), target, :force => true
    end
    
    # remove the now empty folder
    # puts "** Removing #{folder}" if Rake.application.options.trace
    FileUtils.rm_rf File.join(target, folder)
  end

  # remove the temporary directory
  # puts "** Removing #{tmpdir}" if Rake.application.options.trace
  FileUtils.rm_rf tmpdir
end

#seven_zip(target, file) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pik/commands/install_command.rb', line 68

def seven_zip(target, file)
  file = Pathname(file)
  seven_zip = Which::SevenZip.exe.basename 
  puts "** Extracting:  #{file.windows}\n   to:  #{target}" #if verbose
  system("#{seven_zip} x -y -o\"#{target}\" \"#{file.windows}\" > NUL")
  puts 'done'
end