magic_frozen_string_literal 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +8 -5
- data/bin/magic_frozen_string_literal +1 -1
- data/lib/add_magic_comment.rb +18 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea34af2a4526f180999632150118a905ba15954a
|
4
|
+
data.tar.gz: c4c480187e8422e87c2ab91154360887b5e6a1b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2c791c80c142890733e68c55ab4606648e53ae473991d3f57da80a1209868d5fcba930362b8f4c9a69e015fe7dad25cfe8308c22a9f6b85074a92cae8ec9e47
|
7
|
+
data.tar.gz: df218232b077d02775b593bfbd94997a20882f79424efdceb4cd3e55dd9a46433a1bcc6af59dc1b2e8b71c7a423162d5e7699e99b791b95a9b94b79e158d282e
|
data/README.rdoc
CHANGED
@@ -13,19 +13,22 @@ Cloned from https://github.com/m-ryan/magic_encoding
|
|
13
13
|
|
14
14
|
== Usage
|
15
15
|
|
16
|
-
|
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
|
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
|
data/lib/add_magic_comment.rb
CHANGED
@@ -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
|
-
"
|
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
|
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
|
-
#
|
37
|
-
lines.
|
38
|
-
|
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)
|