Class: Nanoc3::CLI::Commands::Compile

Inherits:
Nanoc3::CLI::Command show all
Defined in:
lib/nanoc3/cli/commands/compile.rb

Instance Attribute Summary

Attributes inherited from Nanoc3::CLI::Command

#arguments, #command, #options

Instance Method Summary collapse

Methods inherited from Nanoc3::CLI::Command

#call, call, #initialize, #site

Constructor Details

This class inherits a constructor from Nanoc3::CLI::Command

Instance Method Details

#diff_strings(a, b) ⇒ Object

TODO move this elsewhere



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/nanoc3/cli/commands/compile.rb', line 175

def diff_strings(a, b)
  require 'open3'

  # Create files
  Tempfile.open('old') do |old_file|
    Tempfile.open('new') do |new_file|
      # Write files
      old_file.write(a)
      old_file.flush
      new_file.write(b)
      new_file.flush

      # Diff
      cmd = [ 'diff', '-u', old_file.path, new_file.path ]
      Open3.popen3(*cmd) do |stdin, stdout, stderr|
        result = stdout.read
        return (result == '' ? nil : result)
      end
    end
  end
rescue Errno::ENOENT
  warn 'Failed to run `diff`, so no diff with the previously compiled ' \
       'content will be available.'
  nil
end

#generate_diff_for(rep, snapshot) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/nanoc3/cli/commands/compile.rb', line 149

def generate_diff_for(rep, snapshot)
  return if !self.site.config[:enable_output_diff]
  return if !File.file?(rep.raw_path(:snapshot => snapshot))
  return if rep.binary?

  # Get old and new content
  old_content = File.read(rep.raw_path(:snapshot => snapshot))
  new_content = rep.compiled_content(:snapshot => snapshot, :force => true)

  # Check whether there’s a different
  return if old_content == new_content

  @diff_threads << Thread.new do
    # Generate diff
    diff = diff_strings(old_content, new_content)
    diff.sub!(/^--- .*/,    '--- ' + rep.raw_path)
    diff.sub!(/^\+\+\+ .*/, '+++ ' + rep.raw_path)

    # Write diff
    @diff_lock.synchronize do
      File.open('output.diff', 'a') { |io| io.write(diff) }
    end
  end
end


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nanoc3/cli/commands/compile.rb', line 237

def print_profiling_feedback(reps)
  # Get max filter length
  max_filter_name_length = @filter_times.keys.map { |k| k.to_s.size }.max
  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."
  end

  # Print header
  puts
  puts ' ' * max_filter_name_length + ' | count    min    avg    max     tot'
  puts '-' * max_filter_name_length + '-+-----------------------------------'

  @filter_times.to_a.sort_by { |r| r[1] }.each do |row|
    # Extract data
    filter_name, samples = *row

    # Calculate stats
    count = samples.size
    min   = samples.min
    tot   = samples.inject { |memo, i| memo + i}
    avg   = tot/count
    max   = samples.max

    # Format stats
    count = format('%4d',   count)
    min   = format('%4.2f', min)
    avg   = format('%4.2f', avg)
    max   = format('%4.2f', max)
    tot   = format('%5.2f', tot)

    # Output stats
    filter_name = format("%#{max_filter_name_length}s", filter_name)
    puts "#{filter_name} |  #{count}  #{min}s  #{avg}s  #{max}s  #{tot}s"
  end
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/nanoc3/cli/commands/compile.rb', line 31

def run
  # Make sure we are in a nanoc site directory
  puts "Loading site data..."
  self.require_site

  # 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."
  end

  # Warn if trying to compile a single item
  if arguments.size == 1
    $stderr.puts '-' * 80
    $stderr.puts 'Note: As of nanoc 3.2, it is no longer possible to compile a single item. When invoking the “compile” command, all items in the site will be compiled.'.make_compatible_with_env
    $stderr.puts '-' * 80
  end

  # Give feedback
  puts "Compiling site..."

  # Initialize profiling stuff
  time_before = Time.now
  @rep_times     = {}
  @filter_times  = {}
  setup_notifications

  # Prepare for generating diffs
  setup_diffs

  # Compile
  self.site.compile

  # Find reps
  reps = self.site.items.map { |i| i.reps }.flatten

  # Show skipped reps
  reps.select { |r| !r.compiled? }.each do |rep|
    rep.raw_paths.each do |snapshot_name, filename|
      next if filename.nil?
      duration = @rep_times[filename]
      Nanoc3::CLI::Logger.instance.file(:high, :skip, filename, duration)
    end
  end

  # Stop diffing
  teardown_diffs

  # Give general feedback
  puts
  puts "Site compiled in #{format('%.2f', Time.now - time_before)}s."

  # Give detailed feedback
  if options.has_key?(:verbose)
    print_profiling_feedback(reps)
  end
end

#setup_diffsObject



139
140
141
142
143
# File 'lib/nanoc3/cli/commands/compile.rb', line 139

def setup_diffs
  @diff_lock    = Mutex.new
  @diff_threads = []
  FileUtils.rm('output.diff') if File.file?('output.diff')
end

#setup_notificationsObject



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
# File 'lib/nanoc3/cli/commands/compile.rb', line 88

def setup_notifications
  # File notifications
  Nanoc3::NotificationCenter.on(:will_write_rep) do |rep, snapshot|
    generate_diff_for(rep, snapshot)
  end
  Nanoc3::NotificationCenter.on(:rep_written) do |rep, path, is_created, is_modified|
    action = (is_created ? :create : (is_modified ? :update : :identical))
    duration = Time.now - @rep_times[rep.raw_path] if @rep_times[rep.raw_path]
    Nanoc3::CLI::Logger.instance.file(:high, action, path, duration)
  end

  # Debug notifications
  if self.debug?
    Nanoc3::NotificationCenter.on(:compilation_started) do |rep|
      puts "*** Started compilation of #{rep.inspect}"
    end
    Nanoc3::NotificationCenter.on(:compilation_ended) do |rep|
      puts "*** Ended compilation of #{rep.inspect}"
    end
    Nanoc3::NotificationCenter.on(:compilation_failed) do |rep|
      puts "*** Suspended compilation of #{rep.inspect} due to unmet dependencies"
    end
    Nanoc3::NotificationCenter.on(:cached_content_used) do |rep|
      puts "*** Used cached compiled content for #{rep.inspect} instead of recompiling"
    end
    Nanoc3::NotificationCenter.on(:filtering_started) do |rep, filter_name|
      puts "*** Started filtering #{rep.inspect} with #{filter_name}"
    end
    Nanoc3::NotificationCenter.on(:filtering_ended) do |rep, filter_name|
      puts "*** Ended filtering #{rep.inspect} with #{filter_name}"
    end
  end

  # Timing notifications
  Nanoc3::NotificationCenter.on(:compilation_started) do |rep|
    @rep_times[rep.raw_path] = Time.now
  end
  Nanoc3::NotificationCenter.on(:compilation_ended) do |rep|
    @rep_times[rep.raw_path] = Time.now - @rep_times[rep.raw_path]
  end
  Nanoc3::NotificationCenter.on(:filtering_started) do |rep, filter_name|
    @filter_times[filter_name] ||= []
    @filter_times[filter_name] << Time.now
    start_filter_progress(rep, filter_name)
  end
  Nanoc3::NotificationCenter.on(:filtering_ended) do |rep, filter_name|
    @filter_times[filter_name] << Time.now - @filter_times[filter_name].pop
    stop_filter_progress(rep, filter_name)
  end
end

#start_filter_progress(rep, filter_name) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/nanoc3/cli/commands/compile.rb', line 201

def start_filter_progress(rep, filter_name)
  # Only show progress on terminals
  return if !$stdout.tty?

  @progress_thread = Thread.new do
    delay = 1.0
    step  = 0

    text = "Running #{filter_name} filter… ".make_compatible_with_env

    while !Thread.current[:stopped]
      sleep 0.1

      # Wait for a while before showing text
      delay -= 0.1
      next if delay > 0.05

      # Print progress
      $stdout.print text + %w( | / - \\ )[step] + "\r"
      step = (step + 1) % 4
    end

    # Clear text
    if delay < 0.05
      $stdout.print ' ' * (text.length + 1 + 1) + "\r"
    end
  end
end

#stop_filter_progress(rep, filter_name) ⇒ Object



230
231
232
233
234
235
# File 'lib/nanoc3/cli/commands/compile.rb', line 230

def stop_filter_progress(rep, filter_name)
  # Only show progress on terminals
  return if !$stdout.tty?

  @progress_thread[:stopped] = true
end

#teardown_diffsObject



145
146
147
# File 'lib/nanoc3/cli/commands/compile.rb', line 145

def teardown_diffs
  @diff_threads.each { |t| t.join }
end