File: generate.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (66 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_relative '../../puppet/face'
require_relative '../../puppet/generate/type'

# Create the Generate face
Puppet::Face.define(:generate, '0.1.0') do
  copyright 'Puppet Inc.', 2016
  license   _('Apache 2 license; see COPYING')

  summary _('Generates Puppet code from Ruby definitions.')

  action(:types) do
    summary _('Generates Puppet code for custom types')

    description <<-'EOT'
      Generates definitions for custom resource types using Puppet code.

      Types defined in Puppet code can be used to isolate custom type definitions
      between different environments.
    EOT

    examples <<-'EOT'
      Generate Puppet type definitions for all custom resource types in the current environment:

          $ puppet generate types

      Generate Puppet type definitions for all custom resource types in the specified environment:

          $ puppet generate types --environment development
    EOT

    option '--format ' + _('<format>') do
      summary _('The generation output format to use. Supported formats: pcore.')
      default_to { 'pcore' }

      before_action do |_, _, options|
        raise ArgumentError, _("'%{format}' is not a supported format for type generation.") % { format: options[:format] } unless ['pcore'].include?(options[:format])
      end
    end

    option '--force' do
      summary _('Forces the generation of output files (skips up-to-date checks).')
      default_to { false }
    end

    when_invoked do |options|
      generator = Puppet::Generate::Type
      inputs = generator.find_inputs(options[:format].to_sym)
      environment = Puppet.lookup(:current_environment)

      # get the common output directory (in <envroot>/.resource_types) - create it if it does not exists
      # error if it exists and is not a directory
      #
      path_to_env = environment.configuration.path_to_env
      outputdir = File.join(path_to_env, '.resource_types')
      if Puppet::FileSystem.exist?(outputdir) && !Puppet::FileSystem.directory?(outputdir)
        raise ArgumentError, _("The output directory '%{outputdir}' exists and is not a directory") % { outputdir: outputdir }
      end
      Puppet::FileSystem::mkpath(outputdir)

      generator.generate(inputs, outputdir, options[:force])

      exit(1) if generator.bad_input?
      nil
    end
  end
end