module Cavendish::Utils

Public Instance Methods

copy_file(source, destination) click to toggle source
# File lib/cavendish/utils.rb, line 3
def copy_file(source, destination)
  log 'CREATE', destination
  ::FileUtils.cp(get_template_path(source), get_or_generate_destination_path(destination))
end
copy_template(source, destination, locales = {}) click to toggle source
# File lib/cavendish/utils.rb, line 8
def copy_template(source, destination, locales = {})
  log 'CREATE', destination
  template_path = "#{get_template_path(source)}.erb"
  destination_path = get_or_generate_destination_path(destination)
  template_contents = File.read(template_path)
  File.open(destination_path, 'w+') do |output|
    output.write(parse_erb(template_contents, locales))
  end
end
error(msg) click to toggle source
# File lib/cavendish/utils.rb, line 70
def error(msg)
  puts "\t[#{'ERROR'.red}] #{msg}"
end
get_or_generate_destination_path(destination) click to toggle source
# File lib/cavendish/utils.rb, line 46
def get_or_generate_destination_path(destination)
  destination_path = File.join(project_root_path, destination)
  ::FileUtils.mkdir_p(File.dirname(destination_path))
  destination_path
end
get_template_path(file_path) click to toggle source
# File lib/cavendish/utils.rb, line 52
def get_template_path(file_path)
  File.join(File.dirname(__FILE__), "assets", file_path)
end
inject_to_json_file(path, hash_representation) click to toggle source
# File lib/cavendish/utils.rb, line 39
def inject_to_json_file(path, hash_representation)
  package_path = File.join(project_root_path, path)
  package_contents = JSON.parse(File.read(package_path))
  new_contents = package_contents.deep_merge(hash_representation.with_indifferent_access)
  File.write(package_path, JSON.pretty_generate(new_contents))
end
log(action, msg) click to toggle source
# File lib/cavendish/utils.rb, line 66
def log(action, msg)
  puts "\t[#{action.green}] #{msg}"
end
parse_erb(content, data) click to toggle source
# File lib/cavendish/utils.rb, line 60
def parse_erb(content, data)
  bind = binding
  data.each { |key, value| singleton_class.send(:define_method, key) { value } }
  ERB.new(content, trim_mode: '-').result(bind)
end
project_root_path() click to toggle source
# File lib/cavendish/utils.rb, line 56
def project_root_path
  File.join(Dir.pwd, @config.project_name)
end
remove_in_project(path) click to toggle source
# File lib/cavendish/utils.rb, line 18
def remove_in_project(path)
  log 'REMOVE', path
  full_path = File.join(project_root_path, path)
  return ::FileUtils.rm_rf(File.join(project_root_path, path)) if File.exist?(full_path)

  error "Can't delete because #{full_path} does not exist"
end
run(cmd, error_message = nil) click to toggle source
# File lib/cavendish/utils.rb, line 26
def run(cmd, error_message = nil)
  log 'RUN', cmd
  system cmd
  error(error_message) if $?.exitstatus != 0
end
run_in_project(cmd, error_message = nil) click to toggle source
# File lib/cavendish/utils.rb, line 32
def run_in_project(cmd, error_message = nil)
  log 'RUN', cmd
  system "cd #{project_root_path}; #{cmd}"
  error(error_message) if $?.exitstatus != 0
  system "cd .."
end