Class: Judges::Print

Inherits:
Object show all
Defined in:
lib/judges/commands/print.rb

Overview

The print command.

This class is instantiated by the bin/judge command line interface. You are not supposed to instantiate it yourself.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ Print

Initialize.

Parameters:

  • loog (Loog)

    Logging facility



28
29
30
# File 'lib/judges/commands/print.rb', line 28

def initialize(loog)
  @loog = loog
end

Instance Method Details

#run(opts, args) ⇒ Object

Run the print command (called by the bin/judges script).

Parameters:

  • opts (Hash)

    Command line options (start with ‘–’)

  • args (Array)

    List of command line arguments

Raises:

  • (RuntimeError)

    If no arguments provided



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/judges/commands/print.rb', line 36

def run(opts, args)
  raise 'At least one argument required' if args.empty?
  f = args[0]
  fb = Judges::Impex.new(@loog, f).import
  fb.query("(not #{opts['query']})").delete! unless opts['query'].nil?
  o = args[1]
  fmt = opts['format']&.downcase
  if o.nil?
    raise 'Either provide output file name or use --auto' unless opts['auto']
    o = File.join(File.dirname(f), File.basename(f).gsub(/\.[^.]*$/, ''))
    o = "#{o}.#{fmt}"
  end
  FileUtils.mkdir_p(File.dirname(o))
  if !opts['force'] && File.exist?(o)
    if File.mtime(f) <= File.mtime(o)
      @loog.info("No need to print to #{o.to_rel}, since it's up to date (#{File.size(o)} bytes)")
      return
    end
    @loog.debug("The factbase #{f.to_rel} is younger than the target #{o.to_rel}, need to print")
  end
  elapsed(@loog, level: Logger::INFO) do
    output =
      case fmt
        when 'yaml'
          require 'factbase/to_yaml'
          Factbase::ToYAML.new(fb).yaml
        when 'json'
          require 'factbase/to_json'
          Factbase::ToJSON.new(fb).json
        when 'xml'
          require 'factbase/to_xml'
          Factbase::ToXML.new(fb).xml
        when 'html'
          to_html(opts, fb)
        else
          raise "Unknown format '#{fmt}'"
      end
    File.binwrite(o, output)
    throw :"👍 Factbase printed to #{o.to_rel} (#{File.size(o)} bytes)"
  end
end