magic_frozen_string_literal 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80af2fba8814ca3b424f9ce5e49db53f539a15a3
4
- data.tar.gz: d38df55bdcb2879fbaf57d566d9cdb4c2f44d4ef
3
+ metadata.gz: ea34af2a4526f180999632150118a905ba15954a
4
+ data.tar.gz: c4c480187e8422e87c2ab91154360887b5e6a1b5
5
5
  SHA512:
6
- metadata.gz: 224f8a43b2a67b80a79601f1ed7fa3ad8dafe72e0eac831e0098b8e4edd1a02a1ea08839d5e5d02a6175c1b15110a74ec62a12cd519ddc667ec0e7f8acacab38
7
- data.tar.gz: 5c61397c08ad8b02280b64e5e35728e06e02da0c4f092b4ce337b468ed83b4ac4070e911fe9dec2890ed7330646ee988ca320129861f6557e228b5cc16ca528d
6
+ metadata.gz: d2c791c80c142890733e68c55ab4606648e53ae473991d3f57da80a1209868d5fcba930362b8f4c9a69e015fe7dad25cfe8308c22a9f6b85074a92cae8ec9e47
7
+ data.tar.gz: df218232b077d02775b593bfbd94997a20882f79424efdceb4cd3e55dd9a46433a1bcc6af59dc1b2e8b71c7a423162d5e7699e99b791b95a9b94b79e158d282e
@@ -13,19 +13,22 @@ Cloned from https://github.com/m-ryan/magic_encoding
13
13
 
14
14
  == Usage
15
15
 
16
- you can call the tool from the console with default parameters like so
16
+ Run the tool from the command line:
17
17
 
18
- magic_frozen_string_literal
18
+ magic_frozen_string_literal <directory>
19
19
 
20
- this will prepend every ".rb", ".rake", ".rabl", ".jbuilder", ".haml", ".erb" file in the working directory (recursively)
20
+ this will prepend every ".rb", ".rake", ".rabl", ".jbuilder", ".haml", ".slim", ".erb" file in the given <directory> (recursively)
21
21
  with the following magic comment followed by a blank line:
22
22
 
23
23
  # frozen_string_literal: true
24
24
 
25
- (".haml" files will have a "-" prefix before comment and no blank line after.
25
+ (".haml" and ".slim" files will have a "-" prefix before comment and no blank line after.
26
26
  ".erb" files will have the comment inside <% ... %> and no blank line after.)
27
27
 
28
+ The <directory> parameter is optional. It defaults to the current working directory.
29
+
30
+
28
31
  Notes:
29
- - existing frozen_string_literal magic comments are replaced
32
+ - existing +frozen_string_literal+ magic comments are replaced
30
33
  - the rest of the file remains unchanged
31
34
  - empty files are not touched
@@ -2,6 +2,6 @@
2
2
 
3
3
  # A simple tool to prepend magic '# frozen_string_literal: true' comments to multiple ".rb" files
4
4
 
5
- require 'add_magic_comment'
5
+ require_relative '../lib/add_magic_comment'
6
6
 
7
7
  AddMagicComment.process(ARGV)
@@ -7,6 +7,7 @@ module AddMagicComment
7
7
  MAGIC_COMMENT_PATTERN = /^(-|(<%))?#\s*#{MAGIC_COMMENT_PREFIX}\s*(%>)?/
8
8
  MAGIC_COMMENT = "#{MAGIC_COMMENT_PREFIX}: true".freeze
9
9
  EMPTY_LINE_PATTERN = /^\s$/
10
+ SHEBANG_PATTERN = /^#!/
10
11
 
11
12
  EXTENSION_COMMENTS = {
12
13
  "rb" => "# #{MAGIC_COMMENT}\n\n",
@@ -14,7 +15,8 @@ module AddMagicComment
14
15
  "rabl" => "# #{MAGIC_COMMENT}\n\n",
15
16
  "jbuilder" => "# #{MAGIC_COMMENT}\n\n",
16
17
  "haml" => "-# #{MAGIC_COMMENT}\n",
17
- "erb" => "<%# #{MAGIC_COMMENT} %>\n"
18
+ "slim" => "-# #{MAGIC_COMMENT}\n",
19
+ "erb" => "<%# #{MAGIC_COMMENT} -%>\n"
18
20
  }
19
21
 
20
22
  def self.process(argv)
@@ -24,18 +26,27 @@ module AddMagicComment
24
26
  EXTENSION_COMMENTS.each do |ext, comment|
25
27
  filename_pattern = File.join(directory, "**", "*.#{ext}")
26
28
  Dir.glob(filename_pattern).each do |filename|
27
- next unless File.size(filename) > 0
28
29
  File.open(filename, "r+") do |file|
29
30
  lines = file.readlines
31
+ next unless lines.any?
32
+ count += 1
33
+
34
+ if lines.first =~ SHEBANG_PATTERN
35
+ shebang = lines.shift
36
+ end
30
37
 
31
- # remove current encoding comment(s)
32
- while lines.first && lines.first.match(MAGIC_COMMENT_PATTERN) || lines.first.match(EMPTY_LINE_PATTERN)
38
+ # remove current magic comment(s)
39
+ while lines.first && (lines.first.match(MAGIC_COMMENT_PATTERN) || lines.first.match(EMPTY_LINE_PATTERN))
33
40
  lines.shift
34
41
  end
35
42
 
36
- # set current encoding
37
- lines.insert(0, comment)
38
- count += 1
43
+ # add magic comment as the first line
44
+ lines.unshift(comment)
45
+
46
+ # put shebang back
47
+ if shebang
48
+ lines.unshift(shebang)
49
+ end
39
50
 
40
51
  file.pos = 0
41
52
  file.puts(lines.join)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_frozen_string_literal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Kelley after Jared Roesch after Manuel Ryan