Class: Senv::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/senv/script.rb

Defined Under Namespace

Modules: Util Classes: Slug

Constant Summary collapse

DEFAULT_HELP =
<<-__
  NAME
    #TODO

  SYNOPSIS
    #TODO
     
  DESCRIPTION
    #TODO
     
  EXAMPLES
    #TODO
__

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



25
26
27
# File 'lib/senv/script.rb', line 25

def argv
  @argv
end

#envObject

Returns the value of attribute env.



24
25
26
# File 'lib/senv/script.rb', line 24

def env
  @env
end

#helpObject

Returns the value of attribute help.



29
30
31
# File 'lib/senv/script.rb', line 29

def help
  @help
end

#rootObject

Returns the value of attribute root.



23
24
25
# File 'lib/senv/script.rb', line 23

def root
  @root
end

#sourceObject

Returns the value of attribute source.



22
23
24
# File 'lib/senv/script.rb', line 22

def source
  @source
end

#stderrObject

Returns the value of attribute stderr.



28
29
30
# File 'lib/senv/script.rb', line 28

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



27
28
29
# File 'lib/senv/script.rb', line 27

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



26
27
28
# File 'lib/senv/script.rb', line 26

def stdout
  @stdout
end

Class Method Details

.after(&block) ⇒ Object



60
61
62
63
64
# File 'lib/senv/script.rb', line 60

def self.after(&block)
  @after ||= []
  @after << block if block
  @after
end

.before(&block) ⇒ Object



50
51
52
53
54
# File 'lib/senv/script.rb', line 50

def self.before(&block)
  @before ||= []
  @before << block if block
  @before
end

.help(*args) ⇒ Object



455
456
457
458
459
460
461
462
463
# 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) ⇒ Object



486
487
488
489
490
491
# 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) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# 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) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# 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) ⇒ Object



410
411
412
# File 'lib/senv/script.rb', line 410

def self.utils(&block)
  block ? Util.module_eval(&block) : Util
end

Instance Method Details

#after!Object



66
67
68
# File 'lib/senv/script.rb', line 66

def after!
  self.class.after.each{|block| block.call}
end

#before!Object



56
57
58
# File 'lib/senv/script.rb', line 56

def before!
  self.class.before.each{|block| block.call}
end

#help!Object



157
158
159
160
# File 'lib/senv/script.rb', line 157

def help!
  run_help
  abort
end

#init!(env, argv) ⇒ Object



40
41
42
43
44
45
46
47
48
# 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

#noopObject Also known as: noop?



448
449
450
# File 'lib/senv/script.rb', line 448

def noop
  ENV['SCRIPT_NOOP'] || ENV['NOOP']
end

#parse_command_line!Object



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
# 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) ⇒ Object



31
32
33
34
35
36
37
38
# 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_helpObject



153
154
155
# File 'lib/senv/script.rb', line 153

def run_help
  STDOUT.puts(@help)
end

#run_mode!Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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!Object



128
129
130
131
132
133
134
135
# 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

#uObject



418
419
420
# File 'lib/senv/script.rb', line 418

def u
  Util
end

#utilsObject



414
415
416
# File 'lib/senv/script.rb', line 414

def utils
  Util
end