Skip to content

Commit

Permalink
Merge pull request #1160 from nanoc/pass-checksums-to-outdatedness-ch…
Browse files Browse the repository at this point in the history
…ecker

Pass checksums to outdatedness checker
  • Loading branch information
denisdefreyne committed Apr 17, 2017
2 parents 507ac0b + 2e4ac83 commit 2396ad2
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/nanoc/base/services/compiler.rb
Expand Up @@ -51,6 +51,7 @@ def create_outdatedness_checker
dependency_store: @dependency_store,
action_sequence_store: @action_sequence_store,
action_sequences: @action_sequences,
checksums: @checksums,
reps: reps,
)
end
Expand Down Expand Up @@ -98,6 +99,11 @@ def build_reps
@action_sequences = build_reps_stage.run
end

# TODO: remove
def calculate_checksums
@checksums = run_stage(calculate_checksums_stage)
end

private

def run_stage(stage, *args)
Expand Down
6 changes: 4 additions & 2 deletions lib/nanoc/base/services/outdatedness_checker.rb
Expand Up @@ -78,6 +78,7 @@ def apply_rules_multi(rules, objs)
include Nanoc::Int::ContractsSupport

attr_reader :checksum_store
attr_reader :checksums
attr_reader :dependency_store
attr_reader :action_sequence_store
attr_reader :action_sequences
Expand All @@ -88,10 +89,11 @@ def apply_rules_multi(rules, objs)
C_OBJ = C::Or[Nanoc::Int::Item, Nanoc::Int::ItemRep, Nanoc::Int::Layout]
C_ACTION_SEQUENCES = C::HashOf[C_OBJ => Nanoc::Int::ActionSequence]

contract C::KeywordArgs[site: Nanoc::Int::Site, checksum_store: Nanoc::Int::ChecksumStore, dependency_store: Nanoc::Int::DependencyStore, action_sequence_store: Nanoc::Int::ActionSequenceStore, action_sequences: C_ACTION_SEQUENCES, reps: Nanoc::Int::ItemRepRepo] => C::Any
def initialize(site:, checksum_store:, dependency_store:, action_sequence_store:, action_sequences:, reps:)
contract C::KeywordArgs[site: Nanoc::Int::Site, checksum_store: Nanoc::Int::ChecksumStore, checksums: Nanoc::Int::ChecksumCollection, dependency_store: Nanoc::Int::DependencyStore, action_sequence_store: Nanoc::Int::ActionSequenceStore, action_sequences: C_ACTION_SEQUENCES, reps: Nanoc::Int::ItemRepRepo] => C::Any
def initialize(site:, checksum_store:, checksums:, dependency_store:, action_sequence_store:, action_sequences:, reps:)
@site = site
@checksum_store = checksum_store
@checksums = checksums
@dependency_store = dependency_store
@action_sequence_store = action_sequence_store
@action_sequences = action_sequences
Expand Down
Expand Up @@ -17,7 +17,7 @@ class AttributesModified < Nanoc::Int::OutdatednessRule
return Nanoc::Int::OutdatednessReasons::AttributesModified.new(true)
end

new_checksums = Nanoc::Int::Checksummer.calc_for_each_attribute_of(obj)
new_checksums = outdatedness_checker.checksums.attributes_checksum_for(obj)

attributes = Set.new(old_checksums.keys) + Set.new(new_checksums.keys)
changed_attributes = attributes.reject { |a| old_checksums[a] == new_checksums[a] }
Expand Down
Expand Up @@ -17,7 +17,7 @@ def apply(_obj, outdatedness_checker)
memoized def any_snippets_modified?(outdatedness_checker)
outdatedness_checker.site.code_snippets.any? do |cs|
ch_old = outdatedness_checker.checksum_store[cs]
ch_new = Nanoc::Int::Checksummer.calc(cs)
ch_new = outdatedness_checker.checksums.checksum_for(cs)
ch_old != ch_new
end
end
Expand Down
Expand Up @@ -15,7 +15,7 @@ def apply(_obj, outdatedness_checker)
memoized def config_modified?(outdatedness_checker)
obj = outdatedness_checker.site.config
ch_old = outdatedness_checker.checksum_store[obj]
ch_new = Nanoc::Int::Checksummer.calc(obj)
ch_new = outdatedness_checker.checksums.checksum_for(obj)
ch_old != ch_new
end
end
Expand Down
Expand Up @@ -6,7 +6,7 @@ def apply(obj, outdatedness_checker)
obj = obj.item if obj.is_a?(Nanoc::Int::ItemRep)

ch_old = outdatedness_checker.checksum_store.content_checksum_for(obj)
ch_new = Nanoc::Int::Checksummer.calc_for_content_of(obj)
ch_new = outdatedness_checker.checksums.content_checksum_for(obj)
if ch_old != ch_new
Nanoc::Int::OutdatednessReasons::ContentModified
end
Expand Down
1 change: 1 addition & 0 deletions lib/nanoc/cli/commands/show-data.rb
Expand Up @@ -144,6 +144,7 @@ def print_layouts(layouts, compiler)
end

def print_outdatedness_reasons_for(obj, compiler)
compiler.calculate_checksums
outdatedness_checker = compiler.create_outdatedness_checker
reasons = outdatedness_checker.outdatedness_reasons_for(obj)
if reasons.any?
Expand Down
18 changes: 17 additions & 1 deletion spec/nanoc/base/services/outdatedness_checker_spec.rb
Expand Up @@ -3,6 +3,7 @@
described_class.new(
site: site,
checksum_store: checksum_store,
checksums: checksums,
dependency_store: dependency_store,
action_sequence_store: action_sequence_store,
action_sequences: action_sequences,
Expand All @@ -12,17 +13,28 @@

let(:checksum_store) { double(:checksum_store) }

let(:checksums) do
Nanoc::Int::Compiler::Stages::CalculateChecksums.new(
items: items,
layouts: layouts,
code_snippets: code_snippets,
config: config,
).run
end

let(:dependency_store) do
Nanoc::Int::DependencyStore.new(items, layouts)
end

let(:items) { Nanoc::Int::IdentifiableCollection.new(config, [item]) }
let(:layouts) { Nanoc::Int::IdentifiableCollection.new(config) }

let(:code_snippets) { [] }

let(:site) do
Nanoc::Int::Site.new(
config: config,
code_snippets: [],
code_snippets: code_snippets,
data_source: Nanoc::Int::InMemDataSource.new([], []),
)
end
Expand Down Expand Up @@ -152,6 +164,10 @@
let(:distant_item) { Nanoc::Int::Item.new('distant stuff', {}, '/distant.md') }
let(:distant_item_rep) { Nanoc::Int::ItemRep.new(distant_item, :default) }

let(:items) do
Nanoc::Int::IdentifiableCollection.new(config, [item, other_item, distant_item])
end

let(:action_sequences) do
{
item_rep => new_action_sequence_for_item_rep,
Expand Down
14 changes: 13 additions & 1 deletion spec/nanoc/base/services/outdatedness_rules_spec.rb
Expand Up @@ -8,6 +8,7 @@
Nanoc::Int::OutdatednessChecker.new(
site: site,
checksum_store: checksum_store,
checksums: checksums,
dependency_store: dependency_store,
action_sequence_store: action_sequence_store,
action_sequences: action_sequences,
Expand Down Expand Up @@ -36,6 +37,15 @@
let(:action_sequence_store) { Nanoc::Int::ActionSequenceStore.new }
let(:checksum_store) { Nanoc::Int::ChecksumStore.new(objects: objects) }

let(:checksums) do
Nanoc::Int::Compiler::Stages::CalculateChecksums.new(
items: items,
layouts: layouts,
code_snippets: code_snippets,
config: config,
).run
end

let(:items) { Nanoc::Int::IdentifiableCollection.new(config, [item]) }
let(:layouts) { Nanoc::Int::IdentifiableCollection.new(config) }

Expand All @@ -61,7 +71,7 @@
it { is_expected.not_to be }
end

context 'only non-outdated snippets' do
context 'only outdated snippets' do
let(:code_snippet) { Nanoc::Int::CodeSnippet.new('asdf', 'lib/foo.md') }
let(:code_snippet_old) { Nanoc::Int::CodeSnippet.new('aaaaaaaa', 'lib/foo.md') }
let(:code_snippets) { [code_snippet] }
Expand Down Expand Up @@ -344,6 +354,8 @@
let(:stored_obj) { raise 'override me' }
let(:new_obj) { raise 'override me' }

let(:items) { [new_obj] }

shared_examples 'a document' do
let(:stored_obj) { klass.new('a', {}, '/foo.md') }
let(:new_obj) { stored_obj }
Expand Down
2 changes: 2 additions & 0 deletions spec/nanoc/cli/commands/show_data_spec.rb
Expand Up @@ -167,6 +167,7 @@
allow(site).to receive(:compiler).and_return(compiler)
allow(compiler).to receive(:create_outdatedness_checker).and_return(outdatedness_checker)
allow(compiler).to receive(:reps).and_return(reps)
allow(compiler).to receive(:calculate_checksums)
end

context 'not outdated' do
Expand Down Expand Up @@ -239,6 +240,7 @@
allow(runner).to receive(:site).and_return(site)
allow(site).to receive(:compiler).and_return(compiler)
allow(compiler).to receive(:create_outdatedness_checker).and_return(outdatedness_checker)
allow(compiler).to receive(:calculate_checksums)
end

context 'not outdated' do
Expand Down

0 comments on commit 2396ad2

Please sign in to comment.