lib/nanoc/cli/commands/compile.rb in nanoc-3.6.7 vs lib/nanoc/cli/commands/compile.rb in nanoc-3.6.8

- old
+ new

@@ -31,11 +31,11 @@ # and set up notifications to listen to. # # @abstract Subclasses must override {#start} and may override {#stop}. class Listener - def initialize(params={}) + 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 @@ -49,11 +49,11 @@ # # @return [void] # # @abstract def start - raise NotImplementedError, "Subclasses of Listener should implement #start" + raise NotImplementedError, 'Subclasses of Listener should implement #start' end # Stops the listener. The default implementation removes self from all notification center observers. # # @return [void] @@ -71,11 +71,11 @@ end # @see Listener#start def start require 'tempfile' - self.setup_diffs + setup_diffs old_contents = {} Nanoc::NotificationCenter.on(:will_write_rep) do |rep, snapshot| path = rep.raw_path(:snapshot => snapshot) old_contents[rep] = File.file?(path) ? File.read(path) : nil end @@ -91,11 +91,11 @@ end # @see Listener#stop def stop super - self.teardown_diffs + teardown_diffs end protected def setup_diffs @@ -159,11 +159,11 @@ def self.enable_for?(command_runner) command_runner.options.fetch(:verbose, false) end # @option params [Array<Nanoc::ItemRep>] :reps The list of item representations in the site - def initialize(params={}) + def initialize(params = {}) @times = {} @reps = params.fetch(:reps) end @@ -178,11 +178,11 @@ end end # @see Listener#stop def stop - self.print_profiling_feedback + print_profiling_feedback super end protected @@ -192,33 +192,33 @@ return if max_filter_name_length.nil? # Print warning if necessary if @reps.any? { |r| !r.compiled? } $stderr.puts - $stderr.puts "Warning: profiling information may not be accurate because " + - "some items were not compiled." + $stderr.puts 'Warning: profiling information may not be accurate because ' + + 'some items were not compiled.' end # Print header puts puts ' ' * max_filter_name_length + ' | count min avg max tot' puts '-' * max_filter_name_length + '-+-----------------------------------' durations_per_filter.to_a.sort_by { |r| r[1] }.each do |row| - self.print_row(row, max_filter_name_length) + print_row(row, max_filter_name_length) end end def print_row(row, length) # Extract data filter_name, samples = *row # Calculate stats count = samples.size min = samples.min - tot = samples.inject(0) { |memo, i| memo + i } - avg = tot/count + tot = samples.reduce(0) { |memo, i| memo + i } + avg = tot / count max = samples.max # Format stats count = format('%4d', count) min = format('%4.2f', min) @@ -232,11 +232,12 @@ end def durations_per_filter @_durations_per_filter ||= begin @times.keys.each_with_object({}) do |filter_name, result| - if durations = durations_for_filter(filter_name) + durations = durations_for_filter(filter_name) + if durations result[filter_name] = durations end end end end @@ -254,14 +255,14 @@ # 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.has_key?('TRAVIS') + !ENV.key?('TRAVIS') end - def initialize(params={}) + def initialize(params = {}) @gc_count = 0 end # @see Listener#start def start @@ -327,87 +328,84 @@ # 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={}) + def initialize(params = {}) @start_times = {} - @stop_times = {} @reps = params.fetch(:reps) end # @see Listener#start def start Nanoc::NotificationCenter.on(:compilation_started) do |rep| @start_times[rep.raw_path] = Time.now end - Nanoc::NotificationCenter.on(:compilation_ended) do |rep| - @stop_times[rep.raw_path] = Time.now - end Nanoc::NotificationCenter.on(:rep_written) do |rep, path, is_created, is_modified| - action = (is_created ? :create : (is_modified ? :update : :identical)) - level = (is_created ? :high : (is_modified ? :high : :low)) - log(level, action, path, duration_for(rep)) + duration = path && @start_times[path] ? Time.now - @start_times[path] : nil + action = + case + when is_created then :create + when is_modified then :update + else :identical + end + level = + case + when is_created then :high + when is_modified then :high + else :low + end + log(level, action, path, duration) end end # @see Listener#stop def stop super @reps.select { |r| !r.compiled? }.each do |rep| rep.raw_paths.each do |snapshot_name, raw_path| - log(:low, :skip, raw_path, duration_for(rep)) + log(:low, :skip, raw_path, nil) end end end - private + private - def duration_for(rep) - return nil if rep.raw_path.nil? - - start = @start_times[rep.raw_path] - stop = @stop_times[rep.raw_path] - return nil if start.nil? || stop.nil? - - stop - start - end - def log(level, action, path, duration) Nanoc::CLI::Logger.instance.file(level, action, path, duration) end end - def initialize(options, arguments, command, params={}) + def initialize(options, arguments, command, params = {}) super(options, arguments, command) - @listener_classes = params.fetch(:listener_classes, self.default_listener_classes) + @listener_classes = params.fetch(:listener_classes, default_listener_classes) end def run time_before = Time.now - self.load_site - self.check_for_deprecated_usage + load_site + check_for_deprecated_usage - puts "Compiling site…" - self.run_listeners_while do - self.site.compile - self.prune + puts 'Compiling site…' + run_listeners_while do + site.compile + prune end time_after = Time.now puts puts "Site compiled in #{format('%.2f', time_after - time_before)}s." end protected def prune - if self.site.config[:prune][:auto_prune] - Nanoc::Extra::Pruner.new(self.site, :exclude => self.prune_config_exclude).run + if site.config[:prune][:auto_prune] + Nanoc::Extra::Pruner.new(site, :exclude => prune_config_exclude).run end end def default_listener_classes [ @@ -420,39 +418,39 @@ end def setup_listeners @listeners = @listener_classes. select { |klass| klass.enable_for?(self) }. - map { |klass| klass.new(:reps => self.reps) } + map { |klass| klass.new(:reps => reps) } @listeners.each { |s| s.start } end def listeners @listeners end def run_listeners_while - self.setup_listeners + setup_listeners yield ensure - self.teardown_listeners + teardown_listeners end def teardown_listeners @listeners.each { |s| s.stop } end def reps - self.site.items.map { |i| i.reps }.flatten + site.items.map { |i| i.reps }.flatten end memoize :reps def check_for_deprecated_usage # Check presence of --all option - if options.has_key?(:all) || options.has_key?(:force) - $stderr.puts "Warning: the --force option (and its deprecated --all alias) are, as of nanoc 3.2, no longer supported and have no effect." + if options.key?(:all) || options.key?(:force) + $stderr.puts 'Warning: the --force option (and its deprecated --all alias) are, as of nanoc 3.2, no longer supported and have no effect.' end # Warn if trying to compile a single item if arguments.size == 1 $stderr.puts '-' * 80 @@ -460,14 +458,14 @@ $stderr.puts '-' * 80 end end def prune_config - self.site.config[:prune] || {} + site.config[:prune] || {} end def prune_config_exclude - self.prune_config[:exclude] || {} + prune_config[:exclude] || {} end end end