Sha256: b63561a62de5ac8c0c11f5981b0e04cb8976d0c5955ed4e56b725e347613506d

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

#!/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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proforma-1.0.0.pre.alpha bin/render