Last active
December 13, 2017 01:18
-
-
Save itarato/0b1a2eec47eff8ab8a4904f0a547aef6 to your computer and use it in GitHub Desktop.
Adds frozen headers to all ruby files in a folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Call: ruby add_frozen_heading.rb PATH_TO_FOLDER | |
dir = ARGV[0] || '' | |
if !Dir.exist?(dir) | |
puts 'Missing folder.' | |
exit | |
end | |
def check_headers(dir) | |
# puts "Inspect #{dir}" | |
Dir.foreach(dir) do |subdir| | |
next if subdir == '.' || subdir == '..' | |
fullpath = "#{dir}/#{subdir}" | |
if File.stat(fullpath).directory? | |
check_headers(fullpath) | |
elsif fullpath[-3..-1] == '.rb' | |
begin | |
first_line = File.open(fullpath, &:readline) | |
unless first_line.strip == '# frozen_string_literal: true' | |
content = File.read(fullpath) | |
File.open(fullpath, 'wb') { |f| f.write("# frozen_string_literal: true\n#{content}")} | |
puts "Patched: #{fullpath}" | |
end | |
rescue | |
end | |
end | |
end | |
end | |
check_headers(dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment