Sha256: 51abb8fcdb187d1a4ae1ff9e42853315bf1a5744c4db1d36ad4519a8f9d528dd

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

usage 'show-rules [thing]'
aliases :explain
summary 'describe the rules for each item'
description "
Prints the rules used for all items and layouts in the current site.
"

module Nanoc::CLI::Commands
  class ShowRules < ::Nanoc::CLI::CommandRunner
    def run
      site = load_site

      res = Nanoc::Int::Compiler.new_for(site).run_until_reps_built
      reps = res.fetch(:reps)

      action_provider = Nanoc::Int::ActionProvider.named(:rule_dsl).for(site)
      rules = action_provider.rules_collection

      items = site.items.sort_by(&:identifier)
      layouts = site.layouts.sort_by(&:identifier)

      items.each   { |e| explain_item(e, rules: rules, reps: reps) }
      layouts.each { |e| explain_layout(e, rules: rules) }
    end

    def explain_item(item, rules:, reps:)
      puts(fmt_heading("Item #{item.identifier}") + ':')

      reps[item].each do |rep|
        rule = rules.compilation_rule_for(rep)
        puts "  Rep #{rep.name}: #{rule ? rule.pattern : '(none)'}"
      end

      puts
    end

    def explain_layout(layout, rules:)
      puts(fmt_heading("Layout #{layout.identifier}") + ':')

      found = false
      rules.layout_filter_mapping.each_key do |pattern|
        if pattern.match?(layout.identifier)
          puts "  #{pattern}"
          found = true
          break
        end
      end
      unless found
        puts '  (none)'
      end

      puts
    end

    def fmt_heading(s)
      Nanoc::CLI::ANSIStringColorizer.c(s, :bold, :yellow)
    end
  end
end

runner Nanoc::CLI::Commands::ShowRules

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nanoc-4.8.19 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.18 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.17 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.16 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.15 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.14 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.13 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.12 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.11 lib/nanoc/cli/commands/show-rules.rb
nanoc-4.8.10 lib/nanoc/cli/commands/show-rules.rb