nanoc 4.11.0 → 4.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/NEWS.md +6 -0
  3. data/lib/nanoc.rb +4 -7
  4. data/lib/nanoc/base.rb +3 -3
  5. data/lib/nanoc/base/assertions.rb +1 -1
  6. data/lib/nanoc/base/entities.rb +2 -20
  7. data/lib/nanoc/base/entities/action_sequence.rb +60 -64
  8. data/lib/nanoc/base/entities/checksum_collection.rb +23 -21
  9. data/lib/nanoc/base/entities/dependency.rb +24 -22
  10. data/lib/nanoc/base/entities/outdatedness_reasons.rb +74 -72
  11. data/lib/nanoc/base/entities/outdatedness_status.rb +19 -17
  12. data/lib/nanoc/base/entities/props.rb +119 -117
  13. data/lib/nanoc/base/entities/site.rb +46 -50
  14. data/lib/nanoc/base/errors.rb +183 -198
  15. data/lib/nanoc/base/repos.rb +4 -2
  16. data/lib/nanoc/base/repos/action_sequence_store.rb +44 -42
  17. data/lib/nanoc/base/repos/aggregate_data_source.rb +24 -22
  18. data/lib/nanoc/base/repos/checksum_store.rb +51 -49
  19. data/lib/nanoc/base/repos/compiled_content_cache.rb +47 -45
  20. data/lib/nanoc/base/repos/compiled_content_store.rb +76 -0
  21. data/lib/nanoc/base/repos/config_loader.rb +74 -72
  22. data/lib/nanoc/base/repos/dependency_store.rb +174 -172
  23. data/lib/nanoc/base/repos/in_mem_data_source.rb +17 -15
  24. data/lib/nanoc/base/repos/item_rep_repo.rb +26 -24
  25. data/lib/nanoc/base/repos/outdatedness_store.rb +50 -48
  26. data/lib/nanoc/base/repos/prefixed_data_source.rb +21 -19
  27. data/lib/nanoc/base/repos/site_loader.rb +75 -73
  28. data/lib/nanoc/base/repos/store.rb +93 -91
  29. data/lib/nanoc/base/services.rb +7 -3
  30. data/lib/nanoc/base/services/action_provider.rb +23 -21
  31. data/lib/nanoc/base/services/action_sequence_builder.rb +42 -34
  32. data/lib/nanoc/base/services/compilation_context.rb +49 -47
  33. data/lib/nanoc/base/services/compiler.rb +177 -170
  34. data/lib/nanoc/base/services/compiler/phases.rb +8 -1
  35. data/lib/nanoc/base/services/compiler/phases/abstract.rb +44 -38
  36. data/lib/nanoc/base/services/compiler/phases/cache.rb +34 -28
  37. data/lib/nanoc/base/services/compiler/phases/mark_done.rb +17 -11
  38. data/lib/nanoc/base/services/compiler/phases/notify.rb +21 -0
  39. data/lib/nanoc/base/services/compiler/phases/recalculate.rb +37 -31
  40. data/lib/nanoc/base/services/compiler/phases/resume.rb +47 -48
  41. data/lib/nanoc/base/services/compiler/phases/write.rb +65 -59
  42. data/lib/nanoc/base/services/compiler/stage.rb +27 -8
  43. data/lib/nanoc/base/services/compiler/stages.rb +7 -1
  44. data/lib/nanoc/base/services/compiler/stages/build_reps.rb +25 -19
  45. data/lib/nanoc/base/services/compiler/stages/calculate_checksums.rb +34 -28
  46. data/lib/nanoc/base/services/compiler/stages/cleanup.rb +33 -27
  47. data/lib/nanoc/base/services/compiler/stages/compile_reps.rb +79 -69
  48. data/lib/nanoc/base/services/compiler/stages/determine_outdatedness.rb +46 -40
  49. data/lib/nanoc/base/services/compiler/stages/forget_outdated_dependencies.rb +15 -9
  50. data/lib/nanoc/base/services/compiler/stages/load_stores.rb +28 -22
  51. data/lib/nanoc/base/services/compiler/stages/postprocess.rb +16 -10
  52. data/lib/nanoc/base/services/compiler/stages/preprocess.rb +25 -19
  53. data/lib/nanoc/base/services/compiler/stages/prune.rb +23 -17
  54. data/lib/nanoc/base/services/compiler/stages/store_post_compilation_state.rb +15 -9
  55. data/lib/nanoc/base/services/compiler/stages/store_pre_compilation_state.rb +26 -20
  56. data/lib/nanoc/base/services/compiler_loader.rb +26 -24
  57. data/lib/nanoc/base/services/dependency_tracker.rb +47 -45
  58. data/lib/nanoc/base/services/executor.rb +16 -15
  59. data/lib/nanoc/base/services/filter.rb +37 -5
  60. data/lib/nanoc/base/services/instrumentor.rb +12 -10
  61. data/lib/nanoc/base/services/item_rep_builder.rb +21 -19
  62. data/lib/nanoc/base/services/item_rep_router.rb +72 -70
  63. data/lib/nanoc/base/services/item_rep_selector.rb +48 -46
  64. data/lib/nanoc/base/services/item_rep_writer.rb +58 -53
  65. data/lib/nanoc/base/services/outdatedness_checker.rb +181 -179
  66. data/lib/nanoc/base/services/outdatedness_rule.rb +23 -21
  67. data/lib/nanoc/base/services/outdatedness_rules.rb +5 -3
  68. data/lib/nanoc/base/services/outdatedness_rules/attributes_modified.rb +28 -24
  69. data/lib/nanoc/base/services/outdatedness_rules/code_snippets_modified.rb +20 -16
  70. data/lib/nanoc/base/services/outdatedness_rules/content_modified.rb +13 -9
  71. data/lib/nanoc/base/services/outdatedness_rules/item_collection_extended.rb +12 -8
  72. data/lib/nanoc/base/services/outdatedness_rules/layout_collection_extended.rb +12 -8
  73. data/lib/nanoc/base/services/outdatedness_rules/not_written.rb +10 -6
  74. data/lib/nanoc/base/services/outdatedness_rules/rules_modified.rb +39 -35
  75. data/lib/nanoc/base/services/outdatedness_rules/uses_always_outdated_filter.rb +19 -15
  76. data/lib/nanoc/base/services/pruner.rb +2 -2
  77. data/lib/nanoc/base/views.rb +7 -0
  78. data/lib/nanoc/base/views/basic_item_view.rb +1 -1
  79. data/lib/nanoc/base/views/compilation_item_rep_view.rb +2 -2
  80. data/lib/nanoc/base/views/identifiable_collection_view.rb +2 -2
  81. data/lib/nanoc/base/views/mixins/document_view_mixin.rb +1 -1
  82. data/lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb +5 -5
  83. data/lib/nanoc/base/views/mutable_item_collection_view.rb +3 -3
  84. data/lib/nanoc/base/views/mutable_layout_collection_view.rb +2 -2
  85. data/lib/nanoc/base/views/view_context_for_compilation.rb +6 -6
  86. data/lib/nanoc/base/views/view_context_for_pre_compilation.rb +2 -2
  87. data/lib/nanoc/base/views/view_context_for_shell.rb +2 -2
  88. data/lib/nanoc/checking/check.rb +1 -1
  89. data/lib/nanoc/cli/commands/compile_listeners/abstract.rb +24 -7
  90. data/lib/nanoc/cli/commands/compile_listeners/debug_printer.rb +79 -15
  91. data/lib/nanoc/cli/commands/compile_listeners/diff_generator.rb +4 -7
  92. data/lib/nanoc/cli/commands/compile_listeners/file_action_printer.rb +15 -24
  93. data/lib/nanoc/cli/commands/compile_listeners/timing_recorder.rb +22 -18
  94. data/lib/nanoc/cli/commands/create-site.rb +2 -7
  95. data/lib/nanoc/cli/commands/shell.rb +1 -1
  96. data/lib/nanoc/cli/commands/show-data.rb +9 -9
  97. data/lib/nanoc/cli/logger.rb +1 -1
  98. data/lib/nanoc/data_sources/filesystem.rb +8 -8
  99. data/lib/nanoc/filters/erb.rb +1 -1
  100. data/lib/nanoc/filters/erubi.rb +1 -1
  101. data/lib/nanoc/filters/erubis.rb +1 -1
  102. data/lib/nanoc/filters/haml.rb +1 -1
  103. data/lib/nanoc/filters/sass.rb +1 -1
  104. data/lib/nanoc/filters/slim.rb +1 -1
  105. data/lib/nanoc/helpers/breadcrumbs.rb +2 -2
  106. data/lib/nanoc/helpers/capturing.rb +9 -8
  107. data/lib/nanoc/helpers/filtering.rb +2 -2
  108. data/lib/nanoc/helpers/rendering.rb +1 -1
  109. data/lib/nanoc/rule_dsl.rb +10 -0
  110. data/lib/nanoc/rule_dsl/action_provider.rb +3 -3
  111. data/lib/nanoc/rule_dsl/action_recorder.rb +3 -3
  112. data/lib/nanoc/rule_dsl/action_sequence_calculator.rb +7 -7
  113. data/lib/nanoc/rule_dsl/compilation_rule.rb +2 -2
  114. data/lib/nanoc/rule_dsl/compilation_rule_context.rb +9 -9
  115. data/lib/nanoc/rule_dsl/compiler_dsl.rb +4 -4
  116. data/lib/nanoc/rule_dsl/routing_rule.rb +3 -3
  117. data/lib/nanoc/rule_dsl/rule.rb +5 -5
  118. data/lib/nanoc/rule_dsl/rule_context.rb +3 -3
  119. data/lib/nanoc/rule_dsl/rules_collection.rb +4 -4
  120. data/lib/nanoc/spec.rb +15 -15
  121. data/lib/nanoc/version.rb +1 -1
  122. metadata +10 -111
  123. data/lib/nanoc/base/contracts_support.rb +0 -130
  124. data/lib/nanoc/base/core_ext.rb +0 -5
  125. data/lib/nanoc/base/core_ext/array.rb +0 -50
  126. data/lib/nanoc/base/core_ext/hash.rb +0 -54
  127. data/lib/nanoc/base/core_ext/string.rb +0 -16
  128. data/lib/nanoc/base/entities/code_snippet.rb +0 -53
  129. data/lib/nanoc/base/entities/configuration-schema.json +0 -122
  130. data/lib/nanoc/base/entities/configuration.rb +0 -206
  131. data/lib/nanoc/base/entities/content.rb +0 -112
  132. data/lib/nanoc/base/entities/context.rb +0 -70
  133. data/lib/nanoc/base/entities/directed_graph.rb +0 -195
  134. data/lib/nanoc/base/entities/document.rb +0 -125
  135. data/lib/nanoc/base/entities/identifiable_collection.rb +0 -141
  136. data/lib/nanoc/base/entities/identifier.rb +0 -222
  137. data/lib/nanoc/base/entities/item.rb +0 -10
  138. data/lib/nanoc/base/entities/item_collection.rb +0 -14
  139. data/lib/nanoc/base/entities/item_rep.rb +0 -91
  140. data/lib/nanoc/base/entities/layout.rb +0 -10
  141. data/lib/nanoc/base/entities/layout_collection.rb +0 -14
  142. data/lib/nanoc/base/entities/lazy_value.rb +0 -43
  143. data/lib/nanoc/base/entities/pattern.rb +0 -85
  144. data/lib/nanoc/base/entities/processing_action.rb +0 -21
  145. data/lib/nanoc/base/entities/processing_actions.rb +0 -5
  146. data/lib/nanoc/base/entities/processing_actions/filter.rb +0 -36
  147. data/lib/nanoc/base/entities/processing_actions/layout.rb +0 -36
  148. data/lib/nanoc/base/entities/processing_actions/snapshot.rb +0 -46
  149. data/lib/nanoc/base/entities/snapshot_def.rb +0 -22
  150. data/lib/nanoc/base/repos/data_source.rb +0 -168
  151. data/lib/nanoc/base/repos/snapshot_repo.rb +0 -67
  152. data/lib/nanoc/base/services/checksummer.rb +0 -274
  153. data/lib/nanoc/base/services/notification_center.rb +0 -87
  154. data/lib/nanoc/base/services/temp_filename_factory.rb +0 -52
@@ -1,24 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- class InMemDataSource < Nanoc::DataSource
5
- attr_reader :items
6
- attr_reader :layouts
3
+ module Nanoc
4
+ module Int
5
+ class InMemDataSource < Nanoc::DataSource
6
+ attr_reader :items
7
+ attr_reader :layouts
7
8
 
8
- def initialize(items, layouts, orig_data_source = nil)
9
- super({}, '/', '/', {})
9
+ def initialize(items, layouts, orig_data_source = nil)
10
+ super({}, '/', '/', {})
10
11
 
11
- @items = items
12
- @layouts = layouts
13
- @orig_data_source = orig_data_source
14
- end
12
+ @items = items
13
+ @layouts = layouts
14
+ @orig_data_source = orig_data_source
15
+ end
15
16
 
16
- def item_changes
17
- @orig_data_source ? @orig_data_source.item_changes : super
18
- end
17
+ def item_changes
18
+ @orig_data_source ? @orig_data_source.item_changes : super
19
+ end
19
20
 
20
- def layout_changes
21
- @orig_data_source ? @orig_data_source.layout_changes : super
21
+ def layout_changes
22
+ @orig_data_source ? @orig_data_source.layout_changes : super
23
+ end
22
24
  end
23
25
  end
24
26
  end
@@ -1,35 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- # Stores item reps (in memory).
5
- #
6
- # @api private
7
- class ItemRepRepo
8
- include Enumerable
3
+ module Nanoc
4
+ module Int
5
+ # Stores item reps (in memory).
6
+ #
7
+ # @api private
8
+ class ItemRepRepo
9
+ include Enumerable
9
10
 
10
- def initialize
11
- @reps = []
12
- @reps_by_item = {}
13
- end
11
+ def initialize
12
+ @reps = []
13
+ @reps_by_item = {}
14
+ end
14
15
 
15
- def <<(rep)
16
- @reps << rep
16
+ def <<(rep)
17
+ @reps << rep
17
18
 
18
- @reps_by_item[rep.item] ||= []
19
- @reps_by_item[rep.item] << rep
20
- end
19
+ @reps_by_item[rep.item] ||= []
20
+ @reps_by_item[rep.item] << rep
21
+ end
21
22
 
22
- def to_a
23
- @reps
24
- end
23
+ def to_a
24
+ @reps
25
+ end
25
26
 
26
- def each(&block)
27
- @reps.each(&block)
28
- self
29
- end
27
+ def each(&block)
28
+ @reps.each(&block)
29
+ self
30
+ end
30
31
 
31
- def [](item)
32
- @reps_by_item.fetch(item, [])
32
+ def [](item)
33
+ @reps_by_item.fetch(item, [])
34
+ end
33
35
  end
34
36
  end
35
37
  end
@@ -1,53 +1,55 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- # @api private
5
- class OutdatednessStore < ::Nanoc::Int::Store
6
- include Nanoc::Int::ContractsSupport
7
-
8
- contract C::KeywordArgs[config: Nanoc::Int::Configuration] => C::Any
9
- def initialize(config:)
10
- super(Nanoc::Int::Store.tmp_path_for(config: config, store_name: 'outdatedness'), 1)
11
-
12
- @outdated_refs = Set.new
13
- end
14
-
15
- contract Nanoc::Int::ItemRep => C::Bool
16
- def include?(obj)
17
- @outdated_refs.include?(obj.reference)
18
- end
19
-
20
- contract Nanoc::Int::ItemRep => self
21
- def add(obj)
22
- @outdated_refs << obj.reference
23
- self
24
- end
25
-
26
- contract Nanoc::Int::ItemRep => self
27
- def remove(obj)
28
- @outdated_refs.delete(obj.reference)
29
- self
30
- end
31
-
32
- contract C::None => C::Bool
33
- def empty?
34
- @outdated_refs.empty?
35
- end
36
-
37
- contract C::None => self
38
- def clear
39
- @outdated_refs = Set.new
40
- self
41
- end
42
-
43
- protected
44
-
45
- def data
46
- @outdated_refs
47
- end
48
-
49
- def data=(new_data)
50
- @outdated_refs = Set.new(new_data)
3
+ module Nanoc
4
+ module Int
5
+ # @api private
6
+ class OutdatednessStore < ::Nanoc::Int::Store
7
+ include Nanoc::Core::ContractsSupport
8
+
9
+ contract C::KeywordArgs[config: Nanoc::Core::Configuration] => C::Any
10
+ def initialize(config:)
11
+ super(Nanoc::Int::Store.tmp_path_for(config: config, store_name: 'outdatedness'), 1)
12
+
13
+ @outdated_refs = Set.new
14
+ end
15
+
16
+ contract Nanoc::Core::ItemRep => C::Bool
17
+ def include?(obj)
18
+ @outdated_refs.include?(obj.reference)
19
+ end
20
+
21
+ contract Nanoc::Core::ItemRep => self
22
+ def add(obj)
23
+ @outdated_refs << obj.reference
24
+ self
25
+ end
26
+
27
+ contract Nanoc::Core::ItemRep => self
28
+ def remove(obj)
29
+ @outdated_refs.delete(obj.reference)
30
+ self
31
+ end
32
+
33
+ contract C::None => C::Bool
34
+ def empty?
35
+ @outdated_refs.empty?
36
+ end
37
+
38
+ contract C::None => self
39
+ def clear
40
+ @outdated_refs = Set.new
41
+ self
42
+ end
43
+
44
+ protected
45
+
46
+ def data
47
+ @outdated_refs
48
+ end
49
+
50
+ def data=(new_data)
51
+ @outdated_refs = Set.new(new_data)
52
+ end
51
53
  end
52
54
  end
53
55
  end
@@ -1,29 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- class PrefixedDataSource < Nanoc::DataSource
5
- def initialize(data_source, items_prefix, layouts_prefix)
6
- super({}, '/', '/', {})
3
+ module Nanoc
4
+ module Int
5
+ class PrefixedDataSource < Nanoc::DataSource
6
+ def initialize(data_source, items_prefix, layouts_prefix)
7
+ super({}, '/', '/', {})
7
8
 
8
- @data_source = data_source
9
- @items_prefix = items_prefix
10
- @layouts_prefix = layouts_prefix
11
- end
9
+ @data_source = data_source
10
+ @items_prefix = items_prefix
11
+ @layouts_prefix = layouts_prefix
12
+ end
12
13
 
13
- def items
14
- @data_source.items.map { |d| d.with_identifier_prefix(@items_prefix) }
15
- end
14
+ def items
15
+ @data_source.items.map { |d| d.with_identifier_prefix(@items_prefix) }
16
+ end
16
17
 
17
- def layouts
18
- @data_source.layouts.map { |d| d.with_identifier_prefix(@layouts_prefix) }
19
- end
18
+ def layouts
19
+ @data_source.layouts.map { |d| d.with_identifier_prefix(@layouts_prefix) }
20
+ end
20
21
 
21
- def item_changes
22
- @data_source.item_changes
23
- end
22
+ def item_changes
23
+ @data_source.item_changes
24
+ end
24
25
 
25
- def layout_changes
26
- @data_source.layout_changes
26
+ def layout_changes
27
+ @data_source.layout_changes
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -1,100 +1,102 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- class SiteLoader
5
- def new_from_cwd
6
- site_from_config(Nanoc::Int::ConfigLoader.new.new_from_cwd)
7
- end
3
+ module Nanoc
4
+ module Int
5
+ class SiteLoader
6
+ def new_from_cwd
7
+ site_from_config(Nanoc::Int::ConfigLoader.new.new_from_cwd)
8
+ end
8
9
 
9
- # @return [Boolean]
10
- def self.cwd_is_nanoc_site?
11
- Nanoc::Int::ConfigLoader.cwd_is_nanoc_site?
12
- end
10
+ # @return [Boolean]
11
+ def self.cwd_is_nanoc_site?
12
+ Nanoc::Int::ConfigLoader.cwd_is_nanoc_site?
13
+ end
13
14
 
14
- def gen_data_source_for_config(config)
15
- data_sources_to_aggregate =
16
- with_data_sources(config) do |data_sources|
17
- data_sources.map do |ds|
18
- Nanoc::Int::PrefixedDataSource.new(ds, ds.items_root, ds.layouts_root)
15
+ def gen_data_source_for_config(config)
16
+ data_sources_to_aggregate =
17
+ with_data_sources(config) do |data_sources|
18
+ data_sources.map do |ds|
19
+ Nanoc::Int::PrefixedDataSource.new(ds, ds.items_root, ds.layouts_root)
20
+ end
19
21
  end
20
- end
21
22
 
22
- Nanoc::Int::AggregateDataSource.new(data_sources_to_aggregate, config)
23
- end
23
+ Nanoc::Int::AggregateDataSource.new(data_sources_to_aggregate, config)
24
+ end
24
25
 
25
- private
26
+ private
26
27
 
27
- def site_from_config(config)
28
- code_snippets = code_snippets_from_config(config)
29
- code_snippets.each(&:load)
28
+ def site_from_config(config)
29
+ code_snippets = code_snippets_from_config(config)
30
+ code_snippets.each(&:load)
30
31
 
31
- data_source = gen_data_source_for_config(config)
32
+ data_source = gen_data_source_for_config(config)
32
33
 
33
- Nanoc::Int::Site.new(
34
- config: config,
35
- code_snippets: code_snippets,
36
- data_source: data_source,
37
- )
38
- end
34
+ Nanoc::Int::Site.new(
35
+ config: config,
36
+ code_snippets: code_snippets,
37
+ data_source: data_source,
38
+ )
39
+ end
39
40
 
40
- def with_data_sources(config, &_block)
41
- data_sources = create_data_sources(config)
41
+ def with_data_sources(config, &_block)
42
+ data_sources = create_data_sources(config)
42
43
 
43
- begin
44
- data_sources.each(&:use)
45
- yield(data_sources)
46
- ensure
47
- data_sources.each(&:unuse)
44
+ begin
45
+ data_sources.each(&:use)
46
+ yield(data_sources)
47
+ ensure
48
+ data_sources.each(&:unuse)
49
+ end
48
50
  end
49
- end
50
51
 
51
- def create_data_sources(config)
52
- config[:data_sources].map do |data_source_hash|
53
- # Get data source class
54
- data_source_class = Nanoc::DataSource.named(data_source_hash[:type].to_sym)
55
- if data_source_class.nil?
56
- raise Nanoc::Int::Errors::UnknownDataSource.new(data_source_hash[:type])
57
- end
52
+ def create_data_sources(config)
53
+ config[:data_sources].map do |data_source_hash|
54
+ # Get data source class
55
+ data_source_class = Nanoc::DataSource.named(data_source_hash[:type].to_sym)
56
+ if data_source_class.nil?
57
+ raise Nanoc::Int::Errors::UnknownDataSource.new(data_source_hash[:type])
58
+ end
58
59
 
59
- # Create data source
60
- data_source_class.new(
61
- config,
62
- data_source_hash[:items_root],
63
- data_source_hash[:layouts_root],
64
- data_source_hash.merge(data_source_hash[:config] || {}),
65
- )
60
+ # Create data source
61
+ data_source_class.new(
62
+ config,
63
+ data_source_hash[:items_root],
64
+ data_source_hash[:layouts_root],
65
+ data_source_hash.merge(data_source_hash[:config] || {}),
66
+ )
67
+ end
66
68
  end
67
- end
68
69
 
69
- def code_snippets_from_config(config)
70
- config[:lib_dirs].flat_map do |lib|
71
- Dir["#{lib}/**/*.rb"].sort.map do |filename|
72
- Nanoc::Int::CodeSnippet.new(
73
- read_code_snippet_contents(filename),
74
- filename,
75
- )
70
+ def code_snippets_from_config(config)
71
+ config[:lib_dirs].flat_map do |lib|
72
+ Dir["#{lib}/**/*.rb"].sort.map do |filename|
73
+ Nanoc::Core::CodeSnippet.new(
74
+ read_code_snippet_contents(filename),
75
+ filename,
76
+ )
77
+ end
76
78
  end
77
79
  end
78
- end
79
80
 
80
- ENCODING_REGEX = /\A#\s+(-\*-\s+)?(en)?coding: (?<encoding>[^\s]+)(\s+-\*-\s*)?\n{0,2}/.freeze
81
+ ENCODING_REGEX = /\A#\s+(-\*-\s+)?(en)?coding: (?<encoding>[^\s]+)(\s+-\*-\s*)?\n{0,2}/.freeze
81
82
 
82
- def encoding_from_magic_comment(raw)
83
- match = ENCODING_REGEX.match(raw)
84
- match ? match['encoding'] : nil
85
- end
83
+ def encoding_from_magic_comment(raw)
84
+ match = ENCODING_REGEX.match(raw)
85
+ match ? match['encoding'] : nil
86
+ end
86
87
 
87
- def read_code_snippet_contents(filename)
88
- raw = File.read(filename, encoding: 'ASCII-8BIT')
88
+ def read_code_snippet_contents(filename)
89
+ raw = File.read(filename, encoding: 'ASCII-8BIT')
89
90
 
90
- enc = encoding_from_magic_comment(raw)
91
- if enc
92
- raw = raw.force_encoding(enc).encode('UTF-8').sub(ENCODING_REGEX, '')
93
- else
94
- raw.force_encoding('UTF-8')
95
- end
91
+ enc = encoding_from_magic_comment(raw)
92
+ if enc
93
+ raw = raw.force_encoding(enc).encode('UTF-8').sub(ENCODING_REGEX, '')
94
+ else
95
+ raw.force_encoding('UTF-8')
96
+ end
96
97
 
97
- raw
98
+ raw
99
+ end
98
100
  end
99
101
  end
100
102
  end
@@ -1,112 +1,114 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Nanoc::Int
4
- # An abstract superclass for classes that need to store data to the
5
- # filesystem, such as checksums, cached compiled content and dependency
6
- # graphs.
7
- #
8
- # Each store has a version number. When attempting to load data from a store
9
- # that has an incompatible version number, no data will be loaded, but
10
- # {#version_mismatch_detected} will be called.
11
- #
12
- # @abstract Subclasses must implement {#data} and {#data=}, and may
13
- # implement {#no_data_found} and {#version_mismatch_detected}.
14
- #
15
- # @api private
16
- class Store
17
- include Nanoc::Int::ContractsSupport
18
-
19
- # @return [String] The name of the file where data will be loaded from and
20
- # stored to.
21
- attr_reader :filename
22
-
23
- # @return [Numeric] The version number corresponding to the file format
24
- # the data is in. When the file format changes, the version number
25
- # should be incremented.
26
- attr_reader :version
27
-
28
- # Creates a new store for the given filename.
3
+ module Nanoc
4
+ module Int
5
+ # An abstract superclass for classes that need to store data to the
6
+ # filesystem, such as checksums, cached compiled content and dependency
7
+ # graphs.
29
8
  #
30
- # @param [String] filename The name of the file where data will be loaded
31
- # from and stored to.
9
+ # Each store has a version number. When attempting to load data from a store
10
+ # that has an incompatible version number, no data will be loaded, but
11
+ # {#version_mismatch_detected} will be called.
12
+ #
13
+ # @abstract Subclasses must implement {#data} and {#data=}, and may
14
+ # implement {#no_data_found} and {#version_mismatch_detected}.
32
15
  #
33
- # @param [Numeric] version The version number corresponding to the file
34
- # format the data is in. When the file format changes, the version
35
- # number should be incremented.
36
- def initialize(filename, version)
37
- @filename = filename
38
- @version = version
39
- end
40
-
41
- # Logic for building tmp path from active environment and store name
42
16
  # @api private
43
- contract C::KeywordArgs[config: Nanoc::Int::Configuration, store_name: String] => C::AbsolutePathString
44
- def self.tmp_path_for(store_name:, config:)
45
- File.absolute_path(
46
- File.join(tmp_path_prefix(config.output_dir), store_name),
47
- config.dir,
48
- )
49
- end
50
-
51
- def self.tmp_path_prefix(output_dir)
52
- dir = Digest::SHA1.hexdigest(output_dir)[0..12]
53
- File.join('tmp', 'nanoc', dir)
54
- end
17
+ class Store
18
+ include Nanoc::Core::ContractsSupport
19
+
20
+ # @return [String] The name of the file where data will be loaded from and
21
+ # stored to.
22
+ attr_reader :filename
23
+
24
+ # @return [Numeric] The version number corresponding to the file format
25
+ # the data is in. When the file format changes, the version number
26
+ # should be incremented.
27
+ attr_reader :version
28
+
29
+ # Creates a new store for the given filename.
30
+ #
31
+ # @param [String] filename The name of the file where data will be loaded
32
+ # from and stored to.
33
+ #
34
+ # @param [Numeric] version The version number corresponding to the file
35
+ # format the data is in. When the file format changes, the version
36
+ # number should be incremented.
37
+ def initialize(filename, version)
38
+ @filename = filename
39
+ @version = version
40
+ end
55
41
 
56
- # @group Loading and storing data
42
+ # Logic for building tmp path from active environment and store name
43
+ # @api private
44
+ contract C::KeywordArgs[config: Nanoc::Core::Configuration, store_name: String] => C::AbsolutePathString
45
+ def self.tmp_path_for(store_name:, config:)
46
+ File.absolute_path(
47
+ File.join(tmp_path_prefix(config.output_dir), store_name),
48
+ config.dir,
49
+ )
50
+ end
57
51
 
58
- # @return The data that should be written to the disk
59
- #
60
- # @abstract This method must be implemented by the subclass.
61
- def data
62
- raise NotImplementedError.new('Nanoc::Int::Store subclasses must implement #data and #data=')
63
- end
52
+ def self.tmp_path_prefix(output_dir)
53
+ dir = Digest::SHA1.hexdigest(output_dir)[0..12]
54
+ File.join('tmp', 'nanoc', dir)
55
+ end
64
56
 
65
- # @param new_data The data that has been loaded from the disk
66
- #
67
- # @abstract This method must be implemented by the subclass.
68
- #
69
- # @return [void]
70
- def data=(new_data) # rubocop:disable Lint/UnusedMethodArgument
71
- raise NotImplementedError.new('Nanoc::Int::Store subclasses must implement #data and #data=')
72
- end
57
+ # @group Loading and storing data
73
58
 
74
- # Loads the data from the filesystem into memory. This method will set the
75
- # loaded data using the {#data=} method.
76
- #
77
- # @return [void]
78
- def load
79
- return unless File.file?(filename)
59
+ # @return The data that should be written to the disk
60
+ #
61
+ # @abstract This method must be implemented by the subclass.
62
+ def data
63
+ raise NotImplementedError.new('Nanoc::Int::Store subclasses must implement #data and #data=')
64
+ end
80
65
 
81
- begin
82
- pstore.transaction do
83
- return if pstore[:version] != version
66
+ # @param new_data The data that has been loaded from the disk
67
+ #
68
+ # @abstract This method must be implemented by the subclass.
69
+ #
70
+ # @return [void]
71
+ def data=(new_data) # rubocop:disable Lint/UnusedMethodArgument
72
+ raise NotImplementedError.new('Nanoc::Int::Store subclasses must implement #data and #data=')
73
+ end
84
74
 
85
- self.data = pstore[:data]
75
+ # Loads the data from the filesystem into memory. This method will set the
76
+ # loaded data using the {#data=} method.
77
+ #
78
+ # @return [void]
79
+ def load
80
+ return unless File.file?(filename)
81
+
82
+ begin
83
+ pstore.transaction do
84
+ return if pstore[:version] != version
85
+
86
+ self.data = pstore[:data]
87
+ end
88
+ rescue
89
+ FileUtils.rm_f(filename)
90
+ load
86
91
  end
87
- rescue
88
- FileUtils.rm_f(filename)
89
- load
90
92
  end
91
- end
92
93
 
93
- # Stores the data contained in memory to the filesystem. This method will
94
- # use the {#data} method to fetch the data that should be written.
95
- #
96
- # @return [void]
97
- def store
98
- FileUtils.mkdir_p(File.dirname(filename))
94
+ # Stores the data contained in memory to the filesystem. This method will
95
+ # use the {#data} method to fetch the data that should be written.
96
+ #
97
+ # @return [void]
98
+ def store
99
+ FileUtils.mkdir_p(File.dirname(filename))
99
100
 
100
- pstore.transaction do
101
- pstore[:data] = data
102
- pstore[:version] = version
101
+ pstore.transaction do
102
+ pstore[:data] = data
103
+ pstore[:version] = version
104
+ end
103
105
  end
104
- end
105
106
 
106
- private
107
+ private
107
108
 
108
- def pstore
109
- @pstore ||= PStore.new(filename)
109
+ def pstore
110
+ @pstore ||= PStore.new(filename)
111
+ end
110
112
  end
111
113
  end
112
114
  end