Class: PuppetLanguageServerSidecar::PuppetHelper::PuppetPathFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-languageserver-sidecar/puppet_helper.rb

Overview

A helper class to find the paths for different kinds of things related to Puppet, for example DataType ruby files or manifests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puppet_env, object_types) ⇒ PuppetPathFinder

Returns a new instance of PuppetPathFinder.

Parameters:

  • puppet_env (Puppet::Node::Environment)

    The environment to search within

  • object_types (Symbol)

    The types of objects that will be searched for. See available_documentation_types for the complete list



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 160

def initialize(puppet_env, object_types)
  # Path to every module
  @module_paths = puppet_env.modules.map(&:path)
  # Path to the environment
  @env_path = puppet_env.configuration.path_to_env
  # Path to the puppet installation
  @puppet_path = if puppet_env.loaders.nil? # No loaders have been created yet
                   nil
                 elsif puppet_env.loaders.puppet_system_loader.nil?
                   nil
                 elsif puppet_env.loaders.puppet_system_loader.lib_root?
                   File.join(puppet_env.loaders.puppet_system_loader.path, '..')
                 else
                   puppet_env.loaders.puppet_system_loader.path
                 end
  # Path to the cached puppet files e.g. pluginsync
  @vardir_path = Puppet.settings[:vardir]

  @object_types = object_types
end

Instance Attribute Details

#object_typesObject (readonly)

Returns the value of attribute object_types.



156
157
158
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 156

def object_types
  @object_types
end

#puppet_pathObject (readonly)

Returns the value of attribute puppet_path.



156
157
158
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 156

def puppet_path
  @puppet_path
end

#temp_fileObject (readonly)

Returns the value of attribute temp_file.



156
157
158
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 156

def temp_file
  @temp_file
end

Instance Method Details

#find(from_root_path = nil) ⇒ Array[String]

Find all puppet related files, optionally from within a root path

Parameters:

  • from_root_path (String) (defaults to: nil)

    The path which files can be found within. If nil, only the default Puppet locations are searched e.g. vardir

Returns:

  • (Array[String])

    A list of all files that are found. This is the absolute path to the file.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/puppet-languageserver-sidecar/puppet_helper.rb', line 184

def find(from_root_path = nil)
  require 'tempfile'
  paths = []
  search_paths = @module_paths.nil? ? [] : @module_paths
  search_paths << @env_path unless @env_path.nil?
  search_paths << @vardir_path unless @vardir_path.nil?
  search_paths << @puppet_path unless @puppet_path.nil?

  searched_globs = []
  search_paths.each do |search_root|
    PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Potential search root '#{search_root}'")
    next if search_root.nil?

    # We need absolute paths from here on in.
    search_root = File.expand_path(search_root)
    next unless path_in_root?(from_root_path, search_root) && Dir.exist?(search_root)

    PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Using '#{search_root}' as a directory to search")
    # name of temp file to store the file type definitions (if any)
    @temp_file = Tempfile.new('file.rb')
    all_object_info.each do |object_type, paths_to_search|
      next unless object_types.include?(object_type)

      # TODO: next unless object_type is included
      paths_to_search.each do |path_info|
        path = File.join(search_root, path_info[:relative_dir])
        glob = path + path_info[:glob]
        next if searched_globs.include?(glob) # No point searching twice
        next unless Dir.exist?(path)

        searched_globs << glob
        PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''")

        Dir.glob(glob) do |filename|
          # if filename matches file.rb or /file/<any>.rb then we need to loop through each file type definition
          if filename.match?(%r{/type/file/.*.rb|/type/file.rb})
            PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
            # Read each file type definition and write it to the temp file
            @temp_file.write(File.read(filename))
          else
            paths << filename
          end
        end
      end
    end
  end
  #  Add the temp_file.rb to the paths array for searching (if exists)
  if @temp_file && File.exist?(@temp_file.path)
    paths << @temp_file.path
    @temp_file.close
  end
  paths
end