#!/usr/bin/env ruby # frozen_string_literal: true require 'bundler/setup' require 'proforma' require 'fileutils' require 'yaml' require 'pry' def read_yaml(filename) # rubocop:disable Security/YAMLLoad YAML.load(read(filename)) # rubocop:enable Security/YAMLLoad end def read(filename) File.open(filename, 'r:bom|utf-8').read end jobs_dir = File.join('tmp', 'jobs') output_dir = File.join('tmp', 'jobs_output') jobs = Dir[File.join(jobs_dir, '*.yml')] if jobs.empty? puts "No jobs detected in the path: #{jobs_dir}" puts '--------------------------------------------------------------' puts "To use, place YAML files into #{jobs_dir} directory." puts 'You can find example YAML files in: spec/fixtures/snapshots' puts '--------------------------------------------------------------' exit end jobs.each do |job| job_name = File.basename(job, File.extname(job)) puts "Rendering: #{job_name} (#{job})" contents = read_yaml(job) data = contents['data'] template = contents['template'] evaluator = contents['evaluator'] || Proforma::HashEvaluator.new renderer = contents['renderer'] || Proforma::PlainTextRenderer.new job_output_dir = File.join(output_dir, job_name) FileUtils.mkdir_p(job_output_dir) outputs = Proforma.render( data, template, evaluator: evaluator, renderer: renderer ) outputs.each_with_index do |output, index| name_without_extension = [ index.to_s, output.title.to_s ].reject(&:empty?).join('.') filename = "#{name_without_extension}#{output.extension}" IO.write(File.join(job_output_dir, filename), output.contents) end puts "Done Rendering: #{job} (output can be found in: #{job_output_dir})" end