Sha256: 5ceaede22e6c76a68961ae6d54b923c462c4b3cb7bfc3d2c61117bdb263565d5

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

# typed: strict
# frozen_string_literal: true

require "erb"

module Packwerk
  module Generators
    class ConfigurationFile
      extend T::Sig

      CONFIGURATION_TEMPLATE_FILE_PATH = "templates/packwerk.yml.erb"

      class << self
        extend T::Sig

        sig { params(root: String, out: T.any(IO, StringIO)).returns(T::Boolean) }
        def generate(root:, out:)
          new(root: root, out: out).generate
        end
      end

      sig { params(root: String, out: T.any(StringIO, IO)).void }
      def initialize(root:, out: $stdout)
        @root = root
        @out = out
      end

      sig { returns(T::Boolean) }
      def generate
        @out.puts("📦 Generating Packwerk configuration file...")
        default_config_path = File.join(@root, Configuration::DEFAULT_CONFIG_PATH)

        if File.exist?(default_config_path)
          @out.puts("⚠️  Packwerk configuration file already exists.")
          return true
        end

        File.write(default_config_path, render)

        @out.puts("✅ Packwerk configuration file generated in #{default_config_path}")
        true
      end

      private

      sig { returns(String) }
      def render
        ERB.new(template, trim_mode: "-").result(binding)
      end

      sig { returns(String) }
      def template
        template_file_path = File.join(__dir__, CONFIGURATION_TEMPLATE_FILE_PATH)
        File.read(template_file_path)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
packwerk-3.2.2 lib/packwerk/generators/configuration_file.rb
packwerk-3.2.1 lib/packwerk/generators/configuration_file.rb
packwerk-3.2.0 lib/packwerk/generators/configuration_file.rb
packwerk-3.1.0 lib/packwerk/generators/configuration_file.rb
packwerk-3.0.1 lib/packwerk/generators/configuration_file.rb
packwerk-3.0.0 lib/packwerk/generators/configuration_file.rb