Skip to content

Commit 77878f3

Browse files
committed
(GH-256) Add puppetfile-resolver endpoint
Adds a new endpoint for parsing a Puppetfile and returning a list of modules. This only returns forge modules right now because those are the only ones that are resolved by puppetfile-resolver currently.
1 parent 6a1712f commit 77878f3

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
66

77
## Unreleased
88

9+
### Added
10+
11+
- ([GH-256](https://github.com/puppetlabs/puppet-editor-services/issues/256)) Add Puppetfile dependency endpoint
12+
913
## 0.26.0 - 2020-05-01
1014

1115
### Added

lib/lsp/lsp_custom.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ def from_h!(value)
100100
end
101101
end
102102

103+
# export interface PuppetfileDependencyResponse {
104+
# dotContent: string;
105+
# data: string;
106+
# }
107+
class PuppetfileDependencyResponse < LSPBase
108+
attr_accessor :dependencies # type: string[]
109+
attr_accessor :error # type: string
110+
111+
def initialize(initial_hash = nil)
112+
super
113+
@optional_method_names = %i[error]
114+
end
115+
116+
def from_h!(value)
117+
value = {} if value.nil?
118+
self.dependencies = value['dependencies']
119+
self.error = value['error']
120+
self
121+
end
122+
end
123+
103124
# export interface CompileNodeGraphResponse {
104125
# dotContent: string;
105126
# data: string;

lib/puppet-languageserver/message_handler.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ def request_puppet_compilenodegraph(_, json_rpc_message)
7878
end
7979
end
8080

81+
def request_puppetfile_getdependencies(_, json_rpc_message)
82+
file_uri = json_rpc_message.params['uri']
83+
return LSP::PuppetfileDependencyResponse.new('error' => 'Must be a puppetfile in order to find dependencies.') unless documents.document_type(file_uri) == :puppetfile
84+
85+
content = documents.document(file_uri)
86+
87+
result = []
88+
begin
89+
result = PuppetLanguageServer::Puppetfile::ValidationProvider.find_dependencies(content)
90+
rescue StandardError => e
91+
PuppetLanguageServer.log_message(:error, "(puppetfile/getdependencies) Error parsing puppetfile. #{e}")
92+
return LSP::PuppetfileDependencyResponse.new('error' => 'An internal error occured while parsing the puppetfile. Please see the debug log files for more information.')
93+
end
94+
95+
LSP::PuppetfileDependencyResponse.new('dependencies' => result)
96+
end
97+
8198
def request_puppet_fixdiagnosticerrors(_, json_rpc_message)
8299
formatted_request = LSP::PuppetFixDiagnosticErrorsRequest.new(json_rpc_message.params)
83100
file_uri = formatted_request.documentUri

lib/puppet-languageserver/puppetfile/validation_provider.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ def self.validate_resolution(puppetfile_document, document_uri, cache, module_pa
106106
end
107107
end
108108

109+
def self.find_dependencies(content)
110+
require 'puppetfile-resolver'
111+
require 'puppetfile-resolver/puppetfile/parser/r10k_eval'
112+
parser = PuppetfileResolver::Puppetfile::Parser::R10KEval
113+
114+
result = []
115+
puppetfile = parser.parse(content)
116+
117+
return result if puppetfile.nil?
118+
119+
raise 'Puppetfile is not valid' unless puppetfile.valid?
120+
121+
puppetfile.modules.select { |d| d.module_type == :forge }.each do |dep|
122+
result << {
123+
name: dep.name,
124+
title: dep.title,
125+
owner: dep.owner,
126+
version: dep.version.to_s,
127+
start_line: dep.location.start_line,
128+
end_line: dep.location.end_line
129+
}
130+
end
131+
132+
result
133+
end
134+
109135
def self.resolver_cache
110136
return @resolver_cache unless @resolver_cache.nil?
111137
require 'puppetfile-resolver/cache/base'

spec/languageserver/unit/puppet-languageserver/puppetfile/validation_provider_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,46 @@ def load(name)
430430
end
431431
end
432432
end
433+
434+
describe "#find_dependencies" do
435+
context 'with a valid Puppetfile' do
436+
let(:content) do <<-EOT
437+
forge 'https://forge.puppetlabs.com/'
438+
439+
# Modules from the Puppet Forge
440+
mod 'puppetlabs-somemodule', '1.0.0'
441+
442+
# Git style modules
443+
mod 'gitcommitmodule',
444+
:git => 'https://github.com/username/repo',
445+
:commit => 'abc123'
446+
mod 'gittagmodule',
447+
:git => 'https://github.com/username/repo',
448+
:tag => '0.1'
449+
450+
# Svn style modules
451+
mod 'svnmodule',
452+
:svn => 'svn://host/repo',
453+
:rev => 'abc123'
454+
455+
# local style modules
456+
mod 'localmodule',
457+
:local => 'true'
458+
EOT
459+
end
460+
461+
it 'should return no validation errors' do
462+
result = subject.find_dependencies(content)
463+
464+
expect(result).to eq([{:end_line=>3,
465+
:name=>"somemodule",
466+
:owner=>"puppetlabs",
467+
:start_line=>3,
468+
:title=>"puppetlabs-somemodule",
469+
:version=>"1.0.0"}])
470+
end
471+
end
472+
473+
end
474+
433475
end

0 commit comments

Comments
 (0)