diff --git a/lib/bwoken/cli/test.rb b/lib/bwoken/cli/test.rb index 78f1e5e..58e80e3 100644 --- a/lib/bwoken/cli/test.rb +++ b/lib/bwoken/cli/test.rb @@ -4,12 +4,12 @@ require 'bwoken' require 'bwoken/build' -require 'bwoken/coffeescript' require 'bwoken/device' #TODO: make formatters dynamically loadable during runtime require 'bwoken/formatter' require 'bwoken/formatters/passthru_formatter' require 'bwoken/formatters/colorful_formatter' +require 'bwoken/input' require 'bwoken/script_runner' module Bwoken @@ -91,13 +91,13 @@ def transpile compiled_coffee.zip(coffeescripts).each do |target, source| containing_dir = target.pathmap('%d') ensure_directory containing_dir - Bwoken::Coffeescript.compile source, target + Bwoken::Input.process source, target end copied_javascripts.zip(javascripts).each do |target, source| containing_dir = target.pathmap('%d') ensure_directory containing_dir - FileUtils.cp source, target + Bwoken::Input.process source, target end end diff --git a/lib/bwoken/coffeescript.rb b/lib/bwoken/input.rb similarity index 64% rename from lib/bwoken/coffeescript.rb rename to lib/bwoken/input.rb index a7743a0..cc28eab 100644 --- a/lib/bwoken/coffeescript.rb +++ b/lib/bwoken/input.rb @@ -3,13 +3,17 @@ require 'json' require 'execjs' -require File.expand_path('../coffeescript/import_string', __FILE__) -require File.expand_path('../coffeescript/github_import_string', __FILE__) +require File.expand_path('../input/import_string', __FILE__) +require File.expand_path('../input/github_import_string', __FILE__) module Bwoken - class Coffeescript + class Input class << self + def coffee_script? source + source.downcase.end_with?('.coffee') + end + def coffee_script_source return @coffeescript if @coffeescript @@ -24,21 +28,25 @@ def context @context ||= ExecJS.compile(coffee_script_source) end - def precompile coffeescript - coffeescript.lines.partition {|line| line =~ /^#(?:github|import) .*$/} + def preprocess script + script.lines.partition {|line| line =~ /^#(?:github|import) .*$/} end - def compile source, target - githubs_and_imports, sans_imports = precompile(IO.read source) + def process source, target + githubs_and_imports, sans_imports = preprocess(IO.read source) - javascript = coffeescript_to_javascript sans_imports.join + javascript = compile_to_javascript(source, sans_imports.join) import_strings = githubs_to_imports(githubs_and_imports) write import_strings, javascript, :to => target end - def coffeescript_to_javascript coffee - self.context.call 'CoffeeScript.compile', coffee, :bare => true + def compile_to_javascript source, script + if coffee_script? source + self.context.call 'CoffeeScript.compile', script, :bare => true + else + script + end end def githubs_to_imports strings diff --git a/lib/bwoken/coffeescript/github_import_string.rb b/lib/bwoken/input/github_import_string.rb similarity index 98% rename from lib/bwoken/coffeescript/github_import_string.rb rename to lib/bwoken/input/github_import_string.rb index 369b4b3..034bffd 100644 --- a/lib/bwoken/coffeescript/github_import_string.rb +++ b/lib/bwoken/input/github_import_string.rb @@ -2,7 +2,7 @@ require File.expand_path('../import_string', __FILE__) module Bwoken - class Coffeescript + class Input class GithubImportString < ImportString attr_reader :repo_name, :file_path diff --git a/lib/bwoken/coffeescript/import_string.rb b/lib/bwoken/input/import_string.rb similarity index 89% rename from lib/bwoken/coffeescript/import_string.rb rename to lib/bwoken/input/import_string.rb index 2b6c48c..db994e2 100644 --- a/lib/bwoken/coffeescript/import_string.rb +++ b/lib/bwoken/input/import_string.rb @@ -1,5 +1,5 @@ module Bwoken - class Coffeescript + class Input class ImportString def initialize string diff --git a/spec/lib/bwoken/coffeescript/github_import_string_spec.rb b/spec/lib/bwoken/input/github_import_string_spec.rb similarity index 95% rename from spec/lib/bwoken/coffeescript/github_import_string_spec.rb rename to spec/lib/bwoken/input/github_import_string_spec.rb index dadcf95..cb12658 100644 --- a/spec/lib/bwoken/coffeescript/github_import_string_spec.rb +++ b/spec/lib/bwoken/input/github_import_string_spec.rb @@ -1,8 +1,8 @@ -require 'bwoken/coffeescript/github_import_string' +require 'bwoken/input/github_import_string' -describe Bwoken::Coffeescript::GithubImportString do +describe Bwoken::Input::GithubImportString do let(:string) { '#github alexvollmer/tuneup_js/tuneup.js' } - subject { Bwoken::Coffeescript::GithubImportString.new(string) } + subject { Bwoken::Input::GithubImportString.new(string) } describe '#parse' do it 'ensures the github repo is pulled' do diff --git a/spec/lib/bwoken/coffeescript/import_string_spec.rb b/spec/lib/bwoken/input/import_string_spec.rb similarity index 61% rename from spec/lib/bwoken/coffeescript/import_string_spec.rb rename to spec/lib/bwoken/input/import_string_spec.rb index 3a97026..ebb43c6 100644 --- a/spec/lib/bwoken/coffeescript/import_string_spec.rb +++ b/spec/lib/bwoken/input/import_string_spec.rb @@ -1,8 +1,8 @@ -require 'bwoken/coffeescript/import_string' +require 'bwoken/input/import_string' -describe Bwoken::Coffeescript::ImportString do +describe Bwoken::Input::ImportString do let(:string) { '#import foo.js' } - subject { Bwoken::Coffeescript::ImportString.new(string) } + subject { Bwoken::Input::ImportString.new(string) } describe '#parse' do it 'does not affect @string' do diff --git a/spec/lib/bwoken/coffeescript_spec.rb b/spec/lib/bwoken/input_spec.rb similarity index 61% rename from spec/lib/bwoken/coffeescript_spec.rb rename to spec/lib/bwoken/input_spec.rb index 3e28fd2..03b67a5 100644 --- a/spec/lib/bwoken/coffeescript_spec.rb +++ b/spec/lib/bwoken/input_spec.rb @@ -1,16 +1,16 @@ -require 'bwoken/coffeescript' +require 'bwoken/input' require 'stringio' require 'spec_helper' -describe Bwoken::Coffeescript do - let(:subject) { Bwoken::Coffeescript } +describe Bwoken::Input do + let(:subject) { Bwoken::Input } describe '.precompile' do describe '"#import"' do let(:test_coffee) {"foo = 1\n#import bazzle.js\nbar = 2"} it 'splits #import statements from other statements' do - subject.precompile(test_coffee).should == [ + subject.preprocess(test_coffee).should == [ ["#import bazzle.js\n"], ["foo = 1\n", "bar = 2"] ] @@ -20,7 +20,7 @@ describe '"#github"' do let(:test_coffee) {"#github alexvollmer/tuneup_js\n#import bazzle.js\nfoo = 1\nbar = 2"} it 'converts github to import' do - subject.precompile(test_coffee).should == [ + subject.preprocess(test_coffee).should == [ ["#github alexvollmer/tuneup_js\n", "#import bazzle.js\n"], ["foo = 1\n", "bar = 2"] ] @@ -28,23 +28,36 @@ end end - describe '.compile' do + describe '.coffee_script?' do + context 'is coffee script' do + it 'is true' do + subject.coffee_script?("foo/bar.coffee").should be_true + end + end + context 'is javascript' do + it 'is false' do + subject.coffee_script?("foo/bar.js").should be_false + end + end + end + + describe '.process' do before do - subject.stub(:precompile => [[], []]) + subject.stub(:preprocess => [[], []]) IO.stub(:read) subject.stub(:write) - subject.stub(:coffeescript_to_javascript) + subject.stub(:compile_to_javascript) subject.stub(:githubs_to_imports) end - after { subject.compile 'a', 'b' } + after { subject.process 'a', 'b' } - it 'precompiles' do - subject.should_receive(:precompile) + it 'preprocesses' do + subject.should_receive(:preprocess) end - it 'cofffeescript-compiles' do - subject.should_receive(:coffeescript_to_javascript) + it 'script-compiles' do + subject.should_receive(:compile_to_javascript) end it 'resolves github imports' do