Skip to content

Add support for puppetfile resolver #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 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 @@ -42,6 +44,7 @@ def initialize(*args)
define_task_install(*args)
define_task_validate(*args)
define_task_print_git_conversion(*args)
define_task_resolver(*args)
end
end

Expand Down
55 changes: 55 additions & 0 deletions lib/ra10ke/resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'puppetfile-resolver'
require 'puppetfile-resolver/puppetfile/parser/r10k_eval'

module Ra10ke
# puppetfile-resolver tasks
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

# Instance class
class Instance
attr_reader :puppetfile

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a directory, not a file? Cant read a dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. why yes that would fail.

# Parse the Puppetfile into an object model
content = File.binread(puppetfile_path)

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

# Make sure the Puppetfile is valid
return if puppetfile.valid?

puts 'Puppetfile is not valid'
puppetfile.validation_errors.each { |err| puts err }
exit 1
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puppetfile should be @puppetfile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an attr_reader for puppetfile


# 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
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.7.0'

spec.add_dependency 'git', '~> 1.18'
spec.add_dependency 'puppetfile-resolver'
spec.add_dependency 'puppet_forge', '~> 5.0', '>= 5.0.1'
spec.add_dependency 'r10k', '>= 2.6.5', '< 5'
spec.add_dependency 'rake', '~> 13.0', '>= 13.0.6'
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