gemtronics 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ module Gemtronics
2
+ # Represents the definition of a Gem.
3
+ class Definition < Hash
4
+
5
+ # Get/set the name of the gem.
6
+ attr_accessor :name
7
+ # Get/set the version of the gem. Defaults to <tt>>=0.0.0</tt>
8
+ attr_accessor :version
9
+ # Get/set the source of the gem. Defaults to <tt>http://gems.rubyforge.org</tt>
10
+ attr_accessor :source
11
+
12
+ # Returns true/false if the gem should be required. Defaults to <tt>true</tt>.
13
+ def load?
14
+ # method built dynamically. This is just a stub for RDoc.
15
+ end
16
+
17
+ # Set whether the gem should be required.
18
+ def load=(x)
19
+ # method built dynamically. This is just a stub for RDoc.
20
+ end
21
+
22
+ # Returns an Array of files that should be required. Defaults to <tt>[<name>]</tt>
23
+ def require_list
24
+ return [self.name] unless self.has_key?(:require)
25
+ return [self.fetch(:require)].flatten
26
+ end
27
+
28
+ # Sets the names of the files that should be required.
29
+ def require_list=(x)
30
+ self.store(:require, [x].flatten)
31
+ end
32
+
33
+ # Returns <tt><name>-<version></tt>.
34
+ #
35
+ # Example:
36
+ # gd = Gemtronics::Definition.new
37
+ # gd.name = 'configatron'
38
+ # gd.version = '2.3.0'
39
+ # gd.to_s #=> 'configatron-2.3.0'
40
+ def to_s
41
+ return "#{self.name}-#{self.version}"
42
+ end
43
+
44
+ # Generates an install command string.
45
+ #
46
+ # Example:
47
+ # gd = Gemtronics::Definition.new
48
+ # gd.name = 'configatron'
49
+ # gd.version = '2.3.0'
50
+ # gd.install_command #=> 'gem install configatron --source=http://gems.rubyforge.org --version=2.3.0'
51
+ def install_command
52
+ cmd = "gem install #{self.name} --source=#{self.source}"
53
+ unless self.version.match(/^(\>\=|\>)/)
54
+ cmd << " --version=#{self.version}"
55
+ end
56
+ return cmd
57
+ end
58
+
59
+ # Returns <tt>true/false</tt> depending on whether the gem
60
+ # is installed or not.
61
+ def installed?
62
+ begin
63
+ gem(self.name, self.version)
64
+ return true
65
+ rescue Gem::LoadError => e
66
+ return true if e.message.match(/can't activate/)
67
+ return false
68
+ end
69
+ end
70
+
71
+ private
72
+ def self.build_method(name, defval = nil, key = name) # :nodoc:
73
+ define_method(name) do
74
+ return defval unless self.has_key?(key)
75
+ return self.fetch(key)
76
+ end
77
+ define_method("#{name.to_s.gsub('?', '')}=") do |x|
78
+ self.store(key, x)
79
+ end
80
+ end
81
+
82
+ build_method(:name)
83
+ build_method(:version, '>=0.0.0')
84
+ build_method(:source, 'http://gems.rubyforge.org')
85
+ build_method(:load?, true, :load)
86
+
87
+ end # Definition
88
+ end # Gemtronics
@@ -79,15 +79,14 @@ module Gemtronics
79
79
  ind = self.gems.size
80
80
  g = {}
81
81
  self.gems.each_with_index do |gemdef, i|
82
- if gemdef[:name] == name
82
+ if gemdef.name == name
83
83
  g = gemdef
84
84
  ind = i
85
85
  break
86
86
  end
87
87
  end
88
88
 
89
- g = self.group_options.merge({:name => name, :require => [name]}.merge(g).merge(options))
90
- g[:require] = [g[:require]].flatten
89
+ g = Gemtronics::Definition[self.group_options.merge({:name => name, :require => [name]}.merge(g).merge(options))]
91
90
  self.gems[ind] = g
92
91
  self.dependents.each do |dep|
93
92
  Gemtronics.group(dep).add(name, options)
@@ -112,7 +111,7 @@ module Gemtronics
112
111
  # now have the following gems: <tt>gem1, gem3, gem4</tt>
113
112
  def remove(name)
114
113
  self.gems.each do |g|
115
- if g[:name] = name.to_s
114
+ if g.name = name.to_s
116
115
  self.gems.delete(g)
117
116
  break
118
117
  end
@@ -139,7 +138,7 @@ module Gemtronics
139
138
  if group
140
139
  Gemtronics.group(name.to_sym).dependents << self.name
141
140
  group.gems.dup.each do |gemdef|
142
- self.add(gemdef[:name], gemdef)
141
+ self.add(gemdef.name, gemdef)
143
142
  end
144
143
  end
145
144
  end
@@ -100,9 +100,9 @@ module Gemtronics
100
100
  return if group.nil?
101
101
  options = {:verbose => false}.merge(options)
102
102
  group.gems.each do |g|
103
- if g[:load] == true
104
- gem(g[:name], g[:version])
105
- g[:require].each do |f|
103
+ if g.load? == true
104
+ gem(g.name, g.version)
105
+ g.require_list.each do |f|
106
106
  puts "require #{f}" if options[:verbose]
107
107
  require f
108
108
  end
@@ -138,14 +138,11 @@ module Gemtronics
138
138
  return if group.nil?
139
139
 
140
140
  group.gems.each do |g|
141
- unless gem_installed?(g[:name], g[:version])
142
- cmd = "gem install #{g[:name]} --source=#{g[:source]}"
143
- unless g[:version].match(/^(\>\=|\>)/)
144
- cmd << " --version=#{g[:version]}"
145
- end
141
+ unless gem_installed?(g)
142
+ cmd = g.install_command
146
143
  puts cmd
147
144
  system cmd
148
- self.installed_gems << "#{g[:name]}-#{g[:version]}"
145
+ self.installed_gems << g.to_s
149
146
  end
150
147
  end
151
148
  end
@@ -161,15 +158,9 @@ module Gemtronics
161
158
  end
162
159
 
163
160
  private
164
- def gem_installed?(name, version) # :nodoc:
165
- begin
166
- return true if self.installed_gems.include?("#{name}-#{version}")
167
- gem(name, version)
168
- return true
169
- rescue Gem::LoadError => e
170
- return true if e.message.match(/can't activate/)
171
- return false
172
- end
161
+ def gem_installed?(gemdef) # :nodoc:
162
+ return true if self.installed_gems.include?(gemdef.to_s)
163
+ return gemdef.installed?
173
164
  end
174
165
 
175
166
  end # Manager
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemtronics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-18 00:00:00 -04:00
12
+ date: 2009-08-19 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files:
23
23
  - README
24
24
  - LICENSE
25
25
  files:
26
+ - lib/gemtronics/definition.rb
26
27
  - lib/gemtronics/gemtronics.rb
27
28
  - lib/gemtronics/grouper.rb
28
29
  - lib/gemtronics/manager.rb