Sha256: 9178a2a623add81baba1e4d94827b2b8c788b36c72bd2b478527b399812c6540

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 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]

      unless File.exists?("#{path}/config/pieces.yml")
        puts "We could not find pieces.yml in #{path}/config/"
        puts 'Sorry about that!'
        exit(1)
      end

      @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

1 entries across 1 versions & 1 rubygems

Version Path
pieces-0.4.5 lib/pieces/builder.rb