pragmater 9.3.0 → 10.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.
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "refinements/structs"
4
+
5
+ module Pragmater
6
+ module CLI
7
+ module Parsers
8
+ # Handles parsing of Command Line Interface (CLI) core options.
9
+ class Core
10
+ using Refinements::Structs
11
+
12
+ def self.call(...) = new(...).call
13
+
14
+ def initialize configuration = Container[:configuration], client: Parser::CLIENT
15
+ @configuration = configuration
16
+ @client = client
17
+ end
18
+
19
+ def call arguments = []
20
+ client.banner = "#{Identity::LABEL} - #{Identity::SUMMARY}"
21
+ client.separator "\nUSAGE:\n"
22
+ collate
23
+ client.parse arguments
24
+ configuration
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :configuration, :client
30
+
31
+ def collate = private_methods.sort.grep(/add_/).each { |method| __send__ method }
32
+
33
+ def add_config
34
+ client.on "-c",
35
+ "--config ACTION",
36
+ %i[edit view],
37
+ "Manage gem configuration: edit or view." do |action|
38
+ configuration.merge! action_config: action
39
+ end
40
+ end
41
+
42
+ def add_insert
43
+ client.on "-i", "--insert [PATH]", %(Insert pragmas. Default: "#{root_dir}".) do |path|
44
+ configuration.merge! action_insert: true, root_dir: path || root_dir
45
+ end
46
+ end
47
+
48
+ def add_remove
49
+ client.on "-r", "--remove [PATH]", %(Remove pragmas. Default: "#{root_dir}".) do |path|
50
+ configuration.merge! action_remove: true, root_dir: path || root_dir
51
+ end
52
+ end
53
+
54
+ def add_version
55
+ client.on "-v", "--version", "Show gem version." do
56
+ configuration.merge! action_version: true
57
+ end
58
+ end
59
+
60
+ def add_help
61
+ client.on "-h", "--help", "Show this message." do
62
+ configuration.merge! action_help: true
63
+ end
64
+ end
65
+
66
+ def root_dir = configuration.root_dir
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "refinements/structs"
4
+
5
+ module Pragmater
6
+ module CLI
7
+ module Parsers
8
+ # Parses common command line flags.
9
+ class Flag
10
+ using Refinements::Structs
11
+
12
+ def self.call(...) = new(...).call
13
+
14
+ def initialize configuration = Container[:configuration], client: Parser::CLIENT
15
+ @configuration = configuration
16
+ @client = client
17
+ end
18
+
19
+ def call arguments = []
20
+ client.separator "\nOPTIONS:\n"
21
+ collate
22
+ client.parse arguments
23
+ configuration
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :configuration, :client
29
+
30
+ def collate = private_methods.sort.grep(/add_/).each { |method| __send__ method }
31
+
32
+ def add_comments
33
+ client.on "--comments a,b,c", Array, "Add pragma comments. Default: []." do |comments|
34
+ configuration.merge! comments:
35
+ end
36
+ end
37
+
38
+ def add_includes
39
+ client.on "--includes a,b,c", Array, "Add include patterns. Default: []." do |includes|
40
+ configuration.merge! includes:
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -2,61 +2,42 @@
2
2
 
3
3
  module Pragmater
4
4
  module CLI
5
- # The command line interface for this gem.
5
+ # The main Command Line Interface (CLI) object.
6
6
  class Shell
7
- def initialize merger: Options::Merger.new, runner: Runner, helper: Helper.new
8
- @merger = merger
9
- @runner = runner
10
- @helper = helper
7
+ ACTIONS = {config: Actions::Config.new, run: Actions::Run.new}.freeze
8
+
9
+ def initialize parser: Parser.new, actions: ACTIONS, container: Container
10
+ @parser = parser
11
+ @actions = actions
12
+ @container = container
11
13
  end
12
14
 
13
15
  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
- else print_usage
21
- end
16
+ perform parser.call(arguments)
17
+ rescue OptionParser::ParseError => error
18
+ logger.error { error.message }
22
19
  end
23
20
 
24
21
  private
25
22
 
26
- attr_reader :merger, :runner, :helper
27
-
28
- def insert_pragmas options, path
29
- runner.for(**options.merge(action: :insert, root_dir: path))
30
- .call
31
- .map { |file| helper.info "Processed: #{file}." }
32
- end
33
-
34
- def remove_pragmas options, path
35
- runner.for(**options.merge(action: :remove, root_dir: path))
36
- .call
37
- .map { |file| helper.info "Processed: #{file}." }
38
- end
23
+ attr_reader :parser, :actions, :container
39
24
 
40
- def edit_configuration
41
- helper.run "#{ENV["EDITOR"]} #{merger.configuration_path}"
25
+ def perform configuration
26
+ case configuration
27
+ in action_config: Symbol => action then config action
28
+ in {action_insert: true} | {action_remove: true} then run configuration
29
+ in action_version: true then logger.info Identity::VERSION_LABEL
30
+ else usage
31
+ end
42
32
  end
43
33
 
44
- def print_configuration
45
- merger.configuration_path.then do |path|
46
- return helper.info "No configuration found." unless path
34
+ def config(action) = actions.fetch(__method__).call(action)
47
35
 
48
- helper.info "#{path}\n"
49
- helper.info path.read
50
- end
51
- end
36
+ def run(configuration) = actions.fetch(__method__).call(configuration)
52
37
 
53
- def print_version
54
- helper.info Identity::VERSION_LABEL
55
- end
38
+ def usage = logger.unknown(parser.to_s)
56
39
 
57
- def print_usage
58
- helper.info merger.usage
59
- end
40
+ def logger = container[__method__]
60
41
  end
61
42
  end
62
43
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pragmater
4
+ module Configuration
5
+ # Defines the content of the configuration for use throughout the gem.
6
+ Content = Struct.new(
7
+ :action_config,
8
+ :action_help,
9
+ :action_insert,
10
+ :action_remove,
11
+ :action_version,
12
+ :comments,
13
+ :includes,
14
+ :root_dir,
15
+ keyword_init: true
16
+ ) do
17
+ def initialize *arguments
18
+ super
19
+ freeze
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ :comments: []
2
+ :includes: []
3
+ :root_dir: "."
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "refinements/hashes"
5
+ require "refinements/structs"
6
+ require "runcom"
7
+ require "yaml"
8
+
9
+ module Pragmater
10
+ module Configuration
11
+ # Represents the fully assembled Command Line Interface (CLI) configuration.
12
+ class Loader
13
+ using Refinements::Hashes
14
+ using Refinements::Structs
15
+
16
+ DEFAULTS = YAML.load_file(Pathname(__dir__).join("defaults.yml")).freeze
17
+ CLIENT = Runcom::Config.new "#{Identity::NAME}/configuration.yml", defaults: DEFAULTS
18
+
19
+ def self.call = new.call
20
+
21
+ def self.with_defaults = new(client: DEFAULTS)
22
+
23
+ def initialize content: Content.new, client: CLIENT
24
+ @content = content
25
+ @client = client
26
+ end
27
+
28
+ def call = content.merge(**client.to_h.flatten_keys)
29
+
30
+ private
31
+
32
+ attr_reader :content, :client
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-container"
4
+ require "logger"
5
+ require "pastel"
6
+
7
+ module Pragmater
8
+ # Provides a global gem container for injection into other objects.
9
+ module Container
10
+ extend Dry::Container::Mixin
11
+
12
+ register(:configuration) { Configuration::Loader.call }
13
+ register(:colorizer) { Pastel.new enabled: $stdout.tty? }
14
+ register(:kernel) { Kernel }
15
+
16
+ register :log_colors do
17
+ {
18
+ "DEBUG" => self[:colorizer].white.detach,
19
+ "INFO" => self[:colorizer].green.detach,
20
+ "WARN" => self[:colorizer].yellow.detach,
21
+ "ERROR" => self[:colorizer].red.detach,
22
+ "FATAL" => self[:colorizer].white.bold.on_red.detach,
23
+ "ANY" => self[:colorizer].white.bold.detach
24
+ }
25
+ end
26
+
27
+ register :logger do
28
+ Logger.new $stdout,
29
+ level: Logger.const_get(ENV.fetch("LOG_LEVEL", "INFO")),
30
+ formatter: (
31
+ lambda do |severity, _at, _name, message|
32
+ self[:log_colors][severity].call "#{message}\n"
33
+ end
34
+ )
35
+ end
36
+ end
37
+ end
@@ -5,6 +5,7 @@ module Pragmater
5
5
  # Formats all pragmas in a consistent manner.
6
6
  class Main
7
7
  FORMATTERS = [General, Shebang].freeze
8
+
8
9
  PATTERN = FORMATTERS.map { |formatter| formatter::PATTERN }
9
10
  .then { |patterns| Regexp.union(*patterns) }
10
11
  .freeze
@@ -14,9 +15,7 @@ module Pragmater
14
15
  @formatters = formatters
15
16
  end
16
17
 
17
- def call
18
- formatters.reduce(string) { |pragma, formatter| formatter.new(pragma).call }
19
- end
18
+ def call = formatters.reduce(string) { |pragma, formatter| formatter.new(pragma).call }
20
19
 
21
20
  private
22
21
 
@@ -5,7 +5,7 @@ module Pragmater
5
5
  module Identity
6
6
  NAME = "pragmater"
7
7
  LABEL = "Pragmater"
8
- VERSION = "9.3.0"
8
+ VERSION = "10.0.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  SUMMARY = "A command line interface for managing/formatting source file pragma comments."
11
11
  end
@@ -10,21 +10,15 @@ module Pragmater
10
10
  @newer = format newer
11
11
  end
12
12
 
13
- def insert
14
- older.union newer
15
- end
13
+ def insert = older.union(newer)
16
14
 
17
- def remove
18
- older - older.intersection(newer)
19
- end
15
+ def remove = older - older.intersection(newer)
20
16
 
21
17
  private
22
18
 
23
19
  attr_reader :formatter, :older, :newer
24
20
 
25
- def format pragmas
26
- Array(pragmas).map { |pragma| formatter.new(pragma).call }
27
- end
21
+ def format(pragmas) = Array(pragmas).map { |pragma| formatter.new(pragma).call }
28
22
  end
29
23
  end
30
24
  end
@@ -10,9 +10,7 @@ module Pragmater
10
10
  @processors = processors
11
11
  end
12
12
 
13
- def call action, comments, body
14
- processors.fetch(action).new(comments, body).call
15
- end
13
+ def call(action, comments, body) = processors.fetch(action).new(comments, body).call
16
14
 
17
15
  private
18
16
 
@@ -7,23 +7,31 @@ module Pragmater
7
7
  class Runner
8
8
  using Refinements::Pathnames
9
9
 
10
- def self.for **attributes
11
- new Context[attributes]
12
- end
13
-
14
- def initialize context, parser: Parsers::File.new
15
- @context = context
10
+ def initialize parser: Parsers::File.new, container: Container
16
11
  @parser = parser
12
+ @container = container
17
13
  end
18
14
 
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
15
+ def call configuration = Configuration::Loader.call
16
+ Pathname(configuration.root_dir).files("{#{configuration.includes.join ","}}").map do |path|
17
+ yield path if block_given?
18
+
19
+ case configuration
20
+ in action_insert: true then write path, configuration, :insert
21
+ in action_remove: true then write path, configuration, :remove
22
+ else logger.error { "Unknown run action. Use insert or remove." }
23
+ end
22
24
  end
23
25
  end
24
26
 
25
27
  private
26
28
 
27
- attr_reader :context, :parser
29
+ attr_reader :parser, :container
30
+
31
+ def write path, configuration, action
32
+ path.write parser.call(path, configuration.comments, action:).join
33
+ end
34
+
35
+ def logger = container[__method__]
28
36
  end
29
37
  end
data/lib/pragmater.rb CHANGED
@@ -1,20 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pragmater/identity"
4
- require "pragmater/formatters/general"
5
- require "pragmater/formatters/shebang"
6
- require "pragmater/formatters/main"
7
- require "pragmater/parsers/comments"
8
- require "pragmater/parsers/file"
9
- require "pragmater/processors/inserter"
10
- require "pragmater/processors/remover"
11
- require "pragmater/processors/handler"
12
- require "pragmater/context"
13
- require "pragmater/runner"
14
- require "pragmater/cli/options/configuration"
15
- require "pragmater/cli/options/insert_remove"
16
- require "pragmater/cli/options/core"
17
- require "pragmater/cli/options/assembler"
18
- require "pragmater/cli/options/merger"
19
- require "pragmater/cli/helper"
20
- require "pragmater/cli/shell"
3
+ require "zeitwerk"
4
+
5
+ Zeitwerk::Loader.for_gem
6
+ .tap { |loader| loader.inflector.inflect "cli" => "CLI" }
7
+ .setup
8
+
9
+ # Main namespace.
10
+ module Pragmater
11
+ end
data.tar.gz.sig CHANGED
Binary file
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: 9.3.0
4
+ version: 10.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,36 +28,64 @@ cert_chain:
28
28
  lkHilIrX69jq8wMPpBhlaw2mRmeSL50Wv5u6xVBvOHhXFSP1crXM95vfLhLyRYod
29
29
  W2A=
30
30
  -----END CERTIFICATE-----
31
- date: 2021-11-20 00:00:00.000000000 Z
31
+ date: 2021-12-29 00:00:00.000000000 Z
32
32
  dependencies:
33
+ - !ruby/object:Gem::Dependency
34
+ name: dry-container
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.9'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: refinements
35
49
  requirement: !ruby/object:Gem::Requirement
36
50
  requirements:
37
51
  - - "~>"
38
52
  - !ruby/object:Gem::Version
39
- version: '8.5'
53
+ version: '9.0'
40
54
  type: :runtime
41
55
  prerelease: false
42
56
  version_requirements: !ruby/object:Gem::Requirement
43
57
  requirements:
44
58
  - - "~>"
45
59
  - !ruby/object:Gem::Version
46
- version: '8.5'
60
+ version: '9.0'
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: runcom
49
63
  requirement: !ruby/object:Gem::Requirement
50
64
  requirements:
51
65
  - - "~>"
52
66
  - !ruby/object:Gem::Version
53
- version: '7.0'
67
+ version: '8.0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '8.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: zeitwerk
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.5'
54
82
  type: :runtime
55
83
  prerelease: false
56
84
  version_requirements: !ruby/object:Gem::Requirement
57
85
  requirements:
58
86
  - - "~>"
59
87
  - !ruby/object:Gem::Version
60
- version: '7.0'
88
+ version: '2.5'
61
89
  description:
62
90
  email:
63
91
  - brooke@alchemists.io
@@ -72,15 +100,17 @@ files:
72
100
  - README.adoc
73
101
  - exe/pragmater
74
102
  - lib/pragmater.rb
103
+ - lib/pragmater/cli/actions/config.rb
104
+ - lib/pragmater/cli/actions/run.rb
75
105
  - 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
106
+ - lib/pragmater/cli/parser.rb
107
+ - lib/pragmater/cli/parsers/core.rb
108
+ - lib/pragmater/cli/parsers/flag.rb
82
109
  - lib/pragmater/cli/shell.rb
83
- - lib/pragmater/context.rb
110
+ - lib/pragmater/configuration/content.rb
111
+ - lib/pragmater/configuration/defaults.yml
112
+ - lib/pragmater/configuration/loader.rb
113
+ - lib/pragmater/container.rb
84
114
  - lib/pragmater/formatters/general.rb
85
115
  - lib/pragmater/formatters/main.rb
86
116
  - lib/pragmater/formatters/shebang.rb
@@ -93,7 +123,7 @@ files:
93
123
  - lib/pragmater/runner.rb
94
124
  homepage: https://www.alchemists.io/projects/pragmater
95
125
  licenses:
96
- - Apache-2.0
126
+ - Hippocratic-3.0
97
127
  metadata:
98
128
  bug_tracker_uri: https://github.com/bkuhlmann/pragmater/issues
99
129
  changelog_uri: https://www.alchemists.io/projects/pragmater/changes.html
@@ -108,14 +138,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
138
  requirements:
109
139
  - - "~>"
110
140
  - !ruby/object:Gem::Version
111
- version: '3.0'
141
+ version: '3.1'
112
142
  required_rubygems_version: !ruby/object:Gem::Requirement
113
143
  requirements:
114
144
  - - ">="
115
145
  - !ruby/object:Gem::Version
116
146
  version: '0'
117
147
  requirements: []
118
- rubygems_version: 3.2.31
148
+ rubygems_version: 3.3.3
119
149
  signing_key:
120
150
  specification_version: 4
121
151
  summary: A command line interface for managing/formatting source file pragma comments.
metadata.gz.sig CHANGED
Binary file
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "optparse"
4
- require "forwardable"
5
-
6
- module Pragmater
7
- module CLI
8
- module Options
9
- # Coalesces all options and arguments into a single hash for further processing.
10
- class Assembler
11
- extend Forwardable
12
-
13
- PARSER = OptionParser.new nil, 40, " "
14
- SECTIONS = [Core, InsertRemove, Configuration].freeze
15
-
16
- EXCEPTIONS = [
17
- OptionParser::InvalidOption,
18
- OptionParser::MissingArgument,
19
- OptionParser::InvalidArgument
20
- ].freeze
21
-
22
- delegate %i[to_s] => :parser
23
-
24
- def initialize sections: SECTIONS, parser: PARSER, exceptions: EXCEPTIONS
25
- @sections = sections
26
- @parser = parser
27
- @options = {}
28
- @exceptions = exceptions
29
- end
30
-
31
- def call arguments = []
32
- sections.each { |section| section.new(options, parser: parser).call }
33
- parser.parse! arguments
34
- options
35
- rescue *EXCEPTIONS
36
- {}
37
- end
38
-
39
- private
40
-
41
- attr_reader :parser, :sections, :options, :exceptions
42
- end
43
- end
44
- end
45
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- module CLI
5
- module Options
6
- # Defines gem configuration options.
7
- class Configuration
8
- def initialize values, parser: OptionParser.new
9
- @parser = parser
10
- @values = values
11
- end
12
-
13
- def call
14
- parser.separator "\nConfiguration:\n"
15
- private_methods.grep(/add_/).each { |method| __send__ method }
16
- parser
17
- end
18
-
19
- private
20
-
21
- attr_reader :parser, :values
22
-
23
- def add_edit
24
- parser.on "--edit", "Edit configuration." do
25
- values[:edit] = true
26
- end
27
- end
28
-
29
- def add_info
30
- parser.on "--info", "Print configuration." do
31
- values[:info] = true
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end