Sha256: 4a3c36e735e3b47b49f2fee8994e487fc65f19ae584404021caefef686836f87

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Staticky
  class Builder
    include Dry::Events::Publisher[:builder]
    include Deps[:files, :router]

    register_event("started")
    register_event("finished")
    register_event("before_resource")
    register_event("after_resource")

    def self.call(...) = new(...).call

    def on(event_type, &block) = subscribe(event_type, &block)

    def call
      publish("started")
      copy_public_files
      build_site
      publish("finished")
    end

    private

    def build_site
      @router
        .resources
        .each do |resource|
          publish("before_resource", resource:)
          compile output_path(resource.filepath), resource.build
          publish("after_resource", resource:)
        end
    end

    def copy_public_files
      public_folder = Staticky.root_path.join("public")
      return unless @files.exist? public_folder

      @files.children(public_folder).each do |file|
        # file => "favicon.ico"
        copy(public_path(file), output_path(file))
      end
    end

    def compile(destination, content)
      @files.write destination, content
    end

    def copy(source, destination)
      @files.cp source, destination
    end

    def public_path(path)
      Staticky.root_path.join("public", path)
    end

    def output_path(path)
      Staticky.build_path.join(path)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
staticky-0.1.1 lib/staticky/builder.rb
staticky-0.1.0 lib/staticky/builder.rb