Sha256: 06861c543c8085696962c59cf8fb2dd057360034c853188273fce660a8c66034

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

usage 'compile [options]'
summary 'compile items of this site'
description <<~EOS
  Compile all items of the current site.
EOS
flag nil, :diff, 'generate diff'

module Nanoc::CLI::Commands
  class Compile < ::Nanoc::CLI::CommandRunner
    attr_accessor :listener_classes

    def initialize(options, arguments, command)
      super
      @listener_classes = default_listener_classes
    end

    def run
      time_before = Time.now

      @site = load_site

      puts 'Compiling siteā€¦'
      run_listeners_while do
        @site.compile
      end

      time_after = Time.now
      puts
      puts "Site compiled in #{format('%.2f', time_after - time_before)}s."
    end

    protected

    def default_listener_classes
      [
        Nanoc::CLI::Commands::CompileListeners::DiffGenerator,
        Nanoc::CLI::Commands::CompileListeners::DebugPrinter,
        Nanoc::CLI::Commands::CompileListeners::TimingRecorder,
        Nanoc::CLI::Commands::CompileListeners::FileActionPrinter,
      ]
    end

    def setup_listeners
      @listeners =
        @listener_classes
        .select { |klass| klass.enable_for?(self, @site) }
        .map    { |klass| klass.new(reps: reps) }

      @listeners.each(&:start_safely)
    end

    def listeners
      @listeners
    end

    def run_listeners_while
      setup_listeners
      yield
    ensure
      teardown_listeners
    end

    def teardown_listeners
      return unless @listeners
      @listeners.reverse_each(&:stop_safely)
    end

    def reps
      @site.compiler.reps
    end
  end
end

runner Nanoc::CLI::Commands::Compile

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nanoc-4.8.9 lib/nanoc/cli/commands/compile.rb
nanoc-4.8.8 lib/nanoc/cli/commands/compile.rb
nanoc-4.8.7 lib/nanoc/cli/commands/compile.rb
nanoc-4.8.6 lib/nanoc/cli/commands/compile.rb
nanoc-4.8.5 lib/nanoc/cli/commands/compile.rb
nanoc-4.8.4 lib/nanoc/cli/commands/compile.rb