Path: blob/master/tools/dev/verify_encoding.rb
55632 views
#!/usr/bin/env ruby12require 'find'34def report_non_ascii(file_path)5has_errors = false6File.foreach(file_path).with_index(1) do |line, line_number|7line_utf8 = line.force_encoding('UTF-8')8next unless line_utf8.valid_encoding?910line_utf8.chars.each_with_index do |char, index|11unless char.ascii_only?12has_errors = true13puts "Error - non-ascii character found. #{file_path}: line #{line_number}, char #{index+1} => #{char.inspect}"14end15end16end17has_errors18end1920path_to_search = File.expand_path(File.join(__FILE__, '..', '..', '..'))21puts "Verifying binary encoding files in '#{path_to_search}' do not contain non-ASCII characters..."2223has_errors = false24ran_at_least_once = false2526# Walk all files27Find.find(path_to_search) do |path|28next unless File.file?(path)29next if path.include?('/vendor/')30next unless path.end_with?('.rb')3132# Only check files that declare binary encoding33first_two_lines = File.open(path, "r") { |f| f.each_line.take(2).join }34next unless first_two_lines =~ /coding:\s*binary/3536ran_at_least_once = true37has_errors |= report_non_ascii(path)38end3940if !ran_at_least_once41puts "Did not run on any files, did not find any files with binary encoding declaration. Please check the script and ensure it is looking for the correct encoding declaration."42exit(1)43end4445if has_errors46puts "Finished with errors. Please fix the above issues and run again."47exit(1)48end4950puts "Finished without errors"51525354