lib/nanoc/base/services/compiler.rb in nanoc-4.5.2 vs lib/nanoc/base/services/compiler.rb in nanoc-4.5.3

- old
+ new

@@ -18,15 +18,20 @@ # # @api private class Compiler # Provides common functionality for accesing “context” of an item that is being compiled. class CompilationContext - def initialize(action_provider:, reps:, site:, compiled_content_cache:) + attr_reader :site + attr_reader :compiled_content_cache + attr_reader :snapshot_repo + + def initialize(action_provider:, reps:, site:, compiled_content_cache:, snapshot_repo:) @action_provider = action_provider @reps = reps @site = site @compiled_content_cache = compiled_content_cache + @snapshot_repo = snapshot_repo end 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::ProcessingActions::Filter) @@ -39,19 +44,21 @@ Nanoc::ViewContext.new( reps: @reps, items: @site.items, dependency_tracker: dependency_tracker, compilation_context: self, + snapshot_repo: @snapshot_repo, ) end def assigns_for(rep, dependency_tracker) + last_content = @snapshot_repo.get(rep, :last) content_or_filename_assigns = - if rep.binary? - { filename: rep.snapshot_contents[:last].filename } + if last_content.binary? + { filename: last_content.filename } else - { content: rep.snapshot_contents[:last].string } + { content: last_content.string } end view_context = create_view_context(dependency_tracker) content_or_filename_assigns.merge( @@ -61,18 +68,10 @@ 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 - - def site - @site - end - - def compiled_content_cache - @compiled_content_cache - end end # All phases for the compilation of a single item rep. Phases will be repeated for every rep. module Phases # Provides functionality for (re)calculating the content of an item rep, without caching or @@ -91,10 +90,12 @@ dependency_tracker = Nanoc::Int::DependencyTracker.new(@dependency_store) dependency_tracker.enter(rep.item) executor = Nanoc::Int::Executor.new(rep, @compilation_context, dependency_tracker) + @compilation_context.snapshot_repo.set(rep, :last, rep.item.content) + @action_provider.memory_for(rep).each do |action| case action when Nanoc::Int::ProcessingActions::Filter executor.filter(action.filter_name, action.params) when Nanoc::Int::ProcessingActions::Layout @@ -113,26 +114,28 @@ # Provides functionality for (re)calculating the content of an item rep, with caching or # outdatedness checking. Delegates to s::Recalculate if outdated or no cache available. class Cache include Nanoc::Int::ContractsSupport - def initialize(compiled_content_cache:, wrapped:) + def initialize(compiled_content_cache:, snapshot_repo:, wrapped:) @compiled_content_cache = compiled_content_cache + @snapshot_repo = snapshot_repo @wrapped = wrapped end contract Nanoc::Int::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Any def run(rep, is_outdated:) if can_reuse_content_for_rep?(rep, is_outdated: is_outdated) Nanoc::Int::NotificationCenter.post(:cached_content_used, rep) - rep.snapshot_contents = @compiled_content_cache[rep] + + @snapshot_repo.set_all(rep, @compiled_content_cache[rep]) else @wrapped.run(rep, is_outdated: is_outdated) end rep.compiled = true - @compiled_content_cache[rep] = rep.snapshot_contents + @compiled_content_cache[rep] = @snapshot_repo.get_all(rep) end contract Nanoc::Int::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Bool def can_reuse_content_for_rep?(rep, is_outdated:) !is_outdated && !@compiled_content_cache[rep].nil? @@ -185,20 +188,21 @@ end class Write include Nanoc::Int::ContractsSupport - def initialize(wrapped:) + def initialize(snapshot_repo:, wrapped:) + @snapshot_repo = snapshot_repo @wrapped = wrapped end contract Nanoc::Int::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Any def run(rep, is_outdated:) @wrapped.run(rep, is_outdated: is_outdated) rep.snapshot_defs.each do |sdef| - ItemRepWriter.new.write(rep, sdef.name) + ItemRepWriter.new.write(rep, @snapshot_repo, sdef.name) end end end class MarkDone @@ -319,18 +323,20 @@ compilation_context: @compilation_context, ) cache_phase = Phases::Cache.new( compiled_content_cache: @compiled_content_cache, + snapshot_repo: @compilation_context.snapshot_repo, wrapped: recalculate_phase, ) resume_phase = Phases::Resume.new( wrapped: cache_phase, ) write_phase = Phases::Write.new( + snapshot_repo: @compilation_context.snapshot_repo, wrapped: resume_phase, ) mark_done_phase = Phases::MarkDone.new( wrapped: write_phase, @@ -370,10 +376,13 @@ attr_reader :reps # @api private attr_reader :outdatedness_store + # @api private + attr_reader :snapshot_repo + def initialize(site, compiled_content_cache:, checksum_store:, rule_memory_store:, action_provider:, dependency_store:, outdatedness_checker:, reps:, outdatedness_store:) @site = site @compiled_content_cache = compiled_content_cache @checksum_store = checksum_store @@ -381,10 +390,13 @@ @dependency_store = dependency_store @outdatedness_checker = outdatedness_checker @reps = reps @action_provider = action_provider @outdatedness_store = outdatedness_store + + # TODO: inject + @snapshot_repo = Nanoc::Int::SnapshotRepo.new end def run_all preprocess_stage.run build_reps @@ -441,9 +453,10 @@ @_compilation_context ||= CompilationContext.new( action_provider: action_provider, reps: @reps, site: @site, compiled_content_cache: compiled_content_cache, + snapshot_repo: snapshot_repo, ) end private