2020-01-27 12:05:34 -05:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-09-08 04:08:51 -04:00
|
|
|
import org.apache.rat.Defaults
|
|
|
|
import org.apache.rat.document.impl.FileDocument
|
|
|
|
import org.apache.rat.api.MetaData
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import org.gradle.internal.logging.progress.ProgressLoggerFactory
|
|
|
|
import org.gradle.internal.logging.progress.ProgressLogger
|
2020-01-10 06:02:30 -05:00
|
|
|
|
2020-08-31 06:52:04 -04:00
|
|
|
buildscript {
|
|
|
|
repositories {
|
|
|
|
mavenCentral()
|
2020-01-15 03:55:41 -05:00
|
|
|
}
|
2020-01-10 06:02:30 -05:00
|
|
|
|
2020-01-15 03:55:41 -05:00
|
|
|
dependencies {
|
2020-08-31 06:52:04 -04:00
|
|
|
classpath "org.apache.rat:apache-rat:${scriptDepVersions['apache-rat']}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 04:08:51 -04:00
|
|
|
def extensions = [
|
|
|
|
'adoc',
|
|
|
|
'bat',
|
|
|
|
'cmd',
|
|
|
|
'css',
|
|
|
|
'g4',
|
|
|
|
'gradle',
|
|
|
|
'groovy',
|
|
|
|
'html',
|
|
|
|
'java',
|
|
|
|
'jflex',
|
|
|
|
'jj',
|
|
|
|
'js',
|
|
|
|
'json',
|
|
|
|
'md',
|
|
|
|
'mdtext',
|
|
|
|
'pl',
|
|
|
|
'policy',
|
|
|
|
'properties',
|
|
|
|
'py',
|
|
|
|
'sh',
|
|
|
|
'template',
|
2021-09-30 09:13:42 -04:00
|
|
|
'txt',
|
2020-09-08 04:08:51 -04:00
|
|
|
'vm',
|
|
|
|
'xml',
|
|
|
|
'xsl',
|
|
|
|
]
|
|
|
|
|
2021-11-03 12:19:24 -04:00
|
|
|
// Create source validation task local to each project
|
|
|
|
allprojects {
|
2020-09-08 04:08:51 -04:00
|
|
|
task validateSourcePatterns(type: ValidateSourcePatternsTask) { task ->
|
|
|
|
group = 'Verification'
|
|
|
|
description = 'Validate Source Patterns'
|
|
|
|
|
|
|
|
sourceFiles = fileTree(projectDir) {
|
|
|
|
extensions.each{
|
|
|
|
include "**/*.${it}"
|
|
|
|
}
|
2021-11-01 16:27:01 -04:00
|
|
|
|
2020-09-08 04:08:51 -04:00
|
|
|
// Don't go into child projects (scanned separately).
|
|
|
|
childProjects.keySet().each{
|
|
|
|
exclude "${it}/**"
|
|
|
|
}
|
|
|
|
|
|
|
|
// default excludes.
|
2021-11-03 12:19:24 -04:00
|
|
|
exclude '**/build/**'
|
2021-09-30 09:19:19 -04:00
|
|
|
exclude '**/.idea/**'
|
2023-03-06 13:17:37 -05:00
|
|
|
exclude '**/.gradle/**'
|
2021-09-30 09:13:42 -04:00
|
|
|
|
2021-11-03 12:19:24 -04:00
|
|
|
if (project == rootProject) {
|
|
|
|
// ourselves :-)
|
|
|
|
exclude 'gradle/validation/validate-source-patterns.gradle'
|
|
|
|
} else {
|
|
|
|
// ignore txt files in source resources and tests.
|
|
|
|
exclude 'src/**/*.txt'
|
|
|
|
}
|
2020-09-08 04:08:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add source validation to per-project checks as well.
|
|
|
|
check.dependsOn validateSourcePatterns
|
2021-11-03 12:19:24 -04:00
|
|
|
|
|
|
|
// Ensure validation runs prior to any compilation task. This also means
|
|
|
|
// no executable code can leak out to other modules.
|
|
|
|
tasks.withType(JavaCompile).configureEach {
|
|
|
|
mustRunAfter validateSourcePatterns
|
|
|
|
}
|
2020-09-08 04:08:51 -04:00
|
|
|
}
|
|
|
|
|
2021-03-18 06:04:45 -04:00
|
|
|
configure(project(':lucene:benchmark')) {
|
|
|
|
project.tasks.withType(ValidateSourcePatternsTask) {
|
2021-03-22 07:22:39 -04:00
|
|
|
sourceFiles.exclude 'data/**'
|
2022-03-23 15:51:28 -04:00
|
|
|
sourceFiles.exclude 'work/**'
|
2021-11-01 16:27:01 -04:00
|
|
|
|
2021-09-30 09:13:42 -04:00
|
|
|
// Known .txt offenders.
|
|
|
|
sourceFiles.exclude '**/reuters.first20.lines.txt', '**/trecQRels.txt'
|
2020-09-08 04:08:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 06:52:04 -04:00
|
|
|
|
2021-11-01 16:27:01 -04:00
|
|
|
@CacheableTask
|
2020-08-31 06:52:04 -04:00
|
|
|
class ValidateSourcePatternsTask extends DefaultTask {
|
2022-02-13 12:43:13 -05:00
|
|
|
private static final Object ratBug = new Object()
|
2020-09-08 04:08:51 -04:00
|
|
|
private ProgressLoggerFactory progressLoggerFactory
|
2020-08-31 06:52:04 -04:00
|
|
|
|
|
|
|
@InputFiles
|
2021-11-01 16:27:01 -04:00
|
|
|
@PathSensitive(PathSensitivity.RELATIVE)
|
|
|
|
@IgnoreEmptyDirectories
|
2020-09-08 04:08:51 -04:00
|
|
|
FileTree sourceFiles
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
ValidateSourcePatternsTask(ProgressLoggerFactory progressLoggerFactory) {
|
|
|
|
this.progressLoggerFactory = progressLoggerFactory
|
|
|
|
}
|
2021-11-01 16:27:01 -04:00
|
|
|
|
2020-08-31 06:52:04 -04:00
|
|
|
@TaskAction
|
|
|
|
public void check() {
|
|
|
|
def invalidPatterns = [
|
|
|
|
(~$/@author\b/$) : '@author javadoc tag',
|
|
|
|
(~$/(?i)\bno(n|)commit\b/$) : 'nocommit',
|
|
|
|
(~$/\bTOOD:/$) : 'TOOD instead TODO',
|
|
|
|
(~$/\t/$) : 'tabs instead spaces',
|
2021-11-03 12:19:24 -04:00
|
|
|
(~$/[\u202A-\u202E\u2066-\u2069]/$) : 'misuse of RTL/LTR (https://trojansource.codes)',
|
2020-08-31 06:52:04 -04:00
|
|
|
(~$/\Q/**\E((?:\s)|(?:\*))*\Q{@inheritDoc}\E((?:\s)|(?:\*))*\Q*/\E/$) : '{@inheritDoc} on its own is unnecessary',
|
|
|
|
(~$/\$$(?:LastChanged)?Date\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$(?:(?:LastChanged)?Revision|Rev)\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$(?:LastChangedBy|Author)\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$(?:Head)?URL\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$Id\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$Header\b/$) : 'svn keyword',
|
|
|
|
(~$/\$$Source\b/$) : 'svn keyword',
|
|
|
|
(~$/^\uFEFF/$) : 'UTF-8 byte order mark',
|
|
|
|
(~$/import java\.lang\.\w+;/$) : 'java.lang import is unnecessary'
|
|
|
|
]
|
|
|
|
|
|
|
|
// Python and others merrily use var declarations, this is a problem _only_ in Java at least for 8x where we're forbidding var declarations
|
|
|
|
def invalidJavaOnlyPatterns = [
|
2022-11-18 10:42:59 -05:00
|
|
|
(~$/\n\s*var\s+.*=.*<>.*/$) : 'Diamond operators should not be used with var',
|
|
|
|
(~$/import\s+\w+(\.\w+)\.\*;/$) : 'Expand wildcard imports into explicit imports'
|
2020-08-31 06:52:04 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
def violations = new TreeSet();
|
|
|
|
def reportViolation = { f, name ->
|
2022-01-05 14:42:02 -05:00
|
|
|
String msg = String.format(Locale.ROOT, "%s: %s", f, name)
|
|
|
|
logger.error(msg)
|
|
|
|
violations.add(msg)
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def javadocsPattern = ~$/(?sm)^\Q/**\E(.*?)\Q*/\E/$;
|
|
|
|
def javaCommentPattern = ~$/(?sm)^\Q/*\E(.*?)\Q*/\E/$;
|
|
|
|
def xmlCommentPattern = ~$/(?sm)\Q<!--\E(.*?)\Q-->\E/$;
|
|
|
|
def lineSplitter = ~$/[\r\n]+/$;
|
|
|
|
def packagePattern = ~$/(?m)^\s*package\s+org\.apache.*;/$;
|
|
|
|
def xmlTagPattern = ~$/(?m)\s*<[a-zA-Z].*/$;
|
|
|
|
def validSPINameJavadocTag = ~$/(?s)\s*\*\s*@lucene\.spi\s+\{@value #NAME\}/$;
|
|
|
|
|
|
|
|
def isLicense = { matcher, ratDocument ->
|
2022-02-13 12:43:13 -05:00
|
|
|
// See LUCENE-10419 - rat is not thread safe.
|
|
|
|
synchronized (ratBug) {
|
|
|
|
def licenseMatcher = Defaults.createDefaultMatcher();
|
|
|
|
licenseMatcher.reset()
|
|
|
|
return lineSplitter.split(matcher.group(1)).any { licenseMatcher.match(ratDocument, it) }
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def checkLicenseHeaderPrecedes = { f, description, contentPattern, commentPattern, text, ratDocument ->
|
|
|
|
def contentMatcher = contentPattern.matcher(text);
|
|
|
|
if (contentMatcher.find()) {
|
|
|
|
def contentStartPos = contentMatcher.start();
|
|
|
|
def commentMatcher = commentPattern.matcher(text);
|
|
|
|
while (commentMatcher.find()) {
|
|
|
|
if (isLicense(commentMatcher, ratDocument)) {
|
|
|
|
if (commentMatcher.start() < contentStartPos) {
|
|
|
|
break; // This file is all good, so break loop: license header precedes 'description' definition
|
|
|
|
} else {
|
|
|
|
reportViolation(f, description+' declaration precedes license header');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 04:08:51 -04:00
|
|
|
ProgressLogger progress = progressLoggerFactory.newOperation(this.class)
|
|
|
|
progress.start(this.name, this.name)
|
|
|
|
|
2022-02-10 06:03:54 -05:00
|
|
|
sourceFiles.each { f ->
|
2022-02-12 14:22:49 -05:00
|
|
|
try {
|
2022-02-13 12:34:57 -05:00
|
|
|
progress.progress("Scanning ${f.name}")
|
|
|
|
logger.debug('Scanning source file: {}', f);
|
|
|
|
|
|
|
|
def text = f.getText('UTF-8');
|
2022-02-10 06:03:54 -05:00
|
|
|
invalidPatterns.each { pattern, name ->
|
|
|
|
if (pattern.matcher(text).find()) {
|
|
|
|
reportViolation(f, name);
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
2022-02-10 06:03:54 -05:00
|
|
|
def javadocsMatcher = javadocsPattern.matcher(text);
|
|
|
|
def ratDocument = new FileDocument(f);
|
|
|
|
while (javadocsMatcher.find()) {
|
|
|
|
if (isLicense(javadocsMatcher, ratDocument)) {
|
|
|
|
reportViolation(f, String.format(Locale.ENGLISH, 'javadoc-style license header [%s]',
|
|
|
|
ratDocument.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_NAME)));
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
2022-02-10 06:03:54 -05:00
|
|
|
if (f.name.endsWith('.java')) {
|
|
|
|
// make sure that SPI names of all tokenizers/charfilters/tokenfilters are documented
|
|
|
|
if (!f.name.contains("Test") && !f.name.contains("Mock") && !f.name.contains("Fake") && !text.contains("abstract class") &&
|
|
|
|
!f.name.equals("TokenizerFactory.java") && !f.name.equals("CharFilterFactory.java") && !f.name.equals("TokenFilterFactory.java") &&
|
|
|
|
(f.name.contains("TokenizerFactory") && text.contains("extends TokenizerFactory") ||
|
|
|
|
f.name.contains("CharFilterFactory") && text.contains("extends CharFilterFactory") ||
|
|
|
|
f.name.contains("FilterFactory") && text.contains("extends TokenFilterFactory"))) {
|
|
|
|
if (!validSPINameJavadocTag.matcher(text).find()) {
|
|
|
|
reportViolation(f, 'invalid spi name documentation')
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
2022-02-10 06:03:54 -05:00
|
|
|
checkLicenseHeaderPrecedes(f, 'package', packagePattern, javaCommentPattern, text, ratDocument);
|
2020-08-31 06:52:04 -04:00
|
|
|
|
2022-02-10 06:03:54 -05:00
|
|
|
invalidJavaOnlyPatterns.each { pattern,name ->
|
|
|
|
if (pattern.matcher(text).find()) {
|
|
|
|
reportViolation(f, name);
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 06:03:54 -05:00
|
|
|
if (f.name.endsWith('.xml')) {
|
|
|
|
checkLicenseHeaderPrecedes(f, '<tag>', xmlTagPattern, xmlCommentPattern, text, ratDocument);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
e.printStackTrace()
|
2022-02-13 12:34:57 -05:00
|
|
|
throw new GradleException("Unhandled exception while validating patterns on file: " + f, e)
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|
2020-09-08 04:08:51 -04:00
|
|
|
}
|
2022-02-10 06:03:54 -05:00
|
|
|
|
2020-09-08 04:08:51 -04:00
|
|
|
progress.completed()
|
2020-01-10 06:02:30 -05:00
|
|
|
|
2022-01-05 14:42:02 -05:00
|
|
|
if (!violations.isEmpty()) {
|
|
|
|
throw new GradleException(String.format(Locale.ENGLISH, 'Found %d source violation(s):\n %s',
|
|
|
|
violations.size(),
|
|
|
|
violations.join('\n ')))
|
2020-01-10 06:02:30 -05:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 06:52:04 -04:00
|
|
|
}
|