@@ -24,12 +24,17 @@ allprojects {
2424 }
2525}
2626
27+ interface Injected {
28+ @Inject FileSystemOperations getFs ()
29+ }
30+
2731// Applying this stuff, particularly the idea-ext plugin, has a cost so avoid it unless we're running in the IDE
2832if (providers. systemProperty(' idea.active' ). getOrNull() == ' true' ) {
2933 project. apply(plugin : org.jetbrains.gradle.ext.IdeaExtPlugin )
3034
3135 def elasticsearchProject = locateElasticsearchWorkspace(gradle)
3236
37+ def rootFolder = project. rootDir
3338 tasks. register(' configureIdeCheckstyle' ) {
3439 group = ' ide'
3540 description = ' Generated a suitable checkstyle config for IDEs'
@@ -39,10 +44,10 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
3944 String checkstyleConfig = " ${ resources} /checkstyle.xml"
4045 String checkstyleSuppressions = " ${ resources} /checkstyle_suppressions.xml"
4146 String checkstyleIdeFragment = " ${ resources} /checkstyle_ide_fragment.xml"
42- String checkstyleIdeConfig = " ${ rootDir } /checkstyle_ide.xml"
47+ String checkstyleIdeConfig = " ${ rootFolder } /checkstyle_ide.xml"
4348
4449 String checkstylePluginConfigTemplate = " ${ resources} /checkstyle-idea.xml"
45- String checkstylePluginConfig = " ${ rootDir } /.idea/checkstyle-idea.xml"
50+ String checkstylePluginConfig = " ${ rootFolder } /.idea/checkstyle-idea.xml"
4651
4752 inputs. files(
4853 file(checkstyleConfig),
@@ -53,31 +58,33 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
5358 file(checkstyleIdeConfig),
5459 file(checkstylePluginConfig)
5560 )
61+ def injected = project. objects. newInstance(Injected )
5662
63+ def projectFolder = project. layout. projectDirectory. asFile
5764 doLast {
5865 // Configure the IntelliJ Checkstyle plugin by copying a standard file. We don't simply commit
5966 // the result to version control, because the plugin has a habit of modifying the file and
6067 // replacing the `$PROJECT_DIR$` placeholders, which developers must then revert.
61- project . copy {
68+ injected . fs . copy {
6269 from(checkstylePluginConfigTemplate)
63- into(" ${ rootDir } /.idea" )
70+ into(" ${ rootFolder } /.idea" )
6471 expand(jarLocation : buildConventionsJar, configLocation : checkstyleIdeConfig)
6572 }
6673
6774 // Create an IDE-specific checkstyle config by first copying the standard config
6875 Files . copy(
69- Paths . get(file (checkstyleConfig). getPath()),
70- Paths . get(file (checkstyleIdeConfig). getPath()),
76+ Paths . get(new File (checkstyleConfig). getPath()),
77+ Paths . get(new File (checkstyleIdeConfig). getPath()),
7178 StandardCopyOption . REPLACE_EXISTING
7279 )
7380
7481 // There are some rules that we only want to enable in an IDE. These
7582 // are extracted to a separate file, and merged into the IDE-specific
7683 // Checkstyle config.
77- Node xmlFragment = parseXml(checkstyleIdeFragment)
84+ Node xmlFragment = IdeaXmlUtil . parseXml(checkstyleIdeFragment)
7885
7986 // Edit the copy so that IntelliJ can copy with it
80- modifyXml(checkstyleIdeConfig, { xml ->
87+ IdeaXmlUtil . modifyXml(checkstyleIdeConfig, { xml ->
8188 // Add all the nodes from the fragment file
8289 Node treeWalker = xml. module. find { it. ' @name' == ' TreeWalker' }
8390 xmlFragment. module. each { treeWalker. append(it) }
@@ -103,7 +110,7 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
103110 description = ' Configures the appropriate JVM for Gradle'
104111
105112 doLast {
106- modifyXml(' .idea/gradle.xml' ) { xml ->
113+ IdeaXmlUtil . modifyXml(' .idea/gradle.xml' ) { xml ->
107114 def gradleSettings = xml. component. find { it. ' @name' == ' GradleSettings' }. option[0 ]. GradleProjectSettings
108115 // Remove configured JVM option to force IntelliJ to use the project JDK for Gradle
109116 gradleSettings. option. findAll { it. ' @name' == ' gradleJvm' }. each { it. parent(). remove(it) }
@@ -127,7 +134,7 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
127134 description = ' Enable per-module *.iml files'
128135
129136 doLast {
130- modifyXml(' .idea/misc.xml' ) {xml ->
137+ IdeaXmlUtil . modifyXml(' .idea/misc.xml' ) {xml ->
131138 def externalStorageConfig = xml. component. find { it. ' @name' == ' ExternalStorageConfigurationManager' }
132139 if (externalStorageConfig) {
133140 xml. remove(externalStorageConfig)
@@ -142,13 +149,13 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
142149 description = ' Enables preview features on native library module'
143150 dependsOn tasks. named(" enableExternalConfiguration" )
144151
145- ext {
146- enablePreview = { moduleFile , languageLevel ->
147- modifyXml(moduleFile) { xml ->
152+ // ext {
153+ def enablePreview = { moduleFile , languageLevel ->
154+ IdeaXmlUtil . modifyXml(moduleFile) { xml ->
148155 xml. component. find { it. ' @name' == ' NewModuleRootManager' }?. ' @LANGUAGE_LEVEL' = languageLevel
149156 }
150157 }
151- }
158+ // }
152159
153160 doLast {
154161 enablePreview(' .idea/modules/libs/native/elasticsearch.libs.native.main.iml' , ' JDK_21_PREVIEW' )
@@ -278,33 +285,37 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
278285 * @param preface optional front matter to add after the XML declaration
279286 * but before the XML document, e.g. a doctype or comment
280287 */
281- void modifyXml (Object path , Action<? super Node > action , String preface = null ) {
282- if (project. file(path). exists()) {
283- Node xml = parseXml(path)
284- action. execute(xml)
285-
286- File xmlFile = project. file(path)
287- xmlFile. withPrintWriter { writer ->
288- def printer = new XmlNodePrinter (writer)
289- printer. namespaceAware = true
290- printer. preserveWhitespace = true
291- writer. write(" <?xml version=\" 1.0\" ?>\n " )
292-
293- if (preface != null ) {
294- writer. write(preface)
288+
289+ class IdeaXmlUtil {
290+ static Node parseXml (Object xmlPath ) {
291+ File xmlFile = new File (xmlPath)
292+ XmlParser xmlParser = new XmlParser (false , true , true )
293+ xmlParser. setFeature(" http://apache.org/xml/features/nonvalidating/load-external-dtd" , false )
294+ Node xml = xmlParser. parse(xmlFile)
295+ return xml
296+ }
297+
298+ static void modifyXml (Object xmlPath , Action<? super Node > action , String preface = null ) {
299+ File xmlFile = new File (xmlPath)
300+ if (xmlFile. exists()) {
301+ Node xml = parseXml(xmlPath)
302+ action. execute(xml)
303+
304+ xmlFile. withPrintWriter { writer ->
305+ def printer = new XmlNodePrinter (writer)
306+ printer. namespaceAware = true
307+ printer. preserveWhitespace = true
308+ writer. write(" <?xml version=\" 1.0\" ?>\n " )
309+
310+ if (preface != null ) {
311+ writer. write(preface)
312+ }
313+ printer. print (xml)
295314 }
296- printer. print (xml)
297315 }
298316 }
299317}
300318
301- Node parseXml (Object path ) {
302- File xmlFile = project. file(path)
303- XmlParser xmlParser = new XmlParser (false , true , true )
304- xmlParser. setFeature(" http://apache.org/xml/features/nonvalidating/load-external-dtd" , false )
305- Node xml = xmlParser. parse(xmlFile)
306- return xml
307- }
308319
309320Pair<File , IncludedBuild > locateElasticsearchWorkspace (Gradle gradle ) {
310321 if (gradle. parent == null ) {
0 commit comments