|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "optparse" |
| 5 | +require "json" |
| 6 | + |
| 7 | +class ActionViewRenderer |
| 8 | + def initialize |
| 9 | + @file = nil |
| 10 | + @source = nil |
| 11 | + @json = false |
| 12 | + @silent = false |
| 13 | + @locals = {} |
| 14 | + end |
| 15 | + |
| 16 | + def run(args) |
| 17 | + parse_options(args) |
| 18 | + |
| 19 | + unless @file || @source |
| 20 | + puts "Error: No file or source specified" |
| 21 | + puts "Usage: actionview-render [options] <file>" |
| 22 | + puts " actionview-render -e '<%= link_to \"Profile\", @profile %>'" |
| 23 | + exit(1) |
| 24 | + end |
| 25 | + |
| 26 | + if @file && !File.exist?(@file) |
| 27 | + puts "Error: File '#{@file}' not found" |
| 28 | + exit(1) |
| 29 | + end |
| 30 | + |
| 31 | + render_template |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def parse_options(args) |
| 37 | + OptionParser.new do |opts| |
| 38 | + opts.banner = "Usage: actionview-render [options] <file>" |
| 39 | + opts.separator "" |
| 40 | + opts.separator "Render ERB templates using ActionView helpers" |
| 41 | + opts.separator "" |
| 42 | + opts.separator "Options:" |
| 43 | + |
| 44 | + opts.on("-e", "--eval SOURCE", "Evaluate source string instead of file") do |source| |
| 45 | + @source = source |
| 46 | + end |
| 47 | + |
| 48 | + opts.on("--json", "Output results as JSON") do |
| 49 | + @json = true |
| 50 | + end |
| 51 | + |
| 52 | + opts.on("--silent", "Silent mode (only success/failure)") do |
| 53 | + @silent = true |
| 54 | + end |
| 55 | + |
| 56 | + opts.on("-h", "--help", "Show this help message") do |
| 57 | + puts opts |
| 58 | + exit(0) |
| 59 | + end |
| 60 | + end.parse!(args) |
| 61 | + |
| 62 | + @file = args.first unless @source |
| 63 | + end |
| 64 | + |
| 65 | + def template_source |
| 66 | + @template_source ||= @source || File.read(@file) |
| 67 | + end |
| 68 | + |
| 69 | + def render_template |
| 70 | + require "action_view" |
| 71 | + |
| 72 | + lookup_context = ActionView::LookupContext.new([]) |
| 73 | + view = ActionView::Base.with_empty_template_cache.new(lookup_context, {}, nil) |
| 74 | + handler = ActionView::Template::Handlers::ERB.new |
| 75 | + |
| 76 | + template = ActionView::Template.new( |
| 77 | + template_source, |
| 78 | + @file || "(eval)", |
| 79 | + handler, |
| 80 | + locals: [], |
| 81 | + format: :html |
| 82 | + ) |
| 83 | + |
| 84 | + rendered = template.render(view, {}) |
| 85 | + |
| 86 | + if @json |
| 87 | + puts({ success: true, output: rendered, source: template_source }.to_json) |
| 88 | + elsif @silent |
| 89 | + puts "Success" |
| 90 | + else |
| 91 | + puts rendered |
| 92 | + end |
| 93 | + |
| 94 | + exit(0) |
| 95 | + rescue StandardError => e |
| 96 | + if @json |
| 97 | + puts({ success: false, error: e.message, source: template_source }.to_json) |
| 98 | + elsif @silent |
| 99 | + puts "Failed" |
| 100 | + else |
| 101 | + puts "Error: #{e.class}: #{e.message}" |
| 102 | + puts e.backtrace.first(5).map { |l| " #{l}" }.join("\n") unless @silent |
| 103 | + end |
| 104 | + |
| 105 | + exit(1) |
| 106 | + end |
| 107 | +end |
| 108 | + |
| 109 | +ActionViewRenderer.new.run(ARGV) if __FILE__ == $PROGRAM_NAME |
0 commit comments