Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 317dd061c4c6
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4d492f0666e8
Choose a head ref
  • 7 commits
  • 7 files changed
  • 1 contributor

Commits on Apr 10, 2014

  1. Typo

    elia committed Apr 10, 2014
    Copy the full SHA
    39a7535 View commit details

Commits on Apr 11, 2014

  1. Copy the full SHA
    0a4f67e View commit details
  2. Copy the full SHA
    c96fa34 View commit details
  3. Copy the full SHA
    fa5113d View commit details
  4. Copy the full SHA
    4cb8b65 View commit details
  5. Copy the full SHA
    ad9bf56 View commit details

Commits on Apr 14, 2014

  1. Copy the full SHA
    4d492f0 View commit details
Showing with 180 additions and 171 deletions.
  1. +63 −62 lib/opal/builder.rb
  2. +2 −2 lib/opal/cli.rb
  3. +0 −99 lib/opal/new_builder.rb
  4. +98 −0 lib/opal/old_builder.rb
  5. +8 −3 lib/opal/sprockets/processor.rb
  6. +9 −4 spec/cli/{new_builder_spec.rb → builder_spec.rb}
  7. +0 −1 stdlib/opal-parser.rb
125 changes: 63 additions & 62 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -1,98 +1,99 @@
require 'opal/compiler'
require 'erb'
require 'opal/path_reader'
require 'opal/erb'

module Opal
class Builder

BUILDERS = { ".rb" => :build_ruby, ".js" => :build_js, ".erb" => :build_erb }

def self.build(name)
Builder.new.build name
end

def initialize(options = {})
@paths = options.delete(:paths) || Opal.paths.clone
@options = options
@handled = {}
@compiler_options = options.delete(:compiler_options) || {}
@stubbed_files = options.delete(:stubbed_files) || []
@path_reader = options.delete(:path_reader) || PathReader.new
@compiler_class = options.delete(:compiler_class) || CompilerWrapper
@erb_compiler_class = options.delete(:erb_compiler_class) || Opal::ERB::Compiler
raise ArgumentError, "unknown options: #{options.keys.join(', ')}" unless options.empty?
end

def append_path(path)
@paths << path
def build(path, prerequired = [])
source = path_reader.read(path)
build_str(source, path, prerequired)
end

def build(path)
@segments = []
def build_str(source, path = '(file)', prerequired = [])
compiler = compiler_for(source, :file => path)
sources = []
compiled_requires = {}
prerequired.each {|pr| compiled_requires[pr] = true}

require_asset path
compiler.requires.uniq.each { |r| compile_require(r, sources, compiled_requires) }

@segments.join
sources << compiler.compiled
prerequired.concat(compiled_requires.keys)
sources.join("\n")
end

def build_str(str, options = {})
@segments = []
@segments << compile_ruby(str, options)
@segments.join
end

def require_asset(path)
location = find_asset path
private

unless @handled[location]
@handled[location] = true
build_asset location
end
def javascript? path
path.end_with?('.js')
end

def stubbed? file
stubbed_files.include? file
end

def find_asset(path)
path.untaint if path =~ /\A(\w[-.\w]*\/?)+\Z/
file_types = %w[.rb .js .js.erb]
def erb? path
path.end_with?('.opalerb')
end

@paths.each do |root|
file_types.each do |type|
test = File.join root, "#{path}#{type}"
def compile_require r, sources, compiled_requires
return if compiled_requires.has_key?(r)
compiled_requires[r] = true
require_source = stubbed?(r) ? '' : path_reader.read(r)

if File.exist? test
return test
end
end
if javascript?(r)
sources << require_source
require_source = ''
end

raise "Could not find asset: #{path}"
require_source = prepare_erb(require_source, r) if erb?(r)
require_compiler = compiler_for(require_source, :file => r, :requirable => true)
require_compiler.requires.each { |r| compile_require(r, sources, compiled_requires) }
sources << require_compiler.compiled
end

def build_asset(path)
ext = File.extname path

unless builder = BUILDERS[ext]
raise "Unknown builder for #{ext}"
end
def prepare_erb(source, path)
erb_compiler = erb_compiler_class.new(source, path)
erb_compiler.prepared_source
end

@segments << __send__(builder, path)
def compiler_for(source, options = {})
compiler_class.new(source, compiler_options.merge(options))
end

def compile_ruby(str, options = nil)
options ||= @options.clone
attr_reader :compiler_class, :path_reader, :compiler_options, :stubbed_files,
:erb_compiler_class

compiler = Compiler.new
result = compiler.compile str, options
class CompilerWrapper
def initialize(source, options)
@source, @options = source, options
end

compiler.requires.each do |r|
require_asset r
def compiled
@compiled ||= compiler.compile(source, options)
end

result
end
def requires
@requires ||= compiled && compiler.requires
end

def build_ruby(path)
compile_ruby File.read(path), @options.clone
end
private

def build_js(path)
File.read(path)
end
def compiler
@compiler ||= Compiler.new
end

def build_erb(path)
::ERB.new(File.read(path)).result binding
attr_reader :source, :options
end
end
end
4 changes: 2 additions & 2 deletions lib/opal/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'opal'
require 'rack'
require 'opal/new_builder'
require 'opal/builder'

module Opal
class CLI
@@ -46,7 +46,7 @@ def run_code
def compiled_source include_opal = true
Opal.paths.concat load_paths

builder = Opal::NewBuilder.new
builder = Opal::Builder.new
_requires = []
full_source = []

99 changes: 0 additions & 99 deletions lib/opal/new_builder.rb

This file was deleted.

98 changes: 98 additions & 0 deletions lib/opal/old_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'opal/compiler'
require 'erb'

module Opal
class OldBuilder

BUILDERS = { ".rb" => :build_ruby, ".js" => :build_js, ".erb" => :build_erb }

def self.build(*args)
new.build(*args)
end

def initialize(options = {})
@paths = options.delete(:paths) || Opal.paths.clone
@options = options
@handled = {}
end

def append_path(path)
@paths << path
end

def build(path)
@segments = []

require_asset path

@segments.join
end

def build_str(str, options = {})
@segments = []
@segments << compile_ruby(str, options)
@segments.join
end

def require_asset(path)
location = find_asset path

unless @handled[location]
@handled[location] = true
build_asset location
end
end

def find_asset(path)
path.untaint if path =~ /\A(\w[-.\w]*\/?)+\Z/
file_types = %w[.rb .js .js.erb]

@paths.each do |root|
file_types.each do |type|
test = File.join root, "#{path}#{type}"

if File.exist? test
return test
end
end
end

raise "Could not find asset: #{path}"
end

def build_asset(path)
ext = File.extname path

unless builder = BUILDERS[ext]
raise "Unknown builder for #{ext}"
end

@segments << __send__(builder, path)
end

def compile_ruby(str, options = nil)
options ||= @options.clone

compiler = Compiler.new
result = compiler.compile str, options

compiler.requires.each do |r|
require_asset r
end

result
end

def build_ruby(path)
compile_ruby File.read(path), @options.clone
end

def build_js(path)
File.read(path)
end

def build_erb(path)
::ERB.new(File.read(path)).result binding
end
end
end
11 changes: 8 additions & 3 deletions lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'set'
require 'sprockets'
require 'opal/version'
require 'opal/new_builder'
require 'opal/builder'

$OPAL_SOURCE_MAPS = {}

@@ -92,11 +92,16 @@ def evaluate(context, locals, &block)
path = context.logical_path
prerequired = []

builder = NewBuilder.new({:compiler_options => options, :stubbed_files => stubbed_files}, SprocketsPathReader.new(context.environment))
builder = Builder.new(
:compiler_options => options,
:stubbed_files => stubbed_files,
:path_reader => SprocketsPathReader.new(context.environment)
)
result = builder.build_str(data, path, prerequired)

# prerequired is mutated by the builder
prerequired.uniq.each { |r| context.depend_on path }
dependencies = prerequired.uniq - stubbed_files.to_a
dependencies.each { |asset| context.depend_on(asset) }

if self.class.source_map_enabled
$OPAL_SOURCE_MAPS[context.pathname] = '' #compiler.source_map(source_file_url(context)).to_s
13 changes: 9 additions & 4 deletions spec/cli/new_builder_spec.rb → spec/cli/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require 'cli/spec_helper'
require 'opal/new_builder'
require 'opal/builder'
require 'cli/shared/path_reader_shared'

describe Opal::NewBuilder do
subject(:builder) { described_class.new(options, path_reader, compiler_class, erb_compiler_class) }
describe Opal::Builder do
subject(:builder) { described_class.new(options) }

let(:options) { Hash.new }
let(:options) { {
:path_reader => path_reader,
:compiler_class => compiler_class,
:erb_compiler_class => erb_compiler_class,
} }

let(:path_reader) { double('path reader') }
let(:compiler_class) { double('compiler_class') }
let(:erb_compiler_class) { double('erb_compiler_class') }
1 change: 0 additions & 1 deletion stdlib/opal-parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'opal/compiler'
require 'opal/builder'
require 'opal/erb'
require 'opal/version'