pragmater 7.1.0 → 8.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ :insert:
2
+ :comments: []
3
+ :includes: []
4
+ :remove:
5
+ :comments: []
6
+ :includes: []
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module CLI
5
+ module Options
6
+ # Defines gem insert and remove options.
7
+ class InsertRemove
8
+ def initialize values, parser: OptionParser.new
9
+ @values = values
10
+ @parser = parser
11
+ end
12
+
13
+ def call
14
+ parser.separator "\nOPTIONS:\n"
15
+ parser.separator "\nInsert/Remove:\n"
16
+ private_methods.grep(/add_/).each { |method| __send__ method }
17
+ parser
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :values, :parser
23
+
24
+ def add_comments
25
+ parser.on "--comments a,b,c", Array, "Add pragma comments." do |comments|
26
+ values[:comments] = comments
27
+ end
28
+ end
29
+
30
+ def add_includes
31
+ parser.on "--includes a,b,c", Array, "Add console support." do |includes|
32
+ values[:includes] = includes
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -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.freeze
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).freeze
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.1.0"
8
+ VERSION = "8.3.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