Module: Retrospec::Puppet::TemplateHelpers

Included in:
Retrospec::Plugins::V1::Puppet
Defined in:
lib/retrospec/plugins/v1/plugin/template_helpers.rb

Instance Method Summary collapse

Instance Method Details

#clone_hook_file(template_dir) ⇒ String

Returns window’s specific file path for windows and unix specific path for unix.

Parameters:

  • path (String)

    to the template directory

Returns:

  • (String)

    window’s specific file path for windows and unix specific path for unix



52
53
54
55
56
57
58
59
60
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 52

def clone_hook_file(template_dir)
  hook_file_name = 'clone-hook'
  if File.exist?(File.join(template_dir, hook_file_name))
    hook_file = File.join(template_dir, hook_file_name)
  else
    hook_file = File.join(gem_template_dir, hook_file_name)
  end
  hook_file
end

#create_user_template_dir(user_template_directory = nil) ⇒ String

Note:

creates the user supplied or default template directory

Returns - user_template_dir.

Returns:

  • (String)
    • user_template_dir



8
9
10
11
12
13
14
15
16
17
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 8

def create_user_template_dir(user_template_directory = nil)
  if user_template_directory.nil?
    user_template_directory = default_user_template_dir
  end
  # create default user template path or supplied user template path
  unless File.exist?(user_template_directory)
    FileUtils.mkdir_p(File.expand_path(user_template_directory))
  end
  user_template_directory
end

#default_user_template_dirString

Returns - the default retrospec templates directory.

Returns:

  • (String)
    • the default retrospec templates directory



41
42
43
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 41

def default_user_template_dir
  File.expand_path(File.join(ENV['HOME'], '.retrospec', 'repos', 'retrospec-puppet-templates'))
end

#gem_template_dirString

Returns - the template directory that exists in this gem on the filesytem.

Returns:

  • (String)
    • the template directory that exists in this gem on the filesytem



46
47
48
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 46

def gem_template_dir
  File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
end

#run_clone_hook(template_dir, git_url = nil, branch = nil) ⇒ Object

runs the clone hook file the intention of this method and hook is to download the templates from an external repo. Because templates are updated frequently and users will sometimes have client specific templates I wanted to externalize them for easier management.

Parameters:

  • template_dir (String)
    • the path to the template dir

  • git_url (String) (defaults to: nil)
    • the git url of the template repository

  • branch (String) (defaults to: nil)
    • the branch or ref to use



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 70

def run_clone_hook(template_dir, git_url = nil, branch = nil)
  hook_file = clone_hook_file(template_dir)
  return unless File.exist?(hook_file)
  output = `ruby #{hook_file} #{template_dir} #{git_url} #{branch}`
  puts output
  if $CHILD_STATUS.success?
    puts "Successfully ran hook: #{hook_file}".info
    puts output.info
  else
    puts "Error running hook: #{hook_file}".fatal
    puts output.fatal
  end
end

#setup_user_template_dir(user_template_directory = nil, git_url = nil, branch = nil) ⇒ String

Note:

creates and syncs the specifed user template diretory

Returns - user_template_dir.

Returns:

  • (String)
    • user_template_dir



31
32
33
34
35
36
37
38
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 31

def setup_user_template_dir(user_template_directory = nil, git_url = nil, branch = nil)
  if user_template_directory.nil?
    user_template_directory = default_user_template_dir
  end
  template_dir = create_user_template_dir(user_template_directory)
  run_clone_hook(user_template_directory, git_url, branch)
  template_dir
end

#sync_user_template_dir(user_template_directory) ⇒ String

Note:

creates and/or copies all templates in the gem to the user templates path

Returns - user_template_dir.

Returns:

  • (String)
    • user_template_dir



21
22
23
24
25
26
27
# File 'lib/retrospec/plugins/v1/plugin/template_helpers.rb', line 21

def sync_user_template_dir(user_template_directory)
  Dir.glob(File.join(gem_template_dir, '**', '{*,.*}')).each do |src|
    dest = src.gsub(gem_template_dir, user_template_directory)
    safe_copy_file(src, dest) unless File.directory?(src)
  end
  user_template_directory
end