pragmater 7.2.0 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "pathname"
5
+ require "runcom"
6
+
7
+ module Pragmater
8
+ module CLI
9
+ module Options
10
+ # Merges arguments with configuration for fully assembled configuration for use by shell.
11
+ class Merger
12
+ DEFAULTS = YAML.load_file(Pathname(__dir__).join("defaults.yml")).freeze
13
+ CONFIGURATION = Runcom::Config.new "#{Identity::NAME}/configuration.yml", defaults: DEFAULTS
14
+
15
+ def initialize configuration = CONFIGURATION, assembler = Assembler.new
16
+ @configuration = configuration
17
+ @assembler = assembler
18
+ end
19
+
20
+ def call arguments = []
21
+ assembler.call(arguments).then do |options|
22
+ case options
23
+ in insert: path, **settings then build_insert_options path, settings
24
+ in remove: path, **settings then build_remove_options path, settings
25
+ else options
26
+ end
27
+ end
28
+ end
29
+
30
+ def configuration_path
31
+ configuration.current
32
+ end
33
+
34
+ def usage
35
+ assembler.to_s
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :configuration, :assembler
41
+
42
+ def build_insert_options path, options
43
+ {insert: path, **configuration.to_h.fetch(:insert).merge(options)}
44
+ end
45
+
46
+ def build_remove_options path, options
47
+ {remove: path, **configuration.to_h.fetch(:remove).merge(options)}
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module CLI
5
+ # The command line interface for this gem.
6
+ class Shell
7
+ def initialize merger: Options::Merger.new, runner: Runner, helper: Helper.new
8
+ @merger = merger
9
+ @runner = runner
10
+ @helper = helper
11
+ end
12
+
13
+ def call arguments = []
14
+ case merger.call arguments
15
+ in insert: path, **options then insert_pragmas options, path
16
+ in remove: path, **options then remove_pragmas options, path
17
+ in config:, edit:, **remainder then edit_configuration
18
+ in config:, info:, **remainder then print_configuration
19
+ in version:, **remainder then print_version
20
+ in help:, **remainder then print_usage
21
+ else print_usage
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :merger, :runner, :helper
28
+
29
+ def insert_pragmas options, path
30
+ runner.for(**options.merge(action: :insert, root_dir: path))
31
+ .call
32
+ .map { |file| helper.info "Processed: #{file}." }
33
+ end
34
+
35
+ def remove_pragmas options, path
36
+ runner.for(**options.merge(action: :remove, root_dir: path))
37
+ .call
38
+ .map { |file| helper.info "Processed: #{file}." }
39
+ end
40
+
41
+ def edit_configuration
42
+ helper.run "#{ENV["EDITOR"]} #{merger.configuration_path}"
43
+ end
44
+
45
+ def print_configuration
46
+ merger.configuration_path.then do |path|
47
+ return helper.info "No configuration found." unless path
48
+
49
+ helper.info "#{path}\n"
50
+ helper.info path.read
51
+ end
52
+ end
53
+
54
+ def print_version
55
+ helper.info Identity::VERSION_LABEL
56
+ end
57
+
58
+ def print_usage
59
+ helper.info merger.usage
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ # Provides context for runner.
5
+ Context = Struct.new :action, :root_dir, :comments, :includes, keyword_init: true do
6
+ def initialize *arguments
7
+ super
8
+
9
+ self[:root_dir] ||= "."
10
+ self[:comments] = Array comments
11
+ self[:includes] = Array includes
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Formatters
5
+ # Formats general pragmas in a consistent manner.
6
+ class General
7
+ PATTERN = /
8
+ \A # Start of line.
9
+ \# # Start of comment.
10
+ \s? # Space - optional.
11
+ \w+ # Key - One or more word characters only.
12
+ : # Delimiter.
13
+ \s? # Space - optional.
14
+ [\w-]+ # Value - One or more word or dash characters.
15
+ \Z # End of line.
16
+ /x
17
+
18
+ def initialize string, pattern: PATTERN
19
+ @string = string
20
+ @pattern = pattern
21
+ end
22
+
23
+ def call
24
+ return string unless string.match? pattern
25
+
26
+ string.split(":").then { |key, value| "# #{key.gsub(/\#\s?/, "")}: #{value.strip}" }
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :string, :pattern
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Formatters
5
+ # Formats all pragmas in a consistent manner.
6
+ class Main
7
+ FORMATTERS = [General, Shebang].freeze
8
+ PATTERN = FORMATTERS.map { |formatter| formatter::PATTERN }
9
+ .then { |patterns| Regexp.union(*patterns) }
10
+ .freeze
11
+
12
+ def initialize string, formatters: FORMATTERS
13
+ @string = string
14
+ @formatters = formatters
15
+ end
16
+
17
+ def call
18
+ formatters.reduce(string) { |pragma, formatter| formatter.new(pragma).call }
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :string, :formatters
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Formatters
5
+ # Formats shebang pragmas in a consistent manner.
6
+ class Shebang
7
+ PATTERN = %r(\A\#!\s?/.*ruby\Z)
8
+
9
+ def initialize string, pattern: PATTERN
10
+ @string = string
11
+ @pattern = pattern
12
+ end
13
+
14
+ def call
15
+ return string unless string.match? pattern
16
+
17
+ string.split("!").then { |octothorpe, path| "#{octothorpe}! #{path.strip}" }
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :string, :pattern
23
+ end
24
+ end
25
+ end
@@ -5,7 +5,8 @@ module Pragmater
5
5
  module Identity
6
6
  NAME = "pragmater"
7
7
  LABEL = "Pragmater"
8
- VERSION = "7.2.0"
8
+ VERSION = "9.0.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}"
10
+ SUMMARY = "A command line interface for managing/formatting source file pragma comments."
10
11
  end
11
12
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Parsers
5
+ # Manages pragma comments.
6
+ class Comments
7
+ def initialize older, newer, formatter: Formatters::Main
8
+ @formatter = formatter
9
+ @older = format older
10
+ @newer = format newer
11
+ end
12
+
13
+ def insert
14
+ older.union newer
15
+ end
16
+
17
+ def remove
18
+ older - older.intersection(newer)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :formatter, :older, :newer
24
+
25
+ def format pragmas
26
+ Array(pragmas).map { |pragma| formatter.new(pragma).call }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Parsers
5
+ # Parses a file into pragma comment and body lines.
6
+ class File
7
+ def initialize pattern: Formatters::Main::PATTERN,
8
+ comments: Comments,
9
+ processor: Processors::Handler.new
10
+ @pattern = pattern
11
+ @comments = comments
12
+ @processor = processor
13
+ end
14
+
15
+ def call path, new_comments, action:
16
+ path.each_line
17
+ .partition { |line| line.match? pattern }
18
+ .then do |old_comments, body|
19
+ processor.call action, wrap_in_new_line(old_comments, new_comments, action), body
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :pattern, :comments, :processor
26
+
27
+ def wrap_in_new_line old_comments, new_comments, action
28
+ comments.new(old_comments, new_comments).public_send(action).map { |line| "#{line}\n" }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Processors
5
+ # Handles the insertion or removal of pragma comments.
6
+ class Handler
7
+ DEFAULTS = {
8
+ insert: Inserter,
9
+ remove: Remover
10
+ }.freeze
11
+
12
+ def initialize processors: DEFAULTS
13
+ @processors = processors
14
+ end
15
+
16
+ def call action, comments, body
17
+ processors.fetch(action).new(comments, body).call
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :processors
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Processors
5
+ # Inserts new pragma comments.
6
+ class Inserter
7
+ def initialize comments, body
8
+ @comments = comments
9
+ @body = body
10
+ end
11
+
12
+ def call
13
+ body.first.then do |first|
14
+ comments.append "\n" unless first == "\n" || body.empty?
15
+ comments + body
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :comments, :body
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Processors
5
+ # Removes existing pragma comments.
6
+ class Remover
7
+ def initialize comments, body
8
+ @comments = comments
9
+ @body = body
10
+ end
11
+
12
+ def call
13
+ body.first.then do |first_line|
14
+ body.delete_at 0 if first_line == "\n" && comments.empty?
15
+ comments + body
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :comments, :body
22
+ end
23
+ end
24
+ end
@@ -1,32 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "refinements/pathnames"
4
+
3
5
  module Pragmater
4
6
  # Adds/removes pragma comments for files in given path.
5
7
  class Runner
6
- # rubocop:disable Metrics/ParameterLists
7
- def initialize path = ".", comments: [], includes: [], writer: Writer
8
- @path = Pathname path
9
- @comments = Array comments
10
- @includes = Array includes
11
- @writer = writer
12
- end
13
- # rubocop:enable Metrics/ParameterLists
8
+ using Refinements::Pathnames
14
9
 
15
- def files
16
- return [] unless path.exist? && path.directory? && !includes.empty?
10
+ def self.for **attributes
11
+ new Context[attributes]
12
+ end
17
13
 
18
- Pathname.glob(%(#{path}/{#{includes.join ","}})).select(&:file?)
14
+ def initialize context, parser: Parsers::File.new
15
+ @context = context
16
+ @parser = parser
19
17
  end
20
18
 
21
- def run action:
22
- files.each do |file|
23
- writer.new(file, comments).public_send action
24
- yield file if block_given?
19
+ def call
20
+ Pathname(context.root_dir).files("{#{context.includes.join ","}}").map do |path|
21
+ path.write parser.call(path, context.comments, action: context.action).join
25
22
  end
26
23
  end
27
24
 
28
25
  private
29
26
 
30
- attr_reader :path, :comments, :includes, :writer
27
+ attr_reader :context, :parser
31
28
  end
32
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragmater
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.2.0
4
+ version: 9.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,246 +28,36 @@ cert_chain:
28
28
  2XV8FRa7/JimI07sPLC13eLY3xd/aYTi85Z782KIA4j0G8XEEWAX0ouBhlXPocZv
29
29
  QWc=
30
30
  -----END CERTIFICATE-----
31
- date: 2020-07-23 00:00:00.000000000 Z
31
+ date: 2020-12-29 00:00:00.000000000 Z
32
32
  dependencies:
33
33
  - !ruby/object:Gem::Dependency
34
- name: runcom
34
+ name: refinements
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '6.0'
39
+ version: '8.0'
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '6.0'
46
+ version: '8.0'
47
47
  - !ruby/object:Gem::Dependency
48
- name: thor
48
+ name: runcom
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.20'
53
+ version: '7.0'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0.20'
61
- - !ruby/object:Gem::Dependency
62
- name: bundler-audit
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '0.6'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '0.6'
75
- - !ruby/object:Gem::Dependency
76
- name: climate_control
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '0.2'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '0.2'
89
- - !ruby/object:Gem::Dependency
90
- name: gemsmith
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '14.2'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '14.2'
103
- - !ruby/object:Gem::Dependency
104
- name: git-lint
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '1.0'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '1.0'
117
- - !ruby/object:Gem::Dependency
118
- name: guard-rspec
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '4.7'
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '4.7'
131
- - !ruby/object:Gem::Dependency
132
- name: pry
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '0.13'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '0.13'
145
- - !ruby/object:Gem::Dependency
146
- name: pry-byebug
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '3.9'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - "~>"
157
- - !ruby/object:Gem::Version
158
- version: '3.9'
159
- - !ruby/object:Gem::Dependency
160
- name: rake
161
- requirement: !ruby/object:Gem::Requirement
162
- requirements:
163
- - - "~>"
164
- - !ruby/object:Gem::Version
165
- version: '13.0'
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- requirements:
170
- - - "~>"
171
- - !ruby/object:Gem::Version
172
- version: '13.0'
173
- - !ruby/object:Gem::Dependency
174
- name: reek
175
- requirement: !ruby/object:Gem::Requirement
176
- requirements:
177
- - - "~>"
178
- - !ruby/object:Gem::Version
179
- version: '6.0'
180
- type: :development
181
- prerelease: false
182
- version_requirements: !ruby/object:Gem::Requirement
183
- requirements:
184
- - - "~>"
185
- - !ruby/object:Gem::Version
186
- version: '6.0'
187
- - !ruby/object:Gem::Dependency
188
- name: rspec
189
- requirement: !ruby/object:Gem::Requirement
190
- requirements:
191
- - - "~>"
192
- - !ruby/object:Gem::Version
193
- version: '3.9'
194
- type: :development
195
- prerelease: false
196
- version_requirements: !ruby/object:Gem::Requirement
197
- requirements:
198
- - - "~>"
199
- - !ruby/object:Gem::Version
200
- version: '3.9'
201
- - !ruby/object:Gem::Dependency
202
- name: rubocop
203
- requirement: !ruby/object:Gem::Requirement
204
- requirements:
205
- - - "~>"
206
- - !ruby/object:Gem::Version
207
- version: '0.83'
208
- type: :development
209
- prerelease: false
210
- version_requirements: !ruby/object:Gem::Requirement
211
- requirements:
212
- - - "~>"
213
- - !ruby/object:Gem::Version
214
- version: '0.83'
215
- - !ruby/object:Gem::Dependency
216
- name: rubocop-performance
217
- requirement: !ruby/object:Gem::Requirement
218
- requirements:
219
- - - "~>"
220
- - !ruby/object:Gem::Version
221
- version: '1.5'
222
- type: :development
223
- prerelease: false
224
- version_requirements: !ruby/object:Gem::Requirement
225
- requirements:
226
- - - "~>"
227
- - !ruby/object:Gem::Version
228
- version: '1.5'
229
- - !ruby/object:Gem::Dependency
230
- name: rubocop-rake
231
- requirement: !ruby/object:Gem::Requirement
232
- requirements:
233
- - - "~>"
234
- - !ruby/object:Gem::Version
235
- version: '0.5'
236
- type: :development
237
- prerelease: false
238
- version_requirements: !ruby/object:Gem::Requirement
239
- requirements:
240
- - - "~>"
241
- - !ruby/object:Gem::Version
242
- version: '0.5'
243
- - !ruby/object:Gem::Dependency
244
- name: rubocop-rspec
245
- requirement: !ruby/object:Gem::Requirement
246
- requirements:
247
- - - "~>"
248
- - !ruby/object:Gem::Version
249
- version: '1.39'
250
- type: :development
251
- prerelease: false
252
- version_requirements: !ruby/object:Gem::Requirement
253
- requirements:
254
- - - "~>"
255
- - !ruby/object:Gem::Version
256
- version: '1.39'
257
- - !ruby/object:Gem::Dependency
258
- name: simplecov
259
- requirement: !ruby/object:Gem::Requirement
260
- requirements:
261
- - - "~>"
262
- - !ruby/object:Gem::Version
263
- version: '0.18'
264
- type: :development
265
- prerelease: false
266
- version_requirements: !ruby/object:Gem::Requirement
267
- requirements:
268
- - - "~>"
269
- - !ruby/object:Gem::Version
270
- version: '0.18'
60
+ version: '7.0'
271
61
  description:
272
62
  email:
273
63
  - brooke@alchemists.io
@@ -282,12 +72,25 @@ files:
282
72
  - README.adoc
283
73
  - bin/pragmater
284
74
  - lib/pragmater.rb
285
- - lib/pragmater/cli.rb
286
- - lib/pragmater/commenter.rb
287
- - lib/pragmater/formatter.rb
75
+ - lib/pragmater/cli/helper.rb
76
+ - lib/pragmater/cli/options/assembler.rb
77
+ - lib/pragmater/cli/options/configuration.rb
78
+ - lib/pragmater/cli/options/core.rb
79
+ - lib/pragmater/cli/options/defaults.yml
80
+ - lib/pragmater/cli/options/insert_remove.rb
81
+ - lib/pragmater/cli/options/merger.rb
82
+ - lib/pragmater/cli/shell.rb
83
+ - lib/pragmater/context.rb
84
+ - lib/pragmater/formatters/general.rb
85
+ - lib/pragmater/formatters/main.rb
86
+ - lib/pragmater/formatters/shebang.rb
288
87
  - lib/pragmater/identity.rb
88
+ - lib/pragmater/parsers/comments.rb
89
+ - lib/pragmater/parsers/file.rb
90
+ - lib/pragmater/processors/handler.rb
91
+ - lib/pragmater/processors/inserter.rb
92
+ - lib/pragmater/processors/remover.rb
289
93
  - lib/pragmater/runner.rb
290
- - lib/pragmater/writer.rb
291
94
  homepage: https://www.alchemists.io/projects/pragmater
292
95
  licenses:
293
96
  - Apache-2.0
@@ -304,14 +107,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
304
107
  requirements:
305
108
  - - "~>"
306
109
  - !ruby/object:Gem::Version
307
- version: '2.7'
110
+ version: '3.0'
308
111
  required_rubygems_version: !ruby/object:Gem::Requirement
309
112
  requirements:
310
113
  - - ">="
311
114
  - !ruby/object:Gem::Version
312
115
  version: '0'
313
116
  requirements: []
314
- rubygems_version: 3.1.4
117
+ rubygems_version: 3.2.3
315
118
  signing_key:
316
119
  specification_version: 4
317
120
  summary: A command line interface for managing/formatting source file pragma comments.