Sha256: ec3d879359d7d60a2ac4647b91b166b3837a9388f0a27dbff37039dd36d20506

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

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

9 entries across 9 versions & 1 rubygems

Version Path
nanoc-4.7.9 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.8 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.7 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.6 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.5 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.4 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.3 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.2 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb
nanoc-4.7.1 lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb