Module: EasyType::PathHelpers

Included in:
Generators::Base
Defined in:
lib/easy_type/path_helpers.rb

Overview

docs

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(parent) ⇒ Object



10
11
12
# File 'lib/easy_type/path_helpers.rb', line 10

def self.included(parent)
  parent.extend(PathHelpers)
end

Instance Method Details

#attribute_pathObject

Give the full path name of a ruby file describing the parameter



69
70
71
# File 'lib/easy_type/path_helpers.rb', line 69

def attribute_path
  type_attribute_directory + "#{@attribute_name}.rb"
end

#create_directory(path) ⇒ Object

Create a directory and notify user of it’s creation



116
117
118
119
120
# File 'lib/easy_type/path_helpers.rb', line 116

def create_directory(path)
  return if File.exist?(path)
  FileUtils.mkdir_p path
  Puppet.notice "Created directory #{path}"
end

#create_source(name, destination) ⇒ Object

Create a source file based on a ERB template



138
139
140
141
142
143
144
# File 'lib/easy_type/path_helpers.rb', line 138

def create_source(name, destination)
  template = File.read("#{template_path(name)}/easy_type/templates/#{name}")
  content = ERB.new(template, trim_mode: '-').result(binding)
  write_file(destination, content)
rescue SyntaxError
  raise "Error in erb template #{name}"
end

#modulepathObject

Returns the system wide module path



104
105
106
107
108
109
110
111
# File 'lib/easy_type/path_helpers.rb', line 104

def modulepath
  env = Puppet.lookup(:current_environment)
  @modulepath ||= if env.modulepath == []
                    ['./spec/fixtures/modules']
                  else
                    env.modulepath
                  end
end

#name_attribute_pathObject

Give the full path name of a ruby file describing the name parameter



62
63
64
# File 'lib/easy_type/path_helpers.rb', line 62

def name_attribute_path
  type_attribute_directory + "#{@namevar}.rb"
end

#provider_directoryObject

Return the name of the provider directory



41
42
43
# File 'lib/easy_type/path_helpers.rb', line 41

def provider_directory
  Pathname.new(puppet_lib) + 'provider' + @name
end

#provider_pathObject

Return the full path name of a ruby file describing the provider



48
49
50
# File 'lib/easy_type/path_helpers.rb', line 48

def provider_path
  provider_directory + "#{@provider}.rb"
end

#puppet_libObject

Return the puppet library path of the current custom type



90
91
92
# File 'lib/easy_type/path_helpers.rb', line 90

def puppet_lib
  File.expand_path('./lib/puppet')
end

#puppet_lib?Boolean

Returns a true if the expected puppet library path exists

Returns:

  • (Boolean)


97
98
99
# File 'lib/easy_type/path_helpers.rb', line 97

def puppet_lib?
  File.exist?(puppet_lib)
end

#save_file(path, content) ⇒ Object

Save some content to a file



149
150
151
152
153
# File 'lib/easy_type/path_helpers.rb', line 149

def save_file(path, content)
  File.open(path, 'w') do |f|
    f.write content
  end
end

#shared_attribute_pathObject

Give the full path name of a ruby file describing the shared property



76
77
78
# File 'lib/easy_type/path_helpers.rb', line 76

def shared_attribute_path
  type_shared_directory + "#{@attribute_name}.rb"
end

#template_path(template) ⇒ Object

Search the modulepath for where easy_type’s template’s are stored



17
18
19
20
21
22
# File 'lib/easy_type/path_helpers.rb', line 17

def template_path(template)
  modulepath.each do |path|
    return path if File.exist?("#{path}/easy_type/templates/#{template}")
  end
  raise "Template #{template} not found in modulepath #{modulepath}"
end

#type_attribute_directoryObject

Give the directory name where the type definitions reside



83
84
85
# File 'lib/easy_type/path_helpers.rb', line 83

def type_attribute_directory
  Pathname.new(puppet_lib) + 'type' + @name
end

#type_directoryObject

return the directory where the types reside



27
28
29
# File 'lib/easy_type/path_helpers.rb', line 27

def type_directory
  Pathname.new(puppet_lib) + 'type'
end

#type_pathObject

Give the full path name of a ruby file containing the type definition



34
35
36
# File 'lib/easy_type/path_helpers.rb', line 34

def type_path
  type_directory + "#{@name}.rb"
end

#type_shared_directoryObject

Return the directory name where the shared type definitions reside



55
56
57
# File 'lib/easy_type/path_helpers.rb', line 55

def type_shared_directory
  Pathname.new(puppet_lib) + 'type' + 'shared'
end

#write_file(path, content) ⇒ Object

Write the content to a file. If the file exists, it will not be overwritten unless the –force option is set. The user will be notified of the creation of the file



127
128
129
130
131
132
133
# File 'lib/easy_type/path_helpers.rb', line 127

def write_file(path, content)
  file_exists = File.exist?(path)
  raise "File #{path} already exists. Not overwritten. Use --force to overwrite" if file_exists && !@force
  save_file(path, content)
  message = file_exists ? "File #{path} overwriten with new content" : "File #{path} created"
  Puppet.notice message
end