class Senv::Script
Constants
- DEFAULT_HELP
Attributes
argv[RW]
env[RW]
help[RW]
root[RW]
source[RW]
stderr[RW]
stdin[RW]
stdout[RW]
Public Class Methods
after(&block)
click to toggle source
# File lib/senv/script.rb, line 60 def self.after(&block) @after ||= [] @after << block if block @after end
before(&block)
click to toggle source
# File lib/senv/script.rb, line 50 def self.before(&block) @before ||= [] @before << block if block @before end
help(*args)
click to toggle source
# File lib/senv/script.rb, line 455 def self.help(*args) @help ||= nil unless args.empty? @help = utils.unindent(args.join) end @help end
klass_for(&block)
click to toggle source
# File lib/senv/script.rb, line 486 def Script.klass_for(&block) Class.new(Script) do |klass| def klass.name; "Script::Klass__#{ SecureRandom.uuid.to_s.gsub('-', '_') }"; end klass.class_eval(&block) end end
run(*args, &block)
click to toggle source
# File lib/senv/script.rb, line 465 def self.run(*args, &block) modes = if args.empty? [nil] else args end modes.each do |mode| method_name = if mode "run_#{ mode }" else "run" end define_method(method_name, &block) end end
run!(*args, &block)
click to toggle source
# File lib/senv/script.rb, line 493 def self.run!(*args, &block) STDOUT.sync = true STDERR.sync = true %w[ PIPE INT ].each{|signal| Signal.trap(signal, "EXIT")} script = ( source = if binding.respond_to?(:source_location) File.expand_path(binding.source_location.first) else File.expand_path(eval('__FILE__', block.binding)) end root = File.dirname(source) klass = Script.klass_for(&block) instance = klass.new instance.source = source instance.root = root instance ) script.run!(*args) end
utils(&block)
click to toggle source
# File lib/senv/script.rb, line 410 def self.utils(&block) block ? Util.module_eval(&block) : Util end
Public Instance Methods
after!()
click to toggle source
# File lib/senv/script.rb, line 66 def after! self.class.after.each{|block| block.call} end
before!()
click to toggle source
# File lib/senv/script.rb, line 56 def before! self.class.before.each{|block| block.call} end
help!()
click to toggle source
# File lib/senv/script.rb, line 157 def help! run_help abort end
init!(env, argv)
click to toggle source
# File lib/senv/script.rb, line 40 def init!(env, argv) @klass = self.class @env = env.to_hash.dup @argv = argv.map{|arg| arg.dup} @stdout = $stdout.dup @stdin = $stdin.dup @stderr = $stderr.dup @help = @klass.help || Util.unindent(DEFAULT_HELP) end
noop()
click to toggle source
# File lib/senv/script.rb, line 448 def noop ENV['SCRIPT_NOOP'] || ENV['NOOP'] end
Also aliased as: noop?
parse_command_line!()
click to toggle source
# File lib/senv/script.rb, line 84 def parse_command_line! @options = Hash.new @opts = Hash.new argv = [] head = [] tail = [] %w[ :: -- ].each do |stop| if((i = @argv.index(stop))) head = @argv.slice(0 ... i) tail = @argv.slice((i + 1) ... @argv.size) @argv = head break end end @argv.each do |arg| case when arg =~ %r`^\s*:([^:\s]+)[=](.+)` key = $1 val = $2 @options[key] = val when arg =~ %r`^\s*(:+)(.+)` switch = $1 key = $2 val = switch.size.odd? @options[key] = val else argv.push(arg) end end argv += tail @argv.replace(argv) @options.each do |key, val| @opts[key.to_s.to_sym] = val end [@options, @opts] end
run!(env = ENV, argv = ARGV)
click to toggle source
# File lib/senv/script.rb, line 31 def run!(env = ENV, argv = ARGV) before! init!(env, argv) parse_command_line! set_mode! run_mode! after! end
run_help()
click to toggle source
# File lib/senv/script.rb, line 153 def run_help STDOUT.puts(@help) end
run_mode!()
click to toggle source
# File lib/senv/script.rb, line 137 def run_mode! if @mode return send("run_#{ @mode }") else if respond_to?(:run) return send(:run) end if @argv.empty? run_help else abort("#{ $0 } help") end end end
set_mode!()
click to toggle source
# File lib/senv/script.rb, line 128 def set_mode! case when respond_to?("run_#{ @argv[0] }") @mode = @argv.shift else @mode = nil end end
u()
click to toggle source
# File lib/senv/script.rb, line 418 def u Util end
utils()
click to toggle source
# File lib/senv/script.rb, line 414 def utils Util end