puppet-strings 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +18 -3
  3. data/Gemfile +3 -2
  4. data/Rakefile +5 -0
  5. data/lib/puppet-strings.rb +10 -1
  6. data/lib/puppet-strings/describe.rb +68 -0
  7. data/lib/puppet-strings/json.rb +0 -38
  8. data/lib/puppet-strings/markdown/base.rb +18 -16
  9. data/lib/puppet-strings/markdown/templates/function.erb +26 -0
  10. data/lib/puppet-strings/markdown/templates/resource_type.erb +4 -0
  11. data/lib/puppet-strings/tasks/generate.rb +7 -1
  12. data/lib/puppet-strings/version.rb +1 -1
  13. data/lib/puppet-strings/yard/code_objects/class.rb +1 -1
  14. data/lib/puppet-strings/yard/code_objects/defined_type.rb +1 -1
  15. data/lib/puppet-strings/yard/code_objects/function.rb +3 -3
  16. data/lib/puppet-strings/yard/code_objects/plan.rb +1 -1
  17. data/lib/puppet-strings/yard/code_objects/provider.rb +1 -1
  18. data/lib/puppet-strings/yard/code_objects/type.rb +3 -2
  19. data/lib/puppet-strings/yard/handlers.rb +1 -0
  20. data/lib/puppet-strings/yard/handlers/ruby/base.rb +11 -0
  21. data/lib/puppet-strings/yard/handlers/ruby/function_handler.rb +1 -9
  22. data/lib/puppet-strings/yard/handlers/ruby/provider_handler.rb +1 -9
  23. data/lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb +1 -1
  24. data/lib/puppet-strings/yard/handlers/ruby/type_base.rb +130 -0
  25. data/lib/puppet-strings/yard/handlers/ruby/type_extras_handler.rb +56 -0
  26. data/lib/puppet-strings/yard/handlers/ruby/type_handler.rb +3 -115
  27. data/lib/puppet-strings/yard/parsers/json/parser.rb +3 -1
  28. data/lib/puppet-strings/yard/tags/overload_tag.rb +1 -1
  29. data/lib/puppet-strings/yard/util.rb +48 -0
  30. data/lib/puppet/face/strings.rb +66 -1
  31. data/spec/fixtures/unit/markdown/output.md +64 -0
  32. data/spec/fixtures/unit/markdown/output_with_plan.md +64 -0
  33. data/spec/spec_helper.rb +1 -0
  34. data/spec/unit/puppet-strings/describe_spec.rb +141 -0
  35. data/spec/unit/puppet-strings/json_spec.rb +65 -11
  36. data/spec/unit/puppet-strings/markdown_spec.rb +13 -0
  37. data/spec/unit/puppet-strings/yard/handlers/json/task_handler_spec.rb +4 -12
  38. data/spec/unit/puppet-strings/yard/handlers/ruby/function_handler_spec.rb +1 -1
  39. data/spec/unit/puppet-strings/yard/handlers/ruby/rsapi_handler_spec.rb +21 -0
  40. data/spec/unit/puppet-strings/yard/handlers/ruby/type_handler_spec.rb +26 -0
  41. data/spec/unit/puppet-strings/yard/parsers/json/parser_spec.rb +5 -3
  42. metadata +7 -6
  43. data/spec/fixtures/unit/json/output.json +0 -660
  44. data/spec/fixtures/unit/json/output_with_plan.json +0 -697
  45. data/spec/fixtures/unit/json/output_without_puppet_function.json +0 -480
@@ -99,7 +99,7 @@ class PuppetStrings::Yard::Tags::OverloadTag < YARD::Tags::Tag
99
99
  hash[:tag_name] = tag_name
100
100
  hash[:text] = text if text
101
101
  hash[:signature] = signature
102
- hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring) if !docstring.blank?
102
+ hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring) if !docstring.blank?
103
103
  defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
104
104
  hash[:defaults] = defaults unless defaults.empty?
105
105
  hash[:types] = types if types
@@ -28,4 +28,52 @@ module PuppetStrings::Yard::Util
28
28
  end
29
29
  data
30
30
  end
31
+
32
+ # Converts a list of tags into an array of hashes.
33
+ # @param [Array] tags List of tags to be converted into an array of hashes.
34
+ # @return [Array] Returns an array of tag hashes.
35
+ def self.tags_to_hashes(tags)
36
+ # Skip over the API tags that are public
37
+ tags.select { |t| (t.tag_name != 'api' || t.text != 'public') }.map do |t|
38
+ next t.to_hash if t.respond_to?(:to_hash)
39
+
40
+ tag = { tag_name: t.tag_name }
41
+ # grab nested information for @option tags
42
+ if tag[:tag_name] == 'option'
43
+ tag[:opt_name] = t.pair.name
44
+ tag[:opt_text] = t.pair.text
45
+ tag[:opt_types] = t.pair.types
46
+ tag[:parent] = t.name
47
+ end
48
+ tag[:text] = t.text if t.text
49
+ tag[:types] = t.types if t.types
50
+ tag[:name] = t.name if t.name
51
+ tag
52
+ end
53
+ end
54
+
55
+ # Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
56
+ # @param [YARD::Docstring, String] docstring The docstring to convert to a hash.
57
+ # @param [Array] select_tags List of tags to select. Other tags will be filtered out.
58
+ # @return [Hash] Returns a hash representation of the given docstring.
59
+ def self.docstring_to_hash(docstring, select_tags=nil)
60
+ hash = {}
61
+ hash[:text] = docstring
62
+
63
+ if docstring.is_a? YARD::Docstring
64
+ tags = tags_to_hashes(docstring.tags.select { |t| select_tags.nil? || select_tags.include?(t.tag_name.to_sym) })
65
+
66
+ unless tags.empty?
67
+ hash[:tags] = tags
68
+ # .sort_by do |tag|
69
+ # sort_key = tag[:tag_name].dup
70
+ # sort_key << "-#{tag[:name]}" if tag[:name]
71
+ # sort_key << "-#{tag[:opt_name]}" if tag[:opt_name]
72
+ # sort_key
73
+ # end
74
+ end
75
+ end
76
+
77
+ hash
78
+ end
31
79
  end
@@ -81,6 +81,65 @@ Puppet::Face.define(:strings, '0.0.1') do
81
81
  end
82
82
  end
83
83
 
84
+ action(:describe) do #This is Kris' experiment with string based describe
85
+ option "--list" do
86
+ summary "list types"
87
+ end
88
+ option "--providers" do
89
+ summary "provide details on providers"
90
+ end
91
+
92
+ #TODO: Implement the rest of describe behavior
93
+ # * --help:
94
+ # Print this help text
95
+
96
+ # * --providers:
97
+ # Describe providers in detail for each type
98
+
99
+ # * --list:
100
+ # List all types
101
+
102
+ # * --meta:
103
+ # List all metaparameters
104
+
105
+ # * --short:
106
+ # List only parameters without detail
107
+
108
+
109
+ when_invoked do |*args|
110
+ check_required_features
111
+ require 'puppet-strings'
112
+
113
+ options = args.last
114
+ options[:describe] = true
115
+ options[:stdout] = true
116
+ options[:format] = 'json'
117
+
118
+ if args.length > 1
119
+ if options[:list]
120
+ $stderr.puts "WARNING: ignoring types when listing all types."
121
+ else
122
+ options[:describe_types] = args[0..-2]
123
+ end
124
+ end
125
+
126
+ #TODO: Set up search_patterns and whatever else needed to collect data for describe - currently missing some
127
+ # manifests/**/*.pp
128
+ # functions/**/*.pp
129
+ # tasks/*.json
130
+ # plans/*.pp
131
+ search_patterns = %w(
132
+ types/**/*.pp
133
+ lib/**/*.rb
134
+ )
135
+ PuppetStrings::generate(
136
+ search_patterns,
137
+ build_generate_options(options)
138
+ )
139
+ nil
140
+ end
141
+ end
142
+
84
143
  # Checks that the required features are installed.
85
144
  # @return [void]
86
145
  def check_required_features
@@ -98,7 +157,6 @@ Puppet::Face.define(:strings, '0.0.1') do
98
157
  generate_options[:debug] = Puppet[:debug]
99
158
  generate_options[:backtrace] = Puppet[:trace]
100
159
  generate_options[:yard_args] = yard_args unless yard_args.empty?
101
-
102
160
  if options
103
161
  if options[:emit_json]
104
162
  $stderr.puts "WARNING: '--emit-json PATH' is deprecated. Use '--format json --out PATH' instead."
@@ -110,6 +168,13 @@ Puppet::Face.define(:strings, '0.0.1') do
110
168
  generate_options[:markup] = markup if markup
111
169
  generate_options[:path] = options[:out] if options[:out]
112
170
  generate_options[:stdout] = options[:stdout]
171
+
172
+ if options[:describe]
173
+ generate_options[:describe] = true
174
+ generate_options[:describe_types] = options[:describe_types]
175
+ generate_options[:describe_list] = options[:list]
176
+ end
177
+
113
178
  format = options[:format]
114
179
  if format
115
180
  if format.casecmp('markdown').zero?
@@ -266,6 +266,8 @@ The database server name.
266
266
 
267
267
  The encryption key to use.
268
268
 
269
+ Required features: encryption.
270
+
269
271
  ##### `encrypt`
270
272
 
271
273
  Valid values: `true`, `false`, yes, no
@@ -282,6 +284,14 @@ Type: Puppet Language
282
284
 
283
285
  A simple Puppet function.
284
286
 
287
+ #### Examples
288
+
289
+ ##### Test
290
+
291
+ ```puppet
292
+ $result = func(1, 2)
293
+ ```
294
+
285
295
  #### `func(Integer $param1, Any $param2, String $param3 = hi)`
286
296
 
287
297
  A simple Puppet function.
@@ -291,6 +301,14 @@ Returns: `Undef` Returns nothing.
291
301
  Raises:
292
302
  * `SomeError` this is some error
293
303
 
304
+ ##### Examples
305
+
306
+ ###### Test
307
+
308
+ ```puppet
309
+ $result = func(1, 2)
310
+ ```
311
+
294
312
  ##### `param1`
295
313
 
296
314
  Data type: `Integer`
@@ -319,12 +337,28 @@ Type: Ruby 3.x API
319
337
 
320
338
  Documentation for an example 3.x function.
321
339
 
340
+ #### Examples
341
+
342
+ ##### Calling the function.
343
+
344
+ ```puppet
345
+ func3x('hi', 10)
346
+ ```
347
+
322
348
  #### `func3x(String $param1, Integer $param2)`
323
349
 
324
350
  Documentation for an example 3.x function.
325
351
 
326
352
  Returns: `Undef`
327
353
 
354
+ ##### Examples
355
+
356
+ ###### Calling the function.
357
+
358
+ ```puppet
359
+ func3x('hi', 10)
360
+ ```
361
+
328
362
  ##### `param1`
329
363
 
330
364
  Data type: `String`
@@ -343,12 +377,34 @@ Type: Ruby 4.x API
343
377
 
344
378
  An example 4.x function.
345
379
 
380
+ #### Examples
381
+
382
+ ##### Calling the function
383
+
384
+ ```puppet
385
+ $result = func4x(1, 'foo')
386
+ ```
387
+
388
+ ##### Calling the function with all args
389
+
390
+ ```puppet
391
+ $result = func4x(1, 'foo', ['bar'])
392
+ ```
393
+
346
394
  #### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3)`
347
395
 
348
396
  An overview for the first overload.
349
397
 
350
398
  Returns: `Undef` Returns nothing.
351
399
 
400
+ ##### Examples
401
+
402
+ ###### Calling the function foo
403
+
404
+ ```puppet
405
+ $result = func4x(1, 'foooo')
406
+ ```
407
+
352
408
  ##### `param1`
353
409
 
354
410
  Data type: `Integer`
@@ -378,6 +434,14 @@ An overview for the second overload.
378
434
 
379
435
  Returns: `String` Returns a string.
380
436
 
437
+ ##### Examples
438
+
439
+ ###### Calling the function bar
440
+
441
+ ```puppet
442
+ $result = func4x(1, 'bar', ['foo'])
443
+ ```
444
+
381
445
  ##### `param`
382
446
 
383
447
  Data type: `Boolean`
@@ -270,6 +270,8 @@ The database server name.
270
270
 
271
271
  The encryption key to use.
272
272
 
273
+ Required features: encryption.
274
+
273
275
  ##### `encrypt`
274
276
 
275
277
  Valid values: `true`, `false`, yes, no
@@ -286,6 +288,14 @@ Type: Puppet Language
286
288
 
287
289
  A simple Puppet function.
288
290
 
291
+ #### Examples
292
+
293
+ ##### Test
294
+
295
+ ```puppet
296
+ $result = func(1, 2)
297
+ ```
298
+
289
299
  #### `func(Integer $param1, Any $param2, String $param3 = hi)`
290
300
 
291
301
  A simple Puppet function.
@@ -295,6 +305,14 @@ Returns: `Undef` Returns nothing.
295
305
  Raises:
296
306
  * `SomeError` this is some error
297
307
 
308
+ ##### Examples
309
+
310
+ ###### Test
311
+
312
+ ```puppet
313
+ $result = func(1, 2)
314
+ ```
315
+
298
316
  ##### `param1`
299
317
 
300
318
  Data type: `Integer`
@@ -323,12 +341,28 @@ Type: Ruby 3.x API
323
341
 
324
342
  Documentation for an example 3.x function.
325
343
 
344
+ #### Examples
345
+
346
+ ##### Calling the function.
347
+
348
+ ```puppet
349
+ func3x('hi', 10)
350
+ ```
351
+
326
352
  #### `func3x(String $param1, Integer $param2)`
327
353
 
328
354
  Documentation for an example 3.x function.
329
355
 
330
356
  Returns: `Undef`
331
357
 
358
+ ##### Examples
359
+
360
+ ###### Calling the function.
361
+
362
+ ```puppet
363
+ func3x('hi', 10)
364
+ ```
365
+
332
366
  ##### `param1`
333
367
 
334
368
  Data type: `String`
@@ -347,12 +381,34 @@ Type: Ruby 4.x API
347
381
 
348
382
  An example 4.x function.
349
383
 
384
+ #### Examples
385
+
386
+ ##### Calling the function
387
+
388
+ ```puppet
389
+ $result = func4x(1, 'foo')
390
+ ```
391
+
392
+ ##### Calling the function with all args
393
+
394
+ ```puppet
395
+ $result = func4x(1, 'foo', ['bar'])
396
+ ```
397
+
350
398
  #### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3)`
351
399
 
352
400
  An overview for the first overload.
353
401
 
354
402
  Returns: `Undef` Returns nothing.
355
403
 
404
+ ##### Examples
405
+
406
+ ###### Calling the function foo
407
+
408
+ ```puppet
409
+ $result = func4x(1, 'foooo')
410
+ ```
411
+
356
412
  ##### `param1`
357
413
 
358
414
  Data type: `Integer`
@@ -382,6 +438,14 @@ An overview for the second overload.
382
438
 
383
439
  Returns: `String` Returns a string.
384
440
 
441
+ ##### Examples
442
+
443
+ ###### Calling the function bar
444
+
445
+ ```puppet
446
+ $result = func4x(1, 'bar', ['foo'])
447
+ ```
448
+
385
449
  ##### `param`
386
450
 
387
451
  Data type: `Boolean`
@@ -17,6 +17,7 @@ end
17
17
 
18
18
  require 'mocha'
19
19
  require 'rspec'
20
+ require 'json_spec'
20
21
  require 'puppet/version'
21
22
  require 'puppet-strings'
22
23
  require 'puppet-strings/markdown'
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+ require 'puppet-strings/describe'
3
+ require 'tempfile'
4
+
5
+ #TODO:
6
+ #basic describe
7
+ #params from other files (e.g. file content)
8
+ #--providers - list providers in detail
9
+ #X--list - list all providers summary
10
+ #--meta - List all metaparameters
11
+ #--short - only list params
12
+
13
+ describe PuppetStrings::Describe do
14
+ before :each do
15
+ # Populate the YARD registry with both Puppet and Ruby source
16
+
17
+
18
+ YARD::Parser::SourceParser.parse_string(<<-SOURCE, :ruby)
19
+ Puppet::Type.newtype(:database) do
20
+ desc 'An example database server resource type.'
21
+ end
22
+ SOURCE
23
+
24
+ YARD::Parser::SourceParser.parse_string(<<-SOURCE, :ruby)
25
+ Puppet::ResourceApi.register_type(
26
+ name: 'apt_key',
27
+ docs: <<-EOS,
28
+ @summary Example resource type using the new API.
29
+ @raise SomeError
30
+ This type provides Puppet with the capabilities to manage GPG keys needed
31
+ by apt to perform package validation. Apt has it's own GPG keyring that can
32
+ be manipulated through the `apt-key` command.
33
+ @example here's an example
34
+ apt_key { '6F6B15509CF8E59E6E469F327F438280EF8D349F':
35
+ source => 'http://apt.puppetlabs.com/pubkey.gpg'
36
+ }
37
+
38
+ **Autorequires**:
39
+ If Puppet is given the location of a key file which looks like an absolute
40
+ path this type will autorequire that file.
41
+ EOS
42
+ )
43
+ SOURCE
44
+
45
+ YARD::Parser::SourceParser.parse_string(<<-SOURCE, :ruby)
46
+ Puppet::Type.type(:file).newproperty(:content) do
47
+ include Puppet::Util::Checksums
48
+ include Puppet::DataSync
49
+
50
+ attr_reader :actual_content
51
+
52
+ desc <<-'EOT'
53
+ The desired contents of a file, as a string. This attribute is mutually
54
+ exclusive with `source` and `target`.
55
+ EOT
56
+ end
57
+ SOURCE
58
+
59
+ YARD::Parser::SourceParser.parse_string(<<-SOURCE, :ruby)
60
+ Puppet::Type.newtype(:file) do
61
+ include Puppet::Util::Checksums
62
+ include Puppet::Util::Backups
63
+ include Puppet::Util::SymbolicFileMode
64
+
65
+ @doc = "Manages files, including their content, ownership, and permissions.
66
+
67
+ The `file` type can manage normal files, directories, and symlinks; the
68
+ type should be specified in the `ensure` attribute."
69
+
70
+ newparam(:path) do
71
+ desc <<-'EOT'
72
+ The path to the file to manage. Must be fully qualified.
73
+
74
+ On Windows, the path should include the drive letter and should use `/` as
75
+ the separator character (rather than `\\`).
76
+ EOT
77
+ isnamevar
78
+ end
79
+
80
+ end
81
+ SOURCE
82
+
83
+ YARD::Parser::SourceParser.parse_string(<<-SOURCE, :ruby)
84
+ Puppet::Type.type(:file).newproperty(:source) do
85
+ include Puppet::Util::Checksums
86
+ include Puppet::DataSync
87
+
88
+ attr_reader :actual_content
89
+
90
+ desc <<-'EOT'
91
+ The desired contents of a file, as a string. This attribute is mutually
92
+ exclusive with `source` and `target`.
93
+ EOT
94
+ end
95
+ SOURCE
96
+ end
97
+
98
+ describe 'rendering DESCRIBE to stdout' do
99
+ it 'should output the expected describe content for the list of types' do
100
+ output = <<-DATA
101
+ These are the types known to puppet:
102
+ apt_key - This type provides Puppet with the capabiliti ...
103
+ database - An example database server resource type.
104
+ file - Manages files, including their content, owner ...
105
+ DATA
106
+ expect{ PuppetStrings::Describe.render(nil, true) }.to output(output).to_stdout
107
+ end
108
+ it 'should output the expected describe content for a type' do
109
+ output = <<-DATA
110
+
111
+ file
112
+ ====
113
+ Manages files, including their content, ownership, and permissions.
114
+
115
+ The `file` type can manage normal files, directories, and symlinks; the
116
+ type should be specified in the `ensure` attribute.
117
+
118
+ Parameters
119
+ ----------
120
+
121
+ - **content**
122
+ The desired contents of a file, as a string. This attribute is mutually
123
+ exclusive with `source` and `target`.
124
+
125
+ - **path**
126
+ The path to the file to manage. Must be fully qualified.
127
+
128
+ On Windows, the path should include the drive letter and should use `/` as
129
+ the separator character (rather than `\\`).
130
+
131
+ - **source**
132
+ The desired contents of a file, as a string. This attribute is mutually
133
+ exclusive with `source` and `target`.
134
+
135
+ Providers
136
+ ---------
137
+ DATA
138
+ expect{ PuppetStrings::Describe.render(['file']) }.to output(output).to_stdout
139
+ end
140
+ end
141
+ end