3 # Copyright 2004 Austin Ziegler <ruby-install@halostatue.ca>
4 # Install utility. Based on the original installation script for rdoc by the
5 # Pragmatic Programmers.
7 # This program is free software. It may be redistributed and/or modified under
8 # the terms of the GPL version 2 (or later) or the Ruby licence.
12 # In most cases, if you have a typical project layout, you will need to do
13 # absolutely nothing to make this work for you. This layout is:
15 # bin/ # executable files -- "commands"
16 # lib/ # the source of the library
18 # The default behaviour:
19 # 1) Build Rdoc documentation from all files in bin/ (excluding .bat and .cmd),
20 # all .rb files in lib/, ./README, ./ChangeLog, and ./Install.
21 # and all .rb files in lib/.
22 # 2) Install configuration files in etc/.
23 # 3) Install commands from bin/ into the Ruby bin directory.
24 # 4) Install system commands from bin/ into the Ruby sbin directory.
25 # 5) Install all library files from lib/ into Ruby's site_lib/version
27 # 6) Install all plugins from plugins/ into the plugins directory
28 # (usually $libexecdir/mcollective).
44 puts "Missing rdoc; skipping documentation"
48 if (defined?(RbConfig) ? RbConfig : Config)::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i
49 $stderr.puts "install.rb does not support Microsoft Windows. See ext/windows/README.md for information on installing on Microsoft Windows."
55 InstallOptions = OpenStruct.new
58 g = list.map { |i| Dir.glob(i) }
70 puts "Could not load #{pre} Ruby library; cannot install"
76 def do_configs(configs, target, strip = 'etc/')
77 Dir.mkdir(target) unless File.directory? target
79 ocf = File.join(target, cf.gsub(Regexp.new(strip), ''))
80 oc = File.dirname(ocf)
81 makedirs(oc, {:mode => 0755, :verbose => true})
82 install(cf, ocf, {:mode => 0644, :preserve => true, :verbose => true})
86 def do_bins(bins, target, strip = 's?bin/')
87 Dir.mkdir(target) unless File.directory? target
89 obf = bf.gsub(/#{strip}/, '')
90 install_binfile(bf, obf, target)
94 def do_libs(libs, target, strip = 'lib/')
96 olf = File.join(target, lf.sub(/^#{strip}/, ''))
97 op = File.dirname(olf)
98 if File.directory?(lf)
99 makedirs(olf, {:mode => 0755, :verbose => true})
101 makedirs(op, {:mode => 0755, :verbose => true})
102 install(lf, olf, {:mode => 0644, :preserve => true, :verbose => true})
108 # Prepare the file installation.
110 def prepare_installation
111 InstallOptions.configs = true
113 # Only try to do docs if we're sure they have rdoc
115 InstallOptions.rdoc = true
117 InstallOptions.rdoc = false
121 ARGV.options do |opts|
122 opts.banner = "Usage: #{File.basename($0)} [options]"
124 opts.on('--[no-]rdoc', 'Creation of RDoc output.', 'Default is create rdoc.') do |onrdoc|
125 InstallOptions.rdoc = onrdoc
127 opts.on('--[no-]configs', 'Installation of config files', 'Default is install configs.') do |onconfigs|
128 InstallOptions.configs = onconfigs
130 opts.on('--destdir[=OPTIONAL]', 'Installation prefix for all targets', 'Default essentially /') do |destdir|
131 InstallOptions.destdir = destdir
133 opts.on('--configdir[=OPTIONAL]', 'Installation directory for config files', 'Default /etc/mcollective') do |configdir|
134 InstallOptions.configdir = configdir
136 opts.on('--bindir[=OPTIONAL]', 'Installation directory for binaries', 'overrides RbConfig::CONFIG["bindir"]') do |bindir|
137 InstallOptions.bindir = bindir
139 opts.on('--sbindir[=OPTIONAL]', 'Installation directory for system binaries', 'overrides RbConfig::CONFIG["sbindir"]') do |sbindir|
140 InstallOptions.sbindir = sbindir
142 opts.on('--ruby[=OPTIONAL]', 'Ruby interpreter to use with installation', 'overrides ruby used to call install.rb') do |ruby|
143 InstallOptions.ruby = ruby
145 opts.on('--sitelibdir[=OPTIONAL]', 'Installation directory for libraries', 'overrides RbConfig::CONFIG["sitelibdir"]') do |sitelibdir|
146 InstallOptions.sitelibdir = sitelibdir
148 opts.on('--plugindir[=OPTIONAL]', 'Installation directory for plugins', 'Default /usr/libexec/mcollective') do |plugindir|
149 InstallOptions.plugindir = plugindir
151 opts.on('--quick', 'Performs a quick installation. Only the', 'installation is done.') do |quick|
152 InstallOptions.rdoc = false
153 InstallOptions.ri = false
154 InstallOptions.configs = true
156 opts.on('--full', 'Performs a full installation. All', 'optional installation steps are run.') do |full|
157 InstallOptions.rdoc = true
158 InstallOptions.ri = true
159 InstallOptions.configs = true
162 opts.on_tail('--help', "Shows this help text.") do
170 version = [RbConfig::CONFIG["MAJOR"], RbConfig::CONFIG["MINOR"]].join(".")
171 libdir = File.join(RbConfig::CONFIG["libdir"], "ruby", version)
173 # Mac OS X 10.5 and higher declare bindir
174 # /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin
175 # which is not generally where people expect executables to be installed
176 # These settings are appropriate defaults for all OS X versions.
177 if RUBY_PLATFORM =~ /^universal-darwin[\d\.]+$/
178 RbConfig::CONFIG['bindir'] = "/usr/bin"
179 RbConfig::CONFIG['sbindir'] = "/usr/sbin"
182 if InstallOptions.configdir
183 configdir = InstallOptions.configdir
185 configdir = "/etc/mcollective"
188 if InstallOptions.bindir
189 bindir = InstallOptions.bindir
191 bindir = RbConfig::CONFIG['bindir']
194 if InstallOptions.sbindir
195 sbindir = InstallOptions.sbindir
197 sbindir = RbConfig::CONFIG['sbindir']
200 if InstallOptions.sitelibdir
201 sitelibdir = InstallOptions.sitelibdir
203 sitelibdir = RbConfig::CONFIG["sitelibdir"]
205 sitelibdir = $LOAD_PATH.find { |x| x =~ /site_ruby/ }
207 sitelibdir = File.join(libdir, "site_ruby")
208 elsif sitelibdir !~ Regexp.quote(version)
209 sitelibdir = File.join(sitelibdir, version)
214 if InstallOptions.plugindir
215 plugindir = InstallOptions.plugindir
217 plugindir = "/usr/libexec/mcollective"
220 if InstallOptions.destdir
221 destdir = InstallOptions.destdir
226 configdir = File.join(destdir, configdir)
227 bindir = File.join(destdir, bindir)
228 sbindir = File.join(destdir, sbindir)
229 sitelibdir = File.join(destdir, sitelibdir)
230 plugindir = File.join(destdir, plugindir)
232 makedirs(configdir) if InstallOptions.configs
238 InstallOptions.sitelibdir = sitelibdir
239 InstallOptions.configdir = configdir
240 InstallOptions.bindir = bindir
241 InstallOptions.sbindir = sbindir
242 InstallOptions.plugindir = plugindir
246 # Build the rdoc documentation.
248 def build_rdoc(files)
249 return unless $haverdoc
252 r.document(["--main", "MCollective", "--line-numbers"] + files)
253 rescue RDoc::RDocError => e
254 $stderr.puts e.message
255 rescue Exception => e
256 $stderr.puts "Couldn't build RDoc documentation\n#{e.message}"
261 # Install file(s) from ./bin to RbConfig::CONFIG['bindir']. Patch it on the way
262 # to insert a #! line; on a Unix install, the command is named as expected
263 def install_binfile(from, op_file, target)
264 tmp_file = Tempfile.new('mcollective-binfile')
266 if InstallOptions.ruby
267 ruby = InstallOptions.ruby
269 ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
272 File.open(from) do |ip|
273 File.open(tmp_file.path, "w") do |op|
275 contents = ip.readlines
276 contents.shift if contents[0] =~ /^#!/
277 op.write contents.join
281 install(tmp_file.path, File.join(target, op_file), :mode => 0755, :preserve => true, :verbose => true)
285 # Change directory into the mcollective root so we don't get the wrong files for install.
286 cd File.dirname(__FILE__) do
287 # Set these values to what you want installed.
288 configs = glob(%w{etc/*.dist})
289 erbs = glob(%w{etc/*.erb})
290 bins = glob(%w{bin/mco})
291 sbins = glob(%w{bin/mcollectived bin/mc-call-agent})
292 rdoc = glob(%w{bin/* lib/**/*.rb README* })
293 libs = glob(%w{lib/**/*})
294 plugins = glob(%w{plugins/**/*})
299 build_rdoc(rdoc) if InstallOptions.rdoc
300 do_configs(configs, InstallOptions.configdir, 'etc/|\.dist') if InstallOptions.configs
301 do_configs(erbs, InstallOptions.configdir) if InstallOptions.configs
302 do_bins(bins, InstallOptions.bindir)
303 do_bins(sbins, InstallOptions.sbindir)
304 do_libs(libs, InstallOptions.sitelibdir)
305 do_libs(plugins, InstallOptions.plugindir, 'plugins/')