defmastership 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.gitlab-ci.yml +37 -11
  4. data/Gemfile +71 -1
  5. data/Guardfile +44 -0
  6. data/Rakefile +16 -61
  7. data/bin/defmastership +9 -6
  8. data/config/cucumber.yml +3 -0
  9. data/config/mutant.yml +27 -3
  10. data/config/reek.yml +129 -105
  11. data/config/rubocop.yml +72 -28
  12. data/defmastership.gemspec +5 -13
  13. data/features/changeref.feature +0 -8
  14. data/features/definition_checksum.feature +30 -10
  15. data/features/definition_version.feature +168 -10
  16. data/features/export.feature +64 -17
  17. data/features/modify.feature +1 -5
  18. data/features/rename_included_files.feature +27 -4
  19. data/features/step_definitions/git_steps.rb +19 -0
  20. data/lib/defmastership/batch_modifier.rb +17 -12
  21. data/lib/defmastership/change_ref_modifier.rb +88 -6
  22. data/lib/defmastership/comment_filter.rb +1 -1
  23. data/lib/defmastership/constants.rb +10 -7
  24. data/lib/defmastership/csv_formatter.rb +16 -12
  25. data/lib/defmastership/csv_formatter_body.rb +18 -15
  26. data/lib/defmastership/csv_formatter_header.rb +1 -1
  27. data/lib/defmastership/definition.rb +59 -20
  28. data/lib/defmastership/document.rb +101 -74
  29. data/lib/defmastership/matching_line.rb +17 -0
  30. data/lib/defmastership/modifier.rb +42 -0
  31. data/lib/defmastership/modifier_factory.rb +12 -0
  32. data/lib/defmastership/parsing_state.rb +15 -9
  33. data/lib/defmastership/rename_included_files_modifier.rb +172 -5
  34. data/lib/defmastership/set_join_hack.rb +11 -0
  35. data/lib/defmastership/update_def_checksum_modifier.rb +8 -13
  36. data/lib/defmastership/update_def_modifier.rb +49 -0
  37. data/lib/defmastership/update_def_version_modifier.rb +81 -15
  38. data/lib/defmastership/version.rb +1 -1
  39. data/lib/defmastership.rb +1 -6
  40. data/spec/spec_helper.rb +3 -1
  41. data/spec/unit/def_mastership/batch_modifier_spec.rb +40 -36
  42. data/spec/unit/def_mastership/change_ref_modifier_spec.rb +196 -51
  43. data/spec/unit/def_mastership/csv_formatter_body_spec.rb +60 -31
  44. data/spec/unit/def_mastership/csv_formatter_header_spec.rb +1 -1
  45. data/spec/unit/def_mastership/csv_formatter_spec.rb +79 -87
  46. data/spec/unit/def_mastership/definition_parser_spec.rb +1 -1
  47. data/spec/unit/def_mastership/definition_spec.rb +16 -6
  48. data/spec/unit/def_mastership/document_spec.rb +81 -38
  49. data/spec/unit/def_mastership/matching_line_spec.rb +37 -0
  50. data/spec/unit/def_mastership/modifier_factory_spec.rb +38 -0
  51. data/spec/unit/def_mastership/modifier_spec.rb +85 -0
  52. data/spec/unit/def_mastership/parsing_state_spec.rb +1 -1
  53. data/spec/unit/def_mastership/rename_included_files_modifier_spec.rb +219 -47
  54. data/spec/unit/def_mastership/string_spec.rb +1 -1
  55. data/spec/unit/def_mastership/update_def_checksum_modifier_spec.rb +82 -50
  56. data/spec/unit/def_mastership/update_def_modifier_spec.rb +121 -0
  57. data/spec/unit/def_mastership/update_def_version_modifier_spec.rb +327 -56
  58. data/tasks/code_quality.rake +74 -0
  59. data/tasks/console.rake +8 -0
  60. data/tasks/package.task +9 -0
  61. data/tasks/test.rake +30 -0
  62. metadata +33 -145
  63. data/.rubocop.yml +0 -76
  64. data/config/devtools.yml +0 -2
  65. data/config/flay.yml +0 -3
  66. data/config/flog.yml +0 -2
  67. data/config/yardstick.yml +0 -2
  68. data/cucumber.yml +0 -2
  69. data/lib/defmastership/change_ref_line_modifier.rb +0 -85
  70. data/lib/defmastership/line_modifier_base.rb +0 -29
  71. data/lib/defmastership/modifier_base.rb +0 -36
  72. data/lib/defmastership/rename_included_files_line_modifier.rb +0 -126
  73. data/lib/defmastership/update_def_checksum_line_modifier.rb +0 -38
  74. data/lib/defmastership/update_def_version_line_modifier.rb +0 -58
  75. data/spec/unit/def_mastership/change_ref_line_modifier_spec.rb +0 -250
  76. data/spec/unit/def_mastership/rename_included_files_line_modifier_spec.rb +0 -207
  77. data/spec/unit/def_mastership/update_def_checksum_line_modifier_spec.rb +0 -82
  78. data/spec/unit/def_mastership/update_def_version_line_modifier_spec.rb +0 -131
  79. /data/{.rspec → config/rspec} +0 -0
@@ -20,7 +20,6 @@ Feature: The extract command
20
20
  Second line.",~b86dcbde
21
21
  """
22
22
  And the stdout should not contain anything
23
- And the stderr should not contain anything
24
23
 
25
24
  Scenario: Extract to CSV with alternative CSV separator
26
25
  Given a file named "toto.adoc" with:
@@ -60,7 +59,6 @@ Feature: The extract command
60
59
  Third line with {not_defined_variable}.",~3bf370ed
61
60
  """
62
61
  And the stdout should not contain anything
63
- And the stderr should not contain anything
64
62
 
65
63
 
66
64
  Scenario: Extract some definitions to CSV
@@ -93,7 +91,6 @@ Feature: The extract command
93
91
  ignored multiline
94
92
  """
95
93
  And the stdout should not contain anything
96
- And the stderr should not contain anything
97
94
 
98
95
  Scenario: Ignore definitions in literal block
99
96
  Given a file named "toto.adoc" with:
@@ -114,7 +111,6 @@ Feature: The extract command
114
111
  something_else
115
112
  """
116
113
  And the stdout should not contain anything
117
- And the stderr should not contain anything
118
114
 
119
115
  Scenario: Ignore definitions in example block
120
116
  Given a file named "toto.adoc" with:
@@ -135,7 +131,6 @@ Feature: The extract command
135
131
  something_else
136
132
  """
137
133
  And the stdout should not contain anything
138
- And the stderr should not contain anything
139
134
 
140
135
  Scenario: Ignore definitions in commment block
141
136
  Given a file named "toto.adoc" with:
@@ -156,7 +151,6 @@ Feature: The extract command
156
151
  something_else
157
152
  """
158
153
  And the stdout should not contain anything
159
- And the stderr should not contain anything
160
154
 
161
155
  Scenario: Handle mixes comment and literal blocks
162
156
  Given a file named "toto.adoc" with:
@@ -179,7 +173,6 @@ Feature: The extract command
179
173
  something_else
180
174
  """
181
175
  And the stdout should not contain anything
182
- And the stderr should not contain anything
183
176
 
184
177
  Scenario: Extract definition with labels
185
178
  Given a file named "toto.adoc" with:
@@ -199,9 +192,8 @@ Feature: The extract command
199
192
  label2"
200
193
  """
201
194
  And the stdout should not contain anything
202
- And the stderr should not contain anything
203
195
 
204
- Scenario: Extract one definition with external ref to CSV
196
+ Scenario: Extract one definition with external ref to CSV
205
197
  Given a file named "toto.adoc" with:
206
198
  """
207
199
  :eref-implements-prefix: Participate to:
@@ -222,7 +214,49 @@ Feature: The extract command
222
214
  SYSTEM-0014"
223
215
  """
224
216
  And the stdout should not contain anything
225
- And the stderr should not contain anything
217
+
218
+ Scenario: Extract one definition with external ref (with version and checksum) to CSV
219
+ Given a file named "toto.adoc" with:
220
+ """
221
+ :eref-implements-prefix: Participate to:
222
+ :eref-implements-url: ./the_other_document.html
223
+ [define, requirement, TOTO-0001]
224
+ --
225
+ Exemple of multiline requirement.
226
+ Second line.
227
+ --
228
+ defs:eref[implements, [SYSTEM-0012(a), SYSTEM-0014, SYSTEM-0015(b~1223abcd)]]
229
+ """
230
+ When I successfully run `defmastership export toto.adoc`
231
+ Then the file "toto.csv" should contain:
232
+ """
233
+ Type,Reference,Value,Checksum,Participate to:
234
+ requirement,TOTO-0001,"Exemple of multiline requirement.
235
+ Second line.",~b86dcbde,"SYSTEM-0012(a)
236
+ SYSTEM-0014
237
+ SYSTEM-0015(b~1223abcd)"
238
+ """
239
+ And the stdout should not contain anything
240
+
241
+ Scenario: Extract one definition with missing external ref to CSV
242
+ Given a file named "toto.adoc" with:
243
+ """
244
+ :eref-implements-prefix: Participate to:
245
+ :eref-implements-url: ./the_other_document.html
246
+ [define, requirement, TOTO-0001]
247
+ --
248
+ Exemple of multiline requirement.
249
+ Second line.
250
+ --
251
+ """
252
+ When I successfully run `defmastership export toto.adoc`
253
+ Then the file "toto.csv" should contain:
254
+ """
255
+ Type,Reference,Value,Checksum,Participate to:
256
+ requirement,TOTO-0001,"Exemple of multiline requirement.
257
+ Second line.",~b86dcbde,
258
+ """
259
+ And the stdout should not contain anything
226
260
 
227
261
  Scenario: Extract one definition with external ref without url to CSV
228
262
  Given a file named "toto.adoc" with:
@@ -245,7 +279,6 @@ Feature: The extract command
245
279
  SYSTEM-0014"
246
280
  """
247
281
  And the stdout should not contain anything
248
- And the stderr should not contain anything
249
282
 
250
283
  Scenario: Extract one definition with external ref to CSV without URL
251
284
  Given a file named "toto.adoc" with:
@@ -260,7 +293,6 @@ Feature: The extract command
260
293
  """
261
294
  When I successfully run `defmastership export toto.adoc`
262
295
  Then the stdout should not contain anything
263
- And the stderr should not contain anything
264
296
  And the file "toto.csv" should contain:
265
297
  """
266
298
  Type,Reference,Value,Checksum,Participate to:
@@ -289,7 +321,6 @@ Feature: The extract command
289
321
  TOTO-0002"
290
322
  """
291
323
  And the stdout should not contain anything
292
- And the stderr should not contain anything
293
324
 
294
325
  Scenario: Extract one definition with attributes to CSV
295
326
  Given a file named "toto.adoc" with:
@@ -311,7 +342,26 @@ Feature: The extract command
311
342
  requirement,TOTO-0001,One single line.,~554b0ff5,Beautiful Test,Very cool
312
343
  """
313
344
  And the stdout should not contain anything
314
- And the stderr should not contain anything
345
+
346
+ Scenario: Extract one definition with missing attributes to CSV
347
+ Given a file named "toto.adoc" with:
348
+ """
349
+ :attr-verifiedby-prefix: Verified by:
350
+ :attr-criticity-prefix: Criticity:
351
+ [define, requirement, TOTO-0001]
352
+ One single line.
353
+
354
+ something else.
355
+ defs:attribute[verifiedby, Beautiful Test]
356
+ """
357
+ When I successfully run `defmastership export toto.adoc`
358
+ And the stdout should not contain anything
359
+ Then the file "toto.csv" should contain:
360
+ """
361
+ Type,Reference,Value,Checksum,Verified by:,Criticity:
362
+ requirement,TOTO-0001,One single line.,~554b0ff5,Beautiful Test,
363
+ """
364
+ And the stdout should not contain anything
315
365
 
316
366
  Scenario: Extract one definition with example in it
317
367
  Given a file named "toto.adoc" with:
@@ -336,7 +386,6 @@ Feature: The extract command
336
386
  ----",~8f20a443
337
387
  """
338
388
  And the stdout should not contain anything
339
- And the stderr should not contain anything
340
389
 
341
390
  Scenario: Extract one definition with single line comment in it
342
391
  Given a file named "toto.adoc" with:
@@ -353,7 +402,6 @@ Feature: The extract command
353
402
  requirement,TOTO-0001,// comment,~b0b506c3
354
403
  """
355
404
  And the stdout should not contain anything
356
- And the stderr should not contain anything
357
405
 
358
406
  Scenario: Extract one definition with multi line comment or literal in it
359
407
  Given a file named "toto.adoc" with:
@@ -380,4 +428,3 @@ Feature: The extract command
380
428
  ....",~7eb3c490
381
429
  """
382
430
  And the stdout should not contain anything
383
- And the stderr should not contain anything
@@ -20,7 +20,6 @@ Feature: The modify command
20
20
  """
21
21
  When I successfully run `defmastership modify --modifications temp-references thedoc.adoc`
22
22
  Then the stdout should not contain anything
23
- And the stderr should not contain anything
24
23
  And the file "modifications.yml" should contain:
25
24
  """
26
25
  ---
@@ -75,7 +74,6 @@ Feature: The modify command
75
74
  """
76
75
  When I successfully run `defmastership modify --modifications-file=pouet.yml --modifications toto thedoc.adoc`
77
76
  Then the stdout should not contain anything
78
- And the stderr should not contain anything
79
77
  And the file "pouet.yml" should contain:
80
78
  """
81
79
  ---
@@ -108,8 +106,7 @@ Feature: The modify command
108
106
  [define, requirement, TOTO-TEMP-XXX2]
109
107
  """
110
108
  When I successfully run `defmastership modify --modifications temp-references --changes-summary=changes.csv thedoc.adoc`
111
- Then the stderr should not contain anything
112
- And the stdout should not contain anything
109
+ Then the stdout should not contain anything
113
110
  And the file "changes.csv" should contain:
114
111
  """
115
112
  Modifier,Was,Becomes
@@ -141,7 +138,6 @@ Feature: The modify command
141
138
  """
142
139
  When I successfully run `defmastership modify --modifications temp-references,tata_or_titi --changes-summary=changes.csv thedoc.adoc`
143
140
  Then the stdout should not contain anything
144
- And the stderr should not contain anything
145
141
  And the file "modifications.yml" should contain:
146
142
  """
147
143
  ---
@@ -23,7 +23,6 @@ Feature: The rename_included_files command
23
23
  And an empty file named "any_path/one_file.png"
24
24
  When I successfully run `defmastership modify --modifications rename_included_png thedoc.adoc`
25
25
  Then the stdout should not contain anything
26
- And the stderr should not contain anything
27
26
  And the file "thedoc.adoc" should contain:
28
27
  """
29
28
  include::any_path/one_file.png[]
@@ -49,7 +48,6 @@ Feature: The rename_included_files command
49
48
  And an empty file named "any_path/one_file.png"
50
49
  When I successfully run `defmastership modify --modifications rename_included_png thedoc.adoc`
51
50
  Then the stdout should not contain anything
52
- And the stderr should not contain anything
53
51
  And the file "thedoc.adoc" should contain:
54
52
  """
55
53
  [define, requirement, TOTO-WHATEVER-123]
@@ -58,6 +56,33 @@ Feature: The rename_included_files command
58
56
  And the file "any_path/one_file.png" should not exist
59
57
  And the file "any_path/TOTO-WHATEVER-123_one_file.png" should exist
60
58
 
59
+ Scenario: change the filename with options in include macro
60
+ Given a file named "modifications.yml" with:
61
+ """
62
+ ---
63
+ :rename_included_png:
64
+ :type: rename_included_files
65
+ :config:
66
+ :from_regexp: (?<origin>.*\.png)
67
+ :to_template: "%<reference>s_%<origin>s"
68
+ """
69
+ And a file named "thedoc.adoc" with:
70
+ """
71
+ [define, requirement, TOTO-WHATEVER-123]
72
+ include::any_path/one_file.png[leveloffset=offset,lines=ranges]
73
+ """
74
+ And a directory named "any_path"
75
+ And an empty file named "any_path/one_file.png"
76
+ When I successfully run `defmastership modify --modifications rename_included_png thedoc.adoc`
77
+ Then the stdout should not contain anything
78
+ And the file "thedoc.adoc" should contain:
79
+ """
80
+ [define, requirement, TOTO-WHATEVER-123]
81
+ include::any_path/TOTO-WHATEVER-123_one_file.png[leveloffset=offset,lines=ranges]
82
+ """
83
+ And the file "any_path/one_file.png" should not exist
84
+ And the file "any_path/TOTO-WHATEVER-123_one_file.png" should exist
85
+
61
86
  Scenario: change the filename with variable in path
62
87
  Given a file named "modifications.yml" with:
63
88
  """
@@ -81,7 +106,6 @@ Feature: The rename_included_files command
81
106
  And an empty file named "one_two/one_file.png"
82
107
  When I successfully run `defmastership modify --modifications rename_included_png thedoc.adoc`
83
108
  Then the stdout should not contain anything
84
- And the stderr should not contain anything
85
109
  And the file "thedoc.adoc" should contain:
86
110
  """
87
111
  :any: one
@@ -114,7 +138,6 @@ Feature: The rename_included_files command
114
138
  And an empty file named "any_path/TOTO-WHATEVER-123_one_file.png"
115
139
  When I successfully run `defmastership modify --modifications rename_included_png thedoc.adoc`
116
140
  Then the stdout should not contain anything
117
- And the stderr should not contain anything
118
141
  And the file "thedoc.adoc" should contain:
119
142
  """
120
143
  [define, requirement, TOTO-WHATEVER-123]
@@ -0,0 +1,19 @@
1
+ # Copyright (c) 2024 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require 'git'
5
+
6
+ Given('I initialize a git repo') do
7
+ Git.init(Aruba.config.home_directory)
8
+ end
9
+
10
+ Given('I add and commit the {string} file') do |filename|
11
+ git = Git.open(Aruba.config.home_directory)
12
+ git.add(filename)
13
+ git.commit('whatever')
14
+ end
15
+
16
+ Given('I set the {string} tag') do |tag_name|
17
+ git = Git.open(Aruba.config.home_directory)
18
+ git.add_tag(tag_name)
19
+ end
@@ -4,32 +4,37 @@
4
4
  module DefMastership
5
5
  # Change references from temporary to definitive with multiple RefChangers
6
6
  class BatchModifier
7
- attr_reader :config, :adoc_texts, :changes
7
+ attr_reader :config, :adoc_sources, :changes
8
8
 
9
- def initialize(config, adoc_texts)
9
+ def initialize(config, adoc_sources)
10
10
  @config = config
11
- @adoc_texts = adoc_texts
11
+ @adoc_sources = adoc_sources
12
12
  @changes = []
13
13
  end
14
14
 
15
15
  def apply(modifs)
16
- modifs.split(/\s*,\s*/).each do |modif|
16
+ modifs.each do |modif|
17
17
  modifier = modifier_from(modif)
18
- @adoc_texts = modifier.do_modifications(@adoc_texts)
19
- @config[modif.to_sym][:config] = modifier.config
20
- modifier.changes.each do |change|
21
- @changes << ([modif] + change)
22
- end
18
+ @adoc_sources = modifier.do_modifications(adoc_sources)
19
+ config.fetch(modif)[:config] = modifier.config
20
+
21
+ collect_changes(modifier, modif)
23
22
  end
24
23
  end
25
24
 
26
25
  private
27
26
 
28
27
  def modifier_from(modif)
29
- raise(ArgumentError, "#{modif} is not a known modification") if @config[modif.to_sym].nil?
28
+ config_modif_sym = config[modif]
29
+ raise(ArgumentError, "#{modif} is not a known modification") unless config_modif_sym
30
+
31
+ ModifierFactory.from_config(config_modif_sym)
32
+ end
30
33
 
31
- class_name = "#{@config[modif.to_sym][:type].split('_').map(&:capitalize).join}Modifier"
32
- DefMastership.const_get(class_name).new(@config[modif.to_sym][:config])
34
+ def collect_changes(modifier, modif)
35
+ modifier.changes.reduce(changes) do |acc, change|
36
+ acc << ([modif.to_s] + change)
37
+ end
33
38
  end
34
39
  end
35
40
  end
@@ -1,17 +1,99 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'defmastership/modifier_base'
4
+ require 'defmastership/constants'
5
+ require 'defmastership/modifier'
5
6
 
6
7
  module DefMastership
7
- # Change all refs of a given project
8
- class ChangeRefModifier < ModifierBase
9
- def replacements
8
+ # Change references from temporary to definitive with multiple RefChangers
9
+ class ChangeRefModifier
10
+ include Modifier
11
+
12
+ REGEXP_FROM = {
13
+ definition: {
14
+ before: DMRegexp::DEF_BEFORE_REF,
15
+ after: DMRegexp::DEF_AFTER_REF
16
+ },
17
+ iref: {
18
+ before: DMRegexp::IREF_DEF_BEF,
19
+ after: DMRegexp::IREF_DEF_AFT
20
+ }
21
+ }.freeze
22
+
23
+ private_constant :REGEXP_FROM
24
+
25
+ def self.replacement_methods
10
26
  %i[replace_refdef replace_irefs]
11
27
  end
12
28
 
13
- def new_line_modifier(config, _adoc_texts)
14
- ChangeRefLineModifier.from_config(config)
29
+ def self.default_config
30
+ {
31
+ from_regexp: '',
32
+ to_template: '',
33
+ next_ref: 0
34
+ }
35
+ end
36
+
37
+ def initialize(config)
38
+ @parsing_state = ParsingState.new
39
+
40
+ setup_modifier_module(config)
41
+ end
42
+
43
+ def replace(method, line)
44
+ public_send(:"replace_#{method}", line)
45
+ end
46
+
47
+ def replace_refdef(line)
48
+ if @parsing_state.enabled?(line)
49
+ do_replace_refdef(line)
50
+ else
51
+ line
52
+ end
53
+ end
54
+
55
+ def replace_irefs(line)
56
+ changes.reduce(line) do |res_line, (from, to)|
57
+ res_line.gsub(Helper.regexp_from(:iref, from)) do
58
+ Helper.text_with(Regexp.last_match, to)
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def do_replace_refdef(line)
66
+ line.sub(Helper.regexp_from(:definition, from_regexp)) do
67
+ replacement_from(Regexp.last_match)
68
+ end
69
+ end
70
+
71
+ def replacement_from(match)
72
+ replacement = to_template % hmerge(match)
73
+ changes << [match[:from], replacement]
74
+ config[:next_ref] += 1
75
+ Helper.text_with(match, replacement)
76
+ end
77
+
78
+ def hmerge(match)
79
+ config.merge(match.named_captures.transform_keys(&:to_sym))
80
+ end
81
+
82
+ # Helper functions
83
+ module Helper
84
+ def self.regexp_from(const, from)
85
+ regexps = REGEXP_FROM.fetch(const)
86
+ regexp_str =
87
+ "(?<before>#{regexps[:before]})" \
88
+ "(?<from>#{from})" \
89
+ "#{DMRegexp::DEF_VERSION_AND_CHECKSUM}" \
90
+ "(?<after>#{regexps[:after]})"
91
+ Regexp.new(regexp_str, Regexp::EXTENDED)
92
+ end
93
+
94
+ def self.text_with(match, replacement)
95
+ match[:before] + replacement + (match[:version_and_checksum] || '') + match[:after]
96
+ end
15
97
  end
16
98
  end
17
99
  end
@@ -5,6 +5,6 @@
5
5
  # not.
6
6
  class String
7
7
  def commented?
8
- !match(DefMastership::DMRegexp::SINGLE_LINE_COMMENT).nil?
8
+ !!match(DefMastership::DMRegexp::SINGLE_LINE_COMMENT)
9
9
  end
10
10
  end
@@ -3,6 +3,7 @@
3
3
 
4
4
  module DefMastership
5
5
  # set of regexp of added asciidoctor constructions
6
+ # This module smells of :reek:TooManyConstants
6
7
  module DMRegexp
7
8
  SINGLE_LINE_COMMENT = %r{^//[^/]}.freeze
8
9
 
@@ -22,22 +23,24 @@ module DefMastership
22
23
  '\((?<explicit_version>[^~]+)?(?<explicit_checksum>~[[:alnum:]]+)?\)' \
23
24
  ')?'
24
25
 
26
+ REFERENCE = '(?<reference>[\\w:_-]+)'
27
+
25
28
  DEFINITION = Regexp.new(
26
- "#{DEF_BEFORE_REF}(?<reference>[\\w:_-]+)#{DEF_VERSION_AND_CHECKSUM}#{DEF_AFTER_REF}",
29
+ "#{DEF_BEFORE_REF}#{REFERENCE}#{DEF_VERSION_AND_CHECKSUM}#{DEF_AFTER_REF}",
27
30
  Regexp::EXTENDED
28
31
  )
29
32
 
30
- VARIABLE_DEF = /^\s*:(?<varname>[\w:_-]+):\s*
31
- \s*(?<value>\S.*\S)\s*$/x.freeze
33
+ VARIABLE_DEF = /^\s*:(?<varname>[\w:_-]+):\s+
34
+ (?<value>\S.*\S)\s*$/x.freeze
32
35
 
33
36
  VARIABLE_USE = /{(?<varname>[\w:_-]+)}/x.freeze
34
37
 
35
- EREF_CONFIG = /^\s*:eref-(?<refname>[\w:_-]+)-(?<symb>prefix|url):\s*
38
+ EREF_CONFIG = /^\s*:eref-(?<reference>[\w:_-]+)-(?<symb>prefix|url):\s*
36
39
  \s*(?<value>\S.*\S)\s*/x.freeze
37
40
  EREF_DEF = /^\s*
38
41
  defs:eref\[
39
- \s*(?<refname>[\w:_-]+)\s*,
40
- \s*\[(?<extrefs>[^\]]+)\]\s*\]/x.freeze
42
+ \s*(?<reference>[\w:_-]+)\s*,
43
+ \s*\[\s*(?<extrefs>[^\]]+?)\s*\]\s*\]/x.freeze
41
44
  BLOCK = /^--\s*$/.freeze
42
45
 
43
46
  IREF_DEF_BEF = 'defs:iref\[\s*'
@@ -61,7 +64,7 @@ module DefMastership
61
64
  INCLUDE_KEYWORD = '\binclude::'
62
65
  INCLUDE_PATH = '(?<path>.*/)?'
63
66
  INCLUDE_FILENAME = '(?<filename>[^\\/]+)'
64
- INCLUDE_OPTIONS = '\[(?<options>[\]]*)\]'
67
+ INCLUDE_OPTIONS = '\[(?<options>[^\]]*)\]'
65
68
 
66
69
  INCLUDE = Regexp.new(
67
70
  INCLUDE_KEYWORD + INCLUDE_PATH + INCLUDE_FILENAME + INCLUDE_OPTIONS,
@@ -8,7 +8,7 @@ require('defmastership/csv_formatter_header')
8
8
  module DefMastership
9
9
  # to export a CSV file
10
10
  class CSVFormatter
11
- def initialize(doc, sep = ',')
11
+ def initialize(doc, sep)
12
12
  @doc = doc
13
13
  @sep = sep
14
14
  end
@@ -17,12 +17,16 @@ module DefMastership
17
17
  column_list = build_column_list
18
18
  CSV.open(output_file, 'w:ISO-8859-1', col_sep: @sep) do |csv|
19
19
  csv << header(column_list)
20
- @doc.definitions.each { |definition| csv << body(definition, column_list) }
20
+ export_definitions_in(csv, column_list)
21
21
  end
22
22
  end
23
23
 
24
24
  private
25
25
 
26
+ def export_definitions_in(csv, column_list)
27
+ @doc.definitions.each { |definition| csv << body(definition, column_list) }
28
+ end
29
+
26
30
  def header(column_list)
27
31
  header_formatter = CSVFormatterHeader.new(@doc)
28
32
  header_line = column_list.map { |part| header_formatter.public_send(part) }
@@ -30,20 +34,20 @@ module DefMastership
30
34
  end
31
35
 
32
36
  def body(definition, column_list)
33
- body_formatter = CSVFormatterBody.new(@doc)
34
- body_line = column_list.map { |part| body_formatter.public_send(part, definition) }
37
+ body_formatter = CSVFormatterBody.new(@doc, definition)
38
+ body_line = column_list.map { |part| body_formatter.public_send(part) }
35
39
  body_line.flatten
36
40
  end
37
41
 
38
42
  def build_column_list
39
- column_list = [:fixed]
40
- column_list += [:wrong_explicit_checksum] if @doc.wrong_explicit_checksum?
41
- column_list += [:explicit_version] if @doc.explicit_version?
42
- column_list += [:labels] unless @doc.labels.empty?
43
- column_list += [:eref] unless @doc.eref.empty?
44
- column_list += [:iref] if @doc.iref
45
- column_list += [:attributes] unless @doc.attributes.empty?
46
- column_list
43
+ [
44
+ [:wrong_explicit_checksum, @doc.wrong_explicit_checksum?],
45
+ [:explicit_version, @doc.explicit_version?],
46
+ [:labels, !@doc.labels.empty?],
47
+ [:eref, !@doc.eref.empty?],
48
+ [:iref, @doc.iref],
49
+ [:attributes, !@doc.attributes.empty?]
50
+ ].reduce([:fixed]) { |acc, elem| acc + (elem.fetch(1) ? [elem.first] : []) }
47
51
  end
48
52
  end
49
53
  end
@@ -3,41 +3,44 @@
3
3
 
4
4
  require('csv')
5
5
 
6
+ require('defmastership/set_join_hack')
7
+
6
8
  module DefMastership
7
9
  # format lines per definition
8
10
  class CSVFormatterBody
9
- def initialize(doc)
11
+ def initialize(doc, definition)
10
12
  @doc = doc
13
+ @definition = definition
11
14
  end
12
15
 
13
- def fixed(definition)
14
- [definition.type, definition.reference, definition.value, definition.sha256]
16
+ def fixed
17
+ [@definition.type, @definition.reference, @definition.value, @definition.sha256_short]
15
18
  end
16
19
 
17
- def wrong_explicit_checksum(definition)
18
- wrong_explicit_checksum = definition.wrong_explicit_checksum
20
+ def wrong_explicit_checksum
21
+ wrong_explicit_checksum = @definition.wrong_explicit_checksum
19
22
  wrong_explicit_checksum ? [wrong_explicit_checksum] : ['']
20
23
  end
21
24
 
22
- def explicit_version(definition)
23
- explicit_version = definition.explicit_version
25
+ def explicit_version
26
+ explicit_version = @definition.explicit_version
24
27
  explicit_version ? [explicit_version] : ['']
25
28
  end
26
29
 
27
- def labels(definition)
28
- [definition.labels.to_a.join("\n")]
30
+ def labels
31
+ [@definition.labels.join("\n")]
29
32
  end
30
33
 
31
- def eref(definition)
32
- @doc.eref.map { |key, _| definition.eref[key].join("\n") }
34
+ def eref
35
+ @doc.eref.map { |key,| @definition.eref.fetch(key, []).join("\n") }
33
36
  end
34
37
 
35
- def iref(definition)
36
- [definition.iref.join("\n")]
38
+ def iref
39
+ [@definition.iref.join("\n")]
37
40
  end
38
41
 
39
- def attributes(definition)
40
- @doc.attributes.map { |key, _| definition.attributes[key] }
42
+ def attributes
43
+ @doc.attributes.map { |key,| @definition.attributes.fetch(key, '') }
41
44
  end
42
45
  end
43
46
  end
@@ -27,7 +27,7 @@ module DefMastership
27
27
  end
28
28
 
29
29
  def eref
30
- @doc.eref.map { |_, ref| ref[:prefix] }
30
+ @doc.eref.map { |_, ref| ref.fetch(:prefix) }
31
31
  end
32
32
 
33
33
  def iref