Skip to content

Commit

Permalink
Add support for #github in javascript. Fixes bendyworks#29
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanAmos committed Nov 12, 2013
1 parent fbf71e7 commit e37791d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/bwoken/cli/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
28 changes: 18 additions & 10 deletions lib/bwoken/coffeescript.rb → lib/bwoken/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Bwoken
class Coffeescript
class Input
class ImportString

def initialize string
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]
]
Expand All @@ -20,31 +20,44 @@
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"]
]
end
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
Expand Down

0 comments on commit e37791d

Please sign in to comment.