Skip to content

Commit

Permalink
Resolves opal#1281, do not generate source block when module option i…
Browse files Browse the repository at this point in the history
…s specified
  • Loading branch information
ggrossetie committed Jan 10, 2016
1 parent 633a409 commit a2eb6c1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/opal/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Opal
class CLI
attr_reader :options, :file, :compiler_options, :evals, :load_paths, :argv,
:output, :requires, :gems, :stubs, :verbose, :port, :preload,
:filename, :debug, :no_exit
:filename, :debug, :no_exit, :module_only

def compile?
@compile
Expand All @@ -32,22 +32,23 @@ def initialize options = nil
@runner_type = options.delete(:runner) || :nodejs
@port = options.delete(:port) || 3000

@options = options
@compile = !!options.delete(:compile)
@sexp = options.delete(:sexp)
@file = options.delete(:file)
@no_exit = options.delete(:no_exit)
@argv = options.delete(:argv) || []
@evals = options.delete(:evals) || []
@requires = options.delete(:requires) || []
@load_paths = options.delete(:load_paths) || []
@gems = options.delete(:gems) || []
@stubs = options.delete(:stubs) || []
@preload = options.delete(:preload) || []
@output = options.delete(:output) || self.class.stdout || $stdout
@verbose = options.fetch(:verbose, false); options.delete(:verbose)
@debug = options.fetch(:debug, false); options.delete(:debug)
@filename = options.fetch(:filename) { @file && @file.path }; options.delete(:filename)
@options = options
@compile = !!options.delete(:compile)
@sexp = options.delete(:sexp)
@file = options.delete(:file)
@no_exit = options.delete(:no_exit)
@module_only = options.delete(:module_only)
@argv = options.delete(:argv) || []
@evals = options.delete(:evals) || []
@requires = options.delete(:requires) || []
@load_paths = options.delete(:load_paths) || []
@gems = options.delete(:gems) || []
@stubs = options.delete(:stubs) || []
@preload = options.delete(:preload) || []
@output = options.delete(:output) || self.class.stdout || $stdout
@verbose = options.fetch(:verbose, false); options.delete(:verbose)
@debug = options.fetch(:debug, false); options.delete(:debug)
@filename = options.fetch(:filename) { @file && @file.path }; options.delete(:filename)
@skip_opal_require = options.delete(:skip_opal_require)
@compiler_options = Hash[
*compiler_option_names.map do |option|
Expand Down Expand Up @@ -104,8 +105,10 @@ def build
builder.build(local_require)
end

evals_or_file do |contents, filename|
builder.build_str(contents, filename)
unless module_only
evals_or_file do |contents, filename|
builder.build_str(contents, filename)
end
end

builder.build_str 'Kernel.exit', '(exit)' unless no_exit
Expand Down
3 changes: 3 additions & 0 deletions lib/opal/cli_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def initialize
options[:no_exit] = true
end

on('-M', '--module', 'Generate only the code of module. Omit [programfile]') do |no_exit|
options[:module_only] = true
end

section 'Compilation Options:'

Expand Down
16 changes: 16 additions & 0 deletions spec/lib/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@
end
end

describe ':module_only option' do
context 'when false' do
let(:options) { {module_only: false, compile: true, evals: [''], skip_opal_require: true, no_exit: true} }
it 'appends an empty code block at the end of the source' do
expect_output_of{ subject.run }.to include("function(Opal)")
end
end

context 'when true' do
let(:options) { {module_only: true, compile: true, evals: [''], skip_opal_require: true, no_exit: true} }
it 'does not append code block at the end of the source' do
expect_output_of{ subject.run }.to eq("\n")
end
end
end

describe ':requires options' do
context 'with an absolute path' do
let(:options) { {:requires => [file], :evals => ['']} }
Expand Down

0 comments on commit a2eb6c1

Please sign in to comment.