Sha256: 6050de5867a380f2b83e29515671490b0b885da61c0523075ccc117d8c6328ee

Contents?: true

Size: 991 Bytes

Versions: 1

Compression:

Stored size: 991 Bytes

Contents

# frozen_string_literal: true

module Umwelt::Tree
  class Imprint
    include Hanami::Interactor
    extend Forwardable
    def_delegators FileUtils, :mkpath

    expose :written_paths

    def initialize(trunk, location: nil)
      @trunk = trunk
      @location = location
      @written_paths = {}
    end

    def call(semantic_name)
      error! not_clean unless location_clean?

      @trunk.nodes.map do |node|
        write node.semantic(semantic_name)
      end
    end

    private

    def write(semantic)
      path = semantic.path(location: @location)

      mkpath path.dirname

      count = path.write(semantic.code)

      @written_paths[path] = count
    end

    def location_clean?
      mkpath imprint_root
      imprint_root.empty?
    end

    def not_clean
      <<~WARN_MESSAGE
        #{imprint_root} contain files.
        Try use another --target, or delete them.
      WARN_MESSAGE
    end

    def imprint_root
      Pathname.pwd / @location
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
umwelt-0.1.0 lib/umwelt/tree/imprint.rb