Script Ruby para instalar várias Gems

22/12/2008 by RubyBR
Categoria: Aprenda Ruby, Ruby 

O Akhil Bansal conta que recentemente preparou vários servidores com Rails, tendo que instalar algums gems pelo tradicional processo do "gem install xyz" muitas vezes. Isso é muito chato.

Enquanto ele estava lá, digitando pela enésima vez "gem install xyz", teve a idéia de criar um script que pegasse uma lista de gems e as instalasse automaticamente.

O script funcionou muito bem para ele, salvando muito de seu precioso tempo. Ele disponibilizou o código do script salvador no seu site para download, e nós reproduzimos aqui, abaixo do break.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
#!/usr/bin/env ruby
gems =<<EOS
ParseTree, RedCloth, acts_as_paranoid, builder, capistrano, chronic, colored, daemons, facets, fastercsv, fastthread, flickr, gem_plugin, 
haml, heckle, highline, hoe, hpricot, libxml-ruby, mime-types, mislav-will_paginate, mongrel, mysql, net-scp, net-sftp, net-ssh, 
net-ssh-gateway, passenger, piston, rails, rcov, rmagick, rspec, ruby-debug, rubyforge, sake, termios, xml-simple
EOS
 
 
 
msg =<<EOS
###############################################################
#
# This script is written by Akhil Bansal(http://webonrails.com)
# It worked fine for me. Use this script at your own risk, if
# your computer explode, it will not be my fault ;-)
#
# Thanks Rishav (http://sazwqa.com/blog/) for hacking it for windows.
###############################################################
EOS
 
def clear(n = 2)
  n.times do puts ""; end
end
 
clear(50);
puts msg;
clear(5)
 
gems_array = gems.split(',')
 
gems = {}
 
gems_array.each do |g|
	g = g.strip
	gems.merge!({g , false})
end
 
 
puts "This script will install all the gems listed below. "
puts "If you want to install more gems, add them as comma separated list in the first line of this script file."
clear(2)
puts gems.keys.sort.join(', ')
clear(2)
puts "Install all gems listed above [Yes(y) | No(n) | Abort(a)]: "
 
option = gets.chop 
 
if option.upcase == 'A'
	puts "Aborting..."
	exit(0)
end
 
 
if option.upcase == 'Y'
		gems.each do |gem, value|
			gems[gem]=true
		end
else
	gems.each do |gem, value|
		puts "install gem -> #{gem} (y/n): "
		option = gets.chop
		if option.upcase == 'Y'
			gems[gem]=true
		end
	end
end
puts "Adding gem source http://gems.github.com"
`gem sources -a http://gems.github.com`
@size = gems.size
@count = 0
 
install_string = "gem install "
if RUBY_PLATFORM =~ /mingw|mswin/
	install_string = "cmd /c" + " "  + install_string
end
 
gems.each do |gem, value|
	if gems[gem]
		@count +=1	
		puts "installing #{gem}    (#{@count}/#{@size})..."
		system("#{install_string} #{gem} --no-ri --no-rdoc")
	end
end
 
 
clear(5)
 
puts "If this script helped you in any way, please Recommend me on Working With Rails by clicking on the link below:"
puts "http://workingwithrails.com/recommendation/new/person/2892-akhil-bansal"
 
clear(3)

Copie o código e salve com o nome de install_gems.rb. Para testar, abra o console ou o prompt do Windows e digite ruby install_gems.rb. Depois, siga as instruções na tela. As gems que serão instaladas estão listadas no início do código separadas por vírgulas; caso queira acrescentar/excluir uma gem, basta alterar essa lista.

[Via WebOnRails]


Veja também:
  • Como conectar no Orkut com Ruby?
  • Desenvolvendo um spider (crawler) em Ruby
  • Interagindo no Twitter com Ruby - parte 2
  • Tutorial: Como instalar o WARR (Windows + Apache + Ruby + Rails)
  • E-book grátis: The Book of Ruby

  • Comentários

    2 Comentários para Script Ruby para instalar várias Gems

    1. Junio Vitorino on seg, 22nd dez 2008 12:00
    2. Maneiro, eu costumava fazer scripts quando trabalhava com linux também, já cheguei a ter um script com mais de 4.5 mil linhas, que instalava um server completo. Bons tempos…..

    3. Marcos on ter, 23rd dez 2008 2:41
    4. putz!
      eu tb tinha uns scripts legais.
      mas esse dai é bom pra estudar.

    Diga-nos o que pensa...
    e se você pocura uma imagem para colocar em seu comentário, procure o gravatar!