Class: Opal::CLI
- Inherits:
-
Object
- Object
- Opal::CLI
- Defined in:
- lib/opal/cli.rb
Defined Under Namespace
Classes: Evals
Class Attribute Summary collapse
-
.stdout ⇒ Object
Returns the value of attribute stdout.
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#compiler_options ⇒ Object
readonly
Returns the value of attribute compiler_options.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#evals ⇒ Object
readonly
Returns the value of attribute evals.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#gems ⇒ Object
readonly
Returns the value of attribute gems.
-
#lib_only ⇒ Object
readonly
Returns the value of attribute lib_only.
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
-
#missing_require_severity ⇒ Object
readonly
Returns the value of attribute missing_require_severity.
-
#no_cache ⇒ Object
readonly
Returns the value of attribute no_cache.
-
#no_exit ⇒ Object
readonly
Returns the value of attribute no_exit.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#preload ⇒ Object
readonly
Returns the value of attribute preload.
-
#rbrequires ⇒ Object
readonly
Returns the value of attribute rbrequires.
-
#requires ⇒ Object
readonly
Returns the value of attribute requires.
-
#runner_options ⇒ Object
readonly
Returns the value of attribute runner_options.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stubs ⇒ Object
readonly
Returns the value of attribute stubs.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #compiler_option_names ⇒ Object
- #create_builder ⇒ Object
- #debug_source_map ⇒ Object
-
#evals_or_file_source ⇒ Object
Internal: Yields a string of source code and the proper filename for either evals, stdin or a filepath.
-
#initialize(options = nil) ⇒ CLI
constructor
A new instance of CLI.
- #run ⇒ Object
- #run_repl ⇒ Object
- #runner ⇒ Object
- #show_sexp ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ CLI
Returns a new instance of CLI.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 |
# File 'lib/opal/cli.rb', line 25 def initialize( = nil) ||= {} # Runner @runner_type = .delete(:runner) || :nodejs @runner_options = .delete(:runner_options) || {} @options = @sexp = .delete(:sexp) @repl = .delete(:repl) @no_exit = .delete(:no_exit) @lib_only = .delete(:lib_only) @argv = .delete(:argv) { [] } @evals = .delete(:evals) { [] } @load_paths = .delete(:load_paths) { [] } @gems = .delete(:gems) { [] } @stubs = .delete(:stubs) { [] } @preload = .delete(:preload) { [] } @output = .delete(:output) { self.class.stdout || $stdout } @verbose = .delete(:verbose) { false } @debug = .delete(:debug) { false } @requires = .delete(:requires) { [] } @rbrequires = .delete(:rbrequires) { [] } @no_cache = .delete(:no_cache) { false } @stdin = .delete(:stdin) { $stdin } @debug_source_map = .delete(:debug_source_map) { false } @missing_require_severity = .delete(:missing_require_severity) { Opal::Config.missing_require_severity } @requires.unshift('opal') unless .delete(:skip_opal_require) @compiler_options = compiler_option_names.map do |option| key = option.to_sym next unless .key? key value = .delete(key) [key, value] end.compact.to_h if @lib_only raise ArgumentError, 'no libraries to compile' if @requires.empty? raise ArgumentError, "can't accept evals, file, or extra arguments in `library only` mode" if @argv.any? || @evals.any? elsif @evals.any? @filename = '-e' @file = Evals.new(@evals.join("\n")) elsif @argv.first && @argv.first != '-' @filename = @argv.shift @file = File.open(@filename) else @filename = @argv.shift || '-' @file = @stdin end raise ArgumentError, "unknown options: #{.inspect}" unless @options.empty? end |
Class Attribute Details
.stdout ⇒ Object
Returns the value of attribute stdout.
16 17 18 |
# File 'lib/opal/cli.rb', line 16 def stdout @stdout end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def argv @argv end |
#compiler_options ⇒ Object (readonly)
Returns the value of attribute compiler_options.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def @compiler_options end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def debug @debug end |
#evals ⇒ Object (readonly)
Returns the value of attribute evals.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def evals @evals end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status.
116 117 118 |
# File 'lib/opal/cli.rb', line 116 def exit_status @exit_status end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def file @file end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def filename @filename end |
#gems ⇒ Object (readonly)
Returns the value of attribute gems.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def gems @gems end |
#lib_only ⇒ Object (readonly)
Returns the value of attribute lib_only.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def lib_only @lib_only end |
#load_paths ⇒ Object (readonly)
Returns the value of attribute load_paths.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def load_paths @load_paths end |
#missing_require_severity ⇒ Object (readonly)
Returns the value of attribute missing_require_severity.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def missing_require_severity @missing_require_severity end |
#no_cache ⇒ Object (readonly)
Returns the value of attribute no_cache.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def no_cache @no_cache end |
#no_exit ⇒ Object (readonly)
Returns the value of attribute no_exit.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def no_exit @no_exit end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def output @output end |
#preload ⇒ Object (readonly)
Returns the value of attribute preload.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def preload @preload end |
#rbrequires ⇒ Object (readonly)
Returns the value of attribute rbrequires.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def rbrequires @rbrequires end |
#requires ⇒ Object (readonly)
Returns the value of attribute requires.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def requires @requires end |
#runner_options ⇒ Object (readonly)
Returns the value of attribute runner_options.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def @runner_options end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def stdin @stdin end |
#stubs ⇒ Object (readonly)
Returns the value of attribute stubs.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def stubs @stubs end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
10 11 12 |
# File 'lib/opal/cli.rb', line 10 def verbose @verbose end |
Instance Method Details
#compiler_option_names ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/opal/cli.rb', line 181 def compiler_option_names %w[ method_missing arity_check dynamic_require_severity source_map_enabled irb_enabled inline_operators enable_source_location enable_file_source_embed use_strict parse_comments esm ] end |
#create_builder ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/opal/cli.rb', line 118 def create_builder builder = Opal::Builder.new( stubs: stubs, compiler_options: , missing_require_severity: missing_require_severity, ) # --no-cache builder.cache = Opal::Cache::NullCache.new if no_cache # --include builder.append_paths(*load_paths) # --gem gems.each { |gem_name| builder.use_gem gem_name } # --require requires.each { |required| builder.build(required, requirable: true, load: true) } # --preload preload.each { |path| builder.build_require(path) } # --verbose builder.build_str '$VERBOSE = true', '(flags)', no_export: true if verbose # --debug builder.build_str '$DEBUG = true', '(flags)', no_export: true if debug # --eval / stdin / file source = evals_or_file_source builder.build_str(source, filename) if source # --no-exit builder.build_str '::Kernel.exit', '(exit)', no_export: true unless no_exit builder end |
#debug_source_map ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/opal/cli.rb', line 165 def debug_source_map source = evals_or_file_source or return # rubocop:disable Style/AndOr compiler = Opal::Compiler.new(source, file: filename, **) compiler.compile b64 = [ compiler.result, compiler.source_map.to_json, evals_or_file_source, ].map { |i| Base64.strict_encode64(i) }.join(',') output.puts "https://sokra.github.io/source-map-visualization/#base64,#{b64}" end |
#evals_or_file_source ⇒ Object
Internal: Yields a string of source code and the proper filename for either evals, stdin or a filepath.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/opal/cli.rb', line 199 def evals_or_file_source return if lib_only # --library return @cached_content if @cached_content unless file.tty? begin file.rewind can_read_again = true rescue Errno::ESPIPE # rubocop:disable Lint/HandleExceptions # noop end end if @cached_content.nil? || can_read_again # On MacOS file.read is not enough to pick up changes, probably due to some # cache or buffer, unclear if coming from ruby or the OS. content = File.file?(file) ? File.read(file) : file.read end @cached_content ||= content unless can_read_again content end |
#run ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/opal/cli.rb', line 81 def run return show_sexp if @sexp return debug_source_map if @debug_source_map return run_repl if @repl rbrequires.each { |file| require file } runner = self.runner # Some runners may need to use a dynamic builder, that is, # a builder that will try to build the entire package every time # a page is loaded - for example a Server runner that needs to # rerun if files are changed. builder = proc { create_builder } @exit_status = runner.call( options: , output: output, argv: argv, builder: builder, ) end |
#run_repl ⇒ Object
109 110 111 112 113 114 |
# File 'lib/opal/cli.rb', line 109 def run_repl require 'opal/repl' repl = REPL.new repl.run(argv) end |
#runner ⇒ Object
104 105 106 107 |
# File 'lib/opal/cli.rb', line 104 def runner CliRunners[@runner_type] || raise(ArgumentError, "unknown runner: #{@runner_type.inspect}") end |
#show_sexp ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/opal/cli.rb', line 156 def show_sexp source = evals_or_file_source or return # rubocop:disable Style/AndOr buffer = ::Opal::Parser::SourceBuffer.new(filename) buffer.source = source sexp = Opal::Parser.default_parser.parse(buffer) output.puts sexp.inspect end |