Class: Nanoc::Int::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/base/compilation/compiler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for compiling a site’s item representations.

The compilation process makes use of notifications (see NotificationCenter) to track dependencies between items, layouts, etc. The following notifications are used:

  • ‘compilation_started` — indicates that the compiler has started compiling this item representation. Has one argument: the item representation itself. Only one item can be compiled at a given moment; therefore, it is not possible to get two consecutive `compilation_started` notifications without also getting a `compilation_ended` notification in between them.

  • ‘compilation_ended` — indicates that the compiler has finished compiling this item representation (either successfully or with failure). Has one argument: the item representation itself.

  • ‘processing_started` — indicates that the compiler has started processing the specified object, which can be an item representation (when it is compiled) or a layout (when it is used to lay out an item representation or when it is used as a partial)

  • ‘processing_ended` — indicates that the compiler has finished processing the specified object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, compiled_content_cache:, checksum_store:, rule_memory_store:, action_provider:, dependency_store:, outdatedness_checker:, reps:) ⇒ Compiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Compiler.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nanoc/base/compilation/compiler.rb', line 60

def initialize(site, compiled_content_cache:, checksum_store:, rule_memory_store:, action_provider:, dependency_store:, outdatedness_checker:, reps:)
  @site = site

  @compiled_content_cache = compiled_content_cache
  @checksum_store         = checksum_store
  @rule_memory_store      = rule_memory_store
  @dependency_store       = dependency_store
  @outdatedness_checker   = outdatedness_checker
  @reps                   = reps
  @action_provider        = action_provider

  @stack = []
end

Instance Attribute Details

#action_providerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/nanoc/base/compilation/compiler.rb', line 49

def action_provider
  @action_provider
end

#checksum_storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/nanoc/base/compilation/compiler.rb', line 43

def checksum_store
  @checksum_store
end

#compiled_content_cacheObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/nanoc/base/compilation/compiler.rb', line 40

def compiled_content_cache
  @compiled_content_cache
end

#dependency_storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
# File 'lib/nanoc/base/compilation/compiler.rb', line 52

def dependency_store
  @dependency_store
end

#outdatedness_checkerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/nanoc/base/compilation/compiler.rb', line 55

def outdatedness_checker
  @outdatedness_checker
end

#repsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/nanoc/base/compilation/compiler.rb', line 58

def reps
  @reps
end

#rule_memory_storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
# File 'lib/nanoc/base/compilation/compiler.rb', line 46

def rule_memory_store
  @rule_memory_store
end

#siteObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/nanoc/base/compilation/compiler.rb', line 30

def site
  @site
end

#stackArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The compilation stack. When the compiler begins compiling a rep or a layout, it will be placed on the stack; when it is done compiling the rep or layout, it will be removed from the stack.

Returns:

  • (Array)

    The compilation stack



37
38
39
# File 'lib/nanoc/base/compilation/compiler.rb', line 37

def stack
  @stack
end

Instance Method Details

#assigns_for(rep, dependency_tracker) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The assigns that should be used in the next filter/layout operation.

Parameters:

  • rep (Nanoc::Int::ItemRep)

    The item representation for which the assigns should be fetched

Returns:

  • (Hash)

    The assigns that should be used in the next filter/layout operation



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nanoc/base/compilation/compiler.rb', line 142

def assigns_for(rep, dependency_tracker)
  content_or_filename_assigns =
    if rep.binary?
      { filename: rep.snapshot_contents[:last].filename }
    else
      { content: rep.snapshot_contents[:last].string }
    end

  view_context = create_view_context(dependency_tracker)

  content_or_filename_assigns.merge(
    item: Nanoc::ItemWithRepsView.new(rep.item, view_context),
    rep: Nanoc::ItemRepView.new(rep, view_context),
    item_rep: Nanoc::ItemRepView.new(rep, view_context),
    items: Nanoc::ItemCollectionWithRepsView.new(site.items, view_context),
    layouts: Nanoc::LayoutCollectionView.new(site.layouts, view_context),
    config: Nanoc::ConfigView.new(site.config, view_context),
  )
end

#build_repsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



128
129
130
131
132
133
# File 'lib/nanoc/base/compilation/compiler.rb', line 128

def build_reps
  builder = Nanoc::Int::ItemRepBuilder.new(
    site, action_provider, @reps
  )
  builder.run
end

#create_view_context(dependency_tracker) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



162
163
164
165
166
167
168
169
# File 'lib/nanoc/base/compilation/compiler.rb', line 162

def create_view_context(dependency_tracker)
  Nanoc::ViewContext.new(
    reps: @reps,
    items: @site.items,
    dependency_tracker: dependency_tracker,
    compiler: self,
  )
end

#filter_name_and_args_for_layout(layout) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



172
173
174
175
176
177
178
179
# File 'lib/nanoc/base/compilation/compiler.rb', line 172

def filter_name_and_args_for_layout(layout)
  mem = action_provider.memory_for(layout)
  if mem.nil? || mem.size != 1 || !mem[0].is_a?(Nanoc::Int::RuleMemoryActions::Filter)
    # FIXME: Provide a nicer error message
    raise Nanoc::Int::Errors::Generic, "No rule memory found for #{layout.identifier}"
  end
  [mem[0].filter_name, mem[0].params]
end

#load_storesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
103
104
105
106
# File 'lib/nanoc/base/compilation/compiler.rb', line 100

def load_stores
  # FIXME: icky hack to update the dependency store’s list of objects
  # (does not include preprocessed objects otherwise)
  dependency_store.objects = site.items.to_a + site.layouts.to_a

  stores.each(&:load)
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nanoc/base/compilation/compiler.rb', line 81

def run
  load_stores
  @site.freeze

  # Determine which reps need to be recompiled
  forget_dependencies_if_outdated

  @stack = []
  compile_reps
  store
ensure
  Nanoc::Int::TempFilenameFactory.instance.cleanup(
    Nanoc::Filter::TMP_BINARY_ITEMS_DIR,
  )
  Nanoc::Int::TempFilenameFactory.instance.cleanup(
    Nanoc::Int::ItemRepWriter::TMP_TEXT_ITEMS_DIR,
  )
end

#run_allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
# File 'lib/nanoc/base/compilation/compiler.rb', line 74

def run_all
  @action_provider.preprocess(@site)
  build_reps
  run
  @action_provider.postprocess(@site, @reps)
end

#storevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Store the modified helper data used for compiling the site.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/nanoc/base/compilation/compiler.rb', line 111

def store
  # Calculate rule memory
  (@reps.to_a + @site.layouts.to_a).each do |obj|
    rule_memory_store[obj] = action_provider.memory_for(obj).serialize
  end

  # Calculate checksums
  objects_to_checksum =
    site.items.to_a + site.layouts.to_a + site.code_snippets + [site.config]
  objects_to_checksum.each do |obj|
    checksum_store[obj] = Nanoc::Int::Checksummer.calc(obj)
  end

  # Store
  stores.each(&:store)
end