Module: PuppetLanguageServerSidecar

Defined in:
lib/puppet_languageserver_sidecar.rb,
lib/puppet-languageserver-sidecar/workspace.rb,
lib/puppet-languageserver-sidecar/cache/base.rb,
lib/puppet-languageserver-sidecar/cache/null.rb,
lib/puppet-languageserver-sidecar/facter_helper.rb,
lib/puppet-languageserver-sidecar/puppet_helper.rb,
lib/puppet-languageserver-sidecar/cache/filesystem.rb,
lib/puppet-languageserver-sidecar/puppet_parser_helper.rb,
lib/puppet-languageserver-sidecar/puppet_strings_helper.rb,
lib/puppet-languageserver-sidecar/sidecar_protocol_extensions.rb

Defined Under Namespace

Modules: Cache, FacterHelper, Protocol, PuppetHelper, PuppetParserHelper, PuppetStringsHelper, Workspace Classes: CommandLineParser, FileDocumentation, FileDocumentationCache

Constant Summary collapse

ACTION_LIST =
%w[
  noop
  default_aggregate
  default_classes
  default_datatypes
  default_functions
  default_types
  node_graph
  resource_list
  workspace_aggregate
  workspace_classes
  workspace_datatypes
  workspace_functions
  workspace_types
  facts
].freeze

Class Method Summary collapse

Class Method Details

.configure_featureflags(flags) ⇒ Object



28
29
30
# File 'lib/puppet_languageserver_sidecar.rb', line 28

def self.configure_featureflags(flags)
  @flags = flags
end

.execute(options) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/puppet_languageserver_sidecar.rb', line 237

def self.execute(options)
  unless Gem::Version.new(Puppet.version) >= Gem::Version.new('5.0.0')
    log_message(:error, 'Only Puppet Version 5.0 and above is supported.')
    return []
  end

  log_message(:debug, "Executing #{options[:action]} action")
  case options[:action].downcase
  when 'noop'
    []

  when 'default_aggregate'
    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, object_types: PuppetLanguageServerSidecar::PuppetHelper.available_documentation_types)

  when 'default_classes'
    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, object_types: [:class]).classes

  when 'default_datatypes'
    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, object_types: [:datatype]).datatypes

  when 'default_functions'
    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, object_types: [:function]).functions

  when 'default_types'
    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache, object_types: [:type]).types

  when 'node_graph'
    inject_workspace_as_module || inject_workspace_as_environment
    result = PuppetLanguageServerSidecar::Protocol::PuppetNodeGraph.new
    if options[:action_parameters]['source'].nil?
      log_message(:error, 'Missing source action parameter')
      return result.set_error('Missing source action parameter')
    end
    begin
      manifest = File.open(options[:action_parameters]['source'], 'r:UTF-8') { |f| f.read }
      PuppetLanguageServerSidecar::PuppetParserHelper.compile_node_graph(manifest)
    rescue StandardError => e
      log_message(:error, "Unable to compile the manifest. #{e}")
      result.set_error("Unable to compile the manifest. #{e}")
    end

  when 'resource_list'
    inject_workspace_as_module || inject_workspace_as_environment
    typename = options[:action_parameters]['typename']
    title = options[:action_parameters]['title']
    if typename.nil?
      log_message(:error, 'Missing typename action paramater')
      return []
    end
    PuppetLanguageServerSidecar::PuppetHelper.get_puppet_resource(typename, title)

  when 'workspace_aggregate'
    return nil unless inject_workspace_as_module || inject_workspace_as_environment

    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache,
                                                                          object_types: PuppetLanguageServerSidecar::PuppetHelper.available_documentation_types,
                                                                          root_path: PuppetLanguageServerSidecar::Workspace.root_path)

  when 'workspace_classes'
    return nil unless inject_workspace_as_module || inject_workspace_as_environment

    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache,
                                                                          object_types: [:class],
                                                                          root_path: PuppetLanguageServerSidecar::Workspace.root_path).classes

  when 'workspace_datatypes'
    return nil unless inject_workspace_as_module || inject_workspace_as_environment

    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache,
                                                                          object_types: [:datatype],
                                                                          root_path: PuppetLanguageServerSidecar::Workspace.root_path).datatypes

  when 'workspace_functions'
    return nil unless inject_workspace_as_module || inject_workspace_as_environment

    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache,
                                                                          object_types: [:function],
                                                                          root_path: PuppetLanguageServerSidecar::Workspace.root_path).functions

  when 'workspace_types'
    return nil unless inject_workspace_as_module || inject_workspace_as_environment

    cache = options[:disable_cache] ? PuppetLanguageServerSidecar::Cache::Null.new : PuppetLanguageServerSidecar::Cache::FileSystem.new
    PuppetLanguageServerSidecar::PuppetHelper.retrieve_via_puppet_strings(cache,
                                                                          object_types: [:type],
                                                                          root_path: PuppetLanguageServerSidecar::Workspace.root_path).types

  when 'facts'
    # Can't cache for facts
    cache = PuppetLanguageServerSidecar::Cache::Null.new
    # Inject the workspace etc. if present
    injected = inject_workspace_as_module
    inject_workspace_as_environment unless injected
    PuppetLanguageServerSidecar::FacterHelper.retrieve_facts(cache)

  else
    log_message(:error, "Unknown action #{options[:action]}. Expected one of #{ACTION_LIST}")
  end
end

.execute_and_output(options) ⇒ Object



357
358
359
360
# File 'lib/puppet_languageserver_sidecar.rb', line 357

def self.execute_and_output(options)
  output(execute(options), options)
  nil
end

.featureflag?(flagname) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/puppet_languageserver_sidecar.rb', line 32

def self.featureflag?(flagname)
  return false if @flags.nil? || @flags.empty?

  @flags.include?(flagname)
end

.init_puppet_sidecar(options) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/puppet_languageserver_sidecar.rb', line 186

def self.init_puppet_sidecar(options)
  PuppetEditorServices.init_logging(options)
  log_message(:info, "Language Server Sidecar is v#{PuppetLanguageServerSidecar.version}")
  log_message(:debug, 'Loading gems...')
  require_gems(options)
  log_message(:info, "Using Puppet v#{Puppet.version}")

  raise("Detected Puppet #{Puppet.version} however the Language Server Sidecar requires Puppet 5.0 and above") if Gem::Version.new(Puppet.version) < Gem::Version.new('5.0.0')

  log_message(:debug, "Detected additional puppet settings #{options[:puppet_settings]}")
  options[:puppet_settings].nil? ? Puppet.initialize_settings : Puppet.initialize_settings(options[:puppet_settings])

  PuppetLanguageServerSidecar::Workspace.detect_workspace(options[:workspace])
  log_message(:debug, 'Detected Module Metadata in the workspace') if PuppetLanguageServerSidecar::Workspace.has_module_metadata?
  log_message(:debug, 'Detected Environment Config in the workspace') if PuppetLanguageServerSidecar::Workspace.has_environmentconf?

  # Remove all other logging destinations except for ours
  Puppet::Util::Log.destinations.clear
  Puppet::Util::Log.newdestination('null_logger')

  true
end

.inject_workspace_as_environmentObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/puppet_languageserver_sidecar.rb', line 222

def self.inject_workspace_as_environment
  return false unless PuppetLanguageServerSidecar::Workspace.has_environmentconf?

  Puppet.settings[:environment] = PuppetLanguageServerSidecar::PuppetHelper::SIDECAR_PUPPET_ENVIRONMENT

  %w[puppet_environment_monkey_patches].each do |lib|
    require "puppet-languageserver-sidecar/#{lib}"
  rescue LoadError
    require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib))
  end

  log_message(:debug, 'Injected the workspace into the environment loader')
  true
end

.inject_workspace_as_moduleObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/puppet_languageserver_sidecar.rb', line 209

def self.inject_workspace_as_module
  return false unless PuppetLanguageServerSidecar::Workspace.has_module_metadata?

  %w[puppet_modulepath_monkey_patches].each do |lib|
    require "puppet-languageserver-sidecar/#{lib}"
  rescue LoadError
    require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib))
  end

  log_message(:debug, 'Injected the workspace into the module loader')
  true
end

.log_message(severity, message) ⇒ Object



182
183
184
# File 'lib/puppet_languageserver_sidecar.rb', line 182

def self.log_message(severity, message)
  PuppetEditorServices.log_message(severity, message)
end

.output(result, options) ⇒ Object



346
347
348
349
350
351
352
353
354
355
# File 'lib/puppet_languageserver_sidecar.rb', line 346

def self.output(result, options)
  if options[:output].nil? || options[:output].empty?
    $stdout.binmode
    $stdout.write(result.to_json)
  else
    File.open(options[:output], 'wb:UTF-8') do |f|
      f.write result.to_json
    end
  end
end

.require_gems(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet_languageserver_sidecar.rb', line 38

def self.require_gems(options)
  original_verbose = $VERBOSE
  $VERBOSE = nil

  # Use specific Puppet Gem version if possible
  unless options[:puppet_version].nil?
    available_puppet_gems = Gem::Specification
                            .select { |item| item.name.casecmp('puppet').zero? }
                            .map { |item| item.version.to_s }
    if available_puppet_gems.include?(options[:puppet_version])
      gem 'puppet', options[:puppet_version]
    else
      log_message(:warn, "Unable to use puppet version #{options[:puppet_version]}, as only the following versions are available [#{available_puppet_gems.join(', ')}]")
    end
  end

  require 'puppet'

  # Validate the feature flags
  unless options[:flags].nil? || options[:flags].empty?
    flags = options[:flags]
    log_message(:debug, "Detected feature flags [#{options[:flags].join(', ')}]")

    configure_featureflags(flags)
  end

  require_list = %w[
    cache/base
    cache/null
    cache/filesystem
    puppet_parser_helper
    sidecar_protocol_extensions
    workspace
    facter_helper
    puppet_helper
    puppet_monkey_patches
    puppet_strings_helper
  ]

  require_list.each do |lib|
    require "puppet-languageserver-sidecar/#{lib}"
  rescue LoadError
    require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib))
  end
ensure
  $VERBOSE = original_verbose
end

.versionObject



24
25
26
# File 'lib/puppet_languageserver_sidecar.rb', line 24

def self.version
  PuppetEditorServices.version
end