Class: Pik::Install

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

Instance Attribute Summary

Attributes inherited from Command

#config, #options, #output, #version

Instance Method Summary collapse

Methods inherited from Command

#add_sigint_handler, aka, choose_from, clean_gem_batch, #close, cmd_name, #create, #current_gem_bin_path, #current_version?, #default_gem_home, #delete_old_pik_batches, description, #editors, #find_config_from_path, #get_version, hl, inherited, #initialize, it, names, #parse_options, summary

Constructor Details

This class inherits a constructor from Pik::Command

Instance Method Details

#add(path) ⇒ Object



101
102
103
104
105
106
# File 'lib/pik/commands/install_command.rb', line 101

def add(path)
  puts
  p = Pik::Add.new([path], config)
  p.execute
  p.close
end

#command_optionsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pik/commands/install_command.rb', line 60

def command_options
  super
  sep ="  Choices are: ruby, jruby, or ironruby\n  \n  If no version is specified, the latest version will be installed.\n  Download and install locations can be configured with 'pik config'.\n  \n  Examples:\n\n# install the latest version of JRuby (currently 1.4.0RC1)\n>pik install jruby\n\n# install the latest 1.8 version of MinGW Ruby \n>pik install ruby 1.8    \n\n"
  options.separator sep  
end

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



84
85
86
87
88
89
90
# File 'lib/pik/commands/install_command.rb', line 84

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

#download_seven_zipObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pik/commands/install_command.rb', line 116

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

#executeObject



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
# File 'lib/pik/commands/install_command.rb', line 33

def execute
  target = ''
  @download_dir = config.global[:download_dir] || PIK_HOME + 'downloads'
  FileUtils.mkdir_p @download_dir.to_s
  version = @args.shift
  if versions[version]
    target += version
    version = versions[version]
    @args.each{ |i| version = version.select{|k,v| k =~ Regexp.new(Regexp.escape(i)) } }
    t, version = most_recent(version)
    target += "_#{t}"
  elsif URI.parse(version.to_s) === URI::HTTP
    version = URI.parse(version.to_s)
    target  = @args.shift
  else
    raise VersionUnknown
  end
  install_root = config.global[:install_dir] || PIK_BATCH.dirname + 'pik'
  target =  install_root + target.gsub('.','')
  FileUtils.mkdir_p target
  file = download(version)
  extract(target, file)
  add( Pathname(target) + 'bin' )
rescue VersionUnknown
  puts 'unknown'   
end

#extract(target, file) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/pik/commands/install_command.rb', line 92

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

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pik/commands/install_command.rb', line 128

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

#most_recent(version) ⇒ Object



80
81
82
# File 'lib/pik/commands/install_command.rb', line 80

def most_recent(version)
  version.sort.last
end

#seven_zip(target, file) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/pik/commands/install_command.rb', line 108

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

#versionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pik/commands/install_command.rb', line 12

def versions
   {
    'ruby' => {
      '1.8.6-p383' =>
      'http://rubyforge.org/frs/download.php/62267/ruby-1.8.6-p383-i386-mingw32.7z',
      '1.9.1-p243' =>
      'http://rubyforge.org/frs/download.php/62269/ruby-1.9.1-p243-i386-mingw32.7z'
      },
    'ironruby' => {
      '0.9.1' =>
      'http://rubyforge.org/frs/download.php/64504/ironruby-0.9.1.zip',
      '0.9.0' =>
      'http://rubyforge.org/frs/download.php/61382/ironruby-0.9.0.zip'
    },
    'jruby' => {
      '1.4.0RC1' =>
      'http://dist.codehaus.org/jruby/1.4.0RC1/jruby-bin-1.4.0RC1.zip'
    }
  }
end