|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "pathname" |
| 5 | +require "optparse" |
| 6 | + |
| 7 | +require_relative "lib/compare_helpers" |
| 8 | + |
| 9 | +include CompareHelpers |
| 10 | + |
| 11 | +def usage |
| 12 | + puts "Usage: #{$PROGRAM_NAME} [options] <file>" |
| 13 | + puts "" |
| 14 | + puts "Compares an ERB template with both Herb and Erubi (compile and render)." |
| 15 | + puts "" |
| 16 | + puts "Options:" |
| 17 | + puts " --no-escape Disable HTML escaping" |
| 18 | + puts " --escape Enable HTML escaping (default: false)" |
| 19 | + puts " -h, --help Show this help message" |
| 20 | + exit(1) |
| 21 | +end |
| 22 | + |
| 23 | +options = {} |
| 24 | +options[:escape] = false |
| 25 | + |
| 26 | +OptionParser.new do |opts| |
| 27 | + opts.banner = "Usage: #{$PROGRAM_NAME} [options] <file>" |
| 28 | + |
| 29 | + opts.on("--no-escape", "Disable HTML escaping") do |
| 30 | + options[:escape] = false |
| 31 | + end |
| 32 | + |
| 33 | + opts.on("--escape", "Enable HTML escaping") do |
| 34 | + options[:escape] = true |
| 35 | + end |
| 36 | + |
| 37 | + opts.on("-h", "--help", "Show this help message") do |
| 38 | + usage |
| 39 | + end |
| 40 | +end.parse! |
| 41 | + |
| 42 | +file_path = ARGV[0] |
| 43 | + |
| 44 | +unless file_path |
| 45 | + puts "Error: No file specified" |
| 46 | + usage |
| 47 | +end |
| 48 | + |
| 49 | +unless File.exist?(file_path) |
| 50 | + puts "Error: File '#{file_path}' not found" |
| 51 | + exit(1) |
| 52 | +end |
| 53 | + |
| 54 | +bin_dir = File.expand_path(__dir__) |
| 55 | + |
| 56 | +template = File.read(file_path) |
| 57 | +show_template(file_path, template) |
| 58 | + |
| 59 | +args = [] |
| 60 | +args << "--escape" if options[:escape] |
| 61 | +args << "--no-escape" unless options[:escape] |
| 62 | +args << "--no-template" |
| 63 | +args << file_path |
| 64 | + |
| 65 | +box_header("Compiled HTML+ERB Template Output") |
| 66 | +puts "" |
| 67 | + |
| 68 | +compile_result = system(File.join(bin_dir, "compare-compile"), *args) |
| 69 | + |
| 70 | +puts "" |
| 71 | +box_header("Rendered HTML+ERB Template Output") |
| 72 | +puts "" |
| 73 | + |
| 74 | +render_result = system(File.join(bin_dir, "compare-render"), *args) |
| 75 | + |
| 76 | +puts "" |
| 77 | +box_header("Summary") |
| 78 | + |
| 79 | +if render_result && compile_result |
| 80 | + puts "✓ All comparisons passed!" |
| 81 | + puts "" |
| 82 | + puts " • Rendered outputs match" |
| 83 | + puts " • Compiled sources match" |
| 84 | + exit(0) |
| 85 | +elsif render_result |
| 86 | + puts "⚠ Rendered outputs match, but compiled sources differ" |
| 87 | + puts "" |
| 88 | + puts " • ✓ Rendered outputs match (what matters!)" |
| 89 | + puts " • ✗ Compiled sources differ (different formatting is OK)" |
| 90 | + puts "" |
| 91 | + puts "This is usually fine. Herb and Erubi format generated code differently," |
| 92 | + puts "but produce the same final output." |
| 93 | + exit(0) |
| 94 | +else |
| 95 | + puts "✗ Comparisons failed" |
| 96 | + puts "" |
| 97 | + puts " • Rendered output: #{render_result ? "✓ Match" : "✗ Differ"}" |
| 98 | + puts " • Compiled source: #{compile_result ? "✓ Match" : "✗ Differ"}" |
| 99 | + exit(1) |
| 100 | +end |
0 commit comments