Path: blob/master/tools/maintenance/copyright_update.rb
1874 views
require 'yaml'1require 'logger'23# Set up logging4@log = Logger.new(STDOUT)5@log.level = Logger::INFO6log_file = File.open('copyright_update.log', 'w')7@log_file_logger = Logger.new(log_file)8@log_file_logger.level = Logger::INFO910def update_copyright(file_path, copyright_pattern, new_copyright)11@log.info("Processing file: #{file_path}")12@log_file_logger.info("Processing file: #{file_path}")1314content = File.read(file_path)15if content.empty?16@log.info("File is empty, no copyright update needed: #{file_path}")17@log_file_logger.info("File is empty, no copyright update needed: #{file_path}")18elsif content.match?(copyright_pattern)19updated_content = content.gsub(copyright_pattern, "\\1#{new_copyright}")20if updated_content != content21File.write(file_path, updated_content)22@log.info("Updated copyright in #{file_path}")23@log_file_logger.info("Updated copyright in #{file_path}")24end25else26@log.warn("Copyright pattern not found in #{file_path}")27@log_file_logger.warn("Copyright pattern not found in #{file_path}")28end29rescue => e30@log.error("Error processing file #{file_path}: #{e.message}")31@log_file_logger.error("Error processing file #{file_path}: #{e.message}")32end3334# Regex to match "Copyright (c) 2006-YYYY" or "(C) 2006-YYYY"35# Captures the prefix so we can replace only the year part if needed,36# or better yet, replace the whole match but preserve the "Copyright (c)" part.37copyright_pattern = /((?:Copyright \(c\) |\(C\) )2006-)20\d{2}/38new_year = '2026'3940Dir.glob("../../**/*.{rb,js,yaml,html,md,txt,css,c,nasm,java,php,as}").each do |file|41next if File.basename(file) == 'copyright_update.rb'42update_copyright(file, copyright_pattern, new_year)43end4445# Handle files without extensions, excluding copyright_update.rb46Dir.glob("../../**/*").reject { |f|47File.directory?(f) || File.extname(f) != '' || File.basename(f) == 'copyright_update.rb'48}.each do |file|49update_copyright(file, copyright_pattern, new_year)50end5152@log.info("Copyright update process completed.")53@log_file_logger.info("Copyright update process completed.")54log_file.close5556