forked from puppetlabs/puppet-editor-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(puppetlabsGH-121) Add documentation for Puppet 4 Functions
Previously the Puppet 4 API functions would be loaded however they would not have any documentation notes as this was removed in the newer API. Instead we need to use Puppet Strings and YARD to generate the documentation. This commit: Note that Puppet Strings and YARD are only available in the PDK, not Puppet Agent. Once this feature is no longer hidden behind a feature flag, they can be vendored into Editor Services * Adds the puppet-strings gem for development purpsoes * Sets up YARD via Puppet Strings which adds the Puppet Language parsing into YARD * Caches the result from YARD to speed up documentation resolution for multiple items in the same file * Post processes the function loader and adds the documetation for Puppet 4 API functions
- Loading branch information
1 parent
01d125d
commit aa31944
Showing
5 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
lib/puppet-languageserver-sidecar/puppet_strings_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module PuppetLanguageServerSidecar | ||
module PuppetStringsHelper | ||
def self.file_documentation(path) | ||
return nil unless require_puppet_strings | ||
return @helper_cache[path] unless @helper_cache.nil? || @helper_cache[path].nil? | ||
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetStringsHelper::file_documentation] Fetching documentation for #{path}") | ||
|
||
setup_yard! | ||
|
||
# For now, assume a single file path | ||
search_patterns = [path] | ||
|
||
# Format the arguments to YARD | ||
args = ['doc'] | ||
args << '--no-output' | ||
args << '--quiet' | ||
args << '--no-stats' | ||
args << '--no-progress' | ||
args << '--no-save' | ||
args << '--api public' | ||
args << '--api private' | ||
args << '--no-api' | ||
args += search_patterns | ||
|
||
# Run YARD | ||
::YARD::CLI::Yardoc.run(*args) | ||
|
||
# Extract all of the information | ||
# Ref - https://github.com/puppetlabs/puppet-strings/blob/87a8e10f45bfeb7b6b8e766324bfb126de59f791/lib/puppet-strings/json.rb#L10-L16 | ||
@helper_cache = {} if @helper_cache.nil? | ||
extract_puppet_functions | ||
|
||
@helper_cache[path] | ||
end | ||
|
||
def self.require_puppet_strings | ||
return @puppet_strings_loaded unless @puppet_strings_loaded.nil? | ||
begin | ||
require 'puppet-strings' | ||
require 'puppet-strings/yard' | ||
require 'puppet-strings/json' | ||
@puppet_strings_loaded = true | ||
rescue LoadError => e | ||
PuppetLanguageServerSidecar.log_message(:error, "[PuppetStringsHelper::require_puppet_strings] Unable to load puppet-strings gem: #{e}") | ||
@puppet_strings_loaded = false | ||
end | ||
@puppet_strings_loaded | ||
end | ||
private_class_method :require_puppet_strings | ||
|
||
def self.setup_yard! | ||
unless @yard_setup # rubocop:disable Style/GuardClause | ||
::PuppetStrings::Yard.setup! | ||
@yard_setup = true | ||
end | ||
end | ||
private_class_method :setup_yard! | ||
|
||
def self.assert_cache_has_file(source_path) | ||
if @helper_cache[source_path].nil? # rubocop:disable Style/GuardClause | ||
@helper_cache[source_path] = { | ||
:functions => {} | ||
} | ||
end | ||
end | ||
private_class_method :assert_cache_has_file | ||
|
||
def self.extract_puppet_functions | ||
::YARD::Registry.all(:puppet_function).map(&:to_hash).each do |item| | ||
source_path = item[:file] | ||
assert_cache_has_file(source_path) | ||
|
||
func_name = item[:name].to_s | ||
@helper_cache[source_path][:functions][func_name] = {} if @helper_cache[source_path][:functions][func_name].nil? | ||
|
||
# Build the function documentation | ||
@helper_cache[source_path][:functions][func_name][:doc] = item[:docstring][:text] | ||
end | ||
end | ||
private_class_method :extract_puppet_functions | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters