Skip to content

Commit

Permalink
Fix #51 - Add support for puppetfile resolver
Browse files Browse the repository at this point in the history
  * This adds experimental support for the puppetfile resolver.
    Since the resolver is better, faster, stronger it might be
    useful to use this library in the future.  A resolve task
    task has been added but it is not clear what that yet provides.
  • Loading branch information
logicminds committed Apr 22, 2022
1 parent 4ba308b commit a625e10
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ra10ke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'ra10ke/version'
require 'ra10ke/solve'
require 'ra10ke/syntax'
require 'ra10ke/resolver'
require 'ra10ke/dependencies'
require 'ra10ke/deprecation'
require 'ra10ke/duplicates'
Expand All @@ -20,6 +21,7 @@ class RakeTask < ::Rake::TaskLib
include Ra10ke::Duplicates
include Ra10ke::Install
include Ra10ke::Validate
include Ra10ke::Resolver

attr_accessor :basedir, :moduledir, :puppetfile_path, :puppetfile_name, :force, :purge

Expand All @@ -41,6 +43,8 @@ def initialize(*args)
define_task_duplicates(*args)
define_task_install(*args)
define_task_validate(*args)
define_task_resolver(*args)

end
end

Expand Down
56 changes: 56 additions & 0 deletions lib/ra10ke/resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'puppetfile-resolver'
require 'puppetfile-resolver/puppetfile/parser/r10k_eval'

module Ra10ke
module Resolver

def define_task_resolver(*args)
desc 'Run the puppetfile Resolver'
task :resolver do
resolver = Ra10ke::Resolver::Instance.new(get_puppetfile.puppetfile_path)
result = resolver.resolve
# Output resolution validation errors
result.validation_errors.each { |err| puts "Resolution Validation Error: #{err}\n"}
end
end

class Instance
attr_reader :puppetfile

def initialize(puppetfile_path = File.expand_path(Dir.pwd))

# Parse the Puppetfile into an object model
content = File.open(puppetfile_path, 'rb') { |f| f.read }

@puppetfile = ::PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(content)

# Make sure the Puppetfile is valid
unless puppetfile.valid?
puts 'Puppetfile is not valid'
puppetfile.validation_errors.each { |err| puts err }
exit 1
end
end

def resolve
# Create the resolver
# - Use the document we just parsed (puppetfile)
# - Don't restrict by Puppet version (nil)
resolver = ::PuppetfileResolver::Resolver.new(puppetfile, nil)

# Configure the resolver
cache = nil # Use the default inmemory cache
ui = nil # Don't output any information
module_paths = [] # List of paths to search for modules on the local filesystem
allow_missing_modules = true # Allow missing dependencies to be resolved
opts = { cache: cache, ui: ui, module_paths: module_paths, allow_missing_modules: allow_missing_modules }

# Resolve
result = resolver.resolve(opts)


end

end
end
end
1 change: 1 addition & 0 deletions ra10ke.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.4.0'

spec.add_dependency "rake"
spec.add_dependency 'puppetfile-resolver'
spec.add_dependency "puppet_forge"
spec.add_dependency "r10k"
spec.add_dependency "git"
Expand Down
20 changes: 20 additions & 0 deletions spec/ra10ke/resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper'
require 'ra10ke/resolver'
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true

RSpec.describe 'Ra10ke::Resolver::Instance' do
include Ra10ke::Resolver
let(:instance) do
Ra10ke::Resolver::Instance.new(puppetfile)
end

let(:puppetfile) do
File.join(fixtures_dir, 'Puppetfile')
end

it 'resolves the puppetfile' do
expect(instance.resolve).to be nil
end
end

0 comments on commit a625e10

Please sign in to comment.