Class: Opal::CLI
Defined Under Namespace
Classes: MissingNodeJS, PathFinder
Class Attribute Summary collapse
-
.stdout ⇒ Object
Returns the value of attribute stdout.
Instance Attribute Summary collapse
-
#evals ⇒ Object
readonly
Returns the value of attribute evals.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#requires ⇒ Object
readonly
Returns the value of attribute requires.
Instance Method Summary collapse
-
#evals_source ⇒ Object
EVALS.
-
#initialize(options = nil) ⇒ CLI
constructor
A new instance of CLI.
- #map ⇒ Object
- #prepare_eval_code ⇒ Object
- #processor_options ⇒ Object
-
#puts(*args) ⇒ Object
OUTPUT.
- #run ⇒ Object
- #run_code ⇒ Object
- #run_with_node(code) ⇒ Object
- #server ⇒ Object
-
#set_processor_options ⇒ Object
PROCESSOR.
-
#sexp ⇒ Object
SOURCE.
- #show_compiled_source ⇒ Object
- #show_sexp ⇒ Object
- #source ⇒ Object
-
#sprockets ⇒ Object
SPROCKETS.
- #start_server ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ CLI
Returns a new instance of CLI
13 14 15 16 17 18 19 20 21 22 |
# File 'opal/lib/opal/cli.rb', line 13 def initialize = nil ||= {} @options = @evals = [:evals] || [] @requires = [:requires] || [] @filename = [:filename] @load_paths = [:load_paths] || [] @output = [:output] || self.class.stdout || $stdout raise ArgumentError if @evals.empty? and @filename.nil? end |
Class Attribute Details
.stdout ⇒ Object
Returns the value of attribute stdout
10 11 12 |
# File 'opal/lib/opal/cli.rb', line 10 def stdout @stdout end |
Instance Attribute Details
#evals ⇒ Object (readonly)
Returns the value of attribute evals
7 8 9 |
# File 'opal/lib/opal/cli.rb', line 7 def evals @evals end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename
6 7 8 |
# File 'opal/lib/opal/cli.rb', line 6 def filename @filename end |
#load_paths ⇒ Object (readonly)
Returns the value of attribute load_paths
7 8 9 |
# File 'opal/lib/opal/cli.rb', line 7 def load_paths @load_paths end |
#options ⇒ Object (readonly)
Returns the value of attribute options
6 7 8 |
# File 'opal/lib/opal/cli.rb', line 6 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output
7 8 9 |
# File 'opal/lib/opal/cli.rb', line 7 def output @output end |
#requires ⇒ Object (readonly)
Returns the value of attribute requires
7 8 9 |
# File 'opal/lib/opal/cli.rb', line 7 def requires @requires end |
Instance Method Details
#evals_source ⇒ Object
EVALS
180 181 182 |
# File 'opal/lib/opal/cli.rb', line 180 def evals_source evals.inject('', &:<<) end |
#map ⇒ Object
133 134 135 136 137 |
# File 'opal/lib/opal/cli.rb', line 133 def map compiler = Opal::Compiler.new compiler.compile(filename, ) compiler.source_map end |
#prepare_eval_code ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'opal/lib/opal/cli.rb', line 184 def prepare_eval_code if evals.any? require 'tmpdir' path = File.join(Dir.mktmpdir,"opal-#{$$}.js.rb") File.open(path, 'w') do |tempfile| load_paths << File.dirname(path) tempfile.puts 'require "opal"' tempfile.puts evals_source end @filename = File.basename(path) end end |
#processor_options ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'opal/lib/opal/cli.rb', line 143 def %w[ method_missing_enabled arity_check_enabled const_missing_enabled dynamic_require_severity source_map_enabled irb_enabled ] end |
#puts(*args) ⇒ Object
OUTPUT
173 174 175 |
# File 'opal/lib/opal/cli.rb', line 173 def puts(*args) output.puts(*args) end |
#run ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'opal/lib/opal/cli.rb', line 24 def run case when [:sexp]; prepare_eval_code; show_sexp when [:compile]; prepare_eval_code; show_compiled_source when [:server]; prepare_eval_code; start_server else run_code end end |
#run_code ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'opal/lib/opal/cli.rb', line 51 def run_code Opal.paths.concat load_paths path_finder = PathFinder.new(Opal.paths) builder = Opal::Builder.new full_source = builder.build('opal') require 'pathname' requires.each do |path| path = Pathname(path) path = Pathname(path_finder.find(path)) unless path.absolute? full_source << builder.build_str(path.read, :file => path.to_s) end evals.each_with_index do |code, index| full_source << builder.build_str(code, :file => "(eval #{index+1})") end file = Pathname(filename.to_s) full_source << builder.build_str(file.read, :file => file.to_s) if file.exist? run_with_node(full_source) end |
#run_with_node(code) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'opal/lib/opal/cli.rb', line 74 def run_with_node(code) require 'open3' begin stdin, stdout, stderr = Open3.popen3('node') rescue Errno::ENOENT raise MissingNodeJS, 'Please install Node.js to be able to run Opal scripts.' end stdin.write code stdin.close [stdout, stderr].each do |io| str = io.read puts str unless str.empty? end end |
#server ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'opal/lib/opal/cli.rb', line 161 def server @server ||= Opal::Server.new do |s| load_paths.each do |path| s.append_path path end s.main = File.basename(filename, '.rb') end end |
#set_processor_options ⇒ Object
PROCESSOR
125 126 127 128 129 130 131 |
# File 'opal/lib/opal/cli.rb', line 125 def .each do |option| key = option.to_sym next unless .has_key? key Opal::Processor.send("#{option}=", [key]) end end |
#sexp ⇒ Object
SOURCE
200 201 202 |
# File 'opal/lib/opal/cli.rb', line 200 def sexp Opal::Parser.new.parse(source) end |
#show_compiled_source ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'opal/lib/opal/cli.rb', line 107 def show_compiled_source if sprockets[filename] puts sprockets[filename].to_a.last elsif File.exist?(filename) puts Opal.compile File.read(filename), else puts Opal.compile(filename, ) end end |
#show_sexp ⇒ Object
117 118 119 |
# File 'opal/lib/opal/cli.rb', line 117 def show_sexp puts sexp.inspect end |
#source ⇒ Object
139 140 141 |
# File 'opal/lib/opal/cli.rb', line 139 def source File.exist?(filename) ? File.read(filename) : filename end |
#sprockets ⇒ Object
SPROCKETS
157 158 159 |
# File 'opal/lib/opal/cli.rb', line 157 def sprockets server.sprockets end |
#start_server ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'opal/lib/opal/cli.rb', line 94 def start_server require 'rack' require 'webrick' require 'logger' Rack::Server.start( :app => server, :Port => [:port] || 3000, :AccessLog => [], :Logger => Logger.new($stdout) ) end |