Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds frozen headers to all ruby files in a folder.
# 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