Class: Generamba::CatalogInstaller

Inherits:
AbstractInstaller show all
Defined in:
lib/generamba/template/installer/catalog_installer.rb

Overview

Incapsulates the logic of installing Generamba templates from the template catalog

Instance Method Summary collapse

Instance Method Details

#install_template(template_declaration) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generamba/template/installer/catalog_installer.rb', line 10

def install_template(template_declaration)
  template_name = template_declaration.name
  puts("Installing #{template_name}...")

  template_name = template_declaration.name
  catalogs_path = Pathname.new(ENV['HOME'])
                     .join(GENERAMBA_HOME_DIR)
                     .join(CATALOGS_DIR)

  catalog_path = catalogs_path.children.select { |child|
    child.directory? && child.split.last.to_s[0] != '.'
  }.select { |catalog_path|
    template_path = browse_catalog_for_a_template(catalog_path, template_name)
    template_path != nil
  }.first

  if catalog_path == nil
    error_description = "Cannot find #{template_name} in any catalog. Try another name.".red
    puts(error_description)
    return
  end

  template_path = catalog_path.join(template_name)
  rambaspec_exist = Generamba::RambaspecValidator.validate_spec_existance(template_name, template_path)
  unless rambaspec_exist
    error_description = "Cannot find #{template_name + RAMBASPEC_EXTENSION} in the template catalog #{catalog_path}. Try another name.".red
    puts(error_description)
    return
  end

  rambaspec_valid = Generamba::RambaspecValidator.validate_spec(template_name, template_path)
  unless rambaspec_valid
    error_description = "#{template_name + RAMBASPEC_EXTENSION} is not valid.".red
    puts(error_description)
    return
  end

  install_path = Pathname.new(TEMPLATES_FOLDER)
                     .join(template_name)
  FileUtils.mkdir_p install_path

  src = template_path.to_s + '/.'
  FileUtils.cp_r(src, install_path)
end