Sha256: 7fdc9a0208bbf8af74961c2ca0c8fbce1cb434e304e4f566ba360dcb24cbc7f2

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "dry/files"

module Hanami
  module CLI
    # @since 2.0.0
    # @api private
    class Files < Dry::Files
      # @since 2.0.0
      # @api private
      def initialize(out: $stdout, **args)
        super(**args)
        @out = out
      end

      # @since 2.0.0
      # @api private
      def write(path, *content)
        already_exists = exist?(path)

        super

        delete_keepfiles(path) unless already_exists

        if already_exists
          updated(path)
        else
          created(path)
        end
      end

      # @since 2.0.0
      # @api private
      def mkdir(path)
        unless exist?(path)
          super
          created(_path(path))
        end
      end

      # @since 2.0.0
      # @api private
      def chdir(path, &blk)
        within_folder(path)
        super
      end

      private

      attr_reader :out

      # Removes .keep files in any directories leading up to the given path.
      #
      # Does not attempt to remove `.keep` files in the following scenarios:
      #   - When the given path is a `.keep` file itself.
      #   - When the given path is absolute, since ascending up this path may lead to removal of
      #     files outside the Hanami project directory.
      def delete_keepfiles(path)
        path = Pathname(path)

        return if path.absolute?
        return if path.relative_path_from(path.dirname).to_s == ".keep"

        path.dirname.ascend do |part|
          keepfile = (part + ".keep").to_path
          delete(keepfile) if exist?(keepfile)
        end
      end

      def updated(path)
        out.puts "Updated #{path}"
      end

      def created(path)
        out.puts "Created #{path}"
      end

      def within_folder(path)
        out.puts "-> Within #{_path(path)}"
      end

      def _path(path)
        path + ::File::SEPARATOR
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hanami-cli-2.2.1 lib/hanami/cli/files.rb
hanami-cli-2.2.0 lib/hanami/cli/files.rb
hanami-cli-2.2.0.rc1 lib/hanami/cli/files.rb
hanami-cli-2.2.0.beta2 lib/hanami/cli/files.rb