pragmater 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e3cd60cb96e801aa4fd3cc4e1c6598d2298eb8b
4
- data.tar.gz: f518b806aafe3e671c97651d739b59c81312fce5
3
+ metadata.gz: 019b606499c4c1acfa9567af9fb95091e3d6394e
4
+ data.tar.gz: 6565699d00d5b211e42b34af9370f9bdcbe99685
5
5
  SHA512:
6
- metadata.gz: 714d74c9f14f6a301325e61e662388a2a780f98589f858b73f6a4080ca9afe77a5f8f5db6d56e86e92b7d75aca48efaa9a710b94d83d6ab5218b6529c03b15bf
7
- data.tar.gz: fe04dadb9f1324af93dcaacf37bfdb50c9c8d8945f3f183170c1db3974f86c5b9d42b99d205383b331a55f95bd7d468180003e3d3ab6acc9d872d4c239b3f320
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 a easy way to add pragma comments to single or
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 `*.rb` and `*.rake` files.
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
+ [![asciicast](https://asciinema.org/a/34038.png)](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.my-website.com/gem-public.pem)
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 file
79
- extensions:
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
- -c, [--comments=one two three] # Pragma comments
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
 
@@ -1,5 +1,4 @@
1
- #!/usr/bin/ruby
2
- # frozen_string_literal: true
1
+ #! /usr/bin/ruby
3
2
 
4
3
  require "pragmater"
5
4
  require "pragmater/cli"
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "pragmater/commenter"
4
+ require "pragmater/configuration"
4
5
  require "pragmater/formatter"
5
6
  require "pragmater/identity"
6
7
  require "pragmater/writer"
@@ -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 :extensions, aliases: "-e", desc: "File extension whitelist", type: :array, default: [".rb", ".rake"]
25
+ method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
25
26
  def add path
26
- write path, options[:comments], options[:extensions], :add
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 :extensions, aliases: "-e", desc: "File extension whitelist", type: :array, default: [".rb", ".rake"]
34
+ method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
33
35
  def remove path
34
- write path, options[:comments], options[:extensions], :remove
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
- say "Updated: #{path}."
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 write path, comments, extensions, action
65
- pathname = Pathname path
66
-
78
+ def update_files path, comments, whitelist, action
67
79
  case
68
- when pathname.file?
69
- update_file pathname, comments, action
70
- when pathname.directory?
71
- files = Pathname.glob %(#{pathname}/**/*{#{extensions.join ","}})
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
@@ -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
- Array(comments).map { |comment| formatter.new(comment).format }
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
@@ -16,45 +16,49 @@ module Pragmater
16
16
 
17
17
  def self.pragma_format
18
18
  /
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
- .+ # Value - 1 or more characters.
26
- \Z # End of line.
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 initialize comment
31
- @comment = comment
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 comment unless comment =~ self.class.shebang_format
39
+ return string unless string =~ self.class.shebang_format
36
40
 
37
- _, path = comment.split "!"
41
+ _, path = string.split "!"
38
42
  "#! #{path.strip}"
39
43
  end
40
44
 
41
45
  def format_pragma
42
- return comment unless comment =~ self.class.pragma_format
46
+ return string unless string =~ self.class.pragma_format
43
47
 
44
- key, value = comment.split ":"
48
+ key, value = string.split ":"
45
49
  "# #{key.gsub(/\#\s?/, "")}: #{value.strip}"
46
50
  end
47
51
 
48
52
  def format
49
- case comment
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 comment
56
+ else string
53
57
  end
54
58
  end
55
59
 
56
60
  private
57
61
 
58
- attr_reader :comment
62
+ attr_reader :string
59
63
  end
60
64
  end
@@ -12,7 +12,7 @@ module Pragmater
12
12
  end
13
13
 
14
14
  def self.version
15
- "0.1.0"
15
+ "1.0.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -13,11 +13,16 @@ module Pragmater
13
13
  end
14
14
 
15
15
  def add
16
- write { (format(commenter.new(old_comments, new_comments).add) + file_lines_without_comments).join }
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
- write { (format(commenter.new(old_comments, new_comments).remove) + file_lines_without_comments).join }
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.shebang_format || line =~ formatter.pragma_format }
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
@@ -0,0 +1,7 @@
1
+ desc "Open IRB console for gem development environment"
2
+ task :console do
3
+ require "irb"
4
+ require "pragmater"
5
+ ARGV.clear
6
+ IRB.start
7
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  begin
4
2
  require "rspec/core/rake_task"
5
3
  RSpec::Core::RakeTask.new(:spec)
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  begin
4
2
  require "rubocop/rake_task"
5
3
  RuboCop::RakeTask.new
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: 0.1.0
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: 2015-12-28 00:00:00.000000000 Z
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