Sha256: ae81d41608e51c4741e0feeea8a6fb82e034edfc5aaa1461e2957a08c8d7dcf1

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

require 'opal/compiler'
require '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 = {}
    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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opal-0.6.1 lib/opal/builder.rb
opal-0.6.0 lib/opal/builder.rb
opal-0.5.4 lib/opal/builder.rb
opal-0.5.2 lib/opal/builder.rb
opal-0.5.0 lib/opal/builder.rb