Class: Helpers

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

Class Method Summary collapse

Class Method Details

.create_user_template_dir(user_template_directory = nil) ⇒ Object

creates the user supplied or default template directory returns: user_template_dir



45
46
47
48
49
50
51
52
53
54
# File 'lib/retrospec/helpers.rb', line 45

def self.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
  if not File.exists?(user_template_directory)
    FileUtils.mkdir_p(File.expand_path(user_template_directory))
  end
  user_template_directory
end

.default_user_template_dirObject



75
76
77
# File 'lib/retrospec/helpers.rb', line 75

def self.default_user_template_dir
  File.expand_path(File.join(ENV['HOME'], '.puppet_retrospec_templates' ))
end

.gem_template_dirObject



79
80
81
# File 'lib/retrospec/helpers.rb', line 79

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

.get_module_nameObject



5
6
7
8
9
10
11
12
# File 'lib/retrospec/helpers.rb', line 5

def self.get_module_name
  module_name = nil
  Dir["manifests/*.pp"].entries.each do |manifest|
    module_name = get_module_name_from_file(manifest)
    break unless module_name.nil?
  end
  module_name
end

.get_module_name_from_file(file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/retrospec/helpers.rb', line 14

def self.get_module_name_from_file(file)
  p = Puppet::Parser::Lexer.new
  module_name = nil
  p.string = File.read(file)
  tokens = p.fullscan

  i = tokens.index { |token| [:CLASS, :DEFINE].include? token.first }
  unless i.nil?
    module_name = tokens[i + 1].last[:value].split('::').first
  end

  module_name
end

.is_module_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/retrospec/helpers.rb', line 28

def self.is_module_dir?(dir)
  Dir[File.join(dir,"*")].entries.include? "manifests"
end

.safe_copy_file(src, dest) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/retrospec/helpers.rb', line 87

def self.safe_copy_file(src, dest)
  if File.exists?(dest) and not File.zero?(dest)
    $stderr.puts "!! #{dest} already exists"
  else
    if not File.exists?(src)
      safe_touch(src)
    else
      safe_mkdir(File.dirname(dest))
      FileUtils.cp(src,dest)
    end
    puts " + #{dest}"
  end
end

.safe_create_file(filename, content) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/retrospec/helpers.rb', line 112

def self.safe_create_file(filename, content)
  if File.exists? filename
    old_content = File.read(filename)
    # if we did a better comparison of content we could be smarter about when we create files
    if old_content != content or not File.zero?(filename)
      $stderr.puts "!! #{filename} already exists and differs from template"
    end
  else
    File.open(filename, 'w') do |f|
      f.puts content
    end
    puts " + #{filename}"
  end
end

.safe_mkdir(dir) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/retrospec/helpers.rb', line 32

def self.safe_mkdir(dir)
  if File.exists? dir
    unless File.directory? dir
      $stderr.puts "!! #{dir} already exists and is not a directory"
    end
  else
    FileUtils.mkdir_p dir
    puts " + #{dir}/"
  end
end

.safe_touch(file) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/retrospec/helpers.rb', line 101

def self.safe_touch(file)
  if File.exists? file
    unless File.file? file
      $stderr.puts "!! #{file} already exists and is not a regular file"
    end
  else
    FileUtils.touch file
    puts " + #{file}"
  end
end

.setup_user_template_dir(user_template_directory = nil) ⇒ Object

creates and syncs the specifed user template diretory returns: user_template_dir



68
69
70
71
72
73
# File 'lib/retrospec/helpers.rb', line 68

def self.setup_user_template_dir(user_template_directory=nil)
  if user_template_directory.nil?
    user_template_directory = default_user_template_dir
  end
  sync_user_template_dir(create_user_template_dir(user_template_directory))
end

.sync_user_template_dir(user_template_directory) ⇒ Object

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



58
59
60
61
62
63
64
# File 'lib/retrospec/helpers.rb', line 58

def self.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