pragmater 6.3.3 → 8.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/{LICENSE.md → LICENSE.adoc} +29 -30
- data/README.adoc +323 -0
- data/bin/pragmater +5 -4
- data/lib/pragmater.rb +16 -4
- data/lib/pragmater/cli/helper.rb +40 -0
- data/lib/pragmater/cli/options/assembler.rb +45 -0
- data/lib/pragmater/cli/options/configuration.rb +37 -0
- data/lib/pragmater/cli/options/core.rb +56 -0
- data/lib/pragmater/cli/options/defaults.yml +6 -0
- data/lib/pragmater/cli/options/insert_remove.rb +38 -0
- data/lib/pragmater/cli/options/merger.rb +52 -0
- data/lib/pragmater/cli/shell.rb +63 -0
- data/lib/pragmater/context.rb +14 -0
- data/lib/pragmater/formatters/general.rb +34 -0
- data/lib/pragmater/formatters/main.rb +26 -0
- data/lib/pragmater/formatters/shebang.rb +25 -0
- data/lib/pragmater/identity.rb +5 -15
- data/lib/pragmater/parsers/comments.rb +30 -0
- data/lib/pragmater/parsers/file.rb +32 -0
- data/lib/pragmater/processors/handler.rb +25 -0
- data/lib/pragmater/processors/inserter.rb +24 -0
- data/lib/pragmater/processors/remover.rb +24 -0
- data/lib/pragmater/runner.rb +19 -17
- metadata +70 -126
- metadata.gz.sig +0 -0
- data/README.md +0 -263
- data/lib/pragmater/cli.rb +0 -124
- data/lib/pragmater/commenter.rb +0 -32
- data/lib/pragmater/formatter.rb +0 -59
- data/lib/pragmater/writer.rb +0 -69
metadata.gz.sig
CHANGED
Binary file
|
data/README.md
DELETED
@@ -1,263 +0,0 @@
|
|
1
|
-
<p align="center">
|
2
|
-
<img src="pragmater.png" alt="Pragmater Icon"/>
|
3
|
-
</p>
|
4
|
-
|
5
|
-
# Pragmater
|
6
|
-
|
7
|
-
[![Gem Version](https://badge.fury.io/rb/pragmater.svg)](http://badge.fury.io/rb/pragmater)
|
8
|
-
[![Code Climate Maintainability](https://api.codeclimate.com/v1/badges/f0971ab6985309ce4db4/maintainability)](https://codeclimate.com/github/bkuhlmann/pragmater/maintainability)
|
9
|
-
[![Code Climate Test Coverage](https://api.codeclimate.com/v1/badges/f0971ab6985309ce4db4/test_coverage)](https://codeclimate.com/github/bkuhlmann/pragmater/test_coverage)
|
10
|
-
[![Circle CI Status](https://circleci.com/gh/bkuhlmann/pragmater.svg?style=svg)](https://circleci.com/gh/bkuhlmann/pragmater)
|
11
|
-
|
12
|
-
A command line interface for managing/formatting source file [directive
|
13
|
-
pragmas](https://en.wikipedia.org/wiki/Directive_(programming)) (a.k.a. *magic comments*). Examples:
|
14
|
-
|
15
|
-
#! /usr/bin/env ruby
|
16
|
-
# frozen_string_literal: true
|
17
|
-
# encoding: UTF-8
|
18
|
-
|
19
|
-
With [Ruby 2.3.0](https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-released), frozen strings
|
20
|
-
are supported via a pragma. This gem provides an easy way to add pragmas to single or multiple Ruby
|
21
|
-
source files in order to benefit from improved memory and concurrency performance.
|
22
|
-
|
23
|
-
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
24
|
-
|
25
|
-
## Table of Contents
|
26
|
-
|
27
|
-
- [Features](#features)
|
28
|
-
- [Screencasts](#screencasts)
|
29
|
-
- [Requirements](#requirements)
|
30
|
-
- [Setup](#setup)
|
31
|
-
- [Usage](#usage)
|
32
|
-
- [Command Line Interface (CLI)](#command-line-interface-cli)
|
33
|
-
- [Customization](#customization)
|
34
|
-
- [Available Pragmas](#available-pragmas)
|
35
|
-
- [Syntax](#syntax)
|
36
|
-
- [Precedence](#precedence)
|
37
|
-
- [Frozen String Literals](#frozen-string-literals)
|
38
|
-
- [Tests](#tests)
|
39
|
-
- [Versioning](#versioning)
|
40
|
-
- [Code of Conduct](#code-of-conduct)
|
41
|
-
- [Contributions](#contributions)
|
42
|
-
- [License](#license)
|
43
|
-
- [History](#history)
|
44
|
-
- [Credits](#credits)
|
45
|
-
|
46
|
-
<!-- Tocer[finish]: Auto-generated, don't remove. -->
|
47
|
-
|
48
|
-
## Features
|
49
|
-
|
50
|
-
- Supports adding a pragma or multiple pragmas to single or multiple source files.
|
51
|
-
- Supports removing pragma(s) from single or multiple source files.
|
52
|
-
- Supports file list filtering. Defaults to any file.
|
53
|
-
- Ensures duplicate pragmas never exist.
|
54
|
-
- Ensures pragmas are consistently formatted.
|
55
|
-
|
56
|
-
## Screencasts
|
57
|
-
|
58
|
-
[![asciicast](https://asciinema.org/a/155880.png)](https://asciinema.org/a/155880)
|
59
|
-
|
60
|
-
## Requirements
|
61
|
-
|
62
|
-
1. [Ruby 2.6.x](https://www.ruby-lang.org)
|
63
|
-
|
64
|
-
## Setup
|
65
|
-
|
66
|
-
Type the following to install:
|
67
|
-
|
68
|
-
gem install pragmater
|
69
|
-
|
70
|
-
## Usage
|
71
|
-
|
72
|
-
### Command Line Interface (CLI)
|
73
|
-
|
74
|
-
From the command line, type: `pragmater help`
|
75
|
-
|
76
|
-
pragmater -a, [--add=PATH] # Add comments to source file(s).
|
77
|
-
pragmater -c, [--config] # Manage gem configuration.
|
78
|
-
pragmater -h, [--help=COMMAND] # Show this message or get help for a command.
|
79
|
-
pragmater -r, [--remove=PATH] # Remove comments from source file(s).
|
80
|
-
pragmater -v, [--version] # Show gem version.
|
81
|
-
|
82
|
-
Both the `--add` and `--remove` commands support options for specifying pragmas and/or included
|
83
|
-
files (viewable by running `pragmater --help --add` or `pragmater --help --remove`):
|
84
|
-
|
85
|
-
-c, [--comments=one two three] # Define desired comments
|
86
|
-
-i, [--includes=one two three] # Include specific files and/or directories
|
87
|
-
|
88
|
-
Example (same options could be used for the `--remove` command too):
|
89
|
-
|
90
|
-
pragmater --add --comments "# frozen_string_literal: true" --includes "Gemfile" "Guardfile" "Rakefile" ".gemspec" "config.ru" "bin/**/*" "**/*.rake" "**/*.rb"
|
91
|
-
|
92
|
-
The `--add` and `--remove` commands default to the current working directory so a path isn't
|
93
|
-
necessary unless you want to run Pragmater on a directory structure *other than* your current
|
94
|
-
working directory.
|
95
|
-
|
96
|
-
### Customization
|
97
|
-
|
98
|
-
This gem can be configured via a global configuration:
|
99
|
-
|
100
|
-
~/.config/pragmater/configuration.yml
|
101
|
-
|
102
|
-
It can also be configured via [XDG](https://github.com/bkuhlmann/xdg) environment variables.
|
103
|
-
|
104
|
-
The default configuration is as follows:
|
105
|
-
|
106
|
-
:add:
|
107
|
-
:comments: []
|
108
|
-
:includes: []
|
109
|
-
:remove:
|
110
|
-
:comments: []
|
111
|
-
:includes: []
|
112
|
-
|
113
|
-
Feel free to take this default configuration, modify, and save as your own custom
|
114
|
-
`configuration.yml`.
|
115
|
-
|
116
|
-
The `configuration.yml` file can be configured as follows:
|
117
|
-
|
118
|
-
- `add`: Defines global/local comments and/or file include lists when adding pragmas. The
|
119
|
-
`comments` and `includes` options can be either a single string or an array.
|
120
|
-
- `remove`: Defines global/local comments and/or file include lists when removing pragmas.
|
121
|
-
The `comments` and `includes` options can be either a single string or an array.
|
122
|
-
|
123
|
-
### Available Pragmas
|
124
|
-
|
125
|
-
With Ruby 2.3 and higher, the following pragmas are available:
|
126
|
-
|
127
|
-
- `# encoding:` Defaults to `UTF-8` but any supported encoding can be used. For a list of values,
|
128
|
-
launch an IRB session and run `Encoding.name_list`.
|
129
|
-
- `# coding:` The shorthand for `# encoding:`. Supports the same values as mentioned above.
|
130
|
-
- `# frozen_string_literal:` Defaults to `false` but can take either `true` or `false` as a value.
|
131
|
-
When enabled, Ruby will throw errors when strings are used in a mutable fashion.
|
132
|
-
- `# warn_indent:` Defaults to `false` but can take either `true` or `false` as a value. When
|
133
|
-
enabled, and running Ruby with the `-w` option, it'll throw warnings for code that isn't indented
|
134
|
-
by two spaces.
|
135
|
-
|
136
|
-
### Syntax
|
137
|
-
|
138
|
-
The pragma syntax allows for two kinds of styles. Example:
|
139
|
-
|
140
|
-
# encoding: UTF-8
|
141
|
-
# -*- encoding: UTF-8 -*-
|
142
|
-
|
143
|
-
Only the former syntax is supported by this gem as the latter syntax is more verbose and requires
|
144
|
-
additional typing.
|
145
|
-
|
146
|
-
### Precedence
|
147
|
-
|
148
|
-
When different multiple pragmas are defined, they all take precedence:
|
149
|
-
|
150
|
-
# encoding: binary
|
151
|
-
# frozen_string_literal: true
|
152
|
-
|
153
|
-
In the above example, both *binary* encoding and *frozen string literals* behavior will be applied.
|
154
|
-
|
155
|
-
When defining multiple pragmas that are similar, behavior can differ based on the *kind* of pragma
|
156
|
-
used. The following walks through each use case so you know what to expect:
|
157
|
-
|
158
|
-
# encoding: binary
|
159
|
-
# encoding: UTF-8
|
160
|
-
|
161
|
-
In the above example, only the *binary* encoding will be applied while the *UTF-8* encoding will be
|
162
|
-
ignored (same principle applies for the `coding` pragma too).
|
163
|
-
|
164
|
-
# frozen_string_literal: false
|
165
|
-
# frozen_string_literal: true
|
166
|
-
|
167
|
-
In the above example, frozen string literal support *will be enabled* instead of being disabled.
|
168
|
-
|
169
|
-
# warn_indent: false
|
170
|
-
# warn_indent: true
|
171
|
-
|
172
|
-
In the above example, indentation warnings *will be enabled* instead of being disabled.
|
173
|
-
|
174
|
-
### Frozen String Literals
|
175
|
-
|
176
|
-
Support for frozen string literals was added in Ruby 2.3.0. The ability to freeze strings within a
|
177
|
-
source can be done by placing a frozen string pragma at the top of each source file. Example:
|
178
|
-
|
179
|
-
# frozen_string_literal: true
|
180
|
-
|
181
|
-
This is great for *selective* enablement of frozen string literals but might be too much work for
|
182
|
-
some (even with the aid of this gem). As an alternative, frozen string literals can be enabled via
|
183
|
-
the following Ruby command line option:
|
184
|
-
|
185
|
-
--enable=frozen-string-literal
|
186
|
-
|
187
|
-
It is important to note that, once enabled, this freezes strings program-wide -- It's an all or
|
188
|
-
nothing option.
|
189
|
-
|
190
|
-
Regardless of whether you leverage the capabilities of this gem or the Ruby command line option
|
191
|
-
mentioned above, the following Ruby command line option is available to aid debugging and tracking
|
192
|
-
down frozen string literal issues:
|
193
|
-
|
194
|
-
--debug=frozen-string-literal
|
195
|
-
|
196
|
-
Ruby 2.3.0 also added the following methods to the `String` class:
|
197
|
-
|
198
|
-
- `String#+@`: Answers a duplicated, mutable, string if not already frozen. Example:
|
199
|
-
|
200
|
-
immutable = "test".freeze
|
201
|
-
mutable = +immutable
|
202
|
-
mutable.capitalize! # => "Test"
|
203
|
-
|
204
|
-
- `String#-@`: Answers a immutable string if not already frozen. Example:
|
205
|
-
|
206
|
-
mutable = "test"
|
207
|
-
immutable = -mutable
|
208
|
-
immutable.capitalize! # => FrozenError
|
209
|
-
|
210
|
-
You can also use the methods, shown above, for variable initialization. Example:
|
211
|
-
|
212
|
-
immutable = -"test"
|
213
|
-
mutable = +"test"
|
214
|
-
|
215
|
-
Despite Ruby allowing you to do this, it is *not recommended* to use the above examples as it leads
|
216
|
-
to hard to read code. Instead use the following:
|
217
|
-
|
218
|
-
immutable = "test".freeze
|
219
|
-
mutable = "test"
|
220
|
-
|
221
|
-
While this requires extra typing, it expresses intent more clearly. There is a slight caveat to this
|
222
|
-
rule in which the use of `String#-@` was [enhanced in Ruby 2.5.0](http://bit.ly/2DGAjgG) to
|
223
|
-
*deduplicate* all instances of the same string thus reducing your memory footprint. This can be
|
224
|
-
valuable in situations where you are not using the frozen string comment and need to selectively
|
225
|
-
freeze strings.
|
226
|
-
|
227
|
-
## Tests
|
228
|
-
|
229
|
-
To test, run:
|
230
|
-
|
231
|
-
bundle exec rake
|
232
|
-
|
233
|
-
## Versioning
|
234
|
-
|
235
|
-
Read [Semantic Versioning](https://semver.org) for details. Briefly, it means:
|
236
|
-
|
237
|
-
- Major (X.y.z) - Incremented for any backwards incompatible public API changes.
|
238
|
-
- Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
|
239
|
-
- Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
|
240
|
-
|
241
|
-
## Code of Conduct
|
242
|
-
|
243
|
-
Please note that this project is released with a [CODE OF CONDUCT](CODE_OF_CONDUCT.md). By
|
244
|
-
participating in this project you agree to abide by its terms.
|
245
|
-
|
246
|
-
## Contributions
|
247
|
-
|
248
|
-
Read [CONTRIBUTING](CONTRIBUTING.md) for details.
|
249
|
-
|
250
|
-
## License
|
251
|
-
|
252
|
-
Copyright 2015 [Alchemists](https://www.alchemists.io).
|
253
|
-
Read [LICENSE](LICENSE.md) for details.
|
254
|
-
|
255
|
-
## History
|
256
|
-
|
257
|
-
Read [CHANGES](CHANGES.md) for details.
|
258
|
-
Built with [Gemsmith](https://github.com/bkuhlmann/gemsmith).
|
259
|
-
|
260
|
-
## Credits
|
261
|
-
|
262
|
-
Developed by [Brooke Kuhlmann](https://www.alchemists.io) at
|
263
|
-
[Alchemists](https://www.alchemists.io).
|
data/lib/pragmater/cli.rb
DELETED
@@ -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,
|
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.path
|
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
|
data/lib/pragmater/commenter.rb
DELETED
@@ -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
|
data/lib/pragmater/formatter.rb
DELETED
@@ -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
|