Add check header rule and fixHeaders task
Add "new line" in the header end * Optimization for the `fixHeaders.gradle` * Apply the `fixHeaders` for the affected classes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
^\Q/*\E$
|
||||
^\Q * Copyright \E20\d\d\-20\d\d\Q the original author or authors.\E$
|
||||
^\Q * Copyright \E20\d\d(\-20\d\d)?\Q the original author or authors.\E$
|
||||
^\Q *\E$
|
||||
^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$
|
||||
^\Q * you may not use this file except in compliance with the License.\E$
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
|
||||
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
|
||||
<suppressions>
|
||||
<suppress files="package-info\.java" checks=".*" />
|
||||
</suppressions>
|
||||
|
||||
@@ -2,11 +2,16 @@
|
||||
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
|
||||
<module name="Checker">
|
||||
|
||||
<module name="SuppressionFilter">
|
||||
<property name="file" value="src/checkstyle/checkstyle-suppressions.xml" />
|
||||
</module>
|
||||
|
||||
<!-- Root Checks -->
|
||||
<!-- <module name="RegexpHeader"> -->
|
||||
<!-- <property name="headerFile" value="${checkstyle.header.file}" /> -->
|
||||
<!-- <property name="fileExtensions" value="java" /> -->
|
||||
<!-- </module> -->
|
||||
<module name="RegexpHeader">
|
||||
<property name="headerFile" value="src/checkstyle/checkstyle-header.txt" />
|
||||
<property name="fileExtensions" value="java" />
|
||||
</module>
|
||||
|
||||
<module name="NewlineAtEndOfFile">
|
||||
<property name="lineSeparator" value="lf"/>
|
||||
</module>
|
||||
|
||||
43
src/checkstyle/fixHeaders.gradle
Normal file
43
src/checkstyle/fixHeaders.gradle
Normal file
@@ -0,0 +1,43 @@
|
||||
ext {
|
||||
headerTemplate = new File('src/checkstyle/checkstyle-header.txt')
|
||||
.text
|
||||
.replaceAll(/\^\\Q|\\E\$|\^\$|\^\.\*\$/, '')
|
||||
.replaceFirst(/\\E20\\d\\d\(\\-20\\d\\d\)\?\\Q/, '{year}')
|
||||
.trim() + System.lineSeparator() + System.lineSeparator()
|
||||
|
||||
|
||||
now = Calendar.instance.get(Calendar.YEAR) as String
|
||||
}
|
||||
|
||||
task fixHeaders << {
|
||||
fileTree("${buildDir}/reports/checkstyle").include('*.xml').each { report ->
|
||||
def xml = new XmlParser(false, false).parse(report)
|
||||
xml.file.each {
|
||||
if (it.error.@source == ['com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck']) {
|
||||
def years = now
|
||||
def file = new File(it.@name)
|
||||
def line
|
||||
file.withReader { reader ->
|
||||
while (line = reader.readLine()) {
|
||||
def matcher = line =~ /Copyright (20\d\d)(?:-20\d\d)?/
|
||||
if (matcher.count) {
|
||||
years = matcher[0][1]
|
||||
if (years != now) {
|
||||
years = years + "-$now"
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def header = headerTemplate.replaceFirst(/\{year}/, years)
|
||||
|
||||
def sourceCode = file.text
|
||||
sourceCode = sourceCode.replaceFirst(/(?s)(?:.*)(package org\.springframework\.integration.*)/,
|
||||
"${header}\$1")
|
||||
file.write(sourceCode)
|
||||
println "Header fixed for $file"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user