Class: Viperaptor::TemplateHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/viperaptor/helpers/template_helper.rb

Overview

Provides a number of helper methods for manipulating Viperaptor template files

Class Method Summary collapse

Class Method Details

.detect_template_location(template_name) ⇒ Object

Raises:

  • (StandardError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/viperaptor/helpers/template_helper.rb', line 5

def self.detect_template_location(template_name)

  catalogs_path = Pathname.new(ENV['HOME'])
                          .join(APP_HOME_DIR)
                          .join(CATALOGS_DIR)

  catalog_template_list_helper = CatalogTemplateListHelper.new

  catalogs = catalogs_path.children.select { |child|
    child.directory? && child.split.last.to_s[0] != '.'
  }

  catalog = ([
    Pathname.new(Dir.getwd).join(Rambafile.suffix(TEMPLATES_FOLDER)),
  ] + catalogs)
   .detect do |catalog_path|
    next if !catalog_path.exist?
    templates = catalog_template_list_helper.obtain_all_templates_from_a_catalog(catalog_path)
    templates.include?(template_name)
  end

  return nil if catalog == nil

  path = catalog_template_list_helper.template_path(catalog, template_name)

  error_description = "Cannot find template named '#{template_name}'! Add it to the Rambafile and run *viperaptor template install*".red
  raise StandardError, error_description if path.nil?

  path
end

.global_templatesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/viperaptor/helpers/template_helper.rb', line 60

def self.global_templates

  downloader = CatalogDownloader.new
  catalog_template_list_helper = CatalogTemplateListHelper.new

  templates = []
  catalog_paths = downloader.update_all_catalogs_and_return_filepaths(true)
  catalog_paths.each do |path|
    templates += catalog_template_list_helper.obtain_all_templates_from_a_catalog(path)
    templates = templates.uniq
  end

  templates.sort
end

.obtain_path(template_name) ⇒ Pathname

Returns a file path for a specific template folder

Parameters:

  • template_name (String)

    The Viperaptor template name

Returns:

  • (Pathname)

Raises:

  • (StandardError)


51
52
53
54
55
56
57
58
# File 'lib/viperaptor/helpers/template_helper.rb', line 51

def self.obtain_path(template_name)
  path = self.detect_template_location(template_name)

  error_description = "Cannot find template named #{template_name}! Add it to the Rambafile and run *viperaptor template install*".red
  raise StandardError, error_description unless path != nil && path.exist?

  path
end

.obtain_spec(template_name) ⇒ Pathname

Returns a file path for a specific template .rambaspec file

Parameters:

  • template_name (String)

    The Viperaptor template name

Returns:

  • (Pathname)


40
41
42
43
44
45
# File 'lib/viperaptor/helpers/template_helper.rb', line 40

def self.obtain_spec(template_name)
  template_path = self.obtain_path(template_name)
  spec_path = template_path.join(template_name + RAMBASPEC_EXTENSION)

  spec_path
end