Skip to content

Commit 48a5ba0

Browse files
committed
[Rails3] Gem and rake changes to support RVM.
1 parent c43985d commit 48a5ba0

File tree

3 files changed

+191
-5
lines changed

3 files changed

+191
-5
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
source :rubygems
23

34
gemspec :path => ENV['RAILS_SOURCE']
@@ -12,3 +13,4 @@ group :development do
1213
gem "ruby-debug", ">= 0.10.3"
1314
end
1415
end
16+

Rakefile

Lines changed: 188 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ require 'rake/testtask'
33
require 'rake/rdoctask'
44

55

6+
desc 'Default runs tests for the adapters ODBC mode.'
7+
task :test do
8+
test = Rake::Task['sqlserver:test:odbc']
9+
test.invoke
10+
end
11+
12+
613
namespace :sqlserver do
714

815
namespace :test do
@@ -34,9 +41,186 @@ namespace :sqlserver do
3441
end
3542

3643

37-
desc 'Default runs tests for the adapters ODBC mode.'
38-
task :test do
39-
test = Rake::Task['sqlserver:test:odbc']
40-
test.invoke
44+
namespace :rvm do
45+
46+
RUBIES = {
47+
'ruby-1.8.6' => {:alias => 'sqlsvr186', :odbc => '0.99991'},
48+
'ruby-1.8.7' => {:alias => 'sqlsvr187', :odbc => '0.99991'},
49+
'ruby-1.9.1' => {:alias => 'sqlsvr191', :odbc => '0.99991'},
50+
'ruby-1.9.2-rc2' => {:alias => 'sqlsvr192', :odbc => '0.99992pre3'},
51+
'ree-1.8.7' => {:alias => 'sqlsvrree', :odbc => '0.99991'}
52+
}
53+
54+
task :setup do
55+
unless @rvm_setup
56+
rvm_lib_path = "#{`echo $rvm_path`.strip}/lib"
57+
$LOAD_PATH.unshift(rvm_lib_path) unless $LOAD_PATH.include?(rvm_lib_path)
58+
require 'rvm'
59+
require 'tmpdir'
60+
@rvm_setup = true
61+
end
62+
end
63+
64+
task :debug => :setup do
65+
66+
end
67+
68+
task :test => :setup do
69+
rubies = RUBIES.map { |rubie, info| info[:alias] }
70+
puts "Run this:\nrvm #{rubies.join(',')} rake test"
71+
end
72+
73+
task :wipe => :setup do
74+
rvm_rubies.each { |rubie| RVM.remove(rubie,:gems=>true) }
75+
RVM.cleanup_all
76+
end
77+
78+
namespace :install do
79+
80+
desc "Install the following rubie versions if not already: #{RUBIES.keys.inspect}"
81+
task :rubies => :setup do
82+
installed_rubies = RVM.list_strings
83+
RUBIES.keys.each do |rubie|
84+
if installed_rubies.any? { |ir| ir =~ /#{rubie}/ }
85+
puts "info: Rubie #{rubie} already installed."
86+
else
87+
with_my_environment_vars do
88+
good_msg = "info: Rubie #{rubie} installed."
89+
bad_msg = "Failed #{rubie} install! Check RVM logs here: #{RVM.path}/log/#{rubie}"
90+
puts "info: Rubie #{rubie} installation inprogress. This could take awhile..."
91+
RVM.install(rubie,rvm_install_options) ? puts(good_msg) : abort(bad_msg)
92+
end
93+
end
94+
end
95+
rvm_each_rubie do
96+
RVM.gemset_create rvm_gemset_name
97+
RVM.alias_create rvm_current_rubie_info[:alias], rvm_current_name
98+
end
99+
end
100+
101+
desc "Install ruby-odbc for each rubie version."
102+
task :odbc => :setup do
103+
rvm_each_rubie do
104+
odbc = "ruby-odbc-#{rvm_current_rubie_info[:odbc]}"
105+
RVM.chdir(Dir.tmpdir) do
106+
RVM.run "rm -rf #{odbc}*"
107+
puts "info: RubyODBC downloading #{odbc}..."
108+
RVM.run "curl -O http://www.ch-werner.de/rubyodbc/#{odbc}.tar.gz"
109+
puts "info: RubyODBC extracting clean work directory..."
110+
RVM.run "tar -xf #{odbc}.tar.gz"
111+
RVM.chdir("#{odbc}/ext") do
112+
puts "info: RubyODBC configuring..."
113+
RVM.ruby 'extconf.rb', "--with-odbc-dir=#{rvm_odbc_dir}"
114+
puts "info: RubyODBC make and installing for #{rvm_current_name}..."
115+
RVM.run "make && make install"
116+
end
117+
end
118+
end
119+
end
120+
121+
desc "Install development gems using bundler to each rubie version, installing bundler if not already."
122+
task :bundle => :setup do
123+
rvm_each_rubie do
124+
rvm_install_gem 'bundler', '1.0.0.rc.3'
125+
RVM.run 'bundle install'
126+
end
127+
end
128+
129+
end
130+
41131
end
42132

133+
134+
135+
# RVM Helper Methods
136+
137+
def rvm_each_rubie
138+
rvm_rubies.each do |rubie|
139+
RVM.use(rubie)
140+
yield
141+
end
142+
ensure
143+
RVM.reset_current!
144+
end
145+
146+
def rvm_rubies
147+
RUBIES.keys.map{ |rubie| "#{rubie}@#{rvm_gemset_name}" }
148+
end
149+
150+
def rvm_current_rubie_info
151+
key = rvm_current_rubie_name
152+
while !key.empty?
153+
info = RUBIES[key]
154+
return info if info
155+
new_key = key.split('-') ; new_key.pop
156+
key = new_key.join('-')
157+
end
158+
end
159+
160+
def rvm_current_rubie_name
161+
rvm_current_name.sub("@#{rvm_gemset_name}",'')
162+
end
163+
164+
def rvm_current_name
165+
RVM.current.expanded_name
166+
end
167+
168+
def rvm_gemset_name
169+
'sqlserver'
170+
end
171+
172+
def rvm_with_macports?
173+
`uname`.strip == 'Darwin' && !`which port`.empty?
174+
end
175+
176+
def rvm_install_options
177+
{}
178+
end
179+
180+
def rvm_odbc_dir
181+
rvm_with_macports? ? '/opt/local' : '/usr/local'
182+
end
183+
184+
def rvm_gem_available?(*specs)
185+
available_args = specs.map{ |s| "'#{s}'" }.join(',')
186+
RVM.ruby_eval("require 'rubygems' ; print Gem.available?(#{available_args})").stdout == 'true'
187+
end
188+
189+
def rvm_install_gem(*specs)
190+
gem, version = specs
191+
spec_info = specs.join(', ')
192+
if rvm_gem_available?(*specs)
193+
puts "info: Gem #{spec_info} already installed in #{rvm_current_name}."
194+
else
195+
puts "info: Installing gem #{spec_info} in #{rvm_current_name}..."
196+
install_args = [:gem, 'install', gem]
197+
install_args += ['-v', version] if version && !version.empty?
198+
puts RVM.perform_set_operation(*install_args).stdout
199+
end
200+
end
201+
202+
def with_my_environment_vars
203+
my_vars = my_environment_vars
204+
current_vars = my_vars.inject({}) { |cvars,kv| k,v = kv ; cvars[k] = ENV[k] ; cvars }
205+
set_environment_vars(my_vars)
206+
yield
207+
ensure
208+
set_environment_vars(current_vars)
209+
end
210+
211+
def my_environment_vars
212+
if rvm_with_macports?
213+
{ 'CC' => '/usr/bin/gcc-4.2',
214+
'CFLAGS' => '-O2 -arch x86_64',
215+
'LDFLAGS' => '-L/opt/local/lib -arch x86_64',
216+
'CPPFLAGS' => '-I/opt/local/include' }
217+
else
218+
{}
219+
end
220+
end
221+
222+
def set_environment_vars(vars)
223+
vars.each { |k,v| ENV[k] = v }
224+
end
225+
226+

activerecord-sqlserver-adapter.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ Gem::Specification.new do |s|
1616
s.extra_rdoc_files = ['README.rdoc']
1717
s.rdoc_options.concat ['--main', 'README.rdoc']
1818

19-
s.add_dependency('activerecord', '~> 3.0.0')
19+
s.add_dependency('activerecord', '~> 3.0')
2020
end
2121

0 commit comments

Comments
 (0)