to_pass 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ module ToPass
26
26
 
27
27
  YAML.load_file(fn.expand_path)
28
28
  rescue LoadError
29
- raise LoadError, "algorithm #{@file} could not be found in #{load_path}" if fn.nil?
29
+ raise LoadError, "algorithm #{@file} could not be found in #{load_path.join(', ')}" if fn.nil?
30
30
  end
31
31
  end
32
32
  end
data/lib/to_pass/base.rb CHANGED
@@ -13,7 +13,8 @@ module ToPass
13
13
  attr_reader :password
14
14
 
15
15
  # transform a string according to a certain algorithm
16
- def initialize(string, algorithm)
16
+ def initialize(string, algorithm, options = {})
17
+ Directories[:custom] = options[:path] if options[:path]
17
18
  rules = AlgorithmReader.load(algorithm)
18
19
  converter = Converter.new(rules)
19
20
  @password = converter.convert(string)
data/lib/to_pass/cli.rb CHANGED
@@ -65,6 +65,31 @@ module ToPass
65
65
  puts " ============================================"
66
66
  puts ""
67
67
  end
68
+
69
+ # setup working directory
70
+ def setup(value = nil)
71
+ dir = Pathname.new(value || Directories[:user]).expand_path
72
+
73
+ dir.mkpath
74
+ (dir + 'algorithms').mkpath
75
+ (dir + 'converters').mkpath
76
+
77
+ if File.exist?(dir + 'config')
78
+ puts "configuration in #{dir} not overwritten"
79
+ else
80
+ File.open((dir + 'config'), 'w') do |file|
81
+ config = {}
82
+
83
+ ConfigReader.load.each do |key, value|
84
+ config[key.to_s] = ( value.is_a?(Symbol) ? value.to_s : value )
85
+ end
86
+
87
+ YAML.dump(config, file)
88
+ end
89
+ end
90
+
91
+ puts "successfully created configuration directory in #{dir}"
92
+ end
68
93
  end
69
94
 
70
95
  protected
@@ -91,23 +116,23 @@ module ToPass
91
116
  options[:pipe_out] = value
92
117
  end
93
118
 
119
+ opts.on('-c', '--config [PATH]', "look in PATH for configurations (instead of ~/.to_pass)") do |value|
120
+ dir = Pathname.new(value).expand_path
121
+ if dir.exist?
122
+ options[:path] = dir
123
+ else
124
+ puts 'configuration path not found'
125
+ puts "run '#{File.basename($0)} --setup #{dir}' to set it up"
126
+ exit 1
127
+ end
128
+ end
129
+
94
130
  ## ACTIONS
95
131
 
96
- # not used at the moment
97
- #
98
- # opts.on('-c', '--config [PATH]', "look in PATH for configurations (instead of ~/.to_pass)") do |value|
99
- # if File.exist?(value)
100
- # options[:path] = value
101
- # else
102
- # puts 'configuration path not found'
103
- # puts "run '#{File.basename($0)} --setup --config #{value}' to set it up"
104
- # exit 1
105
- # end
106
- # end
107
- #
108
- # opts.on('--setup', "create a configuration directory") do |value|
109
- # options[:setup] == true
110
- # end
132
+ opts.on('--setup [PATH]', "create a configuration directory") do |value|
133
+ Cli.setup(value)
134
+ exit
135
+ end
111
136
 
112
137
  opts.on('-A', '--algorithms', "list available algorithms") do |value|
113
138
  Cli.algorithms
@@ -150,7 +175,7 @@ module ToPass
150
175
 
151
176
  # perform "heavy" work
152
177
  def transform
153
- Base.new( @string, @options[:algorithm] ).to_s
178
+ Base.new(@string, @options[:algorithm], :path => @options[:path]).to_s
154
179
  end
155
180
  end
156
181
  end
@@ -12,18 +12,44 @@ module ToPass
12
12
  # get a directory or a list of directories
13
13
  def [](key)
14
14
  case key
15
- when :user, :data, :base, :source_data
15
+ when *all.keys - [:standard, :all]
16
16
  all[key]
17
17
  when :standard
18
18
  [ all[:user], all[:data], all[:source_data] ]
19
+ when :all
20
+ user_paths + [ all[:user], all[:data], all[:source_data] ]
19
21
  end
20
22
  end
21
23
 
24
+ # a new path can be added to have a broader or specialised lookup
25
+ def []=(key, value)
26
+ add_user_key(key)
27
+
28
+ all[key] = value
29
+ end
30
+
31
+ # checks wether the path is already known by value or key
32
+ def include?(search)
33
+ all.has_value?(search) || all.has_key?(search)
34
+ end
35
+
22
36
  private
23
37
 
38
+ # adds a new key to the list to distinguish user added keys from built in ones
39
+ def add_user_key(key)
40
+ (@user_keys ||= []) << key unless all.has_key?(key)
41
+ end
42
+
43
+ # return the array of user-supplied paths
44
+ def user_paths
45
+ @user_keys.to_a.map do |key|
46
+ all[key]
47
+ end
48
+ end
49
+
24
50
  # list of all directories used by this project
25
51
  def all
26
- {
52
+ @all ||= {
27
53
  :user => "~/.#{APP_NAME}",
28
54
  :data => "#{ruby_data_dir}/#{APP_NAME}",
29
55
  :base => File.expand_path("#{File.dirname(__FILE__)}/../.."),
@@ -31,6 +57,9 @@ module ToPass
31
57
  }
32
58
  end
33
59
 
60
+ # retrieve the ruby data-dir from different version of ruby.
61
+ #
62
+ # The latter version is present in 1.8.7 p249/302
34
63
  def ruby_data_dir
35
64
  RbConfig::CONFIG['data-dir'] || RbConfig::CONFIG['datadir']
36
65
  end
@@ -69,8 +69,8 @@ module ToPass
69
69
  suffix = suffix.to_s
70
70
  suffix = "/#{suffix}" unless suffix =~ /^\//
71
71
 
72
- ToPass::Directories[:standard].map do |dir|
73
- dir + suffix
72
+ ToPass::Directories[:all].map do |dir|
73
+ dir.to_s + suffix
74
74
  end.map do |dir|
75
75
  dir = Pathname.new(dir).expand_path
76
76
  dir if dir.exist?
@@ -3,7 +3,7 @@
3
3
  unless defined?(ToPass::VERSION)
4
4
  module ToPass
5
5
  # version of gem
6
- VERSION = '0.9.0'
6
+ VERSION = '1.0.0'
7
7
 
8
8
  # name of gem
9
9
  APP_NAME = 'to_pass'
data/setup.rb ADDED
@@ -0,0 +1,1368 @@
1
+ #!/usr/bin/env ruby
2
+ # Setup.rb v5.1.0
3
+ #
4
+ # This is a stand-alone bundle of the setup.rb application.
5
+ # You can place it in your projects script/ directory, or
6
+ # call it 'setup.rb' and place it in your project's
7
+ # root directory (just like old times).
8
+ #
9
+ # NOTE: As of version 5.1.0 this bundled rendition is also
10
+ # being used for the bin/setup.rb exe. Rather than the previous:
11
+ #
12
+ # require 'setup/command'
13
+ # Setup::Command.run
14
+ #
15
+ # By doing so, +rvm+ should be able to use it across all rubies
16
+ # without issue and without needing to install it for each.
17
+ module Setup
18
+ VERSION = '5.1.0'
19
+ end
20
+ class << File #:nodoc: all
21
+ unless respond_to?(:read) # Ruby 1.6 and less
22
+ def read(fname)
23
+ open(fname){ |f| return f.read }
24
+ end
25
+ end
26
+ def dir?(path)
27
+ directory?((path[-1,1] == '/') ? path : path + '/')
28
+ end
29
+ end
30
+ unless Errno.const_defined?(:ENOTEMPTY) # Windows?
31
+ module Errno #:nodoc:
32
+ class ENOTEMPTY #:nodoc:
33
+ end
34
+ end
35
+ end
36
+ module Setup
37
+ META_EXTENSION_DIR = '.setup'
38
+ FILETYPES = %w( bin lib ext data etc man doc )
39
+ INSTALL_RECORD = 'SetupReceipt'
40
+ CONFIG_FILE = 'SetupConfig'
41
+ end
42
+ module Setup
43
+ class Project
44
+ ROOT_MARKER = '{setup.rb,script/setup,meta/,MANIFEST,lib/}'
45
+ def rootdir
46
+ @rootdir ||= (
47
+ root = Dir[File.join(Dir.pwd, ROOT_MARKER)].first
48
+ if !root
49
+ raise Error, "not a project directory"
50
+ else
51
+ Dir.pwd
52
+ end
53
+ )
54
+ end
55
+ def name
56
+ @name = (
57
+ if file = Dir["{script/setup,meta,.meta}/name"].first
58
+ File.read(file).strip
59
+ else
60
+ nil
61
+ end
62
+ )
63
+ end
64
+ def loadpath
65
+ @loadpath ||= (
66
+ if file = Dir.glob('{script/setup,meta,.meta}/loadpath').first
67
+ raw = File.read(file).strip.chomp(']')
68
+ raw.split(/[\n,]/).map do |e|
69
+ e.strip.sub(/^[\[-]\s*/,'')
70
+ end
71
+ else
72
+ nil
73
+ end
74
+ )
75
+ end
76
+ def extconfs
77
+ @extconfs ||= Dir['ext/**/extconf.rb']
78
+ end
79
+ def extensions
80
+ @extensions ||= extconfs.collect{ |f| File.dirname(f) }
81
+ end
82
+ def compiles?
83
+ !extensions.empty?
84
+ end
85
+ end
86
+ end
87
+ module Setup
88
+ class Session
89
+ attr :options
90
+ def initialize(options={})
91
+ @options = options
92
+ self.io ||= StringIO.new # log instead ?
93
+ end
94
+ def io
95
+ @options[:io]
96
+ end
97
+ def io=(anyio)
98
+ @options[:io] = anyio
99
+ end
100
+ def trace?; @options[:trace]; end
101
+ def trace=(val)
102
+ @options[:trace] = val
103
+ end
104
+ def trial?; @options[:trial]; end
105
+ def trial=(val)
106
+ @options[:trial] = val
107
+ end
108
+ def quiet?; @options[:quiet]; end
109
+ def quiet=(val)
110
+ @options[:quiet] = val
111
+ end
112
+ def force?; @options[:force]; end
113
+ def force=(val)
114
+ @options[:force] = val
115
+ end
116
+ def compile?
117
+ configuration.compile? && project.compiles?
118
+ end
119
+ def all
120
+ config
121
+ compile
122
+ if configuration.test?
123
+ ok = test
124
+ exit 1 unless ok
125
+ end
126
+ install
127
+ if configuration.ri?
128
+ document
129
+ end
130
+ end
131
+ def preconfig
132
+ log_header('Preconfig')
133
+ if configuration.save_config
134
+ io.print "#{CONFIG_FILE} was saved. " unless quiet?
135
+ else
136
+ io.print "#{CONFIG_FILE} is current. " unless quiet?
137
+ end
138
+ io.puts "Edit to customize configuration." unless quiet?
139
+ puts configuration if trace? && !quiet?
140
+ end
141
+ def config
142
+ if compile?
143
+ log_header('Config')
144
+ compiler.configure
145
+ end
146
+ end
147
+ def compile
148
+ if compile?
149
+ log_header('Compile')
150
+ compiler.compile
151
+ end
152
+ end
153
+ alias_method :make, :compile
154
+ alias_method :setup, :make
155
+ def install
156
+ log_header('Install')
157
+ installer.install
158
+ end
159
+ def test
160
+ return true unless tester.testable?
161
+ log_header('Test')
162
+ tester.test
163
+ end
164
+ def document
165
+ log_header('Document')
166
+ documentor.document
167
+ end
168
+ def clean
169
+ log_header('Clean')
170
+ compiler.clean
171
+ end
172
+ def distclean
173
+ log_header('Distclean')
174
+ compiler.distclean
175
+ end
176
+ def uninstall
177
+ if !File.exist?(INSTALL_RECORD)
178
+ io.puts "Nothing is installed."
179
+ return
180
+ end
181
+ log_header('Uninstall')
182
+ uninstaller.uninstall
183
+ io.puts('Ok.')
184
+ end
185
+ def show
186
+ puts configuration
187
+ end
188
+ def project
189
+ @project ||= Project.new
190
+ end
191
+ def configuration
192
+ @configuration ||= Configuration.new
193
+ end
194
+ def compiler
195
+ @compiler ||= Compiler.new(project, configuration, options)
196
+ end
197
+ def installer
198
+ @installer ||= Installer.new(project, configuration, options)
199
+ end
200
+ def tester
201
+ @tester ||= Tester.new(project, configuration, options)
202
+ end
203
+ def documentor
204
+ @documentor ||= Documentor.new(project, configuration, options)
205
+ end
206
+ def uninstaller
207
+ @uninstaller ||= Uninstaller.new(project, configuration, options)
208
+ end
209
+ def log_header(phase)
210
+ return if quiet?
211
+ if trial?
212
+ str = "#{phase.upcase} (trial run)"
213
+ else
214
+ str = "#{phase.upcase}"
215
+ end
216
+ line = "- " * 35
217
+ line[0..str.size+3] = str
218
+ io.puts("\n- - #{line}\n\n")
219
+ end
220
+ end
221
+ end
222
+ module Setup
223
+ class Base
224
+ attr :project
225
+ attr :config
226
+ attr_accessor :trial
227
+ attr_accessor :trace
228
+ attr_accessor :quiet
229
+ attr_accessor :force
230
+ attr_accessor :io
231
+ def initialize(project, configuration, options={})
232
+ @project = project
233
+ @config = configuration
234
+ initialize_hooks
235
+ options.each do |k,v|
236
+ __send__("#{k}=", v) if respond_to?("#{k}=")
237
+ end
238
+ end
239
+ def initialize_hooks
240
+ file = META_EXTENSION_DIR + "/#{self.class.name.downcase}.rb"
241
+ if File.exist?(file)
242
+ script = File.read(file)
243
+ (class << self; self; end).class_eval(script)
244
+ end
245
+ end
246
+ def trial? ; @trial ; end
247
+ def trace? ; @trace ; end
248
+ def quiet? ; @quiet ; end
249
+ def force? ; @force ; end
250
+ def rootdir
251
+ project.rootdir
252
+ end
253
+ def bash(*args)
254
+ $stderr.puts args.join(' ') if trace?
255
+ system(*args) or raise RuntimeError, "system(#{args.map{|a| a.inspect }.join(' ')}) failed"
256
+ end
257
+ alias_method :command, :bash
258
+ def ruby(*args)
259
+ bash(config.rubyprog, *args)
260
+ end
261
+ def trace_off #:yield:
262
+ begin
263
+ save, @trace = trace?, false
264
+ yield
265
+ ensure
266
+ @trace = save
267
+ end
268
+ end
269
+ def rm_f(path)
270
+ io.puts "rm -f #{path}" if trace? or trial?
271
+ return if trial?
272
+ force_remove_file(path)
273
+ end
274
+ def force_remove_file(path)
275
+ begin
276
+ remove_file(path)
277
+ rescue
278
+ end
279
+ end
280
+ def remove_file(path)
281
+ File.chmod 0777, path
282
+ File.unlink(path)
283
+ end
284
+ def rmdir(path)
285
+ io.puts "rmdir #{path}" if trace? or trial?
286
+ return if trial?
287
+ Dir.rmdir(path)
288
+ end
289
+ end
290
+ class Error < StandardError
291
+ end
292
+ end
293
+ module Setup
294
+ class Compiler < Base
295
+ def compiles?
296
+ !extdirs.empty?
297
+ end
298
+ def configure
299
+ extdirs.each do |dir|
300
+ Dir.chdir(dir) do
301
+ if File.exist?('extconf.rb') && !FileUtils.uptodate?('Makefile', ['extconf.rb'])
302
+ ruby("extconf.rb")
303
+ end
304
+ end
305
+ end
306
+ end
307
+ def compile
308
+ extdirs.each do |dir|
309
+ Dir.chdir(dir) do
310
+ make
311
+ end
312
+ end
313
+ end
314
+ def clean
315
+ extdirs.each do |dir|
316
+ Dir.chdir(dir) do
317
+ make('clean')
318
+ end
319
+ end
320
+ end
321
+ def distclean
322
+ extdirs.each do |dir|
323
+ Dir.chdir(dir) do
324
+ make('distclean')
325
+ end
326
+ end
327
+ end
328
+ def extdirs
329
+ Dir['ext/**/*/{MANIFEST,extconf.rb}'].map do |f|
330
+ File.dirname(f)
331
+ end.uniq
332
+ end
333
+ def make(task=nil)
334
+ return unless File.exist?('Makefile')
335
+ bash(*[config.makeprog, task].compact)
336
+ end
337
+ end
338
+ end
339
+ require 'rbconfig'
340
+ require 'fileutils'
341
+ require 'erb'
342
+ require 'yaml'
343
+ require 'shellwords'
344
+ module Setup
345
+ class Configuration
346
+ RBCONFIG = ::Config::CONFIG
347
+ META_CONFIG_FILE = META_EXTENSION_DIR + '/configuration.rb'
348
+ def self.options
349
+ @@options ||= []
350
+ end
351
+ def self.option(name, *args) #type, description)
352
+ options << [name.to_s, *args] #type, description]
353
+ attr_accessor(name)
354
+ end
355
+ option :prefix , :path, 'path prefix of target environment'
356
+ option :bindir , :path, 'directory for commands'
357
+ option :libdir , :path, 'directory for libraries'
358
+ option :datadir , :path, 'directory for shared data'
359
+ option :mandir , :path, 'directory for man pages'
360
+ option :docdir , :path, 'directory for documentation'
361
+ option :rbdir , :path, 'directory for ruby scripts'
362
+ option :sodir , :path, 'directory for ruby extentions'
363
+ option :sysconfdir , :path, 'directory for system configuration files'
364
+ option :localstatedir , :path, 'directory for local state data'
365
+ option :libruby , :path, 'directory for ruby libraries'
366
+ option :librubyver , :path, 'directory for standard ruby libraries'
367
+ option :librubyverarch , :path, 'directory for standard ruby extensions'
368
+ option :siteruby , :path, 'directory for version-independent aux ruby libraries'
369
+ option :siterubyver , :path, 'directory for aux ruby libraries'
370
+ option :siterubyverarch , :path, 'directory for aux ruby binaries'
371
+ option :rubypath , :prog, 'path to set to #! line'
372
+ option :rubyprog , :prog, 'ruby program used for installation'
373
+ option :makeprog , :prog, 'make program to compile ruby extentions'
374
+ option :extconfopt , :opts, 'options to pass-thru to extconf.rb'
375
+ option :shebang , :pick, 'shebang line (#!) editing mode (all,ruby,never)'
376
+ option :no_test, :t , :bool, 'run pre-installation tests'
377
+ option :no_ri, :d , :bool, 'generate ri documentation'
378
+ option :no_doc , :bool, 'install doc/ directory'
379
+ option :no_ext , :bool, 'compile/install ruby extentions'
380
+ option :install_prefix , :path, 'install to alternate root location'
381
+ option :root , :path, 'install to alternate root location'
382
+ option :installdirs , :pick, 'install location mode (site,std,home)' #, local)
383
+ option :type , :pick, 'install location mode (site,std,home)'
384
+ ::Config::CONFIG.each do |key,val|
385
+ next if key == "configure_args"
386
+ name = key.to_s.downcase
387
+ define_method(name){ val }
388
+ end
389
+ config_args = Shellwords.shellwords(::Config::CONFIG["configure_args"])
390
+ config_args.each do |ent|
391
+ if ent.index("=")
392
+ key, val = *ent.split("=")
393
+ else
394
+ key, val = ent, true
395
+ end
396
+ name = key.downcase
397
+ name = name.sub(/^--/,'')
398
+ name = name.gsub(/-/,'_')
399
+ define_method(name){ val }
400
+ end
401
+ def options
402
+ self.class.options
403
+ end
404
+ def initialize(values={})
405
+ initialize_metaconfig
406
+ initialize_defaults
407
+ initialize_environment
408
+ initialize_configfile unless values[:reset]
409
+ values.each{ |k,v| __send__("#{k}=", v) }
410
+ yield(self) if block_given?
411
+ end
412
+ def initialize_metaconfig
413
+ if File.exist?(META_CONFIG_FILE)
414
+ script = File.read(META_CONFIG_FILE)
415
+ (class << self; self; end).class_eval(script)
416
+ end
417
+ end
418
+ def initialize_defaults
419
+ self.type = 'site'
420
+ self.no_ri = true
421
+ self.no_test = true
422
+ self.no_doc = true
423
+ self.no_ext = false
424
+ end
425
+ def initialize_environment
426
+ options.each do |name, *args|
427
+ if value = ENV["RUBYSETUP_#{name.to_s.upcase}"]
428
+ __send__("#{name}=", value)
429
+ end
430
+ end
431
+ end
432
+ def initialize_configfile
433
+ if exist?
434
+ erb = ERB.new(File.read(CONFIG_FILE))
435
+ txt = erb.result(binding)
436
+ dat = YAML.load(txt)
437
+ dat.each do |k, v|
438
+ next if 'type' == k
439
+ next if 'installdirs' == k
440
+ k = k.gsub('-','_')
441
+ __send__("#{k}=", v)
442
+ end
443
+ if dat['type']
444
+ self.type = dat['type']
445
+ end
446
+ if dat['installdirs']
447
+ self.installdirs = dat['installdirs']
448
+ end
449
+ end
450
+ end
451
+ attr_accessor :reset
452
+ def base_bindir
453
+ @base_bindir ||= subprefix('bindir')
454
+ end
455
+ def base_libdir
456
+ @base_libdir ||= subprefix('libdir')
457
+ end
458
+ def base_datadir
459
+ @base_datadir ||= subprefix('datadir')
460
+ end
461
+ def base_mandir
462
+ @base_mandir ||= subprefix('mandir')
463
+ end
464
+ def base_docdir
465
+ @base_docdir || File.dirname(subprefix('docdir'))
466
+ end
467
+ def base_rubylibdir
468
+ @rubylibdir ||= subprefix('rubylibdir')
469
+ end
470
+ def base_rubyarchdir
471
+ @base_rubyarchdir ||= subprefix('archdir')
472
+ end
473
+ def base_sysconfdir
474
+ @base_sysconfdir ||= subprefix('sysconfdir')
475
+ end
476
+ def base_localstatedir
477
+ @base_localstatedir ||= subprefix('localstatedir')
478
+ end
479
+ def type
480
+ @type ||= 'site'
481
+ end
482
+ def type=(val)
483
+ @type = val
484
+ case val.to_s
485
+ when 'std', 'ruby'
486
+ @rbdir = librubyver #'$librubyver'
487
+ @sodir = librubyverarch #'$librubyverarch'
488
+ when 'site'
489
+ @rbdir = siterubyver #'$siterubyver'
490
+ @sodir = siterubyverarch #'$siterubyverarch'
491
+ when 'home'
492
+ self.prefix = File.join(home, '.local') # TODO: Use XDG
493
+ @rbdir = nil #'$libdir/ruby'
494
+ @sodir = nil #'$libdir/ruby'
495
+ else
496
+ raise Error, "bad config: use type=(std|site|home) [#{val}]"
497
+ end
498
+ end
499
+ alias_method :installdirs, :type
500
+ alias_method :installdirs=, :type=
501
+ alias_method :install_prefix, :root
502
+ alias_method :install_prefix=, :root=
503
+ def prefix
504
+ @prefix ||= RBCONFIG['prefix']
505
+ end
506
+ def prefix=(path)
507
+ @prefix = pathname(path)
508
+ end
509
+ def libruby
510
+ @libruby ||= RBCONFIG['prefix'] + "/lib/ruby"
511
+ end
512
+ def libruby=(path)
513
+ path = pathname(path)
514
+ @librubyver = librubyver.sub(libruby, path)
515
+ @librubyverarch = librubyverarch.sub(libruby, path)
516
+ @libruby = path
517
+ end
518
+ def librubyver
519
+ @librubyver ||= RBCONFIG['rubylibdir']
520
+ end
521
+ def librubyver=(path)
522
+ @librubyver = pathname(path)
523
+ end
524
+ def librubyverarch
525
+ @librubyverarch ||= RBCONFIG['archdir']
526
+ end
527
+ def librubyverarch=(path)
528
+ @librubyverarch = pathname(path)
529
+ end
530
+ def siteruby
531
+ @siteruby ||= RBCONFIG['sitedir']
532
+ end
533
+ def siteruby=(path)
534
+ path = pathname(path)
535
+ @siterubyver = siterubyver.sub(siteruby, path)
536
+ @siterubyverarch = siterubyverarch.sub(siteruby, path)
537
+ @siteruby = path
538
+ end
539
+ def siterubyver
540
+ @siterubyver ||= RBCONFIG['sitelibdir']
541
+ end
542
+ def siterubyver=(path)
543
+ @siterubyver = pathname(path)
544
+ end
545
+ def siterubyverarch
546
+ @siterubyverarch ||= RBCONFIG['sitearchdir']
547
+ end
548
+ def siterubyverarch=(path)
549
+ @siterubyverarch = pathname(path)
550
+ end
551
+ def bindir
552
+ @bindir || File.join(prefix, base_bindir)
553
+ end
554
+ def bindir=(path)
555
+ @bindir = pathname(path)
556
+ end
557
+ def libdir
558
+ @libdir || File.join(prefix, base_libdir)
559
+ end
560
+ def libdir=(path)
561
+ @libdir = pathname(path)
562
+ end
563
+ def datadir
564
+ @datadir || File.join(prefix, base_datadir)
565
+ end
566
+ def datadir=(path)
567
+ @datadir = pathname(path)
568
+ end
569
+ def mandir
570
+ @mandir || File.join(prefix, base_mandir)
571
+ end
572
+ def mandir=(path)
573
+ @mandir = pathname(path)
574
+ end
575
+ def docdir
576
+ @docdir || File.join(prefix, base_docdir)
577
+ end
578
+ def docdir=(path)
579
+ @docdir = pathname(path)
580
+ end
581
+ def rbdir
582
+ @rbdir || File.join(prefix, base_rubylibdir)
583
+ end
584
+ def sodir
585
+ @sodir || File.join(prefix, base_rubyarchdir)
586
+ end
587
+ def sysconfdir
588
+ @sysconfdir ||= base_sysconfdir
589
+ end
590
+ def sysconfdir=(path)
591
+ @sysconfdir = pathname(path)
592
+ end
593
+ def localstatedir
594
+ @localstatedir ||= base_localstatedir
595
+ end
596
+ def localstatedir=(path)
597
+ @localstatedir = pathname(path)
598
+ end
599
+ def rubypath
600
+ @rubypath ||= File.join(RBCONFIG['bindir'], RBCONFIG['ruby_install_name'] + RBCONFIG['EXEEXT'])
601
+ end
602
+ def rubypath=(path)
603
+ @rubypath = pathname(path)
604
+ end
605
+ def rubyprog
606
+ @rubyprog || rubypath
607
+ end
608
+ def rubyprog=(command)
609
+ @rubyprog = command
610
+ end
611
+ def makeprog
612
+ @makeprog ||= (
613
+ if arg = RBCONFIG['configure_args'].split.detect {|arg| /--with-make-prog=/ =~ arg }
614
+ arg.sub(/'/, '').split(/=/, 2)[1]
615
+ else
616
+ 'make'
617
+ end
618
+ )
619
+ end
620
+ def makeprog=(command)
621
+ @makeprog = command
622
+ end
623
+ def extconfopt
624
+ @extconfopt ||= ''
625
+ end
626
+ def extconfopt=(string)
627
+ @extconfopt = string
628
+ end
629
+ def shebang
630
+ @shebang ||= 'ruby'
631
+ end
632
+ def shebang=(val)
633
+ if %w(all ruby never).include?(val)
634
+ @shebang = val
635
+ else
636
+ raise Error, "bad config: use SHEBANG=(all|ruby|never) [#{val}]"
637
+ end
638
+ end
639
+ def no_ext
640
+ @no_ext
641
+ end
642
+ def no_ext=(val)
643
+ @no_ext = boolean(val)
644
+ end
645
+ def no_test
646
+ @no_test
647
+ end
648
+ def no_test=(val)
649
+ @no_test = boolean(val)
650
+ end
651
+ def no_doc
652
+ @no_doc
653
+ end
654
+ def no_doc=(val)
655
+ @no_doc = boolean(val)
656
+ end
657
+ def no_ri
658
+ @no_ri
659
+ end
660
+ def no_ri=(val)
661
+ @no_ri = boolean(val)
662
+ end
663
+ def compile?
664
+ !no_ext
665
+ end
666
+ def test?
667
+ !no_test
668
+ end
669
+ def ri?
670
+ !no_ri
671
+ end
672
+ def doc?
673
+ !no_doc
674
+ end
675
+ def to_h
676
+ h = {}
677
+ options.each do |name, *args|
678
+ h[name.to_s] = __send__(name)
679
+ end
680
+ h
681
+ end
682
+ def to_s
683
+ to_yaml.sub(/\A---\s*\n/,'')
684
+ end
685
+ def to_yaml(*args)
686
+ to_h.to_yaml(*args)
687
+ end
688
+ def save_config
689
+ out = to_yaml
690
+ if not File.exist?(File.dirname(CONFIG_FILE))
691
+ FileUtils.mkdir_p(File.dirname(CONFIG_FILE))
692
+ end
693
+ if File.exist?(CONFIG_FILE)
694
+ txt = File.read(CONFIG_FILE)
695
+ return nil if txt == out
696
+ end
697
+ File.open(CONFIG_FILE, 'w'){ |f| f << out }
698
+ true
699
+ end
700
+ def exist?
701
+ File.exist?(CONFIG_FILE)
702
+ end
703
+ private
704
+ def pathname(path)
705
+ path.gsub(%r<\\$([^/]+)>){ self[$1] }
706
+ end
707
+ def boolean(val, name=nil)
708
+ case val
709
+ when true, false, nil
710
+ val
711
+ else
712
+ case val.to_s.downcase
713
+ when 'y', 'yes', 't', 'true'
714
+ true
715
+ when 'n', 'no', 'f', 'false'
716
+ false
717
+ else
718
+ raise Error, "bad config: use --#{name}=(yes|no) [\#{val}]"
719
+ end
720
+ end
721
+ end
722
+ def subprefix(path, with='')
723
+ val = RBCONFIG[path]
724
+ raise "Unknown path -- #{path}" if val.nil?
725
+ prefix = Regexp.quote(RBCONFIG['prefix'])
726
+ val.sub(/\A#{prefix}/, with)
727
+ end
728
+ def home
729
+ ENV['HOME'] || raise(Error, 'HOME is not set.')
730
+ end
731
+ end #class ConfigTable
732
+ end #module Setup
733
+ =begin
734
+ def inintialize_metaconfig
735
+ path = Dir.glob(METACONFIG_FILE).first
736
+ if path && File.file?(path)
737
+ MetaConfigEnvironment.new(self).instance_eval(File.read(path), path)
738
+ end
739
+ end
740
+ class MetaConfigEnvironment
741
+ def initialize(config) #, installer)
742
+ @config = config
743
+ end
744
+ def config_names
745
+ @config.descriptions.collect{ |n, t, d| n.to_s }
746
+ end
747
+ def config?(name)
748
+ @config.descriptions.find do |sym, type, desc|
749
+ sym.to_s == name.to_s
750
+ end
751
+ end
752
+ def bool_config?(name)
753
+ @config.descriptions.find do |sym, type, desc|
754
+ sym.to_s == name.to_s && type == :bool
755
+ end
756
+ end
757
+ def path_config?(name)
758
+ @config.descriptions.find do |sym, type, desc|
759
+ sym.to_s == name.to_s && type == :path
760
+ end
761
+ end
762
+ def value_config?(name)
763
+ @config.descriptions.find do |sym, type, desc|
764
+ sym.to_s == name.to_s && type != :prog
765
+ end
766
+ end
767
+ def add_config(name, default, desc)
768
+ @config.descriptions << [name.to_sym, nil, desc]
769
+ end
770
+ def add_bool_config(name, default, desc)
771
+ @config.descriptions << [name.to_sym, :bool, desc]
772
+ end
773
+ def add_path_config(name, default, desc)
774
+ @config.descriptions << [name.to_sym, :path, desc]
775
+ end
776
+ def set_config_default(name, default)
777
+ @config[name] = default
778
+ end
779
+ def remove_config(name)
780
+ item = @config.descriptions.find do |sym, type, desc|
781
+ sym.to_s == name.to_s
782
+ end
783
+ index = @config.descriptions.index(item)
784
+ @config.descriptions.delete(index)
785
+ end
786
+ end
787
+ =end
788
+ module Setup
789
+ class Documentor < Base
790
+ def document
791
+ return if config.no_doc
792
+ exec_ri
793
+ end
794
+ def exec_ri
795
+ case config.type #installdirs
796
+ when 'std', 'ruby'
797
+ output = "--ri-site"
798
+ when 'site'
799
+ output = "--ri-site"
800
+ when 'home'
801
+ output = "--ri"
802
+ else
803
+ abort "bad config: should not be possible -- type=#{config.type}"
804
+ end
805
+ if File.exist?('.document')
806
+ files = File.read('.document').split("\n")
807
+ files.reject!{ |l| l =~ /^\s*[#]/ || l !~ /\S/ }
808
+ files.collect!{ |f| f.strip }
809
+ else
810
+ files = []
811
+ files << 'lib' if File.directory?('lib')
812
+ files << 'ext' if File.directory?('ext')
813
+ end
814
+ opt = []
815
+ opt << "-U"
816
+ opt << "-q" #if quiet?
817
+ opt << output
818
+ opt << files
819
+ opt = opt.flatten
820
+ cmd = "rdoc " + opt.join(' ')
821
+ if trial?
822
+ puts cmd
823
+ else
824
+ begin
825
+ success = system(cmd)
826
+ raise unless success
827
+ io.puts "Ok ri." #unless quiet?
828
+ rescue Exception
829
+ $stderr.puts "ri generation failed"
830
+ $stderr.puts "command was: '#{cmd}'"
831
+ end
832
+ end
833
+ end
834
+ def exec_rdoc
835
+ main = Dir.glob("README{,.*}", File::FNM_CASEFOLD).first
836
+ if File.exist?('.document')
837
+ files = File.read('.document').split("\n")
838
+ files.reject!{ |l| l =~ /^\s*[#]/ || l !~ /\S/ }
839
+ files.collect!{ |f| f.strip }
840
+ else
841
+ files = []
842
+ files << main if main
843
+ files << 'lib' if File.directory?('lib')
844
+ files << 'ext' if File.directory?('ext')
845
+ end
846
+ checkfiles = (files + files.map{ |f| Dir[File.join(f,'*','**')] }).flatten.uniq
847
+ if FileUtils.uptodate?('doc/rdoc', checkfiles)
848
+ puts "RDocs look current."
849
+ return
850
+ end
851
+ output = 'doc/rdoc'
852
+ title = (PACKAGE.capitalize + " API").strip if PACKAGE
853
+ template = config.doctemplate || 'html'
854
+ opt = []
855
+ opt << "-U"
856
+ opt << "-q" #if quiet?
857
+ opt << "--op=#{output}"
858
+ opt << "--title=#{title}"
859
+ opt << "--main=#{main}" if main
860
+ opt << files
861
+ opt = opt.flatten
862
+ cmd = "rdoc " + opt.join(' ')
863
+ if trial?
864
+ puts cmd
865
+ else
866
+ begin
867
+ system(cmd)
868
+ puts "Ok rdoc." unless quiet?
869
+ rescue Exception
870
+ puts "Fail rdoc."
871
+ puts "Command was: '#{cmd}'"
872
+ puts "Proceeding with install anyway."
873
+ end
874
+ end
875
+ end
876
+ end
877
+ end
878
+ module Setup
879
+ class Installer < Base
880
+ def install_prefix
881
+ config.install_prefix
882
+ end
883
+ def install
884
+ Dir.chdir(rootdir) do
885
+ install_bin
886
+ install_ext
887
+ install_lib
888
+ install_data
889
+ install_man
890
+ install_doc
891
+ install_etc
892
+ prune_install_record
893
+ end
894
+ end
895
+ def install_bin
896
+ return unless directory?('bin')
897
+ report_transfer('bin', config.bindir)
898
+ files = files('bin')
899
+ install_files('bin', files, config.bindir, 0755)
900
+ end
901
+ def install_ext
902
+ return unless directory?('ext')
903
+ report_transfer('ext', config.sodir)
904
+ files = files('ext')
905
+ files = select_dllext(files)
906
+ files.each do |file|
907
+ name = File.join(File.dirname(File.dirname(file)), File.basename(file))
908
+ dest = destination(config.sodir, name)
909
+ install_file('ext', file, dest, 0555, install_prefix)
910
+ end
911
+ end
912
+ def install_lib
913
+ return unless directory?('lib')
914
+ report_transfer('lib', config.rbdir)
915
+ files = files('lib')
916
+ install_files('lib', files, config.rbdir, 0644)
917
+ end
918
+ def install_data
919
+ return unless directory?('data')
920
+ report_transfer('data', config.datadir)
921
+ files = files('data')
922
+ install_files('data', files, config.datadir, 0644)
923
+ end
924
+ def install_etc
925
+ return unless directory?('etc')
926
+ report_transfer('etc', config.sysconfdir)
927
+ files = files('etc')
928
+ install_files('etc', files, config.sysconfdir, 0644)
929
+ end
930
+ def install_man
931
+ return unless directory?('man')
932
+ report_transfer('man', config.mandir)
933
+ files = files('man')
934
+ install_files('man', files, config.mandir, 0644)
935
+ end
936
+ def install_doc
937
+ return unless config.doc?
938
+ return unless directory?('doc')
939
+ return unless project.name
940
+ dir = File.join(config.docdir, "ruby-#{project.name}")
941
+ report_transfer('doc', dir)
942
+ files = files('doc')
943
+ install_files('doc', files, dir, 0644)
944
+ end
945
+ private
946
+ def report_transfer(source, directory)
947
+ unless quiet?
948
+ if install_prefix
949
+ out = File.join(install_prefix, directory)
950
+ else
951
+ out = directory
952
+ end
953
+ io.puts "* #{source} -> #{out}"
954
+ end
955
+ end
956
+ def directory?(path)
957
+ File.directory?(path)
958
+ end
959
+ def files(dir)
960
+ files = Dir["#{dir}/**/*"]
961
+ files = files.select{ |f| File.file?(f) }
962
+ files = files.map{ |f| f.sub("#{dir}/", '') }
963
+ files
964
+ end
965
+ def select_dllext(files)
966
+ ents = files.select do |file|
967
+ File.extname(file) == ".#{dllext}"
968
+ end
969
+ if ents.empty? && !files.empty?
970
+ raise Error, "ruby extention not compiled: 'setup.rb make' first"
971
+ end
972
+ ents
973
+ end
974
+ def dllext
975
+ config.dlext
976
+ end
977
+ def install_files(dir, list, dest, mode)
978
+ list.each do |fname|
979
+ rdest = destination(dest, fname)
980
+ install_file(dir, fname, rdest, mode, install_prefix)
981
+ end
982
+ end
983
+ def install_file(dir, from, dest, mode, prefix=nil)
984
+ mkdir_p(File.dirname(dest))
985
+ if trace? or trial?
986
+ io.puts "install #{dir}/#{from} #{dest}"
987
+ end
988
+ return if trial?
989
+ str = binread(File.join(dir, from))
990
+ if diff?(str, dest)
991
+ trace_off {
992
+ rm_f(dest) if File.exist?(dest)
993
+ }
994
+ File.open(dest, 'wb'){ |f| f.write(str) }
995
+ File.chmod(mode, dest)
996
+ end
997
+ record_installation(dest) # record file as installed
998
+ end
999
+ def mkdir_p(dirname) #, prefix=nil)
1000
+ return if File.directory?(dirname)
1001
+ io.puts "mkdir -p #{dirname}" if trace? or trial?
1002
+ return if trial?
1003
+ dirs = File.expand_path(dirname).split(%r<(?=/)>)
1004
+ if /\A[a-z]:\z/i =~ dirs[0]
1005
+ disk = dirs.shift
1006
+ dirs[0] = disk + dirs[0]
1007
+ end
1008
+ dirs.each_index do |idx|
1009
+ path = dirs[0..idx].join('')
1010
+ unless File.dir?(path)
1011
+ Dir.mkdir(path)
1012
+ end
1013
+ record_installation(path) # record directories made
1014
+ end
1015
+ end
1016
+ def record_installation(path)
1017
+ File.open(install_record, 'a') do |f|
1018
+ f.puts(path)
1019
+ end
1020
+ end
1021
+ def prune_install_record
1022
+ entries = File.read(install_record).split("\n")
1023
+ entries.uniq!
1024
+ File.open(install_record, 'w') do |f|
1025
+ f << entries.join("\n")
1026
+ f << "\n"
1027
+ end
1028
+ end
1029
+ def install_record
1030
+ @install_record ||= (
1031
+ file = INSTALL_RECORD
1032
+ dir = File.dirname(file)
1033
+ unless File.directory?(dir)
1034
+ FileUtils.mkdir_p(dir)
1035
+ end
1036
+ file
1037
+ )
1038
+ end
1039
+ def destination(dir, file)
1040
+ dest = install_prefix ? File.join(install_prefix, File.expand_path(dir)) : dir
1041
+ dest = File.join(dest, file) #if File.dir?(dest)
1042
+ dest = File.expand_path(dest)
1043
+ dest
1044
+ end
1045
+ def diff?(new_content, path)
1046
+ return true unless File.exist?(path)
1047
+ new_content != binread(path)
1048
+ end
1049
+ def binread(fname)
1050
+ File.open(fname, 'rb') do |f|
1051
+ return f.read
1052
+ end
1053
+ end
1054
+ def install_shebang(files, dir)
1055
+ files.each do |file|
1056
+ path = File.join(dir, File.basename(file))
1057
+ update_shebang_line(path)
1058
+ end
1059
+ end
1060
+ def update_shebang_line(path)
1061
+ return if trial?
1062
+ return if config.shebang == 'never'
1063
+ old = Shebang.load(path)
1064
+ if old
1065
+ if old.args.size > 1
1066
+ $stderr.puts "warning: #{path}"
1067
+ $stderr.puts "Shebang line has too many args."
1068
+ $stderr.puts "It is not portable and your program may not work."
1069
+ end
1070
+ new = new_shebang(old)
1071
+ return if new.to_s == old.to_s
1072
+ else
1073
+ return unless config.shebang == 'all'
1074
+ new = Shebang.new(config.rubypath)
1075
+ end
1076
+ $stderr.puts "updating shebang: #{File.basename(path)}" if trace?
1077
+ open_atomic_writer(path) do |output|
1078
+ File.open(path, 'rb') do |f|
1079
+ f.gets if old # discard
1080
+ output.puts new.to_s
1081
+ output.print f.read
1082
+ end
1083
+ end
1084
+ end
1085
+ def new_shebang(old)
1086
+ if /\Aruby/ =~ File.basename(old.cmd)
1087
+ Shebang.new(config.rubypath, old.args)
1088
+ elsif File.basename(old.cmd) == 'env' and old.args.first == 'ruby'
1089
+ Shebang.new(config.rubypath, old.args[1..-1])
1090
+ else
1091
+ return old unless config.shebang == 'all'
1092
+ Shebang.new(config.rubypath)
1093
+ end
1094
+ end
1095
+ def open_atomic_writer(path, &block)
1096
+ tmpfile = File.basename(path) + '.tmp'
1097
+ begin
1098
+ File.open(tmpfile, 'wb', &block)
1099
+ File.rename tmpfile, File.basename(path)
1100
+ ensure
1101
+ File.unlink tmpfile if File.exist?(tmpfile)
1102
+ end
1103
+ end
1104
+ class Shebang
1105
+ def Shebang.load(path)
1106
+ line = nil
1107
+ File.open(path) {|f|
1108
+ line = f.gets
1109
+ }
1110
+ return nil unless /\A#!/ =~ line
1111
+ parse(line)
1112
+ end
1113
+ def Shebang.parse(line)
1114
+ cmd, *args = *line.strip.sub(/\A\#!/, '').split(' ')
1115
+ new(cmd, args)
1116
+ end
1117
+ def initialize(cmd, args = [])
1118
+ @cmd = cmd
1119
+ @args = args
1120
+ end
1121
+ attr_reader :cmd
1122
+ attr_reader :args
1123
+ def to_s
1124
+ "#! #{@cmd}" + (@args.empty? ? '' : " #{@args.join(' ')}")
1125
+ end
1126
+ end
1127
+ end
1128
+ end
1129
+ module Setup
1130
+ class Tester < Base
1131
+ RUBYSCRIPT = META_EXTENSION_DIR + '/testrc.rb'
1132
+ SHELLSCRIPT = 'script/test'
1133
+ def testable?
1134
+ return false if config.no_test
1135
+ return true if File.exist?(RUBYSCRIPT)
1136
+ return true if File.exist?(SHELLSCRIPT)
1137
+ false
1138
+ end
1139
+ def test
1140
+ return true if !testable?
1141
+ if File.exist?(RUBYSCRIPT)
1142
+ test_rubyscript
1143
+ elsif File.exist?(SHELLSCRIPT)
1144
+ test_shellscript
1145
+ end
1146
+ end
1147
+ def test_shellscript
1148
+ bash(SHELLSCRIPT)
1149
+ end
1150
+ def test_rubyscript
1151
+ ruby(RUBYSCRIPT)
1152
+ end
1153
+ end
1154
+ end
1155
+ module Setup
1156
+ class Uninstaller < Base
1157
+ def uninstall
1158
+ return unless File.exist?(INSTALL_RECORD)
1159
+ files = []
1160
+ dirs = []
1161
+ paths.each do |path|
1162
+ dirs << path if File.dir?(path)
1163
+ files << path if File.file?(path)
1164
+ end
1165
+ if dirs.empty? && files.empty?
1166
+ io.outs "Nothing to remove."
1167
+ return
1168
+ end
1169
+ files.sort!{ |a,b| b.size <=> a.size }
1170
+ dirs.sort!{ |a,b| b.size <=> a.size }
1171
+ if !force? && !trial?
1172
+ puts (files + dirs).collect{ |f| "#{f}" }.join("\n")
1173
+ puts
1174
+ puts "Must use --force option to remove these files and directories that become empty."
1175
+ return
1176
+ end
1177
+ files.each do |file|
1178
+ rm_f(file)
1179
+ end
1180
+ dirs.each do |dir|
1181
+ entries = Dir.entries(dir)
1182
+ entries.delete('.')
1183
+ entries.delete('..')
1184
+ rmdir(dir) if entries.empty?
1185
+ end
1186
+ rm_f(INSTALL_RECORD)
1187
+ end
1188
+ private
1189
+ def paths
1190
+ @paths ||= (
1191
+ lines = File.read(INSTALL_RECORD).split(/\s*\n/)
1192
+ lines = lines.map{ |line| line.strip }
1193
+ lines = lines.uniq
1194
+ lines = lines.reject{ |line| line.empty? } # skip blank lines
1195
+ lines = lines.reject{ |line| line[0,1] == '#' } # skip blank lines
1196
+ lines
1197
+ )
1198
+ end
1199
+ end
1200
+ end
1201
+ require 'optparse'
1202
+ module Setup
1203
+ class Command
1204
+ def self.run(*argv)
1205
+ new.run(*argv)
1206
+ end
1207
+ def self.tasks
1208
+ @tasks ||= {}
1209
+ end
1210
+ def self.order
1211
+ @order ||= []
1212
+ end
1213
+ def self.task(name, description)
1214
+ tasks[name] = description
1215
+ order << name
1216
+ end
1217
+ task 'show' , "show current configuration"
1218
+ task 'all' , "config, compile and install"
1219
+ task 'preconfig', "customize configuration settings"
1220
+ task 'config' , "configure extensions"
1221
+ task 'compile' , "compile ruby extentions"
1222
+ task 'test' , "run test suite"
1223
+ task 'doc' , "generate ri documentation"
1224
+ task 'install' , "install project files"
1225
+ task 'uninstall', "uninstall previously installed files"
1226
+ task 'clean' , "does `make clean' for each extention"
1227
+ task 'distclean', "does `make distclean' for each extention"
1228
+ def run(*argv)
1229
+ ARGV.replace(argv) unless argv.empty?
1230
+ task = ARGV.find{ |a| a !~ /^[-]/ }
1231
+ task = 'all' unless task
1232
+ unless task_names.include?(task)
1233
+ $stderr.puts "Not a valid task -- #{task}"
1234
+ exit 1
1235
+ end
1236
+ parser = OptionParser.new
1237
+ options = {}
1238
+ parser.banner = "Usage: #{File.basename($0)} [TASK] [OPTIONS]"
1239
+ optparse_header(parser, options)
1240
+ case task
1241
+ when 'preconfig'
1242
+ optparse_preconfig(parser, options)
1243
+ when 'config'
1244
+ optparse_config(parser, options)
1245
+ when 'install'
1246
+ optparse_install(parser, options)
1247
+ when 'all'
1248
+ optparse_all(parser, options)
1249
+ end
1250
+ optparse_common(parser, options)
1251
+ begin
1252
+ parser.parse!(ARGV)
1253
+ rescue OptionParser::InvalidOption
1254
+ $stderr.puts $!.to_s.capitalize
1255
+ exit 1
1256
+ end
1257
+ rootdir = session.project.rootdir
1258
+ print_header
1259
+ begin
1260
+ $stderr.puts "(#{RUBY_ENGINE} #{RUBY_VERSION} #{RUBY_PLATFORM})"
1261
+ rescue
1262
+ $stderr.puts "(#{RUBY_VERSION} #{RUBY_PLATFORM})"
1263
+ end
1264
+ begin
1265
+ session.__send__(task)
1266
+ rescue Error => err
1267
+ raise err if $DEBUG
1268
+ $stderr.puts $!.message
1269
+ $stderr.puts "Try 'setup.rb --help' for detailed usage."
1270
+ abort $!.message #exit 1
1271
+ end
1272
+ puts unless session.quiet?
1273
+ end
1274
+ def session
1275
+ @session ||= Session.new(:io=>$stdout)
1276
+ end
1277
+ def configuration
1278
+ @configuration ||= session.configuration
1279
+ end
1280
+ def optparse_header(parser, options)
1281
+ parser.banner = "USAGE: #{File.basename($0)} [command] [options]"
1282
+ end
1283
+ def optparse_all(parser, options)
1284
+ optparse_config(parser, options)
1285
+ end
1286
+ def optparse_preconfig(parser, options)
1287
+ parser.separator ""
1288
+ parser.separator "Configuration options:"
1289
+ configuration.options.each do |args|
1290
+ args = args.dup
1291
+ desc = args.pop
1292
+ type = args.pop
1293
+ name, shortcut = *args
1294
+ optname = name.to_s.gsub('_', '-')
1295
+ case type
1296
+ when :bool
1297
+ if optname.index('no-') == 0
1298
+ optname = "[no-]" + optname.sub(/^no-/, '')
1299
+ opts = shortcut ? ["-#{shortcut}", "--#{optname}", desc] : ["--#{optname}", desc]
1300
+ parser.on(*opts) do |val|
1301
+ configuration.__send__("#{name}=", !val)
1302
+ end
1303
+ else
1304
+ optname = "[no-]" + optname.sub(/^no-/, '')
1305
+ opts = shortcut ? ["-#{shortcut}", "--#{optname}", desc] : ["--#{optname}", desc]
1306
+ parser.on(*opts) do |val|
1307
+ configuration.__send__("#{name}=", val)
1308
+ end
1309
+ end
1310
+ else
1311
+ opts = shortcut ? ["-#{shortcut}", "--#{optname} #{type.to_s.upcase}", desc] :
1312
+ ["--#{optname} #{type.to_s.upcase}", desc]
1313
+ parser.on(*opts) do |val|
1314
+ configuration.__send__("#{name}=", val)
1315
+ end
1316
+ end
1317
+ end
1318
+ end
1319
+ def optparse_config(parser, options)
1320
+ end
1321
+ def optparse_install(parser, options)
1322
+ parser.separator ""
1323
+ parser.separator "Install options:"
1324
+ parser.on("--prefix PATH", "Installation prefix") do |val|
1325
+ configuration.install_prefix = val
1326
+ end
1327
+ end
1328
+ def optparse_common(parser, options)
1329
+ parser.separator ""
1330
+ parser.separator "General options:"
1331
+ parser.on("-q", "--quiet", "Suppress output") do
1332
+ session.quiet = true
1333
+ end
1334
+ parser.on("-f", "--force", "Force operation") do
1335
+ session.force = true
1336
+ end
1337
+ parser.on("--trace", "--verbose", "Watch execution") do |val|
1338
+ session.trace = true
1339
+ end
1340
+ parser.on("--trial", "--no-harm", "Do not write to disk") do |val|
1341
+ session.trial = true
1342
+ end
1343
+ parser.on("--debug", "Turn on debug mode") do |val|
1344
+ $DEBUG = true
1345
+ end
1346
+ parser.separator ""
1347
+ parser.separator "Inform options:"
1348
+ parser.on_tail("-h", "--help", "display this help information") do
1349
+ puts parser
1350
+ exit
1351
+ end
1352
+ parser.on_tail("--version", "-v", "Show version") do
1353
+ puts File.basename($0) + ' v' + Setup::VERSION #Version.join('.')
1354
+ exit
1355
+ end
1356
+ parser.on_tail("--copyright", "Show copyright") do
1357
+ puts Setup::COPYRIGHT #opyright
1358
+ exit
1359
+ end
1360
+ end
1361
+ def task_names
1362
+ self.class.tasks.keys
1363
+ end
1364
+ def print_header
1365
+ end
1366
+ end
1367
+ end
1368
+ Setup::Command.run