module TemplateHelpers

Public Instance Methods

app_name() click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 2
def app_name
  @app_name || app_name_from_file
end
ask(recipe_name) click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 24
def ask(recipe_name)
  recipe = load_recipe(recipe_name)
  recipe.ask
end
cli_options() click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 87
def cli_options
  self.class.cli_options
end
create(recipe_name) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 78
def dir_exist?(dir_path)
  Dir.exist?(dir_path)
end
erase_comments(file) click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 57
def erase_comments(file)
  gsub_file file, /^\s*#[^\n]*\n/, ''
end
eval_file(source) click to toggle source
# 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) click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 74
def file_exist?(file_path)
  File.exist?(file_path)
end
force?() click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 91
def force?
  cli_options[:force]
end
install(recipe_name) click to toggle source
# 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) click to toggle source
# 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_version() click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 6
def node_version
  "#{Potassium::NODE_VERSION}.x"
end
procfile(name, command) click to toggle source
# 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) click to toggle source
# 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_version() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

app_name_from_file() click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 111
def app_name_from_file
  File.read('config/application.rb').match(/module\s(.*)/)[1].underscore.dasherize
end
get_recipe_class(recipe_name) click to toggle source
# File lib/potassium/helpers/template-helpers.rb, line 106
def get_recipe_class(recipe_name)
  require_relative "../recipes/#{recipe_name}"
  Recipes.const_get(recipe_name.to_s.camelize)
end