pragmater 0.1.0 → 1.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.
- checksums.yaml +4 -4
- data/README.md +64 -8
- data/bin/pragmater +1 -2
- data/lib/pragmater.rb +1 -0
- data/lib/pragmater/cli.rb +32 -13
- data/lib/pragmater/commenter.rb +5 -1
- data/lib/pragmater/configuration.rb +59 -0
- data/lib/pragmater/formatter.rb +21 -17
- data/lib/pragmater/identity.rb +1 -1
- data/lib/pragmater/writer.rb +19 -3
- data/lib/tasks/console.rake +7 -0
- data/lib/tasks/rspec.rake +0 -2
- data/lib/tasks/rubocop.rake +0 -2
- metadata +30 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 019b606499c4c1acfa9567af9fb95091e3d6394e
|
4
|
+
data.tar.gz: 6565699d00d5b211e42b34af9370f9bdcbe99685
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f42b618a2a92e9e7740e0a969ed795bd86adcedf6a78f0e85170398e22e8c51d185f08299a0c22fb49cf4d35a6c9f82e83ab253ee2b6176f6a2933fbfb111293
|
7
|
+
data.tar.gz: 3b518cebc1e4835dee5e85f61d3810cd19296c5fbbf5bb2697058beddf27b622cb2beb86796dcacab9742cd57cd1e866499aac145b091bbae2814c7a8d291a1c
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ A command line interface for adding [directive pragma](https://en.wikipedia.org/
|
|
15
15
|
# encoding: UTF-8
|
16
16
|
|
17
17
|
With the release of [Ruby 2.3.0](https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-released), support for frozen
|
18
|
-
strings are now supported via a pragma comment. This gems provides
|
18
|
+
strings are now supported via a pragma comment. This gems provides an easy way to add pragma comments to single or
|
19
19
|
multiple Ruby source files in order to benefit from improved memory and concurrency performance.
|
20
20
|
|
21
21
|
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
@@ -27,6 +27,8 @@ multiple Ruby source files in order to benefit from improved memory and concurre
|
|
27
27
|
- [Setup](#setup)
|
28
28
|
- [Usage](#usage)
|
29
29
|
- [Command Line Interface (CLI)](#command-line-interface-cli)
|
30
|
+
- [Customization](#customization)
|
31
|
+
- [Frozen String Literals](#frozen-string-literals)
|
30
32
|
- [Tests](#tests)
|
31
33
|
- [Versioning](#versioning)
|
32
34
|
- [Code of Conduct](#code-of-conduct)
|
@@ -41,10 +43,12 @@ multiple Ruby source files in order to benefit from improved memory and concurre
|
|
41
43
|
|
42
44
|
- Supports adding a pragma comment or multiple pragma comments to single or multiple source files.
|
43
45
|
- Supports removing a pragma comment or multiple pragma comments from single or multiple source files.
|
44
|
-
- Supports whitelist filtering. Defaults to
|
46
|
+
- Supports whitelist filtering. Defaults to any file.
|
45
47
|
- Ensures duplicate pragma comments never exist.
|
46
48
|
- Ensures pragma commments are always properly formatted.
|
47
49
|
|
50
|
+
[](https://asciinema.org/a/34038)
|
51
|
+
|
48
52
|
# Requirements
|
49
53
|
|
50
54
|
0. [MRI 2.3.x](https://www.ruby-lang.org)
|
@@ -53,7 +57,7 @@ multiple Ruby source files in order to benefit from improved memory and concurre
|
|
53
57
|
|
54
58
|
For a secure install, type the following (recommended):
|
55
59
|
|
56
|
-
gem cert --add <(curl -Ls https://www.
|
60
|
+
gem cert --add <(curl -Ls https://www.alchemists.io/gem-public.pem)
|
57
61
|
gem install pragmater --trust-policy MediumSecurity
|
58
62
|
|
59
63
|
NOTE: A HighSecurity trust policy would be best but MediumSecurity enables signed gem verification while
|
@@ -75,12 +79,64 @@ From the command line, type: `pragmater help`
|
|
75
79
|
pragmater -r, [--remove=REMOVE] # Remove pragma comments from source file(s).
|
76
80
|
pragmater -v, [--version] # Show Pragmater version.
|
77
81
|
|
78
|
-
Both the `--add` and `--remove` options provide the ability to supply specific pragma comments and/or whitelisted
|
79
|
-
|
82
|
+
Both the `--add` and `--remove` options provide the ability to supply specific pragma comments and/or whitelisted files:
|
83
|
+
|
84
|
+
-c, [--comments=one two three] # Pragma comments
|
85
|
+
-w, [--whitelist=one two three] # File extension whitelist
|
86
|
+
|
87
|
+
## Customization
|
88
|
+
|
89
|
+
Should the default settings not be desired, customization is allowed via the `.pragmaterrc` file. The `.pragmaterrc`
|
90
|
+
can be created at a global and/or local level. Example:
|
91
|
+
|
92
|
+
- Global: `~/.pragmaterrc`
|
93
|
+
- Local: `<project repository root>/.pragmaterrc`
|
94
|
+
|
95
|
+
Order of precedence for any setting is resolved as follows (with the last taking top priority):
|
96
|
+
|
97
|
+
0. Global `~/.pragmaterrc`.
|
98
|
+
0. Local project repository `.pragmaterrc`.
|
99
|
+
0. CLI option (i.e. `--add` or `--remove` command options).
|
100
|
+
|
101
|
+
Any setting provided to the CLI during runtime would trump a local/global setting and a local setting would trump a
|
102
|
+
global setting. The global setting is the weakest of all but great for situations where custom settings should be
|
103
|
+
applied to *all* projects. It is important to note that local settings completely trump any global settings -- there is
|
104
|
+
no inheritance when local *and* global settings exist at the same time.
|
105
|
+
|
106
|
+
The `.pragmaterrc` uses the following default settings:
|
107
|
+
|
108
|
+
:add:
|
109
|
+
:comments: []
|
110
|
+
:whitelist: []
|
111
|
+
:remove:
|
112
|
+
:comments: []
|
113
|
+
:whitelist: []
|
114
|
+
|
115
|
+
Each `.pragmaterrc` setting can be configured as follows:
|
116
|
+
|
117
|
+
- `add`: Defines global/local comments and/or whitelists when adding pragma comments. The `comments` and `whitelist`
|
118
|
+
options can be either a single string or an array of values.
|
119
|
+
- `remove`: Defines global/local comments and/or whitelists when removing pragma comments. The `comments` and
|
120
|
+
`whitelist` options can be either a single string or an array of values.
|
121
|
+
|
122
|
+
## Frozen String Literals
|
123
|
+
|
124
|
+
With Ruby 2.3.0, support of frozen string literals are now supported. These comments are meant to be placed at the top
|
125
|
+
of each source file. Example:
|
126
|
+
|
127
|
+
# frozen_string_literal: true
|
128
|
+
|
129
|
+
This is great for *selective* enablement of frozen string literals but might be too much work for some (even with the
|
130
|
+
aid of this gem). As an alternative, frozen string literals can be enabled via the following Ruby command line option:
|
131
|
+
|
132
|
+
--enable=frozen-string-literal
|
133
|
+
|
134
|
+
It is important to note that once enabled, it freezes strings program-wide -- It's an all or nothing option.
|
135
|
+
|
136
|
+
Regardless of whether you leverage the capabilities of this gem or the Ruby command line option mentioned above, the
|
137
|
+
following Ruby command line option is available to aid debugging and tracking down frozen string literal issues:
|
80
138
|
|
81
|
-
|
82
|
-
-e, [--extensions=one two three] # File extension whitelist
|
83
|
-
# Default: [".rb", ".rake"]
|
139
|
+
--debug=frozen-string-literal
|
84
140
|
|
85
141
|
# Tests
|
86
142
|
|
data/bin/pragmater
CHANGED
data/lib/pragmater.rb
CHANGED
data/lib/pragmater/cli.rb
CHANGED
@@ -16,22 +16,25 @@ module Pragmater
|
|
16
16
|
|
17
17
|
def initialize args = [], options = {}, config = {}
|
18
18
|
super args, options, config
|
19
|
+
@configuration = Configuration.new Identity.file_name
|
19
20
|
end
|
20
21
|
|
21
22
|
desc "-a, [--add=ADD]", "Add pragma comments to source file(s)."
|
22
23
|
map %w(-a --add) => :add
|
23
24
|
method_option :comments, aliases: "-c", desc: "Pragma comments", type: :array, default: []
|
24
|
-
method_option :
|
25
|
+
method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
|
25
26
|
def add path
|
26
|
-
|
27
|
+
settings = configuration.merge add: {comments: options[:comments], whitelist: options[:whitelist]}
|
28
|
+
write path, settings, :add
|
27
29
|
end
|
28
30
|
|
29
31
|
desc "-r, [--remove=REMOVE]", "Remove pragma comments from source file(s)."
|
30
32
|
map %w(-r --remove) => :remove
|
31
33
|
method_option :comments, aliases: "-c", desc: "Pragma comments", type: :array, default: []
|
32
|
-
method_option :
|
34
|
+
method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
|
33
35
|
def remove path
|
34
|
-
|
36
|
+
settings = configuration.merge remove: {comments: options[:comments], whitelist: options[:whitelist]}
|
37
|
+
write path, settings, :remove
|
35
38
|
end
|
36
39
|
|
37
40
|
desc "-e, [--edit]", "Edit #{Pragmater::Identity.label} settings in default editor."
|
@@ -56,23 +59,39 @@ module Pragmater
|
|
56
59
|
|
57
60
|
private
|
58
61
|
|
62
|
+
attr_reader :configuration
|
63
|
+
|
64
|
+
def whitelisted_files path, whitelist
|
65
|
+
file_filter = whitelist.empty? ? %(#{path}/**/*) : %(#{path}/**/*{#{whitelist.join ","}})
|
66
|
+
Pathname.glob(file_filter).select(&:file?)
|
67
|
+
end
|
68
|
+
|
59
69
|
def update_file path, comments, action
|
60
70
|
Writer.new(path, comments).public_send action
|
61
|
-
|
71
|
+
info "Processed: #{path}."
|
72
|
+
rescue ArgumentError => error
|
73
|
+
formatted_message = error.message
|
74
|
+
formatted_message[0] = formatted_message[0].capitalize
|
75
|
+
error "#{formatted_message}: #{path}."
|
62
76
|
end
|
63
77
|
|
64
|
-
def
|
65
|
-
pathname = Pathname path
|
66
|
-
|
78
|
+
def update_files path, comments, whitelist, action
|
67
79
|
case
|
68
|
-
when
|
69
|
-
update_file
|
70
|
-
when
|
71
|
-
|
72
|
-
files.each { |file_path| update_file file_path, comments, action }
|
80
|
+
when path.file?
|
81
|
+
update_file path, comments, action
|
82
|
+
when path.directory?
|
83
|
+
whitelisted_files(path, whitelist).each { |file_path| update_file file_path, comments, action }
|
73
84
|
else
|
74
85
|
error "Invalid path: #{path}."
|
75
86
|
end
|
76
87
|
end
|
88
|
+
|
89
|
+
def write path, settings, action
|
90
|
+
pathname = Pathname path
|
91
|
+
comments = Array settings.dig(action).dig(:comments)
|
92
|
+
whitelist = Array settings.dig(action).dig(:whitelist)
|
93
|
+
|
94
|
+
update_files pathname, comments, whitelist, action
|
95
|
+
end
|
77
96
|
end
|
78
97
|
end
|
data/lib/pragmater/commenter.rb
CHANGED
@@ -21,8 +21,12 @@ module Pragmater
|
|
21
21
|
|
22
22
|
attr_reader :older, :newer, :formatter
|
23
23
|
|
24
|
+
def filter comments
|
25
|
+
Array(comments).select { |comment| comment =~ formatter.valid_formats }
|
26
|
+
end
|
27
|
+
|
24
28
|
def format comments
|
25
|
-
|
29
|
+
filter(comments).map { |comment| formatter.new(comment).format }
|
26
30
|
end
|
27
31
|
end
|
28
32
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "refinements/hash_extensions"
|
4
|
+
|
5
|
+
module Pragmater
|
6
|
+
# Default gem configuration with support for custom settings.
|
7
|
+
class Configuration
|
8
|
+
using Refinements::HashExtensions
|
9
|
+
attr_reader :settings
|
10
|
+
|
11
|
+
def self.defaults
|
12
|
+
{
|
13
|
+
add: {
|
14
|
+
comments: "",
|
15
|
+
whitelist: []
|
16
|
+
},
|
17
|
+
remove: {
|
18
|
+
comments: "",
|
19
|
+
whitelist: []
|
20
|
+
}
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize file_name = Identity.file_name, defaults: self.class.defaults
|
25
|
+
@file_name = file_name
|
26
|
+
@defaults = defaults
|
27
|
+
@settings = defaults.deep_merge load_settings
|
28
|
+
end
|
29
|
+
|
30
|
+
def local_file_path
|
31
|
+
File.join Dir.pwd, file_name
|
32
|
+
end
|
33
|
+
|
34
|
+
def global_file_path
|
35
|
+
File.join ENV["HOME"], file_name
|
36
|
+
end
|
37
|
+
|
38
|
+
def computed_file_path
|
39
|
+
File.exist?(local_file_path) ? local_file_path : global_file_path
|
40
|
+
end
|
41
|
+
|
42
|
+
def merge custom_settings
|
43
|
+
custom_settings[:add].delete_if { |_, value| value.empty? } if custom_settings.key?(:add)
|
44
|
+
custom_settings[:remove].delete_if { |_, value| value.empty? } if custom_settings.key?(:remove)
|
45
|
+
settings.deep_merge custom_settings
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
attr_reader :file_name, :defaults
|
51
|
+
|
52
|
+
def load_settings
|
53
|
+
yaml = YAML.load_file computed_file_path
|
54
|
+
yaml.is_a?(Hash) ? yaml : {}
|
55
|
+
rescue
|
56
|
+
defaults
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/pragmater/formatter.rb
CHANGED
@@ -16,45 +16,49 @@ module Pragmater
|
|
16
16
|
|
17
17
|
def self.pragma_format
|
18
18
|
/
|
19
|
-
\A
|
20
|
-
\#
|
21
|
-
\s?
|
22
|
-
\w+
|
23
|
-
\:
|
24
|
-
\s?
|
25
|
-
|
26
|
-
\Z
|
19
|
+
\A # Start of line.
|
20
|
+
\# # Start of comment.
|
21
|
+
\s? # Space - optional.
|
22
|
+
\w+ # Key - 1 or more word characters only.
|
23
|
+
\: # Key and value delimiter.
|
24
|
+
\s? # Space - optional.
|
25
|
+
[\w\-]+ # Value - 1 or more word or dash characters.
|
26
|
+
\Z # End of line.
|
27
27
|
/x
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
30
|
+
def self.valid_formats
|
31
|
+
Regexp.union shebang_format, pragma_format
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize string
|
35
|
+
@string = string
|
32
36
|
end
|
33
37
|
|
34
38
|
def format_shebang
|
35
|
-
return
|
39
|
+
return string unless string =~ self.class.shebang_format
|
36
40
|
|
37
|
-
_, path =
|
41
|
+
_, path = string.split "!"
|
38
42
|
"#! #{path.strip}"
|
39
43
|
end
|
40
44
|
|
41
45
|
def format_pragma
|
42
|
-
return
|
46
|
+
return string unless string =~ self.class.pragma_format
|
43
47
|
|
44
|
-
key, value =
|
48
|
+
key, value = string.split ":"
|
45
49
|
"# #{key.gsub(/\#\s?/, "")}: #{value.strip}"
|
46
50
|
end
|
47
51
|
|
48
52
|
def format
|
49
|
-
case
|
53
|
+
case string
|
50
54
|
when self.class.shebang_format then format_shebang
|
51
55
|
when self.class.pragma_format then format_pragma
|
52
|
-
else
|
56
|
+
else string
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
56
60
|
private
|
57
61
|
|
58
|
-
attr_reader :
|
62
|
+
attr_reader :string
|
59
63
|
end
|
60
64
|
end
|
data/lib/pragmater/identity.rb
CHANGED
data/lib/pragmater/writer.rb
CHANGED
@@ -13,11 +13,16 @@ module Pragmater
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def add
|
16
|
-
|
16
|
+
comments = format commenter.new(old_comments, new_comments).add
|
17
|
+
lines = comments + file_lines_without_comments
|
18
|
+
insert_spacing! lines, comments
|
19
|
+
write { lines.join }
|
17
20
|
end
|
18
21
|
|
19
22
|
def remove
|
20
|
-
|
23
|
+
lines = format(commenter.new(old_comments, new_comments).remove) + file_lines_without_comments
|
24
|
+
remove_spacing! lines
|
25
|
+
write { lines.join }
|
21
26
|
end
|
22
27
|
|
23
28
|
private
|
@@ -25,7 +30,7 @@ module Pragmater
|
|
25
30
|
attr_reader :file_path, :file_lines, :new_comments, :old_comments, :formatter, :commenter
|
26
31
|
|
27
32
|
def file_comments
|
28
|
-
file_lines.select { |line| line =~ formatter.
|
33
|
+
file_lines.select { |line| line =~ formatter.valid_formats }
|
29
34
|
end
|
30
35
|
|
31
36
|
def file_lines_without_comments
|
@@ -36,6 +41,17 @@ module Pragmater
|
|
36
41
|
lines.map { |line| "#{line}\n" }
|
37
42
|
end
|
38
43
|
|
44
|
+
def insert_spacing! lines, comments
|
45
|
+
return if comments.empty?
|
46
|
+
return if lines.size == 1
|
47
|
+
return if lines[comments.size] == "\n"
|
48
|
+
lines.insert comments.size, "\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
def remove_spacing! lines
|
52
|
+
lines.delete_at(0) if lines.first == "\n"
|
53
|
+
end
|
54
|
+
|
39
55
|
def write
|
40
56
|
File.open(file_path, "w") { |file| file.write yield }
|
41
57
|
end
|
data/lib/tasks/rspec.rake
CHANGED
data/lib/tasks/rubocop.rake
CHANGED
metadata
CHANGED
@@ -1,43 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pragmater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '0.19'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
26
|
+
version: '0.19'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: thor_plus
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '3.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: refinements
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +70,16 @@ dependencies:
|
|
56
70
|
name: gemsmith
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
75
|
+
version: '7.0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
82
|
+
version: '7.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: pry
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -278,9 +292,11 @@ files:
|
|
278
292
|
- lib/pragmater.rb
|
279
293
|
- lib/pragmater/cli.rb
|
280
294
|
- lib/pragmater/commenter.rb
|
295
|
+
- lib/pragmater/configuration.rb
|
281
296
|
- lib/pragmater/formatter.rb
|
282
297
|
- lib/pragmater/identity.rb
|
283
298
|
- lib/pragmater/writer.rb
|
299
|
+
- lib/tasks/console.rake
|
284
300
|
- lib/tasks/rspec.rake
|
285
301
|
- lib/tasks/rubocop.rake
|
286
302
|
homepage: https://github.com/bkuhlmann/pragmater
|