Module: Puppet::ModuleTool::Generate

Defined in:
lib/puppet/face/module/generate.rb

Class Method Summary collapse

Class Method Details

.destination(metadata) ⇒ Object

Raises:

  • (ArgumentError)


205
206
207
208
209
210
# File 'lib/puppet/face/module/generate.rb', line 205

def destination()
  return @dest if defined? @dest
  @dest = Pathname.pwd + .name
  raise ArgumentError, _("%{destination} already exists.") % { destination: @dest } if @dest.exist?
  return @dest
end

.duplicate_skeleton(metadata) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/puppet/face/module/generate.rb', line 212

def duplicate_skeleton()
  dest = destination()

  puts
  Puppet.notice _("Generating module at %{dest}...") % { dest: dest }
  FileUtils.cp_r skeleton_path, dest

  populate_templates(, dest)
  return dest
end

.generate(metadata, skip_interview = false) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/puppet/face/module/generate.rb', line 138

def generate(, skip_interview = false)
  interview() unless skip_interview
  destination = duplicate_skeleton()
  all_files = destination.basename + '**/*'

  return Dir[all_files.to_s]
end

.interview(metadata) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/puppet/face/module/generate.rb', line 146

def interview()
  puts _("We need to create a metadata.json file for this module.  Please answer the
  following questions; if the question is not applicable to this module, feel free
  to leave it blank.")

  begin
    puts
    puts _("Puppet uses Semantic Versioning (semver.org) to version modules.")
    puts _("What version is this module?  [%{version}]") % { version: .version }
    .update 'version' => user_input(.version)
  rescue
    Puppet.err _("We're sorry, we could not parse that as a Semantic Version.")
    retry
  end

  puts
  puts _("Who wrote this module?  [%{author}]") % { author: .author }
  .update 'author' => user_input(.author)

  puts
  puts _("What license does this module code fall under?  [%{license}]") % { license: .license }
  .update 'license' => user_input(.license)

  puts
  puts _("How would you describe this module in a single sentence?")
  .update 'summary' => user_input(.summary)

  puts
  puts _("Where is this module's source code repository?")
  .update 'source' => user_input(.source)

  puts
  puts _("Where can others go to learn more about this module?%{project_page}") % { project_page: .project_page && "  [#{.project_page}]" }
  .update 'project_page' => user_input(.project_page)

  puts
  puts _("Where can others go to file issues about this module?%{issues}") % { issues: .issues_url && "  [#{.issues_url}]" }
  .update 'issues_url' => user_input(.issues_url)

  puts
  puts '-' * 40
  puts .to_json
  puts '-' * 40
  puts
  puts _("About to generate this metadata; continue? [n/Y]")

  if user_input('Y') !~ /^y(es)?$/i
    puts _("Aborting...")
    exit 0
  end
end

.populate_templates(metadata, destination) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/puppet/face/module/generate.rb', line 223

def populate_templates(, destination)
  Puppet.notice _("Populating templates...")

  formatters = {
    :erb      => proc { |data, ctx| ERB.new(data).result(ctx) },
    :template => proc { |data, _| data },
  }

  formatters.each do |type, block|
    templates = destination + "**/*.#{type}"

    Dir.glob(templates.to_s, File::FNM_DOTMATCH).each do |erb|
      path = Pathname.new(erb)
      content = block[path.read, binding]

      target = path.parent + path.basename(".#{type}")
      target.open('w:UTF-8') { |f| f.write(content) }
      path.unlink
    end
  end
end

.skeleton_pathObject



245
246
247
248
249
250
# File 'lib/puppet/face/module/generate.rb', line 245

def skeleton_path
  return @path if defined? @path
  path = Pathname(Puppet.settings[:module_skeleton_dir])
  path = Pathname(__FILE__).dirname + '../../module_tool/skeleton/templates/generator' unless path.directory?
  @path = path
end

.user_input(default = nil) ⇒ Object



198
199
200
201
202
203
# File 'lib/puppet/face/module/generate.rb', line 198

def user_input(default=nil)
  print '--> '
  input = STDIN.gets.chomp.strip
  input = default if input == ''
  return input
end