Sha256: 33f5d6513c4bdaae9967d3821b884683d13c8d0068b2dd9c868f71ed196104f5

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "fileutils"
require_relative "logging"

module Katagen
  module Generators
    # Common path utilities :reek:UtilityFunction
    module Utils
      include Generators::Logging
      # Generate a file ifne with custom body
      def generate_file(path, options = {}, &block)
        if !File.exist?(path)
        elsif options[:dup_when_exists]
          _, folder, target_file, ext = path.match(/(.+)\/(.+)\.(.+)\Z/).to_a
          count = Dir["#{folder}/*"].count { |file| file.match(/#{target_file}\d*\.#{ext}/) }
          path = "#{folder}/#{target_file}#{count + 1}.#{ext}"
        else
          warning "File already exists: #{path}, skipping..."
          return nil
        end

        block ||= ->(file) {} unless block_given?
        File.open(path, "w", &block)
        success "Created file #{path}"
      end

      # Mkdir ifne
      def generate_folder(dir_path)
        if File.directory?(dir_path)
          warning "Dir already exists: #{dir_path}, skipping..."
        else
          FileUtils.mkdir_p(dir_path)
          success "Created dir #{dir_path}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katagen-1.0.1 lib/katagen/generators/utils.rb