Module: Gemaker::Util

Included in:
Cmd::Base
Defined in:
lib/gemaker/util.rb

Instance Method Summary collapse

Instance Method Details

#copy_file(source, destination) ⇒ Object



3
4
5
# File 'lib/gemaker/util.rb', line 3

def copy_file(source, destination)
  ::FileUtils.cp(get_template_path(source), get_destination_path(destination))
end

#copy_template(source, destination, locales = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/gemaker/util.rb', line 7

def copy_template(source, destination, locales = {})
  template_path = "#{get_template_path(source)}.erb"
  destination_path = get_destination_path(destination)

  input = File.open(template_path)
  output = File.open(destination_path, "w+")
  output.write(parse_erb(input.read, locales))
  output.close
  input.close
end

#create_dir(path) ⇒ Object



40
41
42
# File 'lib/gemaker/util.rb', line 40

def create_dir(path)
  ::FileUtils.mkdir_p(get_destination_path(path))
end

#error(string) ⇒ Object



72
73
74
75
76
# File 'lib/gemaker/util.rb', line 72

def error(string)
  return if string.blank?

  puts string.to_s.red
end

#execute(cmd, error_message = nil) ⇒ Object



29
30
31
32
# File 'lib/gemaker/util.rb', line 29

def execute(cmd, error_message = nil)
  system cmd
  error(error_message) if $?.exitstatus != 0
end

#execute_in_gem(cmd, error_message = nil) ⇒ Object



34
35
36
37
38
# File 'lib/gemaker/util.rb', line 34

def execute_in_gem(cmd, error_message = nil)
  system "cd #{gem_root_path}; #{cmd}"
  error(error_message) if $?.exitstatus != 0
  system "cd .."
end

#gem_root_pathObject



54
55
56
# File 'lib/gemaker/util.rb', line 54

def gem_root_path
  File.join(Dir.pwd, @config.gem_name)
end

#get_destination_path(destination) ⇒ Object



44
45
46
47
48
# File 'lib/gemaker/util.rb', line 44

def get_destination_path(destination)
  destination_path = File.join(gem_root_path, destination)
  ::FileUtils.mkdir_p(File.dirname(destination_path))
  destination_path
end

#get_template_path(file_path) ⇒ Object



50
51
52
# File 'lib/gemaker/util.rb', line 50

def get_template_path(file_path)
  File.join(utils_path, "templates", file_path)
end

#info(string) ⇒ Object



68
69
70
# File 'lib/gemaker/util.rb', line 68

def info(string)
  puts string.to_s.green
end

#parse_erb(content, data) ⇒ Object



62
63
64
65
66
# File 'lib/gemaker/util.rb', line 62

def parse_erb(content, data)
  b = binding
  data.each { |k, v| singleton_class.send(:define_method, k) { v } }
  ERB.new(content, trim_mode: '-').result(b)
end

#remove_in_gem(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/gemaker/util.rb', line 18

def remove_in_gem(path)
  full_path = File.join(gem_root_path, path)

  if File.exist?(full_path)
    ::FileUtils.rm_rf(File.join(gem_root_path, path))
    return
  end

  puts "Can't delete because #{full_path} does not exist"
end

#utils_pathObject



58
59
60
# File 'lib/gemaker/util.rb', line 58

def utils_path
  File.dirname(__FILE__)
end