Sha256: f6823d3b1c40d523e65860d626f37e1f7df2930137c43890010a692f7b12e8d6

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Nanoc::CLI::Commands::CompileListeners
  class FileActionPrinter < Abstract
    def initialize(reps:)
      @start_times = {}
      @acc_durations = {}

      @reps = reps
    end

    # @see Listener#start
    def start
      Nanoc::Int::NotificationCenter.on(:compilation_started, self) do |rep|
        @start_times[rep] = Time.now
        @acc_durations[rep] ||= 0.0
      end

      Nanoc::Int::NotificationCenter.on(:compilation_suspended, self) do |rep|
        @acc_durations[rep] += Time.now - @start_times[rep]
      end

      Nanoc::Int::NotificationCenter.on(:rep_written, self) do |rep, _binary, path, is_created, is_modified|
        @acc_durations[rep] += Time.now - @start_times[rep]
        duration = @acc_durations[rep]

        action =
          if is_created then :create
          elsif is_modified then :update
          else :identical
          end
        level =
          if is_created then :high
          elsif is_modified then :high
          else :low
          end
        log(level, action, path, duration)
      end
    end

    # @see Listener#stop
    def stop
      super

      Nanoc::Int::NotificationCenter.remove(:compilation_started, self)
      Nanoc::Int::NotificationCenter.remove(:compilation_suspended, self)
      Nanoc::Int::NotificationCenter.remove(:rep_written, self)

      @reps.reject(&:compiled?).each do |rep|
        raw_paths = rep.raw_paths.values.flatten.uniq
        raw_paths.each do |raw_path|
          log(:low, :skip, raw_path, nil)
        end
      end
    end

    private

    def log(level, action, path, duration)
      Nanoc::CLI::Logger.instance.file(level, action, path, duration)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nanoc-4.8.4 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.8.3 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.8.2 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.8.1 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.8.0 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.14 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.13 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.12 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.11 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.10 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb