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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 80af2fba8814ca3b424f9ce5e49db53f539a15a3
4
- data.tar.gz: d38df55bdcb2879fbaf57d566d9cdb4c2f44d4ef
2
+ SHA256:
3
+ metadata.gz: f54032084880af90e371bb807f2128440ab0a843e4a31e896c2ce27704feabdf
4
+ data.tar.gz: 9354d05727956a10a7cf0f5a7d71959ac27e904a4e5ea40262d0c4895658569f
5
5
  SHA512:
6
- metadata.gz: 224f8a43b2a67b80a79601f1ed7fa3ad8dafe72e0eac831e0098b8e4edd1a02a1ea08839d5e5d02a6175c1b15110a74ec62a12cd519ddc667ec0e7f8acacab38
7
- data.tar.gz: 5c61397c08ad8b02280b64e5e35728e06e02da0c4f092b4ce337b468ed83b4ac4070e911fe9dec2890ed7330646ee988ca320129861f6557e228b5cc16ca528d
6
+ metadata.gz: fa79c8714e7305a0eb0924032e4be3cf212816ade15919d450ad156eae13c4bae0cc453633b6438824a4a7bfa78fba9f8dbd3d4e6a32d10855e4d581c950bd1a
7
+ data.tar.gz: 70ec12b6d6ad8e6d60843bae33585170f3ec58c6c6f78c2fff6c369f77fe25c13c6eca52a72e323a91bcb23e5d0bfff1ba096ea5be1bcbcac3ae9da40a544c64
@@ -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", "*.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
- ".erb" files will have the comment inside <% ... %> and no blank line after.)
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
@@ -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,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
- "rake" => "# #{MAGIC_COMMENT}\n\n",
14
- "rabl" => "# #{MAGIC_COMMENT}\n\n",
15
- "jbuilder" => "# #{MAGIC_COMMENT}\n\n",
16
- "haml" => "-# #{MAGIC_COMMENT}\n",
17
- "erb" => "<%# #{MAGIC_COMMENT} %>\n"
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 |ext, comment|
25
- filename_pattern = File.join(directory, "**", "*.#{ext}")
29
+ EXTENSION_COMMENTS.each do |pattern, comment|
30
+ filename_pattern = File.join(directory, "**", "#{pattern}")
26
31
  Dir.glob(filename_pattern).each do |filename|
27
- next unless File.size(filename) > 0
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 encoding comment(s)
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
- # set current encoding
37
- lines.insert(0, comment)
38
- count += 1
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.puts(lines.join)
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.0.0
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: 2018-03-18 00:00:00.000000000 Z
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: '10.0'
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: '10.0'
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
- rubyforge_project:
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
data/CHANGELOG DELETED
@@ -1,8 +0,0 @@
1
- ## 0.0.1 (August 3, 2010)
2
-
3
- Initial Version
4
-
5
- ## 0.0.2 (August 3, 2010)
6
-
7
- Existing magic comments are now erased before the file is prepended with the desired magic comment
8
- Bugfixes