Class: Setup::Command
- Inherits:
-
Object
- Object
- Setup::Command
- Defined in:
- lib/setup/command.rb
Overview
Command-line interface for Setup.rb.
Class Method Summary collapse
-
.order ⇒ Object
Task names listed in order of information.
-
.run(*argv) ⇒ Object
Initialize and run.
-
.task(name, description) ⇒ Object
Define a task.
-
.tasks ⇒ Object
Hash of
task => description.
Instance Method Summary collapse
-
#configuration ⇒ Object
Setup configuration.
-
#optparse_all(parser, options) ⇒ Object
Setup options for
alltask. -
#optparse_common(parser, options) ⇒ Object
Common options for every task.
- #optparse_compile(parser, options) ⇒ Object
-
#optparse_config(parser, options) ⇒ Object
Setup options for
configtask. - #optparse_header(parser, options) ⇒ Object
-
#optparse_install(parser, options) ⇒ Object
Setup options for
installtask. -
#print_header ⇒ Object
Output Header.
-
#run(*argv) ⇒ Object
Run command.
-
#session ⇒ Object
Setup session.
-
#task_names ⇒ Object
List of task names.
Class Method Details
.order ⇒ Object
Task names listed in order of information.
24 25 26 |
# File 'lib/setup/command.rb', line 24 def self.order @order ||= [] end |
.run(*argv) ⇒ Object
Initialize and run.
12 13 14 |
# File 'lib/setup/command.rb', line 12 def self.run(*argv) new.run(*argv) end |
.task(name, description) ⇒ Object
Define a task.
30 31 32 33 |
# File 'lib/setup/command.rb', line 30 def self.task(name, description) tasks[name] = description order << name end |
.tasks ⇒ Object
Hash of task => description.
18 19 20 |
# File 'lib/setup/command.rb', line 18 def self.tasks @tasks ||= {} end |
Instance Method Details
#configuration ⇒ Object
Setup configuration. This comes from the session object.
119 120 121 |
# File 'lib/setup/command.rb', line 119 def configuration @configuration ||= session.configuration end |
#optparse_all(parser, options) ⇒ Object
Setup options for all task.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/setup/command.rb', line 130 def optparse_all(parser, ) optparse_config(parser, ) optparse_compile(parser, ) optparse_install(parser, ) # TODO: why was this remarked out ? #parser.on("-t", "--[no-]test", "run tests (default is --no-test)") do |val| # configuration.no_test = val #end #parser.on("--[no-]doc", "generate ri/yri documentation (default is --doc)") do |val| # configuration.no_doc = val #end end |
#optparse_common(parser, options) ⇒ Object
Common options for every task.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/setup/command.rb', line 227 def optparse_common(parser, ) parser.separator "" parser.separator "General options:" parser.on("-q", "--quiet", "Suppress output") do session.quiet = true end parser.on("-f", "--force", "Force operation") do session.force = true end parser.on("--trace", "--verbose", "Watch execution") do |val| session.trace = true end parser.on("--trial", "--no-harm", "Do not write to disk") do |val| session.trial = true end parser.on("--debug", "Turn on debug mode") do |val| $DEBUG = true end parser.separator "" parser.separator "Inform options:" # Tail options (eg. commands in option form) parser.on_tail("-h", "--help", "display this help information") do #puts help puts parser exit end parser.on_tail("--version", "-v", "Show version") do puts File.basename($0) + ' v' + Setup::VERSION #Version.join('.') exit end parser.on_tail("--copyright", "Show copyright") do puts Setup::COPYRIGHT #opyright exit end end |
#optparse_compile(parser, options) ⇒ Object
183 184 |
# File 'lib/setup/command.rb', line 183 def optparse_compile(parser, ) end |
#optparse_config(parser, options) ⇒ Object
Setup options for config task.
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 |
# File 'lib/setup/command.rb', line 144 def optparse_config(parser, ) parser.separator "" parser.separator "Configuration options:" #parser.on('--reset', 'reset configuration to default settings') do # session.reset = true #end configuration..each do |args| args = args.dup desc = args.pop type = args.pop name, shortcut = *args #raise ArgumentError unless name, type, desc optname = name.to_s.gsub('_', '-') case type when :bool if optname.index('no-') == 0 optname = "[no-]" + optname.sub(/^no-/, '') opts = shortcut ? ["-#{shortcut}", "--#{optname}", desc] : ["--#{optname}", desc] parser.on(*opts) do |val| configuration.__send__("#{name}=", !val) end else optname = "[no-]" + optname.sub(/^no-/, '') opts = shortcut ? ["-#{shortcut}", "--#{optname}", desc] : ["--#{optname}", desc] parser.on(*opts) do |val| configuration.__send__("#{name}=", val) end end else opts = shortcut ? ["-#{shortcut}", "--#{optname} #{type.to_s.upcase}", desc] : ["--#{optname} #{type.to_s.upcase}", desc] parser.on(*opts) do |val| configuration.__send__("#{name}=", val) end end end end |
#optparse_header(parser, options) ⇒ Object
124 125 126 |
# File 'lib/setup/command.rb', line 124 def optparse_header(parser, ) parser. = "USAGE: #{File.basename($0)} [command] [options]" end |
#optparse_install(parser, options) ⇒ Object
Setup options for install task.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/setup/command.rb', line 188 def optparse_install(parser, ) parser.separator '' parser.separator 'Install options:' # install prefix overrides target prefix when installing parser.on('--prefix PATH', 'install to alternate root location') do |val| configuration.install_prefix = val end # type can override config parser.on('--type TYPE', "install location mode (site,std,home)") do |val| configuration.type = val end # test can be override config parser.on('-t', '--[no-]test', "run pre-installation tests") do |bool| configuration.test = bool end end |
#print_header ⇒ Object
Output Header.
TODO: This is not yet used. It might be nice to have, but not sure what it should contain or look like.
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/setup/command.rb', line 287 def print_header #unless session.quiet? # if session.project.name # puts "= #{session.project.name} (#{rootdir})" # else # puts "= #{rootdir}" # end #end #$stderr << "#{session.options.inspect}\n" if session.trace? or session.trial? end |
#run(*argv) ⇒ Object
Run command.
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 |
# File 'lib/setup/command.rb', line 47 def run(*argv) ARGV.replace(argv) unless argv.empty? #session = Session.new(:io=>$stdio) #config = session.configuration task = ARGV.find{ |a| a !~ /^[-]/ } task = 'all' unless task #task = 'doc' if task == 'document' unless task_names.include?(task) $stderr.puts "Not a valid task -- #{task}" exit 1 end parser = OptionParser.new = {} parser. = "Usage: #{File.basename($0)} [TASK] [OPTIONS]" optparse_header(parser, ) case task when 'config' optparse_config(parser, ) when 'compile' optparse_compile(parser, ) when 'install' optparse_install(parser, ) when 'all' optparse_all(parser, ) end optparse_common(parser, ) begin parser.parse!(ARGV) rescue OptionParser::InvalidOption $stderr.puts $!.to_s.capitalize exit 1 end # This ensures we are in a project directory. rootdir = session.project.rootdir print_header begin $stderr.puts "(#{RUBY_ENGINE} #{RUBY_VERSION} #{RUBY_PLATFORM})" rescue $stderr.puts "(#{RUBY_VERSION} #{RUBY_PLATFORM})" end begin session.__send__(task) rescue Error => err raise err if $DEBUG $stderr.puts $!. $stderr.puts "Try 'setup.rb --help' for detailed usage." abort $!. #exit 1 end puts unless session.quiet? end |
#session ⇒ Object
Setup session.
113 114 115 |
# File 'lib/setup/command.rb', line 113 def session @session ||= Session.new(:io=>$stdout) end |
#task_names ⇒ Object
List of task names. – TODO: shouldn’t this use self.class.order ? ++
277 278 279 280 |
# File 'lib/setup/command.rb', line 277 def task_names #self.class.order self.class.tasks.keys end |