Module: Katagen::Generators::Utils

Includes:
Logging
Included in:
Solution
Defined in:
lib/katagen/generators/utils.rb

Overview

Common path utilities :reek:UtilityFunction

Constant Summary

Constants included from Logging

Logging::COLOR_CODES

Instance Method Summary collapse

Methods included from Logging

#info, #success, #warning

Instance Method Details

#generate_file(path, options = {}, &block) ⇒ Object

Generate a file ifne with custom body



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/katagen/generators/utils.rb', line 10

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

#generate_folder(dir_path) ⇒ Object

Mkdir ifne



27
28
29
30
31
32
33
34
# File 'lib/katagen/generators/utils.rb', line 27

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