Module: PuppetLanguageServerSidecar::PuppetHelper

Defined in:
lib/puppet-languageserver-sidecar/puppet_helper.rb

Defined Under Namespace

Classes: PuppetPathFinder

Constant Summary collapse

SIDECAR_PUPPET_ENVIRONMENT =
'sidecarenvironment'
IGNORE_DATATYPE_NAMES =

Ignore certain data types. For more information see tickets.puppetlabs.com/browse/DOCUMENT-1020 TypeReference - Internal to the Puppet Data Type system TypeAlias - Internal to the Puppet Data Type system Object - While useful, typically only needed when extended the type system as opposed to general use TypeSet - While useful, typically only needed when extended the type system as opposed to general use

%w[TypeReference TypeAlias Object TypeSet ObjectTypeExten Iterable AbstractTimeData TypeWithContained].freeze

Class Method Summary collapse

Class Method Details

.available_documentation_typesObject

Puppet Strings loading



36
37
38
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 36

def self.available_documentation_types
  %I[class datatype function type]
end

.get_puppet_resource(typename, title = nil) ⇒ Object

Resource Face



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 17

def self.get_puppet_resource(typename, title = nil)
  result = PuppetLanguageServer::Sidecar::Protocol::PuppetClassList.new
  resources = if title.nil?
                Puppet::Face[:resource, '0.0.1'].search(typename)
              else
                Puppet::Face[:resource, '0.0.1'].find("#{typename}/#{title}")
              end
  return result if resources.nil?

  resources = [resources] unless resources.is_a?(Array)
  prune_resource_parameters(resources).each do |item|
    obj = PuppetLanguageServer::Sidecar::Protocol::Resource.new
    obj.manifest = item.to_manifest
    result << obj
  end
  result
end

.retrieve_via_puppet_strings(cache, options = {}) ⇒ Object

Retrieve objects via the Puppet 4 API loaders



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 78

def self.retrieve_via_puppet_strings(cache, options = {})
  PuppetLanguageServerSidecar.log_message(:debug, '[PuppetHelper::retrieve_via_puppet_strings] Starting')

  object_types = options[:object_types].nil? ? available_documentation_types : options[:object_types]
  object_types.select! { |i| available_documentation_types.include?(i) }

  result = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new
  return result if object_types.empty?

  current_env = current_environment
  for_agent = options[:for_agent].nil? ? true : options[:for_agent]
  Puppet::Pops::Loaders.new(current_env, for_agent)

  finder = PuppetPathFinder.new(current_env, object_types)
  paths = finder.find(options[:root_path])

  paths.each do |path|
    file_doc = PuppetLanguageServerSidecar::PuppetStringsHelper.file_documentation(path, finder.puppet_path, cache)
    next if file_doc.nil?

    if object_types.include?(:class) # rubocop:disable Style/IfUnlessModifier   This reads better
      file_doc.classes.each { |item| result.append!(item) }
    end
    if object_types.include?(:datatype) # rubocop:disable Style/IfUnlessModifier   This reads better
      file_doc.datatypes.each { |item| result.append!(item) }
    end
    if object_types.include?(:function) # rubocop:disable Style/IfUnlessModifier   This reads better
      file_doc.functions.each { |item| result.append!(item) }
    end
    next unless object_types.include?(:type)

    file_doc.types.each do |item|
      result.append!(item) unless name == 'whit' || name == 'component'
      finder.temp_file.unlink if item.key == 'file' && File.exist?(finder.temp_file.path) # Remove the temp_file.rb if it exists
    end
  end

  # Remove Puppet3 functions which have a Puppet4 function already loaded
  if object_types.include?(:function) && !result.functions.nil?
    pup4_functions = result.functions.select { |i| i.function_version == 4 }.map { |i| i.key }
    result.functions.reject! { |i| i.function_version == 3 && pup4_functions.include?(i.key) }
  end

  # Add the inbuilt data types if there's no root path
  result.concat!(retrieve_default_data_types) if object_types.include?(:datatype) && options[:root_path].nil?

  result.each_list { |key, item| PuppetLanguageServerSidecar.log_message(:debug, "[PuppetHelper::retrieve_via_puppet_strings] Finished loading #{item.count} #{key}") }
  result
end