Class: Judges::Update

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

Overview

Update.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ Update

Returns a new instance of Update.



36
37
38
# File 'lib/judges/commands/update.rb', line 36

def initialize(loog)
  @loog = loog
end

Instance Method Details

#run(opts, args) ⇒ Object



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
# File 'lib/judges/commands/update.rb', line 40

def run(opts, args)
  raise 'Exactly two arguments required' unless args.size == 2
  dir = args[0]
  raise "The directory is absent: #{dir}" unless File.exist?(dir)
  file = args[1]
  fb = Factbase.new
  if File.exist?(file)
    fb.import(File.read(file))
    @loog.info("Factbase imported from '#{file.to_rel}' (#{File.size(file)} bytes)")
  else
    @loog.info("There is no Factbase to import from '#{file.to_rel}' (file is absent)")
  end
  options = Judges::Options.new(opts['options'])
  @loog.debug("The following options provided:\n\t#{options.to_s.gsub("\n", "\n\t")}")
  errors = []
  done = Judges::Packs.new(dir, @loog).each_with_index do |p, i|
    @loog.info("Pack ##{i} found in #{p.dir.to_rel}")
    begin
      p.run(fb, options)
    rescue StandardError => e
      @loog.warn(Backtrace.new(e))
      errors << p.script
    end
  end
  @loog.info("#{done} judges processed (#{errors.size} errors)")
  FileUtils.mkdir_p(File.dirname(file))
  File.write(file, fb.export)
  @loog.info("Factbase exported to '#{file.to_rel}' (#{File.size(file)} bytes)")
  raise "Failed to update correctly (#{errors.size} errors)" unless errors.empty?
end