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



24
25
26
27
# File 'lib/potassium/helpers/template-helpers.rb', line 24

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

#cli_optionsObject



87
88
89
# File 'lib/potassium/helpers/template-helpers.rb', line 87

def cli_options
  self.class.cli_options
end

#create(recipe_name) ⇒ Object



19
20
21
22
# File 'lib/potassium/helpers/template-helpers.rb', line 19

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.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/potassium/helpers/template-helpers.rb', line 62

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)


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

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

#erase_comments(file) ⇒ Object



57
58
59
# File 'lib/potassium/helpers/template-helpers.rb', line 57

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

#eval_file(source) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/potassium/helpers/template-helpers.rb', line 40

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)


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

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

#force?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/potassium/helpers/template-helpers.rb', line 91

def force?
  cli_options[:force]
end

#install(recipe_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/potassium/helpers/template-helpers.rb', line 29

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



14
15
16
17
# File 'lib/potassium/helpers/template-helpers.rb', line 14

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

#node_versionObject



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

def node_version
  "#{Potassium::NODE_VERSION}.x"
end

#procfile(name, command) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/potassium/helpers/template-helpers.rb', line 95

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



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

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

#ruby_versionObject



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

def ruby_version
  Semantic::Version.new(Potassium::RUBY_VERSION).instance_eval { "#{major}.#{minor}" }
end

#source_path(path) ⇒ Object



51
52
53
54
55
# File 'lib/potassium/helpers/template-helpers.rb', line 51

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