Sha256: 12f8882190f0fa07766d92fe8dbbdffb28718cc273508df8dd112646cc1e98b1

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'ostruct'
require 'yaml'

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")
      require 'pieces/tilt_extension'
    end

    def build
      save_files(_build_routes(_build_styles))
    end

    def build_styles
      save_files(_build_styles)
    end

    def build_routes
      save_files(_build_routes)
    end

    private

    def _build_styles(files = {})
      StyleCompiler.new(path: path).compile(files)
    end

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

      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.2 lib/pieces/builder.rb
pieces-0.4.1 lib/pieces/builder.rb