Skip to content

Commit afe7515

Browse files
authored
Merge pull request #357 from puppetlabs/CONT-1193-finish_providers_implementation
2 parents 8d24a05 + 3e1b641 commit afe7515

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

Diff for: lib/puppet-strings.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def self.generate(search_patterns = DEFAULT_SEARCH_PATTERNS, options = {})
5555

5656
return unless options[:describe]
5757

58-
render_describe(options[:describe_types], options[:describe_list], options[:providers])
58+
render_describe(options[:describe_types], options[:describe_list], options[:providers], options[:list_providers])
5959
end
6060

6161
def self.puppet_5?
@@ -72,9 +72,9 @@ def self.render_markdown(path)
7272
PuppetStrings::Markdown.render(path)
7373
end
7474

75-
def self.render_describe(describe_types, list = false, providers = false)
75+
def self.render_describe(describe_types, list = false, show_providers = true, list_providers = false)
7676
require 'puppet-strings/describe'
77-
PuppetStrings::Describe.render(describe_types, list, providers)
77+
PuppetStrings::Describe.render(describe_types, list, show_providers, list_providers)
7878
end
7979

8080
# Runs the YARD documentation server.

Diff for: lib/puppet-strings/describe.rb

+24-15
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,38 @@
77
module PuppetStrings::Describe
88
# Renders requested types or a summarized list in the current YARD registry to STDOUT.
99
# @param [Array] describe_types The list of names of the types to be displayed.
10-
# @param [bool] list Create the summarized list instead of describing each type.
11-
# @param [bool] _providers Show details of the providers.
10+
# @param [bool] list_types Create the summarized list instead of describing each type.
11+
# @param [bool] show_type_providers Show details of the providers of a specified type.
12+
# @param [bool] list_providers Create a summarized list of providers.
1213
# @return [void]
13-
def self.render(describe_types = [], list = false, _providers = false)
14+
def self.render(describe_types = [], list_types = false, show_type_providers = true, list_providers = false)
1415
document = {
1516
defined_types: YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash),
16-
resource_types: YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash)
17+
resource_types: YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash),
18+
providers: YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash)
1719
}
18-
19-
if list
20+
# if --list flag passed, produce a summarized list of types
21+
if list_types
2022
puts 'These are the types known to puppet:'
21-
document[:resource_types].each { |t| list_one_type(t) }
22-
else
23-
document[:providers] = YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash)
23+
document[:resource_types].each { |t| list_one(t) }
2424

25+
# if a type(s) has been passed, show the details of that type(s)
26+
elsif describe_types
2527
type_names = {}
2628
describe_types.each { |name| type_names[name] = true }
2729

2830
document[:resource_types].each do |t|
29-
show_one_type(t) if type_names[t[:name].to_s]
31+
show_one_type(t, show_type_providers) if type_names[t[:name].to_s]
3032
end
33+
34+
# if --providers flag passed, produce a summarized list of providers
35+
elsif list_providers
36+
puts 'These are the providers known to puppet:'
37+
document[:providers].each { |t| list_one(t) }
3138
end
3239
end
3340

34-
def self.show_one_type(resource_type)
41+
def self.show_one_type(resource_type, providers = true)
3542
puts format("\n%<name>s\n%<underscore>s", name: resource_type[:name], underscore: '=' * resource_type[:name].length)
3643
puts resource_type[:docstring][:text]
3744

@@ -42,8 +49,10 @@ def self.show_one_type(resource_type)
4249

4350
puts "\nParameters\n----------"
4451
combined_list.sort_by { |p| p[:name] }.each { |p| show_one_parameter(p) }
52+
return unless providers
53+
4554
puts "\nProviders\n---------"
46-
# Show providers here - list or provide details
55+
resource_type[:providers]&.sort_by { |p| p[:name] }&.each { |p| puts p[:name].to_s.ljust(15) }
4756
end
4857

4958
def self.show_one_parameter(parameter)
@@ -53,14 +62,14 @@ def self.show_one_parameter(parameter)
5362
puts format('Requires features %<required_features>s.', required_features: parameter[:required_features]) unless parameter[:required_features].nil?
5463
end
5564

56-
def self.list_one_type(type)
65+
def self.list_one(object)
5766
targetlength = 48
5867
shortento = targetlength - 4
59-
contentstring = type[:docstring][:text]
68+
contentstring = object[:docstring][:text]
6069
end_of_line = contentstring.index("\n") # "." gives closer results to old describeb, but breaks for '.k5login'
6170
contentstring = contentstring[0..end_of_line] unless end_of_line.nil?
6271
contentstring = "#{contentstring[0..shortento]} ..." if contentstring.length > targetlength
6372

64-
puts "#{type[:name].to_s.ljust(15)} - #{contentstring}"
73+
puts "#{object[:name].to_s.ljust(15)} - #{contentstring}"
6574
end
6675
end

Diff for: lib/puppet-strings/yard/handlers/ruby/function_handler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def add_param_tag(object, tags, name, file, line, type = nil, default = nil, opt
322322
end
323323

324324
type ||= tag&.types ? tag.type : 'Any'
325-
type = optional ? "Optional[#{type}]" : type
325+
type = "Optional[#{type}]" if optional
326326

327327
object.parameters << [name, to_puppet_literal(default)]
328328

Diff for: lib/puppet/face/strings.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'puppet/face'
44

55
# Implements the 'puppet strings' interface.
6-
Puppet::Face.define(:strings, '0.0.1') do
6+
Puppet::Face.define(:strings, '0.0.1') do # rubocop:disable Metrics/BlockLength
77
summary 'Generate Puppet documentation with YARD.'
88

99
action(:generate) do
@@ -83,7 +83,10 @@
8383
summary 'list types'
8484
end
8585
option '--providers' do
86-
summary 'provide details on providers'
86+
summary 'provide details on providers for each type'
87+
end
88+
option '--list-providers' do
89+
summary 'list all providers'
8790
end
8891

8992
# TODO: Implement the rest of describe behavior
@@ -96,6 +99,9 @@
9699
# * --list:
97100
# List all types
98101

102+
# * --list-providers:
103+
# list all providers
104+
99105
# * --meta:
100106
# List all metaparameters
101107

@@ -160,6 +166,8 @@ def build_generate_options(options = nil, *yard_args)
160166
generate_options[:describe] = true
161167
generate_options[:describe_types] = options[:describe_types]
162168
generate_options[:describe_list] = options[:list]
169+
generate_options[:providers] = options[:providers]
170+
generate_options[:list_providers] = options[:list_providers]
163171
end
164172

165173
format = options[:format]

0 commit comments

Comments
 (0)