puppet-strings 0.4.0 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +122 -0
  3. data/COMMITTERS.md +185 -0
  4. data/CONTRIBUTING.md +89 -0
  5. data/Gemfile +38 -0
  6. data/JSON.md +511 -0
  7. data/LICENSE +13 -0
  8. data/README.md +416 -0
  9. data/Rakefile +49 -0
  10. data/lib/puppet-strings.rb +63 -0
  11. data/lib/puppet-strings/json.rb +49 -0
  12. data/lib/puppet-strings/tasks.rb +10 -0
  13. data/lib/puppet-strings/tasks/generate.rb +23 -0
  14. data/lib/puppet-strings/tasks/gh_pages.rb +43 -0
  15. data/lib/puppet-strings/yard.rb +96 -0
  16. data/lib/puppet-strings/yard/code_objects.rb +8 -0
  17. data/lib/puppet-strings/yard/code_objects/base.rb +14 -0
  18. data/lib/puppet-strings/yard/code_objects/class.rb +59 -0
  19. data/lib/puppet-strings/yard/code_objects/defined_type.rb +58 -0
  20. data/lib/puppet-strings/yard/code_objects/function.rb +93 -0
  21. data/lib/puppet-strings/yard/code_objects/group.rb +30 -0
  22. data/lib/puppet-strings/yard/code_objects/provider.rb +93 -0
  23. data/lib/puppet-strings/yard/code_objects/type.rb +146 -0
  24. data/lib/puppet-strings/yard/handlers.rb +16 -0
  25. data/lib/puppet-strings/yard/handlers/puppet/base.rb +44 -0
  26. data/lib/puppet-strings/yard/handlers/puppet/class_handler.rb +23 -0
  27. data/lib/puppet-strings/yard/handlers/puppet/defined_type_handler.rb +23 -0
  28. data/lib/puppet-strings/yard/handlers/puppet/function_handler.rb +42 -0
  29. data/lib/puppet-strings/yard/handlers/ruby/base.rb +38 -0
  30. data/lib/puppet-strings/yard/handlers/ruby/function_handler.rb +357 -0
  31. data/lib/puppet-strings/yard/handlers/ruby/provider_handler.rb +113 -0
  32. data/lib/puppet-strings/yard/handlers/ruby/type_handler.rb +194 -0
  33. data/lib/puppet-strings/yard/parsers.rb +7 -0
  34. data/lib/puppet-strings/yard/parsers/puppet/parser.rb +70 -0
  35. data/lib/puppet-strings/yard/parsers/puppet/statement.rb +146 -0
  36. data/lib/puppet-strings/yard/tags.rb +6 -0
  37. data/lib/puppet-strings/yard/tags/overload_tag.rb +109 -0
  38. data/lib/puppet-strings/yard/tags/parameter_directive.rb +24 -0
  39. data/lib/puppet-strings/yard/tags/property_directive.rb +24 -0
  40. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_class.erb +9 -0
  41. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_defined_type.erb +9 -0
  42. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_function.erb +10 -0
  43. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_provider.erb +10 -0
  44. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_type.erb +9 -0
  45. data/lib/puppet-strings/yard/templates/default/fulldoc/html/setup.rb +64 -0
  46. data/lib/puppet-strings/yard/templates/default/layout/html/objects.erb +35 -0
  47. data/lib/puppet-strings/yard/templates/default/layout/html/setup.rb +172 -0
  48. data/lib/puppet-strings/yard/templates/default/puppet_class/html/box_info.erb +26 -0
  49. data/lib/puppet-strings/yard/templates/default/puppet_class/html/header.erb +1 -0
  50. data/lib/puppet-strings/yard/templates/default/puppet_class/html/overview.erb +6 -0
  51. data/lib/puppet-strings/yard/templates/default/puppet_class/html/setup.rb +14 -0
  52. data/lib/puppet-strings/yard/templates/default/puppet_class/html/source.erb +12 -0
  53. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/box_info.erb +10 -0
  54. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/header.erb +1 -0
  55. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/overview.erb +6 -0
  56. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/setup.rb +5 -0
  57. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/source.erb +12 -0
  58. data/lib/puppet-strings/yard/templates/default/puppet_function/html/box_info.erb +14 -0
  59. data/lib/puppet-strings/yard/templates/default/puppet_function/html/header.erb +1 -0
  60. data/lib/puppet-strings/yard/templates/default/puppet_function/html/overview.erb +18 -0
  61. data/lib/puppet-strings/yard/templates/default/puppet_function/html/setup.rb +5 -0
  62. data/lib/puppet-strings/yard/templates/default/puppet_function/html/source.erb +12 -0
  63. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/box_info.erb +14 -0
  64. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/collection.erb +10 -0
  65. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/features.erb +12 -0
  66. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/header.erb +1 -0
  67. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/overview.erb +6 -0
  68. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/setup.rb +29 -0
  69. data/lib/puppet-strings/yard/templates/default/puppet_type/html/box_info.erb +20 -0
  70. data/lib/puppet-strings/yard/templates/default/puppet_type/html/features.erb +13 -0
  71. data/lib/puppet-strings/yard/templates/default/puppet_type/html/header.erb +1 -0
  72. data/lib/puppet-strings/yard/templates/default/puppet_type/html/overview.erb +6 -0
  73. data/lib/puppet-strings/yard/templates/default/puppet_type/html/parameters.erb +35 -0
  74. data/lib/puppet-strings/yard/templates/default/puppet_type/html/setup.rb +32 -0
  75. data/lib/puppet-strings/yard/templates/default/tags/html/puppet_overload.erb +12 -0
  76. data/lib/puppet-strings/yard/templates/default/tags/setup.rb +15 -0
  77. data/lib/puppet/application/strings.rb +1 -0
  78. data/lib/puppet/face/strings.rb +80 -39
  79. data/spec/acceptance/emit_json_options.rb +41 -0
  80. data/spec/acceptance/lib/util.rb +15 -0
  81. data/spec/acceptance/running_strings_generate.rb +54 -0
  82. data/spec/fixtures/acceptance/modules/test/functions/add.pp +9 -0
  83. data/spec/fixtures/acceptance/modules/test/lib/puppet/functions/4x_function.rb +5 -0
  84. data/spec/fixtures/acceptance/modules/test/lib/puppet/parser/functions/function3x.rb +2 -0
  85. data/spec/fixtures/acceptance/modules/test/lib/puppet/provider/server/linux.rb +9 -0
  86. data/spec/fixtures/acceptance/modules/test/lib/puppet/type/database.rb +15 -0
  87. data/spec/fixtures/acceptance/modules/test/manifests/init.pp +27 -0
  88. data/spec/fixtures/acceptance/modules/test/manifests/triple_nested_classes.pp +27 -0
  89. data/spec/fixtures/acceptance/modules/test/metadata.json +6 -0
  90. data/spec/fixtures/unit/json/output.json +348 -0
  91. data/spec/fixtures/unit/json/output_without_puppet_function.json +301 -0
  92. data/spec/spec_helper.rb +21 -0
  93. data/spec/spec_helper_acceptance.rb +27 -0
  94. data/spec/unit/puppet-strings/json_spec.rb +136 -0
  95. data/spec/unit/puppet-strings/yard/handlers/puppet/class_handler_spec.rb +155 -0
  96. data/spec/unit/puppet-strings/yard/handlers/puppet/defined_type_handler_spec.rb +155 -0
  97. data/spec/unit/puppet-strings/yard/handlers/puppet/function_handler_spec.rb +169 -0
  98. data/spec/unit/puppet-strings/yard/handlers/ruby/function_handler_spec.rb +613 -0
  99. data/spec/unit/puppet-strings/yard/handlers/ruby/provider_handler_spec.rb +90 -0
  100. data/spec/unit/puppet-strings/yard/handlers/ruby/type_handler_spec.rb +214 -0
  101. data/spec/unit/puppet-strings/yard/parsers/puppet/parser_spec.rb +171 -0
  102. metadata +115 -92
  103. data/lib/puppet-strings/rake_tasks.rb +0 -18
  104. data/lib/puppet_x/puppetlabs/strings.rb +0 -64
  105. data/lib/puppet_x/puppetlabs/strings/actions.rb +0 -92
  106. data/lib/puppet_x/puppetlabs/strings/pops/yard_statement.rb +0 -79
  107. data/lib/puppet_x/puppetlabs/strings/pops/yard_transformer.rb +0 -47
  108. data/lib/puppet_x/puppetlabs/strings/util.rb +0 -65
  109. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/defined_type_object.rb +0 -33
  110. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/host_class_object.rb +0 -22
  111. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/method_object.rb +0 -62
  112. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/provider_object.rb +0 -24
  113. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/puppet_namespace_object.rb +0 -48
  114. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/type_object.rb +0 -42
  115. data/lib/puppet_x/puppetlabs/strings/yard/core_ext/yard.rb +0 -40
  116. data/lib/puppet_x/puppetlabs/strings/yard/handlers/base.rb +0 -13
  117. data/lib/puppet_x/puppetlabs/strings/yard/handlers/defined_type_handler.rb +0 -31
  118. data/lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb +0 -80
  119. data/lib/puppet_x/puppetlabs/strings/yard/handlers/host_class_handler.rb +0 -42
  120. data/lib/puppet_x/puppetlabs/strings/yard/handlers/provider_handler.rb +0 -95
  121. data/lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_3x_function_handler.rb +0 -54
  122. data/lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb +0 -234
  123. data/lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb +0 -295
  124. data/lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb +0 -85
  125. data/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb +0 -68
  126. data/lib/puppet_x/puppetlabs/strings/yard/parser.rb +0 -30
  127. data/lib/puppet_x/puppetlabs/strings/yard/tags/directives.rb +0 -9
  128. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/docstring.erb +0 -34
  129. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/header.erb +0 -5
  130. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/parameter_details.erb +0 -6
  131. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/setup.rb +0 -1
  132. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/setup.rb +0 -49
  133. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_class.erb +0 -2
  134. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_manifest.erb +0 -1
  135. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_plugin.erb +0 -21
  136. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_provider.erb +0 -1
  137. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_type.erb +0 -1
  138. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/setup.rb +0 -82
  139. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/box_info.erb +0 -22
  140. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/setup.rb +0 -1
  141. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/subclasses.erb +0 -4
  142. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/setup.rb +0 -21
  143. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/html_helper.rb +0 -139
  144. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/layout/html/setup.rb +0 -18
  145. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/html/header.erb +0 -17
  146. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/setup.rb +0 -21
  147. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/text/header.erb +0 -2
  148. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/command_details.erb +0 -8
  149. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/confine_details.erb +0 -10
  150. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/default_details.erb +0 -10
  151. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/docstring.erb +0 -34
  152. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/feature_details.erb +0 -10
  153. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/header.erb +0 -5
  154. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/setup.rb +0 -1
  155. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/setup.rb +0 -50
  156. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/box_info.erb +0 -11
  157. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/header.erb +0 -5
  158. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/method_details_list.erb +0 -53
  159. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/method_summary.erb +0 -20
  160. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/setup.rb +0 -1
  161. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/setup.rb +0 -91
  162. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/template_helper.rb +0 -192
  163. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/docstring.erb +0 -34
  164. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/header.erb +0 -5
  165. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/parameter_details.erb +0 -12
  166. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/provider_details.erb +0 -10
  167. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/setup.rb +0 -1
  168. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/setup.rb +0 -55
@@ -0,0 +1,63 @@
1
+ # The root module for Puppet Strings.
2
+ module PuppetStrings
3
+ # The glob patterns used to search for files to document.
4
+ DEFAULT_SEARCH_PATTERNS = %w(
5
+ manifests/**/*.pp
6
+ functions/**/*.pp
7
+ types/**/*.pp
8
+ lib/**/*.rb
9
+ ).freeze
10
+
11
+ # Generates documentation.
12
+ # @param [Array<String>] search_patterns The search patterns (e.g. manifests/**/*.pp) to look for files.
13
+ # @param [Hash] options The options hash.
14
+ # @option options [Boolean] :debug Enable YARD debug output.
15
+ # @option options [Boolean] :backtrace Enable YARD backtraces.
16
+ # @option options [String] :markup The YARD markup format to use (defaults to 'markdown').
17
+ # @option options [String] :json Enables JSON output to the given file. If the file is nil, STDOUT is used.
18
+ # @option options [Array<String>] :yard_args The arguments to pass to yard.
19
+ # @return [void]
20
+ def self.generate(search_patterns = DEFAULT_SEARCH_PATTERNS, options = {})
21
+ require 'puppet-strings/yard'
22
+ PuppetStrings::Yard.setup!
23
+
24
+ # Format the arguments to YARD
25
+ args = ['doc']
26
+ args << '--debug' if options[:debug]
27
+ args << '--backtrace' if options[:backtrace]
28
+ args << "-m#{options[:markup] || 'markdown'}"
29
+
30
+ render_as_json = options.key? :json
31
+ json_file = nil
32
+ if render_as_json
33
+ json_file = options[:json]
34
+ # Disable output and prevent stats/progress when writing to STDOUT
35
+ args << '-n'
36
+ args << '-q' unless json_file
37
+ args << '--no-stats' unless json_file
38
+ args << '--no-progress' unless json_file
39
+ end
40
+
41
+ yard_args = options[:yard_args]
42
+ args += yard_args if yard_args
43
+ args += search_patterns
44
+
45
+ # Run YARD
46
+ YARD::CLI::Yardoc.run(*args)
47
+
48
+ # If outputting JSON, render the output
49
+ if render_as_json
50
+ require 'puppet-strings/json'
51
+ PuppetStrings::Json.render(json_file)
52
+ end
53
+ end
54
+
55
+ # Runs the YARD documentation server.
56
+ # @param [Array<String>] args The arguments to YARD.
57
+ def self.run_server(*args)
58
+ require 'puppet-strings/yard'
59
+ PuppetStrings::Yard.setup!
60
+
61
+ YARD::CLI::Server.run(*args)
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+
3
+ # The module for JSON related functionality.
4
+ module PuppetStrings::Json
5
+ # Renders the current YARD registry as JSON to the given file (or STDOUT if nil).
6
+ # @param [String] file The path to the output file to render the registry to. If nil, output will be to STDOUT.
7
+ # @return [void]
8
+ def self.render(file = nil)
9
+ document = {
10
+ puppet_classes: YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash),
11
+ defined_types: YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash),
12
+ resource_types: YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash),
13
+ providers: YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash),
14
+ puppet_functions: YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash),
15
+ # TODO: Need Ruby documentation?
16
+ }
17
+
18
+ if file
19
+ File.open(file, 'w') do |f|
20
+ f.write(JSON.pretty_generate(document))
21
+ f.write("\n")
22
+ end
23
+ else
24
+ puts JSON.pretty_generate(document)
25
+ end
26
+ end
27
+
28
+ # Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
29
+ # @param [YARD::Docstring, String] docstring The docstring to convert to a hash.
30
+ # @return [Hash] Returns a hash representation of the given docstring.
31
+ def self.docstring_to_hash(docstring)
32
+ hash = {}
33
+ hash[:text] = docstring
34
+ if docstring.is_a? YARD::Docstring
35
+ # Skip over the API tags that are public
36
+ tags = docstring.tags.select { |t| t.tag_name != 'api' || t.text != 'public' }.map do |t|
37
+ next t.to_hash if t.respond_to?(:to_hash)
38
+
39
+ tag = { tag_name: t.tag_name }
40
+ tag[:text] = t.text if t.text
41
+ tag[:types] = t.types if t.types
42
+ tag[:name] = t.name if t.name
43
+ tag
44
+ end
45
+ hash[:tags] = tags unless tags.empty?
46
+ end
47
+ hash
48
+ end
49
+ end
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module PuppetStrings
5
+ # The module for Puppet Strings rake tasks.
6
+ module Tasks
7
+ require 'puppet-strings/tasks/generate.rb'
8
+ require 'puppet-strings/tasks/gh_pages.rb'
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ require 'puppet-strings'
2
+
3
+ # Implements the strings:generate task.
4
+ namespace :strings do
5
+ desc 'Generate Puppet documentation with YARD.'
6
+ task :generate, :patterns, :debug, :backtrace, :markup, :json, :yard_args do |t, args|
7
+ patterns = args[:patterns]
8
+ patterns = patterns.split if patterns
9
+ patterns ||= PuppetStrings::DEFAULT_SEARCH_PATTERNS
10
+
11
+ options = {
12
+ debug: args[:debug] == 'true',
13
+ backtrace: args[:backtrace] == 'true',
14
+ markup: args[:markup] || 'markdown',
15
+ }
16
+
17
+ options[:json] = args[:json] if args.key? :json
18
+ options[:yard_args] = args[:yard_args].split if args.key? :yard_args
19
+
20
+ PuppetStrings.generate(patterns, options)
21
+ end
22
+ end
23
+
@@ -0,0 +1,43 @@
1
+ require 'puppet-strings/tasks'
2
+
3
+ namespace :strings do
4
+ namespace :gh_pages do
5
+ desc 'Checkout the gh-pages branch for doc generation.'
6
+ task :checkout do
7
+ if Dir.exist?('doc')
8
+ fail "The 'doc' directory (#{File.expand_path('doc')}) is not a Git repository! Remove it and run the Rake task again." unless Dir.exist?('doc/.git')
9
+ Dir.chdir('doc') do
10
+ system 'git checkout gh-pages'
11
+ system 'git pull --rebase origin gh-pages'
12
+ end
13
+ else
14
+ git_uri = `git config --get remote.origin.url`.strip
15
+ fail "Could not determine the remote URL for origin: ensure the current directory is a Git repro with a remote named 'origin'." unless $?.success?
16
+
17
+ Dir.mkdir('doc')
18
+ Dir.chdir('doc') do
19
+ system 'git init'
20
+ system "git remote add origin #{git_uri}"
21
+ system 'git pull origin gh-pages'
22
+ system 'git checkout -b gh-pages'
23
+ end
24
+ end
25
+ end
26
+
27
+ desc 'Push new docs to GitHub.'
28
+ task :push do
29
+ Dir.chdir('doc') do
30
+ system 'git add .'
31
+ system "git commit -m '[strings] Generated Documentation Update'"
32
+ system 'git push origin gh-pages -f'
33
+ end
34
+ end
35
+
36
+ desc 'Run checkout, generate, and push tasks.'
37
+ task :update => [
38
+ :checkout,
39
+ :'strings:generate',
40
+ :push,
41
+ ]
42
+ end
43
+ end
@@ -0,0 +1,96 @@
1
+ require 'yard'
2
+
3
+ # Module for YARD related functionality.
4
+ module PuppetStrings::Yard
5
+ require 'puppet-strings/yard/code_objects'
6
+ require 'puppet-strings/yard/handlers'
7
+ require 'puppet-strings/yard/tags'
8
+ require 'puppet-strings/yard/parsers'
9
+
10
+ # Sets up YARD for use with puppet-strings.
11
+ # @return [void]
12
+ def self.setup!
13
+ # Register the template path
14
+ YARD::Templates::Engine.register_template_path(File.join(File.dirname(__FILE__), 'yard', 'templates'))
15
+
16
+ # Register the Puppet parser
17
+ YARD::Parser::SourceParser.register_parser_type(:puppet, PuppetStrings::Yard::Parsers::Puppet::Parser, ['pp'])
18
+
19
+ # Register our handlers
20
+ YARD::Handlers::Processor.register_handler_namespace(:puppet, PuppetStrings::Yard::Handlers::Puppet)
21
+ YARD::Handlers::Processor.register_handler_namespace(:puppet_ruby, PuppetStrings::Yard::Handlers::Ruby)
22
+
23
+ # Register the tag directives
24
+ PuppetStrings::Yard::Tags::ParameterDirective.register!
25
+ PuppetStrings::Yard::Tags::PropertyDirective.register!
26
+
27
+ # Ignore documentation on Puppet DSL calls
28
+ # This prevents the YARD DSL parser from emitting warnings for Puppet's Ruby DSL
29
+ YARD::Handlers::Ruby::DSLHandlerMethods::IGNORE_METHODS['create_function'] = true
30
+ YARD::Handlers::Ruby::DSLHandlerMethods::IGNORE_METHODS['newtype'] = true
31
+ end
32
+ end
33
+
34
+ # Monkey patch YARD::CLI::Yardoc#all_objects to return our custom code objects.
35
+ # @private
36
+ class YARD::CLI::Yardoc
37
+ def all_objects
38
+ YARD::Registry.all(
39
+ :root,
40
+ :module,
41
+ :class,
42
+ :puppet_class,
43
+ :puppet_defined_type,
44
+ :puppet_type,
45
+ :puppet_provider,
46
+ :puppet_function
47
+ )
48
+ end
49
+ end
50
+
51
+ # Monkey patch the stats object to return statistics for our objects.
52
+ # This is the recommended way to add custom stats.
53
+ # @private
54
+ class YARD::CLI::Stats
55
+ def stats_for_puppet_classes
56
+ output 'Puppet Classes', *type_statistics_all(:puppet_class)
57
+ end
58
+
59
+ def stats_for_puppet_defined_types
60
+ output 'Puppet Defined Types', *type_statistics_all(:puppet_defined_type)
61
+ end
62
+
63
+ def stats_for_puppet_types
64
+ output 'Puppet Types', *type_statistics_all(:puppet_type)
65
+ end
66
+
67
+ def stats_for_puppet_providers
68
+ output 'Puppet Providers', *type_statistics_all(:puppet_provider)
69
+ end
70
+
71
+ def stats_for_puppet_functions
72
+ output 'Puppet Functions', *type_statistics_all(:puppet_function)
73
+ end
74
+
75
+ def output(name, data, undoc = nil)
76
+ # Monkey patch output to accommodate our larger header widths
77
+ @total += data if data.is_a?(Integer) && undoc
78
+ @undocumented += undoc if undoc.is_a?(Integer)
79
+ data =
80
+ if undoc
81
+ ('%5s (% 5d undocumented)' % [data, undoc])
82
+ else
83
+ '%5s' % data
84
+ end
85
+ log.puts('%-21s %s' % [name + ':', data])
86
+ end
87
+
88
+ # This differs from the YARD implementation in that it considers
89
+ # a docstring without text but with tags to be undocumented.
90
+ def type_statistics_all(type)
91
+ objs = all_objects.select {|m| m.type == type }
92
+ undoc = objs.find_all {|m| m.docstring.all.empty? }
93
+ @undoc_list |= undoc if @undoc_list
94
+ [objs.size, undoc.size]
95
+ end
96
+ end
@@ -0,0 +1,8 @@
1
+ # The module for custom YARD code objects.
2
+ module PuppetStrings::Yard::CodeObjects
3
+ require 'puppet-strings/yard/code_objects/class'
4
+ require 'puppet-strings/yard/code_objects/defined_type'
5
+ require 'puppet-strings/yard/code_objects/type'
6
+ require 'puppet-strings/yard/code_objects/provider'
7
+ require 'puppet-strings/yard/code_objects/function'
8
+ end
@@ -0,0 +1,14 @@
1
+ # Implements the base code object.
2
+ class PuppetStrings::Yard::CodeObjects::Base < YARD::CodeObjects::NamespaceObject
3
+ # Allocates a new code object.
4
+ # @param [Array] args The arguments to initialize the code object with.
5
+ # @return Returns the code object.
6
+ def self.new(*args)
7
+ # Skip the super class' implementation because it detects :: in names and this will cause namespaces in the output we don't want
8
+ object = Object.class.instance_method(:new).bind(self).call(*args)
9
+ existing = YARD::Registry.at(object.path)
10
+ object = existing if existing && existing.class == self
11
+ yield(object) if block_given?
12
+ object
13
+ end
14
+ end
@@ -0,0 +1,59 @@
1
+ require 'puppet-strings/yard/code_objects/group'
2
+
3
+ # Implements the group for Puppet classes.
4
+ class PuppetStrings::Yard::CodeObjects::Classes < PuppetStrings::Yard::CodeObjects::Group
5
+ # Gets the singleton instance of the group.
6
+ # @return Returns the singleton instance of the group.
7
+ def self.instance
8
+ super(:puppet_classes)
9
+ end
10
+
11
+ # Gets the display name of the group.
12
+ # @param [Boolean] prefix whether to show a prefix. Ignored for Puppet group namespaces.
13
+ # @return [String] Returns the display name of the group.
14
+ def name(prefix = false)
15
+ 'Puppet Classes'
16
+ end
17
+ end
18
+
19
+ # Implements the Puppet class code object.
20
+ class PuppetStrings::Yard::CodeObjects::Class < PuppetStrings::Yard::CodeObjects::Base
21
+ attr_reader :statement
22
+ attr_reader :parameters
23
+
24
+ # Initializes a Puppet class code object.
25
+ # @param [PuppetStrings::Parsers::ClassStatement] statement The class statement that was parsed.
26
+ # @return [void]
27
+ def initialize(statement)
28
+ @statement = statement
29
+ @parameters = statement.parameters.map { |p| [p.name, p.value] }
30
+ super(PuppetStrings::Yard::CodeObjects::Classes.instance, statement.name)
31
+ end
32
+
33
+ # Gets the type of the code object.
34
+ # @return Returns the type of the code object.
35
+ def type
36
+ :puppet_class
37
+ end
38
+
39
+ # Gets the source of the code object.
40
+ # @return Returns the source of the code object.
41
+ def source
42
+ @statement.source
43
+ end
44
+
45
+ # Converts the code object to a hash representation.
46
+ # @return [Hash] Returns a hash representation of the code object.
47
+ def to_hash
48
+ hash = {}
49
+ hash[:name] = name
50
+ hash[:file] = file
51
+ hash[:line] = line
52
+ hash[:inherits] = statement.parent_class if statement.parent_class
53
+ hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
54
+ defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
55
+ hash[:defaults] = defaults unless defaults.empty?
56
+ hash[:source] = source unless source && source.empty?
57
+ hash
58
+ end
59
+ end
@@ -0,0 +1,58 @@
1
+ require 'puppet-strings/yard/code_objects/group'
2
+
3
+ # Implements the group for Puppet defined types.
4
+ class PuppetStrings::Yard::CodeObjects::DefinedTypes < PuppetStrings::Yard::CodeObjects::Group
5
+ # Gets the singleton instance of the group.
6
+ # @return Returns the singleton instance of the group.
7
+ def self.instance
8
+ super(:puppet_defined_types)
9
+ end
10
+
11
+ # Gets the display name of the group.
12
+ # @param [Boolean] prefix whether to show a prefix. Ignored for Puppet group namespaces.
13
+ # @return [String] Returns the display name of the group.
14
+ def name(prefix = false)
15
+ 'Defined Types'
16
+ end
17
+ end
18
+
19
+ # Implements the Puppet defined type code object.
20
+ class PuppetStrings::Yard::CodeObjects::DefinedType < PuppetStrings::Yard::CodeObjects::Base
21
+ attr_reader :statement
22
+ attr_reader :parameters
23
+
24
+ # Initializes a Puppet defined type code object.
25
+ # @param [PuppetStrings::Parsers::DefinedTypeStatement] statement The defined type statement that was parsed.
26
+ # @return [void]
27
+ def initialize(statement)
28
+ @statement = statement
29
+ @parameters = statement.parameters.map { |p| [p.name, p.value] }
30
+ super(PuppetStrings::Yard::CodeObjects::DefinedTypes.instance, statement.name)
31
+ end
32
+
33
+ # Gets the type of the code object.
34
+ # @return Returns the type of the code object.
35
+ def type
36
+ :puppet_defined_type
37
+ end
38
+
39
+ # Gets the source of the code object.
40
+ # @return Returns the source of the code object.
41
+ def source
42
+ @statement.source
43
+ end
44
+
45
+ # Converts the code object to a hash representation.
46
+ # @return [Hash] Returns a hash representation of the code object.
47
+ def to_hash
48
+ hash = {}
49
+ hash[:name] = name
50
+ hash[:file] = file
51
+ hash[:line] = line
52
+ hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
53
+ defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
54
+ hash[:defaults] = defaults unless defaults.empty?
55
+ hash[:source] = source unless source && source.empty?
56
+ hash
57
+ end
58
+ end
@@ -0,0 +1,93 @@
1
+ require 'puppet-strings/yard/code_objects/group'
2
+
3
+ # Implements the group for Puppet functions.
4
+ class PuppetStrings::Yard::CodeObjects::Functions < PuppetStrings::Yard::CodeObjects::Group
5
+ # Gets the singleton instance of the group.
6
+ # @param [Symbol] type The function type to get the group for.
7
+ # @return Returns the singleton instance of the group.
8
+ def self.instance(type)
9
+ super("puppet_functions_#{type}".intern)
10
+ end
11
+
12
+ # Gets the display name of the group.
13
+ # @param [Boolean] prefix whether to show a prefix. Ignored for Puppet group namespaces.
14
+ # @return [String] Returns the display name of the group.
15
+ def name(prefix = false)
16
+ 'Puppet Functions'
17
+ end
18
+ end
19
+
20
+ # Implements the Puppet function code object.
21
+ class PuppetStrings::Yard::CodeObjects::Function < PuppetStrings::Yard::CodeObjects::Base
22
+ # Identifier for 3.x Ruby API functions
23
+ RUBY_3X = :ruby3x
24
+ # Identifier for 4.x Ruby API functions
25
+ RUBY_4X = :ruby4x
26
+ # Identifier for Puppet language functions
27
+ PUPPET = :puppet
28
+
29
+ attr_accessor :parameters
30
+
31
+ # Initializes a Puppet function code object.
32
+ # @param [String] name The name of the function.
33
+ # @param [Symbol] function_type The type of function (e.g. :ruby3x, :ruby4x, :puppet)
34
+ # @return [void]
35
+ def initialize(name, function_type)
36
+ super(PuppetStrings::Yard::CodeObjects::Functions.instance(function_type), name)
37
+ @parameters = []
38
+ @function_type = function_type
39
+ end
40
+
41
+ # Gets the type of the code object.
42
+ # @return Returns the type of the code object.
43
+ def type
44
+ :puppet_function
45
+ end
46
+
47
+ # Gets the function type display string.
48
+ # @return Returns the function type display string.
49
+ def function_type
50
+ case @function_type
51
+ when RUBY_3X
52
+ 'Ruby 3.x API'
53
+ when RUBY_4X
54
+ 'Ruby 4.x API'
55
+ else
56
+ 'Puppet Language'
57
+ end
58
+ end
59
+
60
+ # Gets the Puppet signature of the function (single overload only).
61
+ # @return [String] Returns the Puppet signature of the function.
62
+ def signature
63
+ return '' if self.has_tag? :overload
64
+ tags = self.tags(:param)
65
+ args = @parameters.map do |parameter|
66
+ name, default = parameter
67
+ tag = tags.find { |tag| tag.name == name } if tags
68
+ type = tag && tag.types ? "#{tag.type} " : 'Any '
69
+ prefix = "#{name[0]}" if name.start_with?('*', '&')
70
+ name = name[1..-1] if prefix
71
+ default = " = #{default}" if default
72
+ "#{type}#{prefix}$#{name}#{default}"
73
+ end.join(', ')
74
+ @name.to_s + '(' + args + ')'
75
+ end
76
+
77
+ # Converts the code object to a hash representation.
78
+ # @return [Hash] Returns a hash representation of the code object.
79
+ def to_hash
80
+ hash = {}
81
+ hash[:name] = name
82
+ hash[:file] = file
83
+ hash[:line] = line
84
+ hash[:type] = @function_type.to_s
85
+ signature = self.signature
86
+ hash[:signature] = signature unless signature.empty?
87
+ hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
88
+ defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
89
+ hash[:defaults] = defaults unless defaults.empty?
90
+ hash[:source] = source unless source && source.empty?
91
+ hash
92
+ end
93
+ end