defmastership 1.0.19 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.gitlab-ci.yml +27 -0
  4. data/Gemfile +27 -16
  5. data/LICENSE +1 -1
  6. data/README.adoc +24 -0
  7. data/Rakefile +0 -1
  8. data/bin/defmastership +5 -5
  9. data/config/mutant.yml +18 -16
  10. data/config/rubocop.yml +5 -10
  11. data/defmastership.gemspec +3 -3
  12. data/lib/defmastership/batch_modifier.rb +23 -5
  13. data/lib/defmastership/comment_filter.rb +2 -1
  14. data/lib/defmastership/definition.rb +24 -4
  15. data/lib/defmastership/definition_parser.rb +2 -2
  16. data/lib/defmastership/document.rb +39 -19
  17. data/lib/defmastership/export/body_formatter.rb +55 -0
  18. data/lib/defmastership/export/csv/formatter.rb +64 -0
  19. data/lib/defmastership/export/header_formatter.rb +51 -0
  20. data/lib/defmastership/filters.rb +15 -12
  21. data/lib/defmastership/matching_line.rb +3 -2
  22. data/lib/defmastership/modifier/change_ref.rb +117 -0
  23. data/lib/defmastership/modifier/factory.rb +17 -0
  24. data/lib/defmastership/modifier/modifier_common.rb +71 -0
  25. data/lib/defmastership/modifier/rename_included_files.rb +223 -0
  26. data/lib/defmastership/modifier/update_def.rb +72 -0
  27. data/lib/defmastership/modifier/update_def_checksum.rb +17 -0
  28. data/lib/defmastership/modifier/update_def_version.rb +110 -0
  29. data/lib/defmastership/set_join_hack.rb +2 -0
  30. data/lib/defmastership/version.rb +3 -2
  31. data/lib/defmastership.rb +6 -8
  32. data/spec/unit/{def_mastership → defmastership}/batch_modifier_spec.rb +13 -13
  33. data/spec/unit/{def_mastership → defmastership}/definition_parser_spec.rb +1 -1
  34. data/spec/unit/{def_mastership → defmastership}/definition_spec.rb +1 -1
  35. data/spec/unit/{def_mastership → defmastership}/document_spec.rb +57 -57
  36. data/spec/unit/{def_mastership/csv_formatter_body_spec.rb → defmastership/export/body_formatter_spec.rb} +4 -4
  37. data/spec/unit/{def_mastership/csv_formatter_spec.rb → defmastership/export/csv/formatter_spec.rb} +13 -8
  38. data/spec/unit/{def_mastership/csv_formatter_header_spec.rb → defmastership/export/header_formatter_spec.rb} +3 -3
  39. data/spec/unit/{def_mastership → defmastership}/matching_line_spec.rb +1 -1
  40. data/spec/unit/{def_mastership/change_ref_modifier_spec.rb → defmastership/modifier/change_ref_spec.rb} +19 -44
  41. data/spec/unit/defmastership/modifier/factory_spec.rb +45 -0
  42. data/spec/unit/{def_mastership/modifier_spec.rb → defmastership/modifier/modifier_common_spec.rb} +3 -12
  43. data/spec/unit/{def_mastership/rename_included_files_modifier_spec.rb → defmastership/modifier/rename_included_files_spec.rb} +3 -3
  44. data/spec/unit/{def_mastership/update_def_checksum_modifier_spec.rb → defmastership/modifier/update_def_checksum_spec.rb} +10 -10
  45. data/spec/unit/{def_mastership/update_def_modifier_spec.rb → defmastership/modifier/update_def_spec.rb} +22 -20
  46. data/spec/unit/{def_mastership/update_def_version_modifier_spec.rb → defmastership/modifier/update_def_version_spec.rb} +38 -38
  47. data/spec/unit/{def_mastership_spec.rb → defmastership_spec.rb} +2 -2
  48. data/tasks/documentation.rake +19 -0
  49. data/tasks/package.rake +4 -0
  50. metadata +63 -43
  51. data/README.rdoc +0 -6
  52. data/defmastership.rdoc +0 -5
  53. data/lib/defmastership/change_ref_modifier.rb +0 -99
  54. data/lib/defmastership/constants.rb +0 -91
  55. data/lib/defmastership/csv_formatter.rb +0 -53
  56. data/lib/defmastership/csv_formatter_body.rb +0 -46
  57. data/lib/defmastership/csv_formatter_header.rb +0 -41
  58. data/lib/defmastership/modifier.rb +0 -42
  59. data/lib/defmastership/modifier_factory.rb +0 -12
  60. data/lib/defmastership/parsing_state.rb +0 -31
  61. data/lib/defmastership/rename_included_files_modifier.rb +0 -182
  62. data/lib/defmastership/update_def_checksum_modifier.rb +0 -16
  63. data/lib/defmastership/update_def_modifier.rb +0 -49
  64. data/lib/defmastership/update_def_version_modifier.rb +0 -91
  65. data/spec/unit/def_mastership/modifier_factory_spec.rb +0 -38
  66. data/spec/unit/def_mastership/parsing_state_spec.rb +0 -62
  67. data/tasks/package.task +0 -9
  68. /data/spec/unit/{def_mastership → defmastership}/string_spec.rb +0 -0
@@ -0,0 +1,110 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require 'defmastership/modifier/update_def'
5
+ require 'git'
6
+ require 'tmpdir'
7
+
8
+ module Defmastership
9
+ module Modifier
10
+ # modify one line after another
11
+ class UpdateDefVersion < UpdateDef
12
+ # @return [Hash{Symbol => Object}] the default configuration
13
+ def self.default_config
14
+ {
15
+ def_type: '',
16
+ ref_document: [],
17
+ ref_tag: '',
18
+ ref_repo: '.',
19
+ first_version: ''
20
+ }
21
+ end
22
+
23
+ # @param config [YAML] the modifier's provided configurations
24
+ def initialize(config)
25
+ @ref_document = Document.new
26
+
27
+ Helper.normalilize_config!(config) if config.key?(:ref_document)
28
+
29
+ super
30
+ end
31
+
32
+ # Apply the modifier on all provided asciidoc sources based on modifier's
33
+ # +self.replacement_methods+ list
34
+ #
35
+ # @param adoc_sources [Hash{String => String}] asciidoc sources
36
+ # * :key filename
37
+ # * :value file content
38
+ def do_modifications(adoc_sources)
39
+ if ref_tag == ''
40
+ ref_document.each { |ref_doc| @ref_document.parse_file_with_preprocessor(ref_doc) }
41
+ else
42
+ Dir.mktmpdir('defmastership') do |tmpdir|
43
+ parse_ref_files_from_git(adoc_sources, tmpdir)
44
+ end
45
+ end
46
+
47
+ super
48
+ end
49
+
50
+ private
51
+
52
+ def parse_ref_files_from_git(adoc_sources, tmpdir)
53
+ Git.clone(ref_repo, tmpdir, branch: ref_tag)
54
+ ref_sources = ref_document.empty? ? adoc_sources.keys : ref_document
55
+ ref_sources.each do |adoc_file|
56
+ @ref_document.parse_file_with_preprocessor("#{tmpdir}/#{adoc_file}")
57
+ end
58
+ end
59
+
60
+ def reference_replacement(reference, match)
61
+ "#{reference}#{version_and_checksum_str(match)}"
62
+ end
63
+
64
+ def version_and_checksum_str(match)
65
+ explicit_checksum = match[:explicit_checksum]
66
+ version = version_string(match)
67
+ return unless explicit_checksum || version
68
+
69
+ "(#{version}#{explicit_checksum})"
70
+ end
71
+
72
+ def version_string(match)
73
+ ref_definition = Helper.def_from_match(@ref_document, match)
74
+ definition = Helper.def_from_match(document, match)
75
+ return unless ref_definition
76
+
77
+ Helper.ref_version(ref_definition, definition, first_version)
78
+ end
79
+
80
+ # Helper functions
81
+ module Helper
82
+ # @param doc [Document] the document with defintions
83
+ # @param match [MatchData] Regexp match matching a reference
84
+ # @return [Definition] the definition from a +Document+ with a matching reference
85
+ def self.def_from_match(doc, match)
86
+ doc.ref_to_def(match[:reference])
87
+ end
88
+
89
+ # @param ref_definition [Definition] the definition from the reference document
90
+ # @param definition [Definition] the definition from the current document
91
+ # @param first_version [String] first version from configuration
92
+ # @return [String] the current definition version
93
+ def self.ref_version(ref_definition, definition, first_version)
94
+ new_ref_version = ref_definition.explicit_version
95
+ return new_ref_version if definition.sha256_short == ref_definition.sha256_short
96
+
97
+ new_ref_version ? new_ref_version.next : first_version
98
+ end
99
+
100
+ # Make sure that ref_document configuration is an Array
101
+ #
102
+ # @param config [YAML] the definition from the reference document
103
+ def self.normalilize_config!(config)
104
+ ref_docs = config.fetch(:ref_document)
105
+ config[:ref_document] = [ref_docs] if ref_docs.instance_of?(String)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -4,6 +4,8 @@
4
4
  if RUBY_VERSION < '3.0'
5
5
  # Ruby 2.7 do not implement join for Set
6
6
  class Set
7
+ # @param args [String] the separator (default: '')
8
+ # @return [String] join value of the set elements
7
9
  def join(*args)
8
10
  to_a.join(*args)
9
11
  end
@@ -1,7 +1,8 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- module DefMastership
5
- VERSION = '1.0.19'
4
+ module Defmastership
5
+ # [String] Gem version
6
+ VERSION = '1.1.0'
6
7
  public_constant :VERSION
7
8
  end
data/lib/defmastership.rb CHANGED
@@ -6,16 +6,14 @@ require('defmastership/version')
6
6
  # Add requires for other files you add to your project here, so
7
7
  # you just need to require this one file in your bin file
8
8
  require('defmastership/batch_modifier')
9
- require('defmastership/change_ref_modifier')
10
9
  require('defmastership/comment_filter')
11
- require('defmastership/constants')
12
- require('defmastership/csv_formatter')
13
10
  require('defmastership/definition')
14
11
  require('defmastership/definition_parser')
15
12
  require('defmastership/document')
13
+ require('defmastership/export/csv/formatter')
16
14
  require('defmastership/filters')
17
- require('defmastership/modifier_factory')
18
- require('defmastership/parsing_state')
19
- require('defmastership/rename_included_files_modifier')
20
- require('defmastership/update_def_checksum_modifier')
21
- require('defmastership/update_def_version_modifier')
15
+ require('defmastership/modifier/change_ref')
16
+ require('defmastership/modifier/factory')
17
+ require('defmastership/modifier/rename_included_files')
18
+ require('defmastership/modifier/update_def_checksum')
19
+ require('defmastership/modifier/update_def_version')
@@ -3,19 +3,19 @@
3
3
 
4
4
  require('defmastership/batch_modifier')
5
5
 
6
- module DefMastership
6
+ module Defmastership
7
7
  # Modifier example
8
- class TotoModifier
8
+ class Toto
9
9
  include Modifier
10
10
  end
11
11
 
12
12
  # Modifier example
13
- class TuTuModifier
13
+ class TuTu
14
14
  include Modifier
15
15
  end
16
16
  end
17
17
 
18
- RSpec.describe(DefMastership::BatchModifier) do
18
+ RSpec.describe(Defmastership::BatchModifier) do
19
19
  subject(:batchmodifier) do
20
20
  described_class.new(config, adoc_sources)
21
21
  end
@@ -45,7 +45,7 @@ RSpec.describe(DefMastership::BatchModifier) do
45
45
 
46
46
  describe '#apply' do
47
47
  context 'with only one modification' do
48
- let(:toto1) { instance_double(DefMastership::TotoModifier, 'toto1') }
48
+ let(:toto1) { instance_double(Defmastership::Modifier::Toto, 'toto1') }
49
49
  let(:adoc_sources_modified) do
50
50
  {
51
51
  'file1.adoc' => 'some text modified',
@@ -62,7 +62,7 @@ RSpec.describe(DefMastership::BatchModifier) do
62
62
  end
63
63
 
64
64
  before do
65
- allow(DefMastership::TotoModifier).to(receive(:new).once.and_return(toto1))
65
+ allow(Defmastership::Modifier::Toto).to(receive(:new).once.and_return(toto1))
66
66
  allow(toto1).to(receive(:do_modifications).once.and_return(adoc_sources_modified))
67
67
  allow(toto1).to(receive(:config).once.and_return(p: 'modified_param'))
68
68
  allow(toto1).to(receive(:changes).once.and_return([%w[from1 to1], %w[from2 to2]]))
@@ -70,7 +70,7 @@ RSpec.describe(DefMastership::BatchModifier) do
70
70
  end
71
71
 
72
72
  it do
73
- expect(DefMastership::TotoModifier).to(have_received(:new).with(p: 1))
73
+ expect(Defmastership::Modifier::Toto).to(have_received(:new).with(p: 1))
74
74
  end
75
75
 
76
76
  it { expect(toto1).to(have_received(:do_modifications).with(adoc_sources)) }
@@ -82,8 +82,8 @@ RSpec.describe(DefMastership::BatchModifier) do
82
82
  end
83
83
 
84
84
  context 'with two modifications' do
85
- let(:toto2) { instance_double(DefMastership::TotoModifier, 'toto2') }
86
- let(:tutu3) { instance_double(DefMastership::TuTuModifier, 'tutu3') }
85
+ let(:toto2) { instance_double(Defmastership::Modifier::Toto, 'toto2') }
86
+ let(:tutu3) { instance_double(Defmastership::Modifier::TuTu, 'tutu3') }
87
87
 
88
88
  let(:config_modified) do
89
89
  {
@@ -94,10 +94,10 @@ RSpec.describe(DefMastership::BatchModifier) do
94
94
  end
95
95
 
96
96
  before do
97
- allow(DefMastership::TotoModifier).to(receive(:new).once.and_return(toto2))
97
+ allow(Defmastership::Modifier::Toto).to(receive(:new).once.and_return(toto2))
98
98
  allow(toto2).to(receive(:do_modifications).once.and_return(:adoc_sources_modified_mod2))
99
99
  allow(toto2).to(receive(:config).once.and_return(:whatever))
100
- allow(DefMastership::TuTuModifier).to(receive(:new).once.and_return(tutu3))
100
+ allow(Defmastership::Modifier::TuTu).to(receive(:new).once.and_return(tutu3))
101
101
  allow(tutu3).to(receive(:do_modifications).once.and_return(:adoc_sources_modified_mod3))
102
102
  allow(tutu3).to(receive(:config).once.and_return(:pouet))
103
103
  allow(toto2).to(receive(:changes).once.and_return([%w[from1 to1]]))
@@ -105,8 +105,8 @@ RSpec.describe(DefMastership::BatchModifier) do
105
105
  batchmodifier.apply(%i[modifier2 modifier3])
106
106
  end
107
107
 
108
- it { expect(DefMastership::TotoModifier).to(have_received(:new).with(p: 'whatever1')) }
109
- it { expect(DefMastership::TuTuModifier).to(have_received(:new).with(p1: 'whatever2', p2: 'whatever3')) }
108
+ it { expect(Defmastership::Modifier::Toto).to(have_received(:new).with(p: 'whatever1')) }
109
+ it { expect(Defmastership::Modifier::TuTu).to(have_received(:new).with(p1: 'whatever2', p2: 'whatever3')) }
110
110
  it { expect(toto2).to(have_received(:do_modifications).with(adoc_sources)) }
111
111
  it { expect(toto2).to(have_received(:config).with(no_args)) }
112
112
  it { expect(tutu3).to(have_received(:do_modifications).with(:adoc_sources_modified_mod2)) }
@@ -3,7 +3,7 @@
3
3
 
4
4
  require('defmastership/definition_parser')
5
5
 
6
- RSpec.describe(DefMastership::DefinitionParser) do
6
+ RSpec.describe(Defmastership::DefinitionParser) do
7
7
  subject(:parser) { described_class.new(callback_object) }
8
8
 
9
9
  let(:callback_object) { double }
@@ -3,7 +3,7 @@
3
3
 
4
4
  require('defmastership/definition')
5
5
 
6
- RSpec.describe(DefMastership::Definition) do
6
+ RSpec.describe(Defmastership::Definition) do
7
7
  describe '.new' do
8
8
  context 'when minimal' do
9
9
  subject do