Class: Nanoc2::CLI::CompileCommand

Inherits:
Cri::Command
  • Object
show all
Defined in:
lib/nanoc2/cli/commands/compile.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#aliasesObject



9
10
11
# File 'lib/nanoc2/cli/commands/compile.rb', line 9

def aliases
  []
end

#long_descObject



17
18
19
20
21
22
# File 'lib/nanoc2/cli/commands/compile.rb', line 17

def long_desc
  'Compile all pages and all assets of the current site. If a path is ' +
  'given, only the page or asset with the given path will be compiled. ' +
  'Additionally, only pages and assets that are outdated will be ' +
  'compiled, unless specified otherwise with the -a option.'
end

#nameObject



5
6
7
# File 'lib/nanoc2/cli/commands/compile.rb', line 5

def name
  'compile'
end

#option_definitionsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nanoc2/cli/commands/compile.rb', line 28

def option_definitions
  [
    # --all
    {
      :long => 'all', :short => 'a', :argument => :forbidden,
      :desc => 'compile all pages and assets, even those that aren\'t outdated'
    },
    # --pages
    {
      :long => 'pages', :short => 'P', :argument => :forbidden,
      :desc => 'only compile pages (no assets)'
    },
    # --assets
    {
      :long => 'assets', :short => 'A', :argument => :forbidden,
      :desc => 'only compile assets (no pages)'
    },
    # --only-outdated
    {
      :long => 'only-outdated', :short => 'o', :argument => :forbidden,
      :desc => 'only compile outdated pages and assets'
    },
  ]
end

#run(options, arguments) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/nanoc2/cli/commands/compile.rb', line 53

def run(options, arguments)
  # Make sure we are in a nanoc site directory
  @base.require_site

  # Find object with given path
  if arguments.size == 0
    # Find all pages and/or assets
    if options.has_key?(:pages)
      objs = @base.site.pages
    elsif options.has_key?(:assets)
      objs = @base.site.assets
    else
      objs = nil
    end
  else
    objs = arguments.map do |path|
      # Find object
      path = path.cleaned_path
      obj = @base.site.pages.find { |page| page.path == path }
      obj = @base.site.assets.find { |asset| asset.path == path } if obj.nil?

      # Ensure object
      if obj.nil?
        $stderr.puts "Unknown page or asset: #{path}"
        exit 1
      end

      obj
    end
  end

  # Compile site
  begin
    # Give feedback
    puts "Compiling #{objs.nil? ? 'site' : 'objects'}..."

    # Initialize profiling stuff
    time_before = Time.now
    @filter_times ||= {}
    @times_stack  ||= []
    setup_notifications

    # Parse all/only-outdated options
    if options.has_key?(:all)
      warn "WARNING: The --all option is no longer necessary as nanoc " +
           "2.2 compiles all pages and assets by default. To change this " +
           "behaviour, use the --only-outdated option."
    end
    compile_all = options.has_key?(:'only-outdated') ? false : true

    # Compile
    @base.site.compiler.run(
      objs,
      :even_when_not_outdated => compile_all
    )

    # Find reps
    page_reps  = @base.site.pages.map { |p| p.reps }.flatten
    asset_reps = @base.site.assets.map { |a| a.reps }.flatten
    reps       = page_reps + asset_reps

    # Show skipped reps
    reps.select { |r| !r.compiled? }.each do |rep|
      duration = @rep_times[rep.disk_path]
      Nanoc2::CLI::Logger.instance.file(:low, :skip, rep.disk_path, duration)
    end

    # Show non-written reps
    reps.select { |r| r.compiled? && r.attribute_named(:skip_output) }.each do |rep|
      duration = @rep_times[rep.disk_path]
      Nanoc2::CLI::Logger.instance.file(:low, :'not written', rep.disk_path, duration)
    end

    # Give general feedback
    puts
    puts "No objects were modified." unless reps.any? { |r| r.modified? }
    puts "#{objs.nil? ? 'Site' : 'Object'} compiled in #{format('%.2f', Time.now - time_before)}s."

    if options.has_key?(:verbose)
      print_state_feedback(reps)
      print_profiling_feedback(reps)
    end
  rescue Interrupt => e
    exit(1)
  rescue Exception => e
    print_error(e)
    exit(1)
  end
end

#short_descObject



13
14
15
# File 'lib/nanoc2/cli/commands/compile.rb', line 13

def short_desc
  'compile pages and assets of this site'
end

#usageObject



24
25
26
# File 'lib/nanoc2/cli/commands/compile.rb', line 24

def usage
  "nanoc2 compile [options] [path]"
end