lib/nanoc/cli/commands/compile.rb in nanoc-3.7.4 vs lib/nanoc/cli/commands/compile.rb in nanoc-3.7.5
- old
+ new
@@ -19,31 +19,28 @@
option :a, :all, '(ignored)'
option :f, :force, '(ignored)'
module Nanoc::CLI::Commands
-
class Compile < ::Nanoc::CLI::CommandRunner
-
extend Nanoc::Memoization
# Listens to compilation events and reacts to them. This abstract class
# does not have a real implementation; subclasses should override {#start}
# and set up notifications to listen to.
#
# @abstract Subclasses must override {#start} and may override {#stop}.
class Listener
-
def initialize(_params = {})
end
# @param [Nanoc::CLI::CommandRunner] command_runner The command runner for this listener
#
# @return [Boolean] true if this listener should be enabled for the given command runner, false otherwise
#
# @abstract Returns `true` by default, but subclasses may override this.
- def self.enable_for?(_command_runner)
+ def self.enable_for?(command_runner) # rubocop:disable Lint/UnusedMethodArgument
true
end
# Starts the listener. Subclasses should override this method and set up listener notifications.
#
@@ -57,16 +54,14 @@
# Stops the listener. The default implementation removes self from all notification center observers.
#
# @return [void]
def stop
end
-
end
# Generates diffs for every output file written
class DiffGenerator < Listener
-
# @see Listener#enable_for?
def self.enable_for?(command_runner)
command_runner.site.config[:enable_output_diff]
end
@@ -74,11 +69,11 @@
def start
require 'tempfile'
setup_diffs
old_contents = {}
Nanoc::NotificationCenter.on(:will_write_rep) do |rep, snapshot|
- path = rep.raw_path(:snapshot => snapshot)
+ path = rep.raw_path(snapshot: snapshot)
old_contents[rep] = File.file?(path) ? File.read(path) : nil
end
Nanoc::NotificationCenter.on(:rep_written) do |rep, path, _is_created, _is_modified|
unless rep.binary?
new_contents = File.file?(path) ? File.read(path) : nil
@@ -103,11 +98,11 @@
@diff_threads = []
FileUtils.rm('output.diff') if File.file?('output.diff')
end
def teardown_diffs
- @diff_threads.each { |t| t.join }
+ @diff_threads.each(&:join)
end
def generate_diff_for(rep, old_content, new_content)
return if old_content == new_content
@@ -147,16 +142,14 @@
rescue Errno::ENOENT
warn 'Failed to run `diff`, so no diff with the previously compiled ' \
'content will be available.'
nil
end
-
end
# Records the time spent per filter and per item representation
class TimingRecorder < Listener
-
# @see Listener#enable_for?
def self.enable_for?(command_runner)
command_runner.options.fetch(:verbose, false)
end
@@ -169,11 +162,11 @@
# @see Listener#start
def start
Nanoc::NotificationCenter.on(:filtering_started) do |_rep, filter_name|
@times[filter_name] ||= []
- @times[filter_name] << { :start => Time.now }
+ @times[filter_name] << { start: Time.now }
end
Nanoc::NotificationCenter.on(:filtering_ended) do |_rep, filter_name|
@times[filter_name].last[:stop] = Time.now
end
end
@@ -213,11 +206,11 @@
filter_name, samples = *row
# Calculate stats
count = samples.size
min = samples.min
- tot = samples.reduce(0) { |memo, i| memo + i }
+ tot = samples.reduce(0) { |a, e| a + e }
avg = tot / count
max = samples.max
# Format stats
count = format('%4d', count)
@@ -251,16 +244,14 @@
result << sample[:stop] - sample[:start]
end
end
result
end
-
end
# Controls garbage collection so that it only occurs once every 20 items
class GCController < Listener
-
# @see Listener#enable_for?
def self.enable_for?(_command_runner)
!ENV.key?('TRAVIS')
end
@@ -283,16 +274,14 @@
# @see Listener#stop
def stop
super
GC.enable
end
-
end
# Prints debug information (compilation started/ended, filtering started/ended, …)
class DebugPrinter < Listener
-
# @see Listener#enable_for?
def self.enable_for?(command_runner)
command_runner.debug?
end
@@ -325,16 +314,14 @@
end
Nanoc::NotificationCenter.on(:dependency_created) do |src, dst|
puts "*** Dependency created from #{src.inspect} onto #{dst.inspect}"
end
end
-
end
# Prints file actions (created, updated, deleted, identical, skipped)
class FileActionPrinter < Listener
-
# @option params [Array<Nanoc::ItemRep>] :reps The list of item representations in the site
def initialize(params = {})
@start_times = {}
@reps = params.fetch(:reps)
@@ -376,11 +363,10 @@
private
def log(level, action, path, duration)
Nanoc::CLI::Logger.instance.file(level, action, path, duration)
end
-
end
def initialize(options, arguments, command, params = {})
super(options, arguments, command)
@listener_classes = params.fetch(:listener_classes, default_listener_classes)
@@ -405,11 +391,11 @@
protected
def prune
if site.config[:prune][:auto_prune]
- Nanoc::Extra::Pruner.new(site, :exclude => prune_config_exclude).run
+ Nanoc::Extra::Pruner.new(site, exclude: prune_config_exclude).run
end
end
def default_listener_classes
[
@@ -420,15 +406,16 @@
Nanoc::CLI::Commands::Compile::FileActionPrinter
]
end
def setup_listeners
- @listeners = @listener_classes
+ @listeners =
+ @listener_classes
.select { |klass| klass.enable_for?(self) }
- .map { |klass| klass.new(:reps => reps) }
+ .map { |klass| klass.new(reps: reps) }
- @listeners.each { |s| s.start }
+ @listeners.each(&:start)
end
def listeners
@listeners
end
@@ -439,15 +426,15 @@
ensure
teardown_listeners
end
def teardown_listeners
- @listeners.each { |s| s.stop }
+ @listeners.each(&:stop)
end
def reps
- site.items.map { |i| i.reps }.flatten
+ site.items.map(&:reps).flatten
end
memoize :reps
def check_for_deprecated_usage
# Check presence of --all option
@@ -468,11 +455,9 @@
end
def prune_config_exclude
prune_config[:exclude] || {}
end
-
end
-
end
runner Nanoc::CLI::Commands::Compile