Class: Helpers
- Inherits:
-
Object
- Object
- Helpers
- Defined in:
- lib/helpers.rb
Class Method Summary collapse
-
.create_user_template_dir(user_template_directory = nil) ⇒ Object
creates the user supplied or default template directory returns: user_template_dir.
- .default_user_template_dir ⇒ Object
- .gem_template_dir ⇒ Object
- .get_module_name ⇒ Object
- .get_module_name_from_file(file) ⇒ Object
- .is_module_dir? ⇒ Boolean
- .run(module_name = nil) ⇒ Object
- .safe_copy_file(src, dest) ⇒ Object
- .safe_create_file(filename, content) ⇒ Object
- .safe_mkdir(dir) ⇒ Object
- .safe_touch(file) ⇒ Object
-
.setup_user_template_dir(user_template_directory = nil) ⇒ Object
creates and syncs the specifed user template diretory returns: user_template_dir.
-
.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.
Class Method Details
.create_user_template_dir(user_template_directory = nil) ⇒ Object
creates the user supplied or default template directory returns: user_template_dir
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/helpers.rb', line 76 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.(user_template_directory)) end user_template_directory end |
.default_user_template_dir ⇒ Object
107 108 109 |
# File 'lib/helpers.rb', line 107 def self.default_user_template_dir File.(File.join(ENV['HOME'], '.puppet_retrospec_templates' )) end |
.gem_template_dir ⇒ Object
111 112 113 |
# File 'lib/helpers.rb', line 111 def self.gem_template_dir File.(File.join(File.dirname(__FILE__), 'templates')) end |
.get_module_name ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/helpers.rb', line 36 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
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/helpers.rb', line 45 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? ⇒ Boolean
59 60 61 |
# File 'lib/helpers.rb', line 59 def self.is_module_dir? Dir["*"].entries.include? "manifests" end |
.run(module_name = nil) ⇒ Object
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/helpers.rb', line 6 def self.run(module_name=nil) unless is_module_dir? $stderr.puts "Does not appear to be a Puppet module. Aborting" return false end if module_name.nil? module_name = get_module_name if module_name.nil? $stderr.puts "Unable to determine module name. Aborting" return false end end [ 'spec', 'spec/classes', 'spec/defines', 'spec/functions', 'spec/unit', 'spec/unit/facter', 'spec/unit/puppet', 'spec/unit/puppet/type', 'spec/unit/puppet/provider', 'spec/hosts', ].each { |dir| safe_mkdir(dir) } safe_create_spec_helper safe_create_fixtures_file safe_create_resource_spec_files safe_make_shared_context end |
.safe_copy_file(src, dest) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/helpers.rb', line 115 def self.safe_copy_file(src, dest) if File.exists?(dest) $stderr.puts "!! #{dest} already exists" else if not File.exists?(src) safe_touch(src) else FileUtils.cp(src,dest) end puts " + #{dest}" end end |
.safe_create_file(filename, content) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/helpers.rb', line 139 def self.safe_create_file(filename, content) if File.exists? filename old_content = File.read(filename) if old_content != content $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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/helpers.rb', line 63 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
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/helpers.rb', line 128 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
100 101 102 103 104 105 |
# File 'lib/helpers.rb', line 100 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
89 90 91 92 93 94 95 96 |
# File 'lib/helpers.rb', line 89 def self.sync_user_template_dir(user_template_directory) Dir.glob(File.join(gem_template_dir, "*.erb")).each do |src| filename = File.basename(src) dest = File.(File.join(user_template_directory, filename)) safe_copy_file(src, dest) end user_template_directory end |