Skip to content

Commit fcf0206

Browse files
Merge pull request opf#18356 from opf/copyright-rb-create-header
Enable copyright task to add missing headers
2 parents 376c632 + a654026 commit fcf0206

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

lib/tasks/copyright.rake

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
#-- copyright
24
# OpenProject is an open source project management software.
35
# Copyright (C) the OpenProject GmbH
@@ -101,6 +103,15 @@ namespace :copyright do
101103
end
102104
end
103105

106+
def missing_copyright_regexp(format)
107+
case format
108+
when :ruby, :rb
109+
/\A(?<shebang>#![^\n]+\n\n?)?(?<additional># frozen_string_literal: (?:true|false)\n\n?)?\n*/m
110+
else
111+
raise "Format #{format} is not yet supported for copyright creation"
112+
end
113+
end
114+
104115
def rewrite_copyright(ending, additional_excluded_globs, format, path, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
105116
regexp = options[:regex] || copyright_regexp(format)
106117
path = "." if path.nil?
@@ -119,6 +130,13 @@ namespace :copyright do
119130
file_content = File.read(file_name)
120131
if file_content.match(regexp)
121132
file_content.gsub!(regexp, "\\k<shebang>\\k<additional>#{copyright}")
133+
elsif options[:create]
134+
if file_content.include?("OpenProject is a fork of ChiliProject")
135+
puts "#{file_name} does not match regexp, but seems to have a copyright header!"
136+
next
137+
end
138+
139+
file_content.gsub!(missing_copyright_regexp(format), "\\k<shebang>\\k<additional>#{copyright}\n\n")
122140
else
123141
puts "#{file_name} does not match regexp. Missing copyright notice?"
124142
end
@@ -137,21 +155,22 @@ namespace :copyright do
137155
.gitignore
138156
]
139157

140-
rewrite_copyright("rb", [], :rb, args[:path], file_list:)
158+
rewrite_copyright("rb", [], :rb, args[:path], file_list:, create: true)
141159
end
142160

143161
desc "Update the copyright on .rb source files"
144162
task :update_rb, :path do |_task, args|
145163
excluded = %w[
146-
lib_static/plugins/{acts_as_tree,rfpdf,verification}
164+
lib_static/plugins/{acts_as_tree,rfpdf,verification}/**/*
165+
lib/chronic_duration.rb
147166
]
148167

149-
rewrite_copyright("rb", excluded, :rb, args[:path])
168+
rewrite_copyright("rb", excluded, :rb, args[:path], create: true)
150169
end
151170

152171
desc "Update the copyright on .rake source files"
153172
task :update_rake, :path do |_task, args|
154-
rewrite_copyright("rake", [], :rb, args[:path])
173+
rewrite_copyright("rake", [], :rb, args[:path], create: true)
155174
end
156175

157176
desc "Update the copyright on .yml source files"
@@ -161,27 +180,27 @@ namespace :copyright do
161180
modules/*/config/locales/crowdin/*.yml
162181
]
163182

164-
rewrite_copyright("yml", excluded, :rb, args[:path])
183+
rewrite_copyright("yml", excluded, :rb, args[:path], create: true)
165184
end
166185

167186
desc "Update the copyright on .yml.example source files"
168187
task :update_yml_example, :path do |_task, args|
169-
rewrite_copyright("yml.example", [], :rb, args[:path])
188+
rewrite_copyright("yml.example", [], :rb, args[:path], create: true)
170189
end
171190

172191
desc "Update the copyright on .rb.example source files"
173192
task :update_rb_example, :path do |_task, args|
174-
rewrite_copyright("rb.example", [], :rb, args[:path])
193+
rewrite_copyright("rb.example", [], :rb, args[:path], create: true)
175194
end
176195

177196
desc "Update the copyright on .rjs source files"
178197
task :update_rjs, :path do |_task, args|
179-
rewrite_copyright("rjs", [], :rb, args[:path])
198+
rewrite_copyright("rjs", [], :rb, args[:path], create: true)
180199
end
181200

182201
desc "Update the copyright on .feature source files"
183202
task :update_feature, :path do |_task, args|
184-
rewrite_copyright("feature", [], :rb, args[:path])
203+
rewrite_copyright("feature", [], :rb, args[:path], create: true)
185204
end
186205

187206
desc "Update the copyright on .css source files"

0 commit comments

Comments
 (0)