Module: PuppetLanguageServer::Puppetfile::ValidationProvider

Defined in:
lib/puppet-languageserver/puppetfile/validation_provider.rb

Class Method Summary collapse

Class Method Details

.find_dependencies(content) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/puppet-languageserver/puppetfile/validation_provider.rb', line 109

def self.find_dependencies(content)
  require 'puppetfile-resolver'
  require 'puppetfile-resolver/puppetfile/parser/r10k_eval'
  parser = PuppetfileResolver::Puppetfile::Parser::R10KEval

  result = []
  puppetfile = parser.parse(content)

  return result if puppetfile.nil?

  raise 'Puppetfile is not valid' unless puppetfile.valid?

  puppetfile.modules.select { |d| d.module_type == :forge }.each do |dep|
    result << {
      name: dep.name,
      title: dep.title,
      owner: dep.owner,
      version: dep.version.to_s,
      start_line: dep.location.start_line,
      end_line: dep.location.end_line
    }
  end

  result
end

.max_line_lengthObject



8
9
10
11
# File 'lib/puppet-languageserver/puppetfile/validation_provider.rb', line 8

def self.max_line_length
  # TODO: ... need to figure out the actual line length
  1000
end

.validate(content, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/puppet-languageserver/puppetfile/validation_provider.rb', line 13

def self.validate(content, options = {})
  options = {
    max_problems: 100,
    resolve_puppetfile: true,
    module_path: [],
    document_uri: '???'
  }.merge(options)

  result = []
  # TODO: Need to implement max_problems
  _problems = 0

  require 'puppetfile-resolver'
  require 'puppetfile-resolver/puppetfile/parser/r10k_eval'
  parser = PuppetfileResolver::Puppetfile::Parser::R10KEval

  # Attempt to parse the file
  puppetfile = nil
  begin
    puppetfile = parser.parse(content)
  rescue PuppetfileResolver::Puppetfile::Parser::ParserError => e
    result << LSP::Diagnostic.new(
      'severity' => LSP::DiagnosticSeverity::ERROR,
      'range' => document_location_to_lsp_range(e.location),
      'source' => 'Puppet',
      'message' => e.to_s
    )
    puppetfile = nil
  end
  return result if puppetfile.nil?

  puppetfile.validation_errors.each do |validation_error|
    related_information = nil

    if validation_error.is_a?(PuppetfileResolver::Puppetfile::DocumentDuplicateModuleError)
      related_information = validation_error.duplicates.map do |dup_mod|
        {
          'location' => {
            'uri' => options[:document_uri],
            'range' => document_location_to_lsp_range(dup_mod.location)
          },
          'message' => validation_error.message
        }
      end
    end

    result << LSP::Diagnostic.new(
      'severity' => LSP::DiagnosticSeverity::ERROR,
      'range' => document_location_to_lsp_range(validation_error.puppet_module.location),
      'source' => 'Puppet',
      'message' => validation_error.message,
      'relatedInformation' => related_information
    )
  end

  return result unless options[:resolve_puppetfile] && puppetfile.valid?

  result + validate_resolution(puppetfile, options[:document_uri], resolver_cache, options[:module_path], options[:puppet_version])
end

.validate_resolution(puppetfile_document, document_uri, cache, module_path, puppet_version) ⇒ Object



73
74
75
76
77
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
# File 'lib/puppet-languageserver/puppetfile/validation_provider.rb', line 73

def self.validate_resolution(puppetfile_document, document_uri, cache, module_path, puppet_version)
  ui = nil
  resolver = PuppetfileResolver::Resolver.new(puppetfile_document, puppet_version)
  opts = { cache: cache, ui: ui, module_paths: module_path, allow_missing_modules: true }
  begin
    resolution = resolver.resolve(opts)
  rescue PuppetfileResolver::Puppetfile::DocumentVersionConflictError,
         PuppetfileResolver::Puppetfile::DocumentCircularDependencyError => e
    return [document_error_to_diagnostic(document_uri, e)]
  rescue PuppetfileResolver::Puppetfile::DocumentResolveError => e
    return [LSP::Diagnostic.new(
      'severity' => LSP::DiagnosticSeverity::ERROR,
      'range' => LSP.create_range(0, 0, 0, max_line_length),
      'source' => 'Puppet',
      'message' => e.message
    )]
  end

  resolution.validation_errors.map do |error|
    severity = case error
               when PuppetfileResolver::Puppetfile::DocumentLatestVersionError
                 LSP::DiagnosticSeverity::INFORMATION
               when PuppetfileResolver::Puppetfile::DocumentMissingModuleError
                 LSP::DiagnosticSeverity::HINT
               else
                 LSP::DiagnosticSeverity::ERROR
               end
    LSP::Diagnostic.new(
      'severity' => severity,
      'range' => document_location_to_lsp_range(error.puppet_module.location),
      'source' => 'Puppet',
      'message' => error.message
    )
  end
end