Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/tools/maintenance/copyright_update.rb
1874 views
1
require 'yaml'
2
require 'logger'
3
4
# Set up logging
5
@log = Logger.new(STDOUT)
6
@log.level = Logger::INFO
7
log_file = File.open('copyright_update.log', 'w')
8
@log_file_logger = Logger.new(log_file)
9
@log_file_logger.level = Logger::INFO
10
11
def update_copyright(file_path, copyright_pattern, new_copyright)
12
@log.info("Processing file: #{file_path}")
13
@log_file_logger.info("Processing file: #{file_path}")
14
15
content = File.read(file_path)
16
if content.empty?
17
@log.info("File is empty, no copyright update needed: #{file_path}")
18
@log_file_logger.info("File is empty, no copyright update needed: #{file_path}")
19
elsif content.match?(copyright_pattern)
20
updated_content = content.gsub(copyright_pattern, "\\1#{new_copyright}")
21
if updated_content != content
22
File.write(file_path, updated_content)
23
@log.info("Updated copyright in #{file_path}")
24
@log_file_logger.info("Updated copyright in #{file_path}")
25
end
26
else
27
@log.warn("Copyright pattern not found in #{file_path}")
28
@log_file_logger.warn("Copyright pattern not found in #{file_path}")
29
end
30
rescue => e
31
@log.error("Error processing file #{file_path}: #{e.message}")
32
@log_file_logger.error("Error processing file #{file_path}: #{e.message}")
33
end
34
35
# Regex to match "Copyright (c) 2006-YYYY" or "(C) 2006-YYYY"
36
# Captures the prefix so we can replace only the year part if needed,
37
# or better yet, replace the whole match but preserve the "Copyright (c)" part.
38
copyright_pattern = /((?:Copyright \(c\) |\(C\) )2006-)20\d{2}/
39
new_year = '2026'
40
41
Dir.glob("../../**/*.{rb,js,yaml,html,md,txt,css,c,nasm,java,php,as}").each do |file|
42
next if File.basename(file) == 'copyright_update.rb'
43
update_copyright(file, copyright_pattern, new_year)
44
end
45
46
# Handle files without extensions, excluding copyright_update.rb
47
Dir.glob("../../**/*").reject { |f|
48
File.directory?(f) || File.extname(f) != '' || File.basename(f) == 'copyright_update.rb'
49
}.each do |file|
50
update_copyright(file, copyright_pattern, new_year)
51
end
52
53
@log.info("Copyright update process completed.")
54
@log_file_logger.info("Copyright update process completed.")
55
log_file.close
56