瀏覽代碼

Improve composite build support for IntelliJ IDE integration (#90655)

Mark Vieira 3 年之前
父節點
當前提交
20d78c2f6c
共有 1 個文件被更改,包括 89 次插入68 次删除
  1. 89 68
      build-tools-internal/src/main/groovy/elasticsearch.ide.gradle

+ 89 - 68
build-tools-internal/src/main/groovy/elasticsearch.ide.gradle

@@ -6,6 +6,7 @@
  * Side Public License, v 1.
  */
 
+import org.elasticsearch.gradle.util.Pair
 import org.elasticsearch.gradle.internal.info.BuildParams
 import org.jetbrains.gradle.ext.JUnit
 
@@ -21,76 +22,78 @@ allprojects {
   }
 }
 
-tasks.register('configureIdeCheckstyle') {
-  group = 'ide'
-  description = 'Generated a suitable checkstyle config for IDEs'
-
-  String resources = 'build-tools-internal/src/main/resources'
-  String checkstyleConfig = "${resources}/checkstyle.xml"
-  String checkstyleSuppressions = "${resources}/checkstyle_suppressions.xml"
-  String checkstyleIdeFragment = "${resources}/checkstyle_ide_fragment.xml"
-  String checkstyleIdeConfig = "${rootDir}/checkstyle_ide.xml"
-
-  String checkstylePluginConfigTemplate = "${resources}/checkstyle-idea.xml"
-  String checkstylePluginConfig = "${rootDir}/.idea/checkstyle-idea.xml"
-
-  inputs.files(
-    file(checkstyleConfig),
-    file(checkstyleIdeFragment),
-    file(checkstylePluginConfigTemplate)
-  )
-  outputs.files(
-    file(checkstyleIdeConfig),
-    file(checkstylePluginConfig)
-  )
-
-  doLast {
-    // Configure the IntelliJ Checkstyle plugin by copying a standard file. We don't simply commit
-    // the result to version control, because the plugin has a habit of modifying the file and
-    // replacing the `$PROJECT_DIR$` placeholders, which developers must then revert.
-    Files.copy(
-      Paths.get(file(checkstylePluginConfigTemplate).getPath()),
-      Paths.get(file(checkstylePluginConfig).getPath()),
-      StandardCopyOption.REPLACE_EXISTING
-    )
+// Applying this stuff, particularly the idea-ext plugin, has a cost so avoid it unless we're running in the IDE
+if (providers.systemProperty('idea.active').getOrNull() == 'true') {
+  project.apply(plugin: org.jetbrains.gradle.ext.IdeaExtPlugin)
 
-    // 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
-    )
+  def elasticsearchProject = locateElasticsearchWorkspace(gradle)
+
+  tasks.register('configureIdeCheckstyle') {
+    group = 'ide'
+    description = 'Generated a suitable checkstyle config for IDEs'
 
-    // There are some rules that we only want to enable in an IDE. These
-    // are extracted to a separate file, and merged into the IDE-specific
-    // Checkstyle config.
-    Node xmlFragment = parseXml(checkstyleIdeFragment)
-
-    // Edit the copy so that IntelliJ can copy with it
-    modifyXml(checkstyleIdeConfig, { xml ->
-      // Add all the nodes from the fragment file
-      Node treeWalker = xml.module.find { it.'@name' == 'TreeWalker' }
-      xmlFragment.module.each { treeWalker.append(it) }
-
-      // Change the checkstyle config to inline the path to the
-      // suppressions config. This removes a configuration step when using
-      // the checkstyle config in an IDE.
-      Node suppressions = xml.module.find { it.'@name' == 'SuppressionFilter' }
-      suppressions.property.findAll { it.'@name' == 'file' }.each { it.'@value' = checkstyleSuppressions }
-    },
-      "<!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 the following - do not edit this file directly. -->\n" +
-      "<!--     ${checkstyleConfig} -->\n" +
-      "<!--     ${checkstyleIdeFragment} -->\n"
+    String resources = "${elasticsearchProject.left()}/build-tools-internal/src/main/resources"
+    String checkstyleConfig = "${resources}/checkstyle.xml"
+    String checkstyleSuppressions = "${resources}/checkstyle_suppressions.xml"
+    String checkstyleIdeFragment = "${resources}/checkstyle_ide_fragment.xml"
+    String checkstyleIdeConfig = "${rootDir}/checkstyle_ide.xml"
+
+    String checkstylePluginConfigTemplate = "${resources}/checkstyle-idea.xml"
+    String checkstylePluginConfig = "${rootDir}/.idea/checkstyle-idea.xml"
+
+    inputs.files(
+      file(checkstyleConfig),
+      file(checkstyleIdeFragment),
+      file(checkstylePluginConfigTemplate)
+    )
+    outputs.files(
+      file(checkstyleIdeConfig),
+      file(checkstylePluginConfig)
     )
-  }
-}
 
-// Applying this stuff, particularly the idea-ext plugin, has a cost so avoid it unless we're running in the IDE
-if (providers.systemProperty('idea.active').getOrNull() == 'true') {
-  project.apply(plugin: org.jetbrains.gradle.ext.IdeaExtPlugin)
+    doLast {
+      // Configure the IntelliJ Checkstyle plugin by copying a standard file. We don't simply commit
+      // the result to version control, because the plugin has a habit of modifying the file and
+      // replacing the `$PROJECT_DIR$` placeholders, which developers must then revert.
+      Files.copy(
+        Paths.get(file(checkstylePluginConfigTemplate).getPath()),
+        Paths.get(file(checkstylePluginConfig).getPath()),
+        StandardCopyOption.REPLACE_EXISTING
+      )
+
+      // 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
+      )
+
+      // There are some rules that we only want to enable in an IDE. These
+      // are extracted to a separate file, and merged into the IDE-specific
+      // Checkstyle config.
+      Node xmlFragment = parseXml(checkstyleIdeFragment)
+
+      // Edit the copy so that IntelliJ can copy with it
+      modifyXml(checkstyleIdeConfig, { xml ->
+        // Add all the nodes from the fragment file
+        Node treeWalker = xml.module.find { it.'@name' == 'TreeWalker' }
+        xmlFragment.module.each { treeWalker.append(it) }
+
+        // Change the checkstyle config to inline the path to the
+        // suppressions config. This removes a configuration step when using
+        // the checkstyle config in an IDE.
+        Node suppressions = xml.module.find { it.'@name' == 'SuppressionFilter' }
+        suppressions.property.findAll { it.'@name' == 'file' }.each { it.'@value' = checkstyleSuppressions }
+      },
+        "<!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 the following - do not edit this file directly. -->\n" +
+          "<!--     ${checkstyleConfig} -->\n" +
+          "<!--     ${checkstyleIdeFragment} -->\n"
+      )
+    }
+  }
 
   tasks.register('configureIdeaGradleJvm') {
     group = 'ide'
@@ -108,11 +111,11 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
   tasks.register('buildDependencyArtifacts') {
     group = 'ide'
     description = 'Builds artifacts needed as dependency for IDE modules'
-    dependsOn ':client:rest-high-level:shadowJar',
+    dependsOn([':client:rest-high-level:shadowJar',
       ':plugins:repository-hdfs:hadoop-client-api:shadowJar',
       ':libs:elasticsearch-x-content:generateProviderImpl',
       ':server:generateModulesList',
-      ':server:generatePluginsList'
+      ':server:generatePluginsList'].collect { elasticsearchProject.right()?.task(it) ?: it })
   }
 
   idea {
@@ -239,3 +242,21 @@ Node parseXml(Object path) {
   Node xml = xmlParser.parse(xmlFile)
   return xml
 }
+
+Pair<File, IncludedBuild> locateElasticsearchWorkspace(Gradle project) {
+  if (project.parent == null) {
+    // See if any of these included builds is the Elasticsearch project
+    for (IncludedBuild includedBuild : project.includedBuilds) {
+      File versionProperties = new File(includedBuild.projectDir, 'build-tools-internal/version.properties')
+      if (versionProperties.exists()) {
+        return Pair.of(includedBuild.projectDir, includedBuild)
+      }
+    }
+
+    // Otherwise assume this project is the root elasticsearch workspace
+    return Pair.of(project.getRootProject().getRootDir(), null)
+  } else {
+    // We're an included build, so keep looking
+    return locateElasticsearchWorkspace(project.parent)
+  }
+}