-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateHeaders.groovy
70 lines (61 loc) · 2.18 KB
/
updateHeaders.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env groovy
/**
* This script updates the copyright headers of all .xtend and .java files in
* the repository. It also replaces Windows line endings with Unix line endings.
*/
header = """\
/*******************************************************************************
* Copyright (c) 2012 - 2018 Signal Iduna Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Signal Iduna Corporation - initial API and implementation
* akquinet AG
* itemis AG
*******************************************************************************/
"""
encoding = "UTF-8"
firstHeaderLine = $/^/\*.*/$
consecutiveHeaderLine = $/\r?\n\s*\*.*/$
lastHeaderLine = $/\r?\n\s*\**//$
headerPattern = ~"$firstHeaderLine($consecutiveHeaderLine)*$lastHeaderLine(\r?\n)*"
// Define a FileFilter to exclude certain directories and filter for .xtend and .java
def excluded = ["bin", "target", "xtend-gen", "src-gen"]
filter = (FileFilter) { file ->
if (file.isDirectory()) {
return !excluded.contains(file.name) && !file.isHidden()
}
return file.name.endsWith(".xtend") || file.name.endsWith(".java")
}
updateHeaders(new File("."))
def void updateHeaders(File file) {
file.listFiles(filter).each { child ->
if (child.isDirectory()) {
updateHeaders(child)
} else {
doUpdate(child)
}
}
}
/**
* Updates the copyright header of the passed file.
*/
def void doUpdate(File file) {
def content = file.getText(encoding)
def matcher = content =~ headerPattern
if (matcher) {
def match = matcher.group()
if (match.contains("Signal Iduna Corporation and others")) {
content = content.replace(match, header)
} else {
// Header found but was not the Signal Iduna header - don't replace it
println "Not updating file header of $file"
}
} else {
content = "$header$content"
}
file.setText(content.replaceAll("\r\n", "\n"), encoding)
}