|
| 1 | +# This script will print the Puppet type docs to stdout in JSON format. |
| 2 | + |
| 3 | +# There are some subtleties that make this a pain to run. Basically: Even if you |
| 4 | +# 'require' a specific copy of the Puppet code, the autoloader will grab bits |
| 5 | +# and pieces of Puppet code from other copies of Puppet scattered about the Ruby |
| 6 | +# load path. This causes a mixture of docs from different versions: although I |
| 7 | +# think the version you require will usually win for things that exist in both |
| 8 | +# versions, providers or attributes that only exist in one version will leak |
| 9 | +# through and you'll get an amalgamation. |
| 10 | + |
| 11 | +# So the only safe thing to do is run this in a completely separate process, and |
| 12 | +# ruthlessly control the Ruby load path. We expect that when you're executing |
| 13 | +# this code, your $RUBYLIB contains the version of Puppet you want to load, and |
| 14 | +# there are no other versions of Puppet available as gems, installed via system |
| 15 | +# packages, etc. etc. etc. |
| 16 | + |
| 17 | +require 'json' |
| 18 | +require 'puppet' |
| 19 | +require 'puppet/util/docs' |
| 20 | +extend Puppet::Util::Docs |
| 21 | +# We use scrub(). |
| 22 | + |
| 23 | + |
| 24 | + # The schema of the typedocs object: |
| 25 | + |
| 26 | + # { :name_of_type => { |
| 27 | + # :description => 'Markdown fragment: description of type', |
| 28 | + # :features => { :feature_name => 'feature description', ... } |
| 29 | + # # If there are no features, the value of :features will be an empty hash. |
| 30 | + # :providers => { # If there are no providers, the value of :providers will be an empty hash. |
| 31 | + # :name_of_provider => { |
| 32 | + # :description => 'Markdown fragment: docs for this provider', |
| 33 | + # :features => [:feature_name, :other_feature, ...] |
| 34 | + # # If provider has no features, the value of :features will be an empty array. |
| 35 | + # }, |
| 36 | + # ...etc... |
| 37 | + # } |
| 38 | + # :attributes => { # Puppet dictates that there will ALWAYS be at least one attribute. |
| 39 | + # :name_of_attribute => { |
| 40 | + # :description => 'Markdown fragment: docs for this attribute', |
| 41 | + # :kind => (:property || :parameter), |
| 42 | + # :namevar => (true || false), # always false if :kind => :property |
| 43 | + # }, |
| 44 | + # ...etc... |
| 45 | + # }, |
| 46 | + # }, |
| 47 | + # ...etc... |
| 48 | + # } |
| 49 | +typedocs = {} |
| 50 | + |
| 51 | +Puppet::Type.loadall |
| 52 | + |
| 53 | +Puppet::Type.eachtype { |type| |
| 54 | + # List of types to ignore: |
| 55 | + next if type.name == :puppet |
| 56 | + next if type.name == :component |
| 57 | + next if type.name == :whit |
| 58 | + |
| 59 | + # Initialize the documentation object for this type |
| 60 | + docobject = { |
| 61 | + :description => scrub(type.doc), |
| 62 | + :attributes => {} |
| 63 | + } |
| 64 | + |
| 65 | + # Handle features: |
| 66 | + # inject will return empty hash if type.features is empty. |
| 67 | + docobject[:features] = type.features.inject( {} ) { |allfeatures, name| |
| 68 | + allfeatures[name] = scrub( type.provider_feature(name).docs ) |
| 69 | + allfeatures |
| 70 | + } |
| 71 | + |
| 72 | + # Handle providers: |
| 73 | + # inject will return empty hash if type.providers is empty. |
| 74 | + docobject[:providers] = type.providers.inject( {} ) { |allproviders, name| |
| 75 | + allproviders[name] = { |
| 76 | + :description => scrub( type.provider(name).doc ), |
| 77 | + :features => type.provider(name).features |
| 78 | + } |
| 79 | + allproviders |
| 80 | + } |
| 81 | + |
| 82 | + # Override several features missing due to bug #18426: |
| 83 | + if type.name == :user |
| 84 | + docobject[:providers][:useradd][:features] << :manages_passwords << :manages_password_age << :libuser |
| 85 | + if docobject[:providers][:openbsd] |
| 86 | + docobject[:providers][:openbsd][:features] << :manages_passwords << :manages_loginclass |
| 87 | + end |
| 88 | + end |
| 89 | + if type.name == :group |
| 90 | + docobject[:providers][:groupadd][:features] << :libuser |
| 91 | + end |
| 92 | + |
| 93 | + |
| 94 | + # Handle properties: |
| 95 | + docobject[:attributes].merge!( |
| 96 | + type.validproperties.inject( {} ) { |allproperties, name| |
| 97 | + property = type.propertybyname(name) |
| 98 | + raise "Could not retrieve property #{propertyname} on type #{type.name}" unless property |
| 99 | + description = property.doc |
| 100 | + $stderr.puts "No docs for property #{name} of #{type.name}" unless description and !description.empty? |
| 101 | + |
| 102 | + allproperties[name] = { |
| 103 | + :description => scrub(description), |
| 104 | + :kind => :property, |
| 105 | + :namevar => false # Properties can't be namevars. |
| 106 | + } |
| 107 | + allproperties |
| 108 | + } |
| 109 | + ) |
| 110 | + |
| 111 | + # Handle parameters: |
| 112 | + docobject[:attributes].merge!( |
| 113 | + type.parameters.inject( {} ) { |allparameters, name| |
| 114 | + description = type.paramdoc(name) |
| 115 | + $stderr.puts "No docs for parameter #{name} of #{type.name}" unless description and !description.empty? |
| 116 | + |
| 117 | + # Strip off the too-huge provider list. The question of what to do about |
| 118 | + # providers is a decision for the formatter, not the fragment collector. |
| 119 | + description = description.split('Available providers are')[0] if name == :provider |
| 120 | + |
| 121 | + allparameters[name] = { |
| 122 | + :description => scrub(description), |
| 123 | + :kind => :parameter, |
| 124 | + :namevar => type.key_attributes.include?(name) # returns a boolean |
| 125 | + } |
| 126 | + allparameters |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + # Finally: |
| 131 | + typedocs[type.name] = docobject |
| 132 | +} |
| 133 | + |
| 134 | +print JSON.dump(typedocs) |
0 commit comments