forked from gaizka/activerecord-odbc-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_odbc.rb
200 lines (183 loc) · 6.2 KB
/
install_odbc.rb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# $Id$
#
# OpenLink ODBC Adapter for Ruby on Rails
# Copyright (C) 2006 OpenLink Software
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# Installation script for Rails ODBC Adapter
#
# * Locates an ActiveRecord installation.
# - Looks for the latest version of any installed ActiveRecord gems.
# (- Mac only: looking first under the Locomotive folder if installed)
# - If no ActiveRecord gems are found, looks for a manually installed
# version of ActiveRecord in site_ruby.
# * Copies the essential ODBC adapter files into the ActiveRecord tree.
# * Checks if Ruby ODBC Bridge is installed.
require 'rbconfig'
require 'find'
require 'ftools'
require 'fileutils'
include Config
puts "\n<< Installation script for Rails ODBC Adapter >>"
# Locate an ActiveRecord installation
if CONFIG['target_vendor'] =~ /apple/i
puts "\nChecking if Locomotive is installed"
lo = Dir.entries("/Applications").find_all { |l| l =~ /^locomotive/i }
if !lo.empty?
# Sort to ensure we use the latest Locomotive version
$locomotiveDir = File.join("/Applications", lo.sort![-1])
Find.find($locomotiveDir) { |p|
$gemDir = p if File.basename(p) =~ /^gems$/i
}
if $gemDir
ar = Dir.entries($gemDir).find_all { |g| g =~ /^activerecord/i }
if !ar.empty?
# Sort to ensure we install the ODBC adapter into the latest ActiveRecord
# tree if multiple gem versions are installed.
$activeRecDir = ar.sort![-1]
$activeRecDir = File.join($gemDir, $activeRecDir, "lib",
"active_record") unless $activeRecDir.nil?
end
end
end
end
if !$activeRecDir
puts "\nChecking if RubyGems is installed."
begin
require 'rubygems'
$rubyGems = true
rescue LoadError
end
if $rubyGems
puts "Looking for installed ActiveRecord gems."
ar = Dir.entries(File.join(Gem::dir,"gems")).find_all { |g|
g =~ /^activerecord-\d+\.\d+\.\d+$/i
}
if !ar.empty?
# Sort to ensure we install the ODBC adapter into the latest ActiveRecord
# tree if multiple gem versions are installed.
ar.sort!
$activeRecDir = ar[-1]
$activeRecDir = File.join(Gem::dir, "gems", $activeRecDir, "lib",
"active_record") unless $activeRecDir.nil?
end
end
end
if !$activeRecDir
$sitedir = CONFIG["sitelibdir"]
unless $sitedir
version = CONFIG["MAJOR"] + "." + CONFIG["MINOR"]
$libdir = File.join(CONFIG["libdir"], "ruby", version)
$sitedir = $:.find { |x| x =~ /site_ruby/ }
if !$sitedir
$sitedir = File.join($libdir, "site_ruby")
elsif $sitedir !~ Regexp.quote(version)
$sitedir = File.join($sitedir, version)
end
end
puts "An ActiveRecord gem could not be found."
# ActiveRecord can be manually installed.
# See install.rb in ActiveRecord distribution directory for details
puts "\nLooking for ActiveRecord under #{$sitedir}"
ar = File.join($sitedir,"active_record")
$activeRecDir = ar if File.directory?(ar)
end
if $activeRecDir.nil?
puts "\nAn ActiveRecord installation could not be found!"
end
while true
while $activeRecDir.nil?
puts <<MSG1
Enter target directory or q to quit...
Please specify the location of your ActiveRecord tree by specifying the path
of the directory containing the connection_adapters and vendor directories.
(Windows users - Use / as the path separator, not \\)
MSG1
i = gets.chomp.strip
exit 1 if i =~/^q$/i
if File.directory?(i) &&
File.directory?(File.join(i, "connection_adapters"))
$activeRecDir = i
break
else
puts "ERROR>> [#{i}] is not a valid directory"
end
end
puts "\nTarget ActiveRecord directory for install:\n[#{$activeRecDir}]"
while true
puts "Enter c to change target ActiveRecord directory, q to quit, " +
"i to install"
i = gets.chomp.strip
break if i =~ /^[ciq]/i
end
break if i =~ /^i/i
exit 2 if i =~ /^q/i
$activeRecDir=nil
end
puts "\nCopying ODBC Adapter files into the ActiveRecord tree."
puts "-"*60
Dir.chdir("lib/active_record")
Find.find(".") { |f|
if f[-3..-1] == ".rb"
dest = File.join($activeRecDir, *f.split(/\//))
# File::install fails if file already exists
FileUtils.rm(dest, {:force => true})
rc = File::install(f, dest, 0644, true)
if rc != 1
puts "ERROR>> File::install(#{f}, #{dest}, ...) failed"
exit 3
end
puts
end
}
puts "-"*60
puts
# Check Ruby ODBC Bridge is installed
puts "\nChecking for Ruby ODBC Bridge."
$odbcBridge = CONFIG['target_vendor'] =~ /apple/i ? "odbc.bundle" : "odbc.so"
if $locomotiveDir
Find.find($locomotiveDir) { |p|
$odbcBridgeDir = File.dirname(p) if File.basename(p) =~ /^odbc.bundle/i
}
if $odbcBridgeDir.nil?
puts <<MSG2
\nWARNING>> odbc.bundle* is not present under:\n\t#{$locomotiveDir}
\nThe Ruby ODBC Bridge must be installed before the Rails ODBC Adapter can be
used.
MSG2
else
puts "\nodbc.bundle found in #{$odbcBridgeDir}"
end
else
$sitearchdir = CONFIG['sitearchdir']
if File.exist?(File.join($sitearchdir, $odbcBridge))
puts "#{$odbcBridge} found in #{$sitearchdir}"
else
puts <<MSG3
\nWARNING>> #{$odbcBridge} is not present in:\n\t#{$sitearchdir}
\nThe Ruby ODBC Bridge must be installed before the Rails ODBC Adapter can be
used.
MSG3
end
end
puts "\nDone."