Class: Spade::CLI::Base

Inherits:
Thor
  • Object
show all
Defined in:
lib/spade/cli/base.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object (private)



171
172
173
174
175
176
177
178
# File 'lib/spade/cli/base.rb', line 171

def method_missing(meth, *)
  if File.exist?(meth.to_s)
    ARGV.unshift("exec")
    invoke :exec
  else
    super
  end
end

Instance Method Details

#consoleObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spade/cli/base.rb', line 22

def console
  require 'readline'

  shell = Spade::Shell.new

  context do |ctx|
    shell.inject(ctx)
    puts "help() for help. quit() to quit."
    puts "Spade #{Spade::VERSION} (V8 #{V8::VERSION})"
    puts "WORKING=#{options[:working]}" if options[:verbose]

    trap("SIGINT") { puts "^C" }
    repl ctx
  end
end

#execObject



40
41
42
43
44
45
46
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
# File 'lib/spade/cli/base.rb', line 40

def exec(*)
  exec_args = ARGV.dup
  arg = exec_args.shift while arg != "exec" && !exec_args.empty?

  filename = exec_args.shift
  puts "Filename: #{filename}" if options[:verbose]

  if filename
    puts "Working: #{options[:working]}" if options[:verbose]
    filename = File.expand_path filename, Dir.pwd
    throw "#{filename} not found" unless File.exists?(filename)
    fp      = File.open filename
    source  = File.basename filename
    rootdir = Spade.discover_root filename

    caller_id = nil
    if rootdir
      json_path = File.join(rootdir, 'package.json')
      if File.exist?(json_path)
        package_json = JSON.parse(File.read(json_path))
        caller_id = "#{package_json['name']}/main"
      end
    end

    # peek at first line.  If it is poundhash, skip. else rewind file
    unless fp.readline =~ /^\#\!/
      fp.rewind
    end

    # Can't set pos on STDIN so we can only do this for files
    if options[:verbose]
      pos = fp.pos
      puts fp.read
      fp.pos = pos
    end
  else
    fp = $stdin
    source = '<stdin>'
    rootdir = options[:working]
    caller_id = nil
  end

  if options[:verbose]
    puts "source: #{source}"
    puts "rootdir: #{rootdir}"
    puts "caller_id: #{caller_id}"
  end

  begin
    # allow for poundhash
    context(:argv => exec_args, :rootdir => rootdir, :caller_id => caller_id) do |ctx|
      ctx.eval(fp, source) # eval the rest
    end
  rescue Interrupt => e
    puts; exit
  end
end

#previewObject



110
111
112
113
114
# File 'lib/spade/cli/base.rb', line 110

def preview
  require 'spade/server'
  trap("SIGINT") { Spade::Server.shutdown }
  Spade::Server.run(options[:working], options[:port]);
end

#updateObject



117
118
119
# File 'lib/spade/cli/base.rb', line 117

def update
  Spade::Bundle.update(options[:working], :verbose => options[:verbose])
end