lib/nanoc/base/services/compiler.rb in nanoc-4.8.9 vs lib/nanoc/base/services/compiler.rb in nanoc-4.8.10

- old
+ new

@@ -2,110 +2,97 @@ module Nanoc::Int class Compiler include Nanoc::Int::ContractsSupport - # @api private - attr_reader :site - - # @api private - attr_reader :compiled_content_cache - - # @api private - attr_reader :checksum_store - - # @api private - attr_reader :action_sequence_store - - # @api private - attr_reader :action_provider - - # @api private - attr_reader :dependency_store - - # @api private - attr_reader :reps - - # @api private - attr_reader :outdatedness_store - - # @api private - attr_reader :snapshot_repo - - def initialize(site, compiled_content_cache:, checksum_store:, action_sequence_store:, action_provider:, dependency_store:, reps:, outdatedness_store:) + def initialize(site, compiled_content_cache:, checksum_store:, action_sequence_store:, action_provider:, dependency_store:, outdatedness_store:) @site = site @compiled_content_cache = compiled_content_cache @checksum_store = checksum_store @action_sequence_store = action_sequence_store @dependency_store = dependency_store - @reps = reps @action_provider = action_provider @outdatedness_store = outdatedness_store - # TODO: inject @snapshot_repo = Nanoc::Int::SnapshotRepo.new end - def create_outdatedness_checker - Nanoc::Int::OutdatednessChecker.new( - site: @site, - checksum_store: @checksum_store, - dependency_store: @dependency_store, - action_sequence_store: @action_sequence_store, - action_sequences: @action_sequences, - checksums: @checksums, - reps: reps, - ) + contract Nanoc::Int::Site => Nanoc::Int::Compiler + def self.new_for(site) + Nanoc::Int::CompilerLoader.new.load(site) end - def run_all - prepare - - run_stage(forget_outdated_dependencies_stage, @outdated_items) - run_stage(store_pre_compilation_state_stage(@action_sequences), @checksums) - run_stage(prune_stage) - run_stage(compile_reps_stage(@action_sequences)) - run_stage(store_post_compilation_state_stage) - run_stage(postprocess_stage) - ensure - run_stage(cleanup_stage) + def run_until_preprocessed + @_res_preprocessed ||= begin + run_stage(preprocess_stage) + {} + end end - def prepare - # FIXME: State is ugly + def run_until_reps_built + @_res_reps_built ||= begin + prev = run_until_preprocessed - run_stage(preprocess_stage) - @action_sequences = run_stage(build_reps_stage) - run_stage(load_stores_stage) - @checksums = run_stage(calculate_checksums_stage) - @outdated_items = run_stage(determine_outdatedness_stage) - end + res = run_stage(build_reps_stage) - def compilation_context - @_compilation_context ||= Nanoc::Int::CompilationContext.new( - action_provider: action_provider, - reps: @reps, - site: @site, - compiled_content_cache: compiled_content_cache, - snapshot_repo: snapshot_repo, - ) + prev.merge( + reps: res.fetch(:reps), + action_sequences: res.fetch(:action_sequences), + ) + end end - # TODO: remove - def load_stores - load_stores_stage.run + def run_until_precompiled + @_res_precompiled ||= begin + prev = run_until_reps_built + action_sequences = prev.fetch(:action_sequences) + reps = prev.fetch(:reps) + + run_stage(load_stores_stage) + checksums = run_stage(calculate_checksums_stage) + outdatedness_checker = create_outdatedness_checker( + checksums: checksums, + action_sequences: action_sequences, + reps: reps, + ) + outdated_items = run_stage(determine_outdatedness_stage(outdatedness_checker, reps)) + + prev.merge( + checksums: checksums, + dependency_store: @dependency_store, + outdatedness_checker: outdatedness_checker, + outdated_items: outdated_items, + ) + end end - # TODO: remove - def build_reps - @action_sequences = build_reps_stage.run + def run_until_end + res = run_until_precompiled + action_sequences = res.fetch(:action_sequences) + reps = res.fetch(:reps) + checksums = res.fetch(:checksums) + outdated_items = res.fetch(:outdated_items) + + run_stage(forget_outdated_dependencies_stage, outdated_items) + run_stage(store_pre_compilation_state_stage(action_sequences, reps), checksums) + run_stage(prune_stage(reps)) + run_stage(compile_reps_stage(action_sequences, reps)) + run_stage(store_post_compilation_state_stage) + run_stage(postprocess_stage(reps), self) + ensure + run_stage(cleanup_stage) end - # TODO: remove - def calculate_checksums - @checksums = run_stage(calculate_checksums_stage) + def compilation_context(reps:) + Nanoc::Int::CompilationContext.new( + action_provider: @action_provider, + reps: reps, + site: @site, + compiled_content_cache: @compiled_content_cache, + snapshot_repo: @snapshot_repo, + ) end private def run_stage(stage, *args) @@ -113,40 +100,51 @@ stage.run(*args) ensure Nanoc::Int::NotificationCenter.post(:stage_ended, stage.class) end + def create_outdatedness_checker(checksums:, action_sequences:, reps:) + Nanoc::Int::OutdatednessChecker.new( + site: @site, + checksum_store: @checksum_store, + dependency_store: @dependency_store, + action_sequence_store: @action_sequence_store, + action_sequences: action_sequences, + checksums: checksums, + reps: reps, + ) + end + def preprocess_stage @_preprocess_stage ||= Stages::Preprocess.new( - action_provider: action_provider, - site: site, - dependency_store: dependency_store, - checksum_store: checksum_store, + action_provider: @action_provider, + site: @site, + dependency_store: @dependency_store, + checksum_store: @checksum_store, ) end def build_reps_stage @_build_reps_stage ||= Stages::BuildReps.new( - site: site, - action_provider: action_provider, - reps: @reps, + site: @site, + action_provider: @action_provider, ) end - def prune_stage + def prune_stage(reps) @_prune_stage ||= Stages::Prune.new( - config: site.config, + config: @site.config, reps: reps, ) end def load_stores_stage @_load_stores_stage ||= Stages::LoadStores.new( - checksum_store: checksum_store, - compiled_content_cache: compiled_content_cache, + checksum_store: @checksum_store, + compiled_content_cache: @compiled_content_cache, dependency_store: @dependency_store, - action_sequence_store: action_sequence_store, + action_sequence_store: @action_sequence_store, outdatedness_store: @outdatedness_store, ) end def calculate_checksums_stage @@ -156,53 +154,54 @@ code_snippets: @site.code_snippets, config: @site.config, ) end - def determine_outdatedness_stage + def determine_outdatedness_stage(outdatedness_checker, reps) @_determine_outdatedness_stage ||= Stages::DetermineOutdatedness.new( reps: reps, - outdatedness_checker: create_outdatedness_checker, - outdatedness_store: outdatedness_store, + outdatedness_checker: outdatedness_checker, + outdatedness_store: @outdatedness_store, ) end - def store_pre_compilation_state_stage(action_sequences) + def store_pre_compilation_state_stage(action_sequences, reps) @_store_pre_compilation_state_stage ||= Stages::StorePreCompilationState.new( - reps: @reps, - layouts: site.layouts, - checksum_store: checksum_store, - action_sequence_store: action_sequence_store, + reps: reps, + layouts: @site.layouts, + checksum_store: @checksum_store, + action_sequence_store: @action_sequence_store, action_sequences: action_sequences, ) end - def compile_reps_stage(action_sequences) + def compile_reps_stage(action_sequences, reps) @_compile_reps_stage ||= Stages::CompileReps.new( + reps: reps, outdatedness_store: @outdatedness_store, dependency_store: @dependency_store, action_sequences: action_sequences, - compilation_context: compilation_context, - compiled_content_cache: compiled_content_cache, + compilation_context: compilation_context(reps: reps), + compiled_content_cache: @compiled_content_cache, ) end def store_post_compilation_state_stage @_store_post_compilation_state_stage ||= Stages::StorePostCompilationState.new( - dependency_store: dependency_store, + dependency_store: @dependency_store, ) end - def postprocess_stage + def postprocess_stage(reps) @_postprocess_stage ||= Stages::Postprocess.new( action_provider: @action_provider, site: @site, - reps: @reps, + reps: reps, ) end def cleanup_stage - @_cleanup_stage ||= Stages::Cleanup.new(site.config) + @_cleanup_stage ||= Stages::Cleanup.new(@site.config) end def forget_outdated_dependencies_stage @_forget_outdated_dependencies_stage ||= Stages::ForgetOutdatedDependencies.new( dependency_store: @dependency_store,