Module: TemplateHelpers

Defined in:
lib/potassium/helpers/template-helpers.rb

Instance Method Summary collapse

Instance Method Details

#app_nameObject



2
3
4
# File 'lib/potassium/helpers/template-helpers.rb', line 2

def app_name
  @app_name || app_name_from_file
end

#ask(recipe_name) ⇒ Object



16
17
18
19
# File 'lib/potassium/helpers/template-helpers.rb', line 16

def ask(recipe_name)
  recipe = load_recipe(recipe_name)
  recipe.ask
end

#cli_optionsObject



79
80
81
# File 'lib/potassium/helpers/template-helpers.rb', line 79

def cli_options
  self.class.cli_options
end

#create(recipe_name) ⇒ Object



11
12
13
14
# File 'lib/potassium/helpers/template-helpers.rb', line 11

def create(recipe_name)
  recipe = load_recipe(recipe_name)
  recipe.create
end

#cut_comments(file, limit: 100) ⇒ Object

TODO: Refactor to be able to reuse it and reduce the duplication and confusion.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/potassium/helpers/template-helpers.rb', line 54

def cut_comments(file, limit: 100)
  gsub_file file, /^\s*#[^\n]*\n/ do |match|
    if match.size > limit
      match.partition(/[\w\W]{#{limit - 1}}/).reject(&:blank?).map do |line|
        (line.size == limit - 1) ? "#{line}-" : "# #{line}"
      end.join("\n")
    else
      match
    end
  end
end

#dir_exist?(dir_path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/potassium/helpers/template-helpers.rb', line 70

def dir_exist?(dir_path)
  Dir.exist?(dir_path)
end

#erase_comments(file) ⇒ Object



49
50
51
# File 'lib/potassium/helpers/template-helpers.rb', line 49

def erase_comments(file)
  gsub_file file, /^\s*#[^\n]*\n/, ''
end

#eval_file(source) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/potassium/helpers/template-helpers.rb', line 32

def eval_file(source)
  location = File.expand_path(find_in_source_paths(source))
  unique_name = SecureRandom.hex

  define_singleton_method unique_name do
    instance_eval File.read(location)
  end

  public_send unique_name
end

#file_exist?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/potassium/helpers/template-helpers.rb', line 66

def file_exist?(file_path)
  File.exist?(file_path)
end

#force?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/potassium/helpers/template-helpers.rb', line 83

def force?
  cli_options[:force]
end

#install(recipe_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/potassium/helpers/template-helpers.rb', line 21

def install(recipe_name)
  recipe = load_recipe(recipe_name)

  if !recipe.respond_to?(:installed?) || !recipe.installed? || force?
    recipe.install
  else
    info "#{recipe_name.to_s.titleize} is already installed"
    info "Use --force to force the installation"
  end
end

#load_recipe(recipe_name) ⇒ Object



6
7
8
9
# File 'lib/potassium/helpers/template-helpers.rb', line 6

def load_recipe(recipe_name)
  @recipes ||= {}
  @recipes[recipe_name] ||= get_recipe_class(recipe_name.to_sym).new(self)
end

#procfile(name, command) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/potassium/helpers/template-helpers.rb', line 87

def procfile(name, command)
  file = 'Procfile'
  if File.read(file).index(/^#{name}:.*$/m).nil?
    append_file file, "#{name}: #{command}\n"
  else
    gsub_file file, /^name:.*$/m, "#{name}: #{command}\n"
  end
end

#read_file(file_path) ⇒ Object



74
75
76
77
# File 'lib/potassium/helpers/template-helpers.rb', line 74

def read_file(file_path)
  fail "#{file_path} does not exist in destination" unless file_exist?(file_path)
  File.read(file_path)
end

#source_path(path) ⇒ Object



43
44
45
46
47
# File 'lib/potassium/helpers/template-helpers.rb', line 43

def source_path(path)
  define_singleton_method :source_paths do
    [File.expand_path(File.dirname(path))]
  end
end