|
| 1 | +require 'puppetfile-source-dereference' |
| 2 | +require "puppetfile-resolver" |
| 3 | +require "puppetfile-resolver/puppetfile/parser/r10k_eval" |
| 4 | +require 'puppet_forge' |
| 5 | +require 'yaml' |
| 6 | + |
| 7 | +class PuppetfileSourceDereference |
| 8 | + attr_accessor :source, :output, :dryrun, :mapping |
| 9 | + |
| 10 | + def initialize(options = {}) |
| 11 | + [:source, :output, :dryrun, :mapping,].each do |key| |
| 12 | + send("#{key}=", options[key]) |
| 13 | + end |
| 14 | + |
| 15 | + @output = File.open(@output, 'w+') if @output.is_a? String |
| 16 | + end |
| 17 | + |
| 18 | + def resolve |
| 19 | + puppetfile = PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(File.read(@source)) |
| 20 | + mapping = YAML.load(open(@mapping).read) |
| 21 | + |
| 22 | + unless puppetfile.valid? |
| 23 | + logger.error("Puppetfile source is not valid") |
| 24 | + puppetfile.validation_errors.each { |err| logger.error(err) } |
| 25 | + return false |
| 26 | + end |
| 27 | + |
| 28 | + File.open(@output, 'w') do |output| |
| 29 | + puppetfile.modules.each do |mod| |
| 30 | + if mod.module_type == :git |
| 31 | + output.write <<~EOF |
| 32 | + mod '#{mod.title}', |
| 33 | + :git => '#{mod.remote}', |
| 34 | + :ref => '#{mod.ref}' |
| 35 | + EOF |
| 36 | + elsif mapping.include? mod.title |
| 37 | + source = mapping[mod.title] |
| 38 | + |
| 39 | + if mod.version.is_a? String |
| 40 | + output.write <<~EOF |
| 41 | + mod '#{mod.title}', |
| 42 | + :git => '#{source}', |
| 43 | + :ref => '#{mod.version.delete_prefix('=')}' |
| 44 | + EOF |
| 45 | + else |
| 46 | + output.write <<~EOF |
| 47 | + mod '#{mod.title}', |
| 48 | + :git => '#{source}' |
| 49 | + EOF |
| 50 | + end |
| 51 | + else |
| 52 | + output.write "mod '#{mod.title}', #{version}\n" |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | +end |
0 commit comments