Sha256: 5066fa06a81c968a2b94a6949bb8f49eb192155a980c03cb66b7e2d8471b09f3

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'ostruct'
require 'yaml'
require 'sprockets'
require 'pieces/compilers/route_compiler'
require 'pieces/server'
require 'pieces/tilt_extension'

module Pieces
  class Builder
    def self.build(config)
      new(config).build
    end

    attr_reader :path
    attr_reader :route_config

    def initialize(config)
      @path = config[:path]
      @route_config ||= YAML.load_file("#{path}/config/pieces.yml")
    end

    def build
      save_files(build_routes(build_styles))
    end

    private

    def env
      @env ||= Server.new(path: path).sprockets_env
    end

    def build_styles(files = {})
      files.merge('assets/pieces.css' => { type: 'css', contents: env['pieces.css'] })
    end

    def build_routes(files = {})
      route_compiler = RouteCompiler.new(path: path,
                                         globals: route_config['_global'],
                                         env: env)

      routes.reduce(files) do |files, (name, route)|
        route_compiler.compile(files, name, route)
      end
    end

    def routes
      route_config.reject { |key, _| key =~ /^_/ }
    end

    def save_files(files)
      Dir.chdir(path) do
        FileUtils.rm_rf('build')
        Dir.mkdir('build')

        files.each do |name, file|
          save_file(name, file)
        end
      end
    end

    def save_file(name, file)
      FileUtils.mkdir_p(File.dirname("build/#{name}"))
      File.open("build/#{name}", 'w') { |f| f.write(file[:contents]) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pieces-0.4.4 lib/pieces/builder.rb
pieces-0.4.3 lib/pieces/builder.rb