|
@@ -1,6 +1,9 @@
|
|
|
import org.elasticsearch.gradle.info.BuildParams
|
|
|
import org.jetbrains.gradle.ext.Remote
|
|
|
import org.jetbrains.gradle.ext.JUnit
|
|
|
+import java.nio.file.Files
|
|
|
+import java.nio.file.StandardCopyOption
|
|
|
+import java.nio.file.Paths
|
|
|
|
|
|
buildscript {
|
|
|
repositories {
|
|
@@ -21,6 +24,43 @@ allprojects {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+tasks.register('configureIdeCheckstyle') {
|
|
|
+ group = 'ide'
|
|
|
+ description = 'Generated a suitable checkstyle config for IDEs'
|
|
|
+
|
|
|
+ String checkstyleConfig = 'buildSrc/src/main/resources/checkstyle.xml'
|
|
|
+ String checkstyleIdeConfig = "${buildDir}/checkstyle_ide.xml"
|
|
|
+
|
|
|
+ inputs.files(file(checkstyleConfig))
|
|
|
+ outputs.files(file(checkstyleIdeConfig))
|
|
|
+
|
|
|
+ doLast {
|
|
|
+ // Create an IDE-specific checkstyle config by first copying the standard config
|
|
|
+ Files.copy(
|
|
|
+ Paths.get(file(checkstyleConfig).getPath()),
|
|
|
+ Paths.get(file(checkstyleIdeConfig).getPath()),
|
|
|
+ StandardCopyOption.REPLACE_EXISTING
|
|
|
+ )
|
|
|
+ // Edit the copy so that IntelliJ can copy with it
|
|
|
+ modifyXml(checkstyleIdeConfig, { xml ->
|
|
|
+ // This module since it is implemented with custom code
|
|
|
+ xml.module.findAll { it.'@name' == 'org.elasticsearch.gradle.checkstyle.SnippetLengthCheck' }.each { it.parent().remove(it) }
|
|
|
+
|
|
|
+ // Move the line length check because the IDE thinks it can't belong under a TreeWalker. Moving the
|
|
|
+ // configuration in the main file causes the command-line Checkstyle task to fail ¯\_(ツ)_/¯
|
|
|
+ Node treeWalker = xml.module.find { it.'@name' == 'TreeWalker' }
|
|
|
+ Node lineLength = treeWalker.module.find { it.'@name' == 'LineLength' }
|
|
|
+ treeWalker.remove(lineLength)
|
|
|
+ xml.append(lineLength)
|
|
|
+ },
|
|
|
+ "<!DOCTYPE module PUBLIC\n" +
|
|
|
+ " \"-//Puppy Crawl//DTD Check Configuration 1.3//EN\"\n" +
|
|
|
+ " \"http://www.puppycrawl.com/dtds/configuration_1_3.dtd\">\n" +
|
|
|
+ "<!-- Generated automatically from ./checkstyle.xml - do not edit this file directly. -->\n"
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// Applying this stuff, particularly the idea-ext plugin, has a cost so avoid it unless we're running in the IDE
|
|
|
if (System.getProperty('idea.active') == 'true') {
|
|
|
apply plugin: org.jetbrains.gradle.ext.IdeaExtPlugin
|
|
@@ -55,7 +95,7 @@ if (System.getProperty('idea.active') == 'true') {
|
|
|
testRunner = 'choose_per_test'
|
|
|
}
|
|
|
taskTriggers {
|
|
|
- afterSync tasks.named('configureIdeaGradleJvm'), tasks.named('buildDependencyArtifacts')
|
|
|
+ afterSync tasks.named('configureIdeCheckstyle'), tasks.named('configureIdeaGradleJvm'), tasks.named('buildDependencyArtifacts')
|
|
|
}
|
|
|
codeStyle {
|
|
|
java {
|
|
@@ -122,13 +162,25 @@ if (System.getProperty('idea.active') == 'true') {
|
|
|
*
|
|
|
* @param path Path to existing XML file
|
|
|
* @param action Action to perform on parsed XML document
|
|
|
+ * @param preface optional front matter to add after the XML declaration
|
|
|
+ * but before the XML document, e.g. a doctype or comment
|
|
|
*/
|
|
|
-void modifyXml(Object path, Action<? super Node> action) {
|
|
|
+void modifyXml(Object path, Action<? super Node> action, String preface = null) {
|
|
|
File xmlFile = project.file(path)
|
|
|
- Node xml = new XmlParser().parse(xmlFile)
|
|
|
+ XmlParser xmlParser = new XmlParser(false, true, true)
|
|
|
+ xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
|
|
|
+ Node xml = xmlParser.parse(xmlFile)
|
|
|
action.execute(xml)
|
|
|
|
|
|
xmlFile.withPrintWriter { writer ->
|
|
|
- new XmlNodePrinter(writer).print(xml)
|
|
|
+ def printer = new XmlNodePrinter(writer)
|
|
|
+ printer.namespaceAware = true
|
|
|
+ printer.preserveWhitespace = true
|
|
|
+ writer.write("<?xml version=\"1.0\"?>\n")
|
|
|
+
|
|
|
+ if (preface != null) {
|
|
|
+ writer.write(preface)
|
|
|
+ }
|
|
|
+ printer.print(xml)
|
|
|
}
|
|
|
}
|