magic_frozen_string_literal 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.rdoc +9 -6
- data/bin/magic_frozen_string_literal +1 -1
- data/lib/add_magic_comment.rb +35 -16
- metadata +23 -10
- data/CHANGELOG +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f54032084880af90e371bb807f2128440ab0a843e4a31e896c2ce27704feabdf
|
4
|
+
data.tar.gz: 9354d05727956a10a7cf0f5a7d71959ac27e904a4e5ea40262d0c4895658569f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa79c8714e7305a0eb0924032e4be3cf212816ade15919d450ad156eae13c4bae0cc453633b6438824a4a7bfa78fba9f8dbd3d4e6a32d10855e4d581c950bd1a
|
7
|
+
data.tar.gz: 70ec12b6d6ad8e6d60843bae33585170f3ec58c6c6f78c2fff6c369f77fe25c13c6eca52a72e323a91bcb23e5d0bfff1ba096ea5be1bcbcac3ae9da40a544c64
|
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 "
|
20
|
+
this will prepend every "*.rb", "*.ru", "Rakefile", "*.rake", "Gemfile", "*.gemspec", "*.rabl", "*.jbuilder", "*.haml", "*.slim" 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.
|
26
|
-
|
25
|
+
(".haml" and ".slim" files will have a "-" prefix before the comment and no blank line after.)
|
26
|
+
|
27
|
+
The <directory> parameter is optional. It defaults to the current working directory.
|
28
|
+
|
27
29
|
|
28
30
|
Notes:
|
29
|
-
- existing frozen_string_literal magic comments are replaced
|
31
|
+
- existing +frozen_string_literal+ magic comments are replaced
|
30
32
|
- the rest of the file remains unchanged
|
31
33
|
- empty files are not touched
|
34
|
+
- if the file starts with #! (shebang), that line is preserved and the magic comment is added just below it
|
data/lib/add_magic_comment.rb
CHANGED
@@ -7,38 +7,53 @@ 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
|
-
"rb" => "# #{MAGIC_COMMENT}\n\n",
|
13
|
-
"
|
14
|
-
"
|
15
|
-
"
|
16
|
-
"
|
17
|
-
"
|
13
|
+
"*.rb" => "# #{MAGIC_COMMENT}\n\n",
|
14
|
+
"*.ru" => "# #{MAGIC_COMMENT}\n\n",
|
15
|
+
"Rakefile" => "# #{MAGIC_COMMENT}\n\n",
|
16
|
+
"*.rake" => "# #{MAGIC_COMMENT}\n\n",
|
17
|
+
"Gemfile" => "# #{MAGIC_COMMENT}\n\n",
|
18
|
+
"*.gemspec" => "# #{MAGIC_COMMENT}\n\n",
|
19
|
+
"*.rabl" => "# #{MAGIC_COMMENT}\n\n",
|
20
|
+
"*.jbuilder" => "# #{MAGIC_COMMENT}\n\n",
|
21
|
+
"*.haml" => "-# #{MAGIC_COMMENT}\n",
|
22
|
+
"*.slim" => "-# #{MAGIC_COMMENT}\n"
|
18
23
|
}
|
19
24
|
|
20
25
|
def self.process(argv)
|
21
26
|
directory = argv.first || Dir.pwd
|
22
27
|
|
23
28
|
count = 0
|
24
|
-
EXTENSION_COMMENTS.each do |
|
25
|
-
filename_pattern = File.join(directory, "**", "
|
29
|
+
EXTENSION_COMMENTS.each do |pattern, comment|
|
30
|
+
filename_pattern = File.join(directory, "**", "#{pattern}")
|
26
31
|
Dir.glob(filename_pattern).each do |filename|
|
27
|
-
|
28
|
-
File.open(filename, "r+") do |file|
|
32
|
+
File.open(filename, "rb+") do |file|
|
29
33
|
lines = file.readlines
|
34
|
+
newline = detect_newline(lines.first)
|
35
|
+
next unless lines.any?
|
36
|
+
count += 1
|
37
|
+
|
38
|
+
if lines.first =~ SHEBANG_PATTERN
|
39
|
+
shebang = lines.shift
|
40
|
+
end
|
30
41
|
|
31
|
-
# remove current
|
32
|
-
while lines.first && lines.first.match(MAGIC_COMMENT_PATTERN) || lines.first.match(EMPTY_LINE_PATTERN)
|
42
|
+
# remove current magic comment(s)
|
43
|
+
while lines.first && (lines.first.match(MAGIC_COMMENT_PATTERN) || lines.first.match(EMPTY_LINE_PATTERN))
|
33
44
|
lines.shift
|
34
45
|
end
|
35
46
|
|
36
|
-
#
|
37
|
-
lines.
|
38
|
-
|
47
|
+
# add magic comment as the first line
|
48
|
+
lines.unshift(comment.gsub("\n", newline))
|
49
|
+
|
50
|
+
# put shebang back
|
51
|
+
if shebang
|
52
|
+
lines.unshift(shebang)
|
53
|
+
end
|
39
54
|
|
40
55
|
file.pos = 0
|
41
|
-
file.
|
56
|
+
file.print(*lines)
|
42
57
|
file.truncate(file.pos)
|
43
58
|
end
|
44
59
|
end
|
@@ -46,4 +61,8 @@ module AddMagicComment
|
|
46
61
|
|
47
62
|
puts "Magic comments added to #{count} source file(s)"
|
48
63
|
end
|
64
|
+
|
65
|
+
def self.detect_newline(line)
|
66
|
+
(line[/\R/] if line) || $/
|
67
|
+
end
|
49
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magic_frozen_string_literal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Kelley after Jared Roesch after Manuel Ryan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,30 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
description:
|
42
56
|
email:
|
43
57
|
- colin@invoca.com
|
@@ -46,7 +60,6 @@ executables:
|
|
46
60
|
extensions: []
|
47
61
|
extra_rdoc_files: []
|
48
62
|
files:
|
49
|
-
- CHANGELOG
|
50
63
|
- LICENCE
|
51
64
|
- README.rdoc
|
52
65
|
- bin/magic_frozen_string_literal
|
@@ -54,7 +67,8 @@ files:
|
|
54
67
|
homepage: https://github.com/Invoca/magic_frozen_string_literal
|
55
68
|
licenses:
|
56
69
|
- MIT
|
57
|
-
metadata:
|
70
|
+
metadata:
|
71
|
+
allowed_push_host: https://rubygems.org
|
58
72
|
post_install_message:
|
59
73
|
rdoc_options: []
|
60
74
|
require_paths:
|
@@ -70,8 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
84
|
- !ruby/object:Gem::Version
|
71
85
|
version: '0'
|
72
86
|
requirements: []
|
73
|
-
|
74
|
-
rubygems_version: 2.2.2
|
87
|
+
rubygems_version: 3.0.3
|
75
88
|
signing_key:
|
76
89
|
specification_version: 4
|
77
90
|
summary: 'Easily add magic comments ''# frozen_string_literal: true'' followed by
|