lib/nanoc/base/compilation/compiler.rb in nanoc-3.8.0 vs lib/nanoc/base/compilation/compiler.rb in nanoc-4.0.0a1

- old
+ new

@@ -1,12 +1,12 @@ # encoding: utf-8 -module Nanoc +module Nanoc::Int # Responsible for compiling a site’s item representations. # # The compilation process makes use of notifications (see - # {Nanoc::NotificationCenter}) to track dependencies between items, + # {Nanoc::Int::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; @@ -35,16 +35,18 @@ # (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. + # + # @api private class Compiler - extend Nanoc::Memoization + extend Nanoc::Int::Memoization # @group Accessors - # @return [Nanoc::Site] The site this compiler belongs to + # @return [Nanoc::Int::Site] The site this compiler belongs to attr_reader :site # 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. @@ -54,11 +56,11 @@ # @group Public instance methods # Creates a new compiler fo the given site # - # @param [Nanoc::Site] site The site this compiler belongs to + # @param [Nanoc::Int::Site] site The site this compiler belongs to def initialize(site) @site = site @stack = [] end @@ -86,29 +88,27 @@ dependency_tracker.start compile_reps(reps) dependency_tracker.stop store ensure - Nanoc::TempFilenameFactory.instance.cleanup( + Nanoc::Int::TempFilenameFactory.instance.cleanup( Nanoc::Filter::TMP_BINARY_ITEMS_DIR) - Nanoc::TempFilenameFactory.instance.cleanup( - Nanoc::ItemRep::TMP_TEXT_ITEMS_DIR) + Nanoc::Int::TempFilenameFactory.instance.cleanup( + Nanoc::Int::ItemRep::TMP_TEXT_ITEMS_DIR) end # @group Private instance methods - # @return [Nanoc::RulesCollection] The collection of rules to be used + # @return [Nanoc::Int::RulesCollection] The collection of rules to be used # for compiling this site def rules_collection - Nanoc::RulesCollection.new(self) + Nanoc::Int::RulesCollection.new(self) end memoize :rules_collection # Load the helper data that is used for compiling the site. # - # @api private - # # @return [void] def load return if @loaded || @loading @loading = true @@ -133,12 +133,10 @@ @loading = false end # Undoes the effects of {#load}. Used when {#load} raises an exception. # - # @api private - # # @return [void] def unload return if @unloading @unloading = true @@ -156,36 +154,32 @@ @unloading = false end # Store the modified helper data used for compiling the site. # - # @api private - # # @return [void] def store # Calculate rule memory (reps + layouts).each do |obj| rule_memory_store[obj] = rule_memory_calculator[obj] end # Calculate checksums objects.each do |obj| - checksum_store[obj] = obj.checksum + checksum_store[obj] = obj.__nanoc_checksum end # Store stores.each(&:store) end # Returns the dependency tracker for this site, creating it first if it # does not yet exist. # - # @api private - # - # @return [Nanoc::DependencyTracker] The dependency tracker for this site + # @return [Nanoc::Int::DependencyTracker] The dependency tracker for this site def dependency_tracker - dt = Nanoc::DependencyTracker.new(@site.items + @site.layouts) + dt = Nanoc::Int::DependencyTracker.new(@site.items + @site.layouts) dt.compiler = self dt end memoize :dependency_tracker @@ -213,16 +207,16 @@ # @api private def build_reps items.each do |item| # Find matching rules matching_rules = rules_collection.item_compilation_rules_for(item) - raise Nanoc::Errors::NoMatchingCompilationRuleFound.new(item) if matching_rules.empty? + raise Nanoc::Int::Errors::NoMatchingCompilationRuleFound.new(item) if matching_rules.empty? # Create reps rep_names = matching_rules.map(&:rep_name).uniq rep_names.each do |rep_name| - item.reps << ItemRep.new(item, rep_name) + item.reps << Nanoc::Int::ItemRep.new(item, rep_name) end end end # Determines the paths of all item representations. @@ -230,11 +224,11 @@ # @api private def route_reps reps.each do |rep| # Find matching rules rules = rules_collection.routing_rules_for(rep) - raise Nanoc::Errors::NoMatchingRoutingRuleFound.new(rep) if rules[:last].nil? + raise Nanoc::Int::Errors::NoMatchingRoutingRuleFound.new(rep) if rules[:last].nil? rules.each_pair do |snapshot, rule| # Get basic path by applying matching rule basic_path = rule.apply_to(rep, compiler: self) next if basic_path.nil? @@ -257,11 +251,11 @@ end end end end - # @param [Nanoc::ItemRep] rep The item representation for which the + # @param [Nanoc::Int::ItemRep] rep The item representation for which the # assigns should be fetched # # @return [Hash] The assigns that should be used in the next filter/layout # operation # @@ -271,45 +265,46 @@ content_or_filename_assigns = { filename: rep.temporary_filenames[:last] } else content_or_filename_assigns = { content: rep.content[:last] } end + # TODO: Do not expose @site (necessary for captures store though…) content_or_filename_assigns.merge({ - item: rep.item, - rep: rep, - item_rep: rep, - items: site.items, - layouts: site.layouts, - config: site.config, - site: site + item: Nanoc::ItemView.new(rep.item), + rep: Nanoc::ItemRepView.new(rep), + item_rep: Nanoc::ItemRepView.new(rep), + items: Nanoc::ItemCollectionView.new(site.items), + layouts: Nanoc::LayoutCollectionView.new(site.layouts), + config: Nanoc::ConfigView.new(site.config), + site: Nanoc::SiteView.new(site), }) end - # @return [Nanoc::OutdatednessChecker] The outdatedness checker + # @return [Nanoc::Int::OutdatednessChecker] The outdatedness checker def outdatedness_checker - Nanoc::OutdatednessChecker.new( + Nanoc::Int::OutdatednessChecker.new( site: @site, checksum_store: checksum_store, dependency_tracker: dependency_tracker) end memoize :outdatedness_checker private - # @return [Array<Nanoc::Item>] The site’s items + # @return [Array<Nanoc::Int::Item>] The site’s items def items @site.items end memoize :items - # @return [Array<Nanoc::ItemRep>] The site’s item representations + # @return [Array<Nanoc::Int::ItemRep>] The site’s item representations def reps items.map(&:reps).flatten end memoize :reps - # @return [Array<Nanoc::Layout>] The site’s layouts + # @return [Array<Nanoc::Int::Layout>] The site’s layouts def layouts @site.layouts end memoize :layouts @@ -317,15 +312,15 @@ # # @param [Array] reps The item representations to compile. # # @return [void] def compile_reps(reps) - content_dependency_graph = Nanoc::DirectedGraph.new(reps) + content_dependency_graph = Nanoc::Int::DirectedGraph.new(reps) # Listen to processing start/stop - Nanoc::NotificationCenter.on(:processing_started, self) { |obj| @stack.push(obj) } - Nanoc::NotificationCenter.on(:processing_ended, self) { |_obj| @stack.pop } + Nanoc::Int::NotificationCenter.on(:processing_started, self) { |obj| @stack.push(obj) } + Nanoc::Int::NotificationCenter.on(:processing_ended, self) { |_obj| @stack.pop } # Assign snapshots reps.each do |rep| rep.snapshots = rules_collection.snapshots_for(rep) end @@ -338,47 +333,47 @@ @stack = [] begin compile_rep(rep) content_dependency_graph.delete_vertex(rep) - rescue Nanoc::Errors::UnmetDependency => e + rescue Nanoc::Int::Errors::UnmetDependency => e content_dependency_graph.add_edge(e.rep, rep) unless content_dependency_graph.vertices.include?(e.rep) content_dependency_graph.add_vertex(e.rep) end end end # Check whether everything was compiled unless content_dependency_graph.vertices.empty? - raise Nanoc::Errors::RecursiveCompilation.new(content_dependency_graph.vertices) + raise Nanoc::Int::Errors::RecursiveCompilation.new(content_dependency_graph.vertices) end ensure - Nanoc::NotificationCenter.remove(:processing_started, self) - Nanoc::NotificationCenter.remove(:processing_ended, self) + Nanoc::Int::NotificationCenter.remove(:processing_started, self) + Nanoc::Int::NotificationCenter.remove(:processing_ended, self) end # Compiles the given item representation. # # This method should not be called directly; please use - # {Nanoc::Compiler#run} instead, and pass this item representation's item + # {Nanoc::Int::Compiler#run} instead, and pass this item representation's item # as its first argument. # - # @param [Nanoc::ItemRep] rep The rep that is to be compiled + # @param [Nanoc::Int::ItemRep] rep The rep that is to be compiled # # @return [void] def compile_rep(rep) - Nanoc::NotificationCenter.post(:compilation_started, rep) - Nanoc::NotificationCenter.post(:processing_started, rep) - Nanoc::NotificationCenter.post(:visit_started, rep.item) + Nanoc::Int::NotificationCenter.post(:compilation_started, rep) + Nanoc::Int::NotificationCenter.post(:processing_started, rep) + Nanoc::Int::NotificationCenter.post(:visit_started, rep.item) # Calculate rule memory if we haven’t yet done do rules_collection.new_rule_memory_for_rep(rep) if !rep.item.forced_outdated? && !outdatedness_checker.outdated?(rep) && compiled_content_cache[rep] # Reuse content - Nanoc::NotificationCenter.post(:cached_content_used, rep) + Nanoc::Int::NotificationCenter.post(:cached_content_used, rep) rep.content = compiled_content_cache[rep] else # Recalculate content rep.snapshot(:raw) rep.snapshot(:pre, final: false) @@ -388,23 +383,23 @@ end rep.compiled = true compiled_content_cache[rep] = rep.content - Nanoc::NotificationCenter.post(:processing_ended, rep) - Nanoc::NotificationCenter.post(:compilation_ended, rep) + Nanoc::Int::NotificationCenter.post(:processing_ended, rep) + Nanoc::Int::NotificationCenter.post(:compilation_ended, rep) rescue => e rep.forget_progress - Nanoc::NotificationCenter.post(:compilation_failed, rep, e) + Nanoc::Int::NotificationCenter.post(:compilation_failed, rep, e) raise e ensure - Nanoc::NotificationCenter.post(:visit_ended, rep.item) + Nanoc::Int::NotificationCenter.post(:visit_ended, rep.item) end # Clears the list of dependencies for items that will be recompiled. # - # @param [Array<Nanoc::Item>] items The list of items for which to forget + # @param [Array<Nanoc::Int::Item>] items The list of items for which to forget # the dependencies # # @return [void] def forget_dependencies_if_outdated(items) items.each do |i| @@ -414,39 +409,39 @@ end end # Returns a preprocessor context, creating one if none exists yet. def preprocessor_context - Nanoc::Context.new({ - site: @site, - config: @site.config, - items: @site.items, - layouts: @site.layouts + Nanoc::Int::Context.new({ + site: Nanoc::SiteView.new(@site), # TODO: remove me + config: Nanoc::MutableConfigView.new(@site.config), + items: Nanoc::MutableItemCollectionView.new(@site.items), + layouts: Nanoc::MutableLayoutCollectionView.new(@site.layouts), }) end memoize :preprocessor_context - # @return [CompiledContentCache] The compiled content cache + # @return [Nanoc:Int::CompiledContentCache] The compiled content cache def compiled_content_cache - Nanoc::CompiledContentCache.new + Nanoc::Int::CompiledContentCache.new end memoize :compiled_content_cache # @return [ChecksumStore] The checksum store def checksum_store - Nanoc::ChecksumStore.new(site: @site) + Nanoc::Int::ChecksumStore.new(site: @site) end memoize :checksum_store # @return [RuleMemoryStore] The rule memory store def rule_memory_store - Nanoc::RuleMemoryStore.new(site: @site) + Nanoc::Int::RuleMemoryStore.new(site: @site) end memoize :rule_memory_store # @return [RuleMemoryCalculator] The rule memory calculator def rule_memory_calculator - Nanoc::RuleMemoryCalculator.new(rules_collection: rules_collection) + Nanoc::Int::RuleMemoryCalculator.new(rules_collection: rules_collection) end memoize :rule_memory_calculator # Returns all stores that can load/store data that can be used for # compilation.