Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 154 lines (133 sloc) 4.632 kb
e9cd7b6 @winton Merge branch 'rails' of github.com:winton/gem_template
authored
1 unless defined?(ActsAsArchive::Gems)
efebcbe @winton Getting rid of bundler
authored
2
74e0e65 @winton Moving all gem-related modules under one module
authored
3 require 'yaml'
efebcbe @winton Getting rid of bundler
authored
4
9f2c954 @winton The great rewrite has begun (Rails 3 coming soon!)
authored
5 class ActsAsArchive
74e0e65 @winton Moving all gem-related modules under one module
authored
6 module Gems
efebcbe @winton Getting rid of bundler
authored
7 class <<self
8
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
9 attr_accessor :config
10 attr_reader :gemset, :gemsets, :versions
efebcbe @winton Getting rid of bundler
authored
11
43db36d @winton Drastically improved Gems module and specs
authored
12 class SimpleStruct
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
13 attr_reader :hash
74e0e65 @winton Moving all gem-related modules under one module
authored
14
15 def initialize(hash)
16 @hash = hash
17 @hash.each do |key, value|
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
18 self.class.send(:define_method, key) { @hash[key] }
19 self.class.send(:define_method, "#{key}=") { |v| @hash[key] = v }
74e0e65 @winton Moving all gem-related modules under one module
authored
20 end
21 end
22 end
23
43db36d @winton Drastically improved Gems module and specs
authored
24 Gems.config = SimpleStruct.new(
25 :gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
26 :gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
27 :warn => true
28 )
29
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
30 def activate(*gems)
31 begin
43db36d @winton Drastically improved Gems module and specs
authored
32 require 'rubygems' unless defined?(::Gem)
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
33 rescue LoadError
43db36d @winton Drastically improved Gems module and specs
authored
34 puts "rubygems library could not be required" if @config.warn
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
35 end
36
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
37 self.gemset ||= gemset_from_loaded_specs
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
38
39 gems.flatten.collect(&:to_sym).each do |name|
40 version = @versions[name]
74e369c @winton If gem vendored, add it to load paths instead of activating
authored
41 vendor = File.expand_path("../../../vendor/#{name}/lib", __FILE__)
42 if File.exists?(vendor)
43 $:.unshift vendor
44 elsif defined?(gem)
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
45 gem name.to_s, version
46 else
43db36d @winton Drastically improved Gems module and specs
authored
47 puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
eca33a4 @winton New system for specifying gem versions, gemspec info, activating gems
authored
48 end
efebcbe @winton Getting rid of bundler
authored
49 end
50 end
74e0e65 @winton Moving all gem-related modules under one module
authored
51
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
52 def dependencies
53 dependency_filter(@gemspec.dependencies, @gemset)
54 end
55
56 def development_dependencies
57 dependency_filter(@gemspec.development_dependencies, @gemset)
58 end
59
74e0e65 @winton Moving all gem-related modules under one module
authored
60 def gemset=(gemset)
61 if gemset
62 @gemset = gemset.to_sym
63
43db36d @winton Drastically improved Gems module and specs
authored
64 @gemsets = @config.gemsets.reverse.collect { |config|
74e0e65 @winton Moving all gem-related modules under one module
authored
65 if config.is_a?(::String)
66 YAML::load(File.read(config)) rescue {}
67 elsif config.is_a?(::Hash)
68 config
69 end
70 }.inject({}) do |hash, config|
71 deep_merge(hash, symbolize_keys(config))
72 end
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
73
74 @versions = (@gemsets[gemspec.name.to_sym] || {}).inject({}) do |hash, (key, value)|
da688ab @winton Allowing for null gemsets and null dependencies (avoid creating solo …
authored
75 if !value.is_a?(::Hash) && value
74e0e65 @winton Moving all gem-related modules under one module
authored
76 hash[key] = value
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
77 elsif key == @gemset
da688ab @winton Allowing for null gemsets and null dependencies (avoid creating solo …
authored
78 (value || {}).each { |k, v| hash[k] = v }
efebcbe @winton Getting rid of bundler
authored
79 end
74e0e65 @winton Moving all gem-related modules under one module
authored
80 hash
efebcbe @winton Getting rid of bundler
authored
81 end
74e0e65 @winton Moving all gem-related modules under one module
authored
82 else
83 @gemset = nil
84 @gemsets = nil
85 @versions = nil
efebcbe @winton Getting rid of bundler
authored
86 end
87 end
88
347be43 @winton Fixing rake gem
authored
89 def gemset_names
80e3df1 @winton Don't return duplicate gemset names
authored
90 (
da688ab @winton Allowing for null gemsets and null dependencies (avoid creating solo …
authored
91 [ :default ] +
80e3df1 @winton Don't return duplicate gemset names
authored
92 @gemsets[gemspec.name.to_sym].inject([]) { |array, (key, value)|
da688ab @winton Allowing for null gemsets and null dependencies (avoid creating solo …
authored
93 array.push(key) if value.is_a?(::Hash) || value.nil?
80e3df1 @winton Don't return duplicate gemset names
authored
94 array
95 }
96 ).uniq
347be43 @winton Fixing rake gem
authored
97 end
98
43db36d @winton Drastically improved Gems module and specs
authored
99 def gemspec(reload=false)
100 if @gemspec && !reload
101 @gemspec
102 else
103 data = YAML::load(File.read(@config.gemspec)) rescue {}
104 @gemspec = SimpleStruct.new(data)
105 end
74e0e65 @winton Moving all gem-related modules under one module
authored
106 end
107
108 private
109
110 def deep_merge(first, second)
111 merger = lambda do |key, v1, v2|
112 Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
113 end
114 first.merge(second, &merger)
115 end
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
116
117 def dependency_filter(dependencies, match)
da688ab @winton Allowing for null gemsets and null dependencies (avoid creating solo …
authored
118 (dependencies || []).inject([]) { |array, value|
b3f7765 @winton Adding ability to build a gem for every gemset (dependencies change b…
authored
119 if value.is_a?(::Hash)
120 array += value[match.to_s] if value[match.to_s]
121 else
122 array << value
123 end
124 array
125 }.uniq.collect(&:to_sym)
126 end
127
128 def gemset_from_loaded_specs
129 if defined?(Gem)
130 Gem.loaded_specs.each do |name, spec|
131 if name == gemspec.name
132 return :default
133 elsif name[0..gemspec.name.length] == "#{gemspec.name}-"
134 return name[gemspec.name.length+1..-1].to_sym
135 end
136 end
137 :default
138 else
139 :none
140 end
141 end
74e0e65 @winton Moving all gem-related modules under one module
authored
142
143 def symbolize_keys(hash)
144 return {} unless hash.is_a?(::Hash)
145 hash.inject({}) do |options, (key, value)|
146 value = symbolize_keys(value) if value.is_a?(::Hash)
147 options[(key.to_sym rescue key) || key] = value
148 options
efebcbe @winton Getting rid of bundler
authored
149 end
150 end
151 end
152 end
153 end
154 end
Something went wrong with that request. Please try again.