pragmater 7.2.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
metadata.gz.sig CHANGED
@@ -1,3 +1,2 @@
1
- \K���u��ȥfg�Z5V����b�E67 s��c��Sf��3�#*F�4�\���i�j�]�@�Ǵ
2
- ���oD��� �07�q����_H}f$���T�?&{���߈�fE:������5[���
3
- �c��a�h�9o�M���U��"�,��#
1
+ ��U�,2�.��u6&0��}�ǒZM*q"4�����Y�֧����U����8�<Ě璳�c����1��Zwב��Df#p����4{I�yP���i�S)����� �I��V����A�gG^P�W����(�qy�v�{����3YSÄp������{��J�x5��}T2"�ʩX�7��p}?�]K"�fW��c�qP���w������Yl> ��u@��;14�%��v8
2
+ x\{�`K�ۭ��k ȸlI
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "pathname"
4
- require "thor"
5
- require "thor/actions"
6
- require "runcom"
7
-
8
- module Pragmater
9
- # The Command Line Interface (CLI) for the gem.
10
- class CLI < Thor
11
- include Thor::Actions
12
-
13
- package_name Identity::VERSION_LABEL
14
-
15
- # rubocop:disable Metrics/MethodLength
16
- def self.configuration
17
- Runcom::Config.new "#{Identity::NAME}/configuration.yml",
18
- defaults: {
19
- add: {
20
- comments: "",
21
- includes: []
22
- },
23
- remove: {
24
- comments: "",
25
- includes: []
26
- }
27
- }
28
- end
29
- # rubocop:enable Metrics/MethodLength
30
-
31
- def initialize args = [], options = {}, config = {}
32
- super args, options, config
33
- @configuration = self.class.configuration
34
- rescue Runcom::Errors::Base => error
35
- abort error.message
36
- end
37
-
38
- desc "-a, [--add=PATH]", "Add comments to source file(s)."
39
- map %w[-a --add] => :add
40
- method_option :comments,
41
- aliases: "-c",
42
- desc: "Define desired comments",
43
- type: :array,
44
- default: configuration.to_h.dig(:add, :comments)
45
- method_option :includes,
46
- aliases: "-i",
47
- desc: "Include specific files and/or directories",
48
- type: :array,
49
- default: configuration.to_h.dig(:add, :includes)
50
- def add path = "."
51
- settings = configuration.merge(
52
- add: {comments: options.comments, includes: options.includes}
53
- ).to_h
54
-
55
- runner = Runner.new path,
56
- comments: settings.dig(:add, :comments),
57
- includes: settings.dig(:add, :includes)
58
-
59
- runner.run(action: :add) { |file| say_status :info, "Processed: #{file}.", :green }
60
- end
61
-
62
- desc "-r, [--remove=PATH]", "Remove comments from source file(s)."
63
- map %w[-r --remove] => :remove
64
- method_option :comments,
65
- aliases: "-c",
66
- desc: "Define desired comments",
67
- type: :array,
68
- default: configuration.to_h.dig(:remove, :comments)
69
- method_option :includes,
70
- aliases: "-i",
71
- desc: "Include specific files and/or directories",
72
- type: :array,
73
- default: configuration.to_h.dig(:remove, :includes)
74
- def remove path = "."
75
- settings = configuration.merge(
76
- remove: {comments: options.comments, includes: options.includes}
77
- ).to_h
78
-
79
- runner = Runner.new path,
80
- comments: settings.dig(:remove, :comments),
81
- includes: settings.dig(:remove, :includes)
82
-
83
- runner.run(action: :remove) { |file| say_status :info, "Processed: #{file}.", :green }
84
- end
85
-
86
- desc "-c, [--config]", "Manage gem configuration."
87
- map %w[-c --config] => :config
88
- method_option :edit,
89
- aliases: "-e",
90
- desc: "Edit gem configuration.",
91
- type: :boolean,
92
- default: false
93
- method_option :info,
94
- aliases: "-i",
95
- desc: "Print gem configuration.",
96
- type: :boolean,
97
- default: false
98
- def config
99
- path = configuration.current
100
-
101
- if options.edit? then `#{ENV["EDITOR"]} #{path}`
102
- elsif options.info?
103
- path ? say(path) : say("Configuration doesn't exist.")
104
- else help :config
105
- end
106
- end
107
-
108
- desc "-v, [--version]", "Show gem version."
109
- map %w[-v --version] => :version
110
- def version
111
- say Identity::VERSION_LABEL
112
- end
113
-
114
- desc "-h, [--help=COMMAND]", "Show this message or get help for a command."
115
- map %w[-h --help] => :help
116
- def help task = nil
117
- say and super
118
- end
119
-
120
- private
121
-
122
- attr_reader :configuration
123
- end
124
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Handles pragma comments.
5
- class Commenter
6
- def initialize older, newer, formatter: Formatter
7
- @formatter = formatter
8
- @older = format older
9
- @newer = format newer
10
- end
11
-
12
- def add
13
- older | newer
14
- end
15
-
16
- def remove
17
- older - (older & newer)
18
- end
19
-
20
- private
21
-
22
- attr_reader :older, :newer, :formatter
23
-
24
- def filter comments
25
- Array(comments).select { |comment| comment =~ formatter.valid_formats }
26
- end
27
-
28
- def format comments
29
- filter(comments).map { |comment| formatter.new(comment).format }
30
- end
31
- end
32
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Formats pragma comments in a consistent manner.
5
- class Formatter
6
- def self.shebang_format
7
- %r(\A\#!\s?/.*ruby\Z)
8
- end
9
-
10
- def self.pragma_format
11
- /
12
- \A # Start of line.
13
- \# # Start of comment.
14
- \s? # Space - optional.
15
- \w+ # Key - 1 or more word characters only.
16
- : # Key and value delimiter.
17
- \s? # Space - optional.
18
- [\w-]+ # Value - 1 or more word or dash characters.
19
- \Z # End of line.
20
- /x
21
- end
22
-
23
- def self.valid_formats
24
- Regexp.union shebang_format, pragma_format
25
- end
26
-
27
- def initialize string
28
- @string = string
29
- end
30
-
31
- def format_shebang
32
- return string unless string.match? self.class.shebang_format
33
-
34
- _, path = string.split "!"
35
- "#! #{path.strip}"
36
- end
37
-
38
- def format_pragma
39
- return string unless string.match? self.class.pragma_format
40
-
41
- key, value = string.split ":"
42
- "# #{key.gsub(/\#\s?/, "")}: #{value.strip}"
43
- end
44
-
45
- def format
46
- klass = self.class
47
-
48
- case string
49
- when klass.shebang_format then format_shebang
50
- when klass.pragma_format then format_pragma
51
- else string
52
- end
53
- end
54
-
55
- private
56
-
57
- attr_reader :string
58
- end
59
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Writes formatted pragma comments to source file.
5
- # :reek:TooManyInstanceVariables
6
- # :reek:MissingSafeMethod
7
- class Writer
8
- # rubocop:disable Metrics/ParameterLists
9
- def initialize file_path, new_comments, formatter: Formatter, commenter: Commenter
10
- @file_path = file_path
11
- @file_lines = File.readlines file_path
12
- @formatter = formatter
13
- @commenter = commenter
14
- @old_comments = file_comments
15
- @new_comments = new_comments
16
- end
17
- # rubocop:enable Metrics/ParameterLists
18
-
19
- def add
20
- comments = format commenter.new(old_comments, new_comments).add
21
- lines = comments + file_lines_without_comments
22
- insert_spacing! lines, comments
23
- write { lines.join }
24
- end
25
-
26
- def remove
27
- lines = format(commenter.new(old_comments, new_comments).remove) + file_lines_without_comments
28
- remove_spacing! lines
29
- write { lines.join }
30
- end
31
-
32
- private
33
-
34
- attr_reader :file_path, :file_lines, :new_comments, :old_comments, :formatter, :commenter
35
-
36
- def file_comments
37
- file_lines.select { |line| line =~ formatter.valid_formats }
38
- end
39
-
40
- def file_lines_without_comments
41
- file_lines.reject { |line| old_comments.include? line }
42
- end
43
-
44
- # :reek:UtilityFunction
45
- def format lines
46
- lines.map { |line| "#{line}\n" }
47
- end
48
-
49
- # :reek:UtilityFunction
50
- def insert_spacing! lines, comments
51
- comment_count = comments.size
52
-
53
- return if comments.empty?
54
- return if lines.size == 1
55
- return if lines[comment_count] == "\n"
56
-
57
- lines.insert comment_count, "\n"
58
- end
59
-
60
- # :reek:UtilityFunction
61
- def remove_spacing! lines
62
- lines.delete_at 0 if lines.first == "\n"
63
- end
64
-
65
- def write
66
- File.open(file_path, "w") { |file| file.write yield }
67
- end
68
- end
69
- end