@@ -25,12 +25,17 @@ allprojects {
2525 }
2626}
2727
28+ interface Injected {
29+ @Inject FileSystemOperations getFs ()
30+ }
31+
2832// Applying this stuff, particularly the idea-ext plugin, has a cost so avoid it unless we're running in the IDE
2933if (providers. systemProperty(' idea.active' ). getOrNull() == ' true' ) {
3034 project. apply(plugin : org.jetbrains.gradle.ext.IdeaExtPlugin )
3135
3236 def elasticsearchProject = locateElasticsearchWorkspace(gradle)
3337
38+ def rootFolder = project. rootDir
3439 tasks. register(' configureIdeCheckstyle' ) {
3540 group = ' ide'
3641 description = ' Generated a suitable checkstyle config for IDEs'
@@ -40,10 +45,10 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
4045 String checkstyleConfig = " ${ resources} /checkstyle.xml"
4146 String checkstyleSuppressions = " ${ resources} /checkstyle_suppressions.xml"
4247 String checkstyleIdeFragment = " ${ resources} /checkstyle_ide_fragment.xml"
43- String checkstyleIdeConfig = " ${ rootDir } /checkstyle_ide.xml"
48+ String checkstyleIdeConfig = " ${ rootFolder } /checkstyle_ide.xml"
4449
4550 String checkstylePluginConfigTemplate = " ${ resources} /checkstyle-idea.xml"
46- String checkstylePluginConfig = " ${ rootDir } /.idea/checkstyle-idea.xml"
51+ String checkstylePluginConfig = " ${ rootFolder } /.idea/checkstyle-idea.xml"
4752
4853 inputs. files(
4954 file(checkstyleConfig),
@@ -54,31 +59,33 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
5459 file(checkstyleIdeConfig),
5560 file(checkstylePluginConfig)
5661 )
62+ def injected = project. objects. newInstance(Injected )
5763
64+ def projectFolder = project. layout. projectDirectory. asFile
5865 doLast {
5966 // Configure the IntelliJ Checkstyle plugin by copying a standard file. We don't simply commit
6067 // the result to version control, because the plugin has a habit of modifying the file and
6168 // replacing the `$PROJECT_DIR$` placeholders, which developers must then revert.
62- project . copy {
69+ injected . fs . copy {
6370 from(checkstylePluginConfigTemplate)
64- into(" ${ rootDir } /.idea" )
71+ into(" ${ rootFolder } /.idea" )
6572 expand(jarLocation : buildConventionsJar, configLocation : checkstyleIdeConfig)
6673 }
6774
6875 // Create an IDE-specific checkstyle config by first copying the standard config
6976 Files . copy(
70- Paths . get(file (checkstyleConfig). getPath()),
71- Paths . get(file (checkstyleIdeConfig). getPath()),
77+ Paths . get(new File (checkstyleConfig). getPath()),
78+ Paths . get(new File (checkstyleIdeConfig). getPath()),
7279 StandardCopyOption . REPLACE_EXISTING
7380 )
7481
7582 // There are some rules that we only want to enable in an IDE. These
7683 // are extracted to a separate file, and merged into the IDE-specific
7784 // Checkstyle config.
78- Node xmlFragment = parseXml(checkstyleIdeFragment)
85+ Node xmlFragment = IdeaXmlUtil . parseXml(checkstyleIdeFragment)
7986
8087 // Edit the copy so that IntelliJ can copy with it
81- modifyXml(checkstyleIdeConfig, { xml ->
88+ IdeaXmlUtil . modifyXml(checkstyleIdeConfig, { xml ->
8289 // Add all the nodes from the fragment file
8390 Node treeWalker = xml. module. find { it. ' @name' == ' TreeWalker' }
8491 xmlFragment. module. each { treeWalker. append(it) }
@@ -104,7 +111,7 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
104111 description = ' Configures the appropriate JVM for Gradle'
105112
106113 doLast {
107- modifyXml(' .idea/gradle.xml' ) { xml ->
114+ IdeaXmlUtil . modifyXml(' .idea/gradle.xml' ) { xml ->
108115 def gradleSettings = xml. component. find { it. ' @name' == ' GradleSettings' }. option[0 ]. GradleProjectSettings
109116 // Remove configured JVM option to force IntelliJ to use the project JDK for Gradle
110117 gradleSettings. option. findAll { it. ' @name' == ' gradleJvm' }. each { it. parent(). remove(it) }
@@ -241,33 +248,37 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
241248 * @param preface optional front matter to add after the XML declaration
242249 * but before the XML document, e.g. a doctype or comment
243250 */
244- void modifyXml (Object path , Action<? super Node > action , String preface = null ) {
245- if (project. file(path). exists()) {
246- Node xml = parseXml(path)
247- action. execute(xml)
248-
249- File xmlFile = project. file(path)
250- xmlFile. withPrintWriter { writer ->
251- def printer = new XmlNodePrinter (writer)
252- printer. namespaceAware = true
253- printer. preserveWhitespace = true
254- writer. write(" <?xml version=\" 1.0\" ?>\n " )
255-
256- if (preface != null ) {
257- writer. write(preface)
251+
252+ class IdeaXmlUtil {
253+ static Node parseXml (Object xmlPath ) {
254+ File xmlFile = new File (xmlPath)
255+ XmlParser xmlParser = new XmlParser (false , true , true )
256+ xmlParser. setFeature(" http://apache.org/xml/features/nonvalidating/load-external-dtd" , false )
257+ Node xml = xmlParser. parse(xmlFile)
258+ return xml
259+ }
260+
261+ static void modifyXml (Object xmlPath , Action<? super Node > action , String preface = null ) {
262+ File xmlFile = new File (xmlPath)
263+ if (xmlFile. exists()) {
264+ Node xml = parseXml(xmlPath)
265+ action. execute(xml)
266+
267+ xmlFile. withPrintWriter { writer ->
268+ def printer = new XmlNodePrinter (writer)
269+ printer. namespaceAware = true
270+ printer. preserveWhitespace = true
271+ writer. write(" <?xml version=\" 1.0\" ?>\n " )
272+
273+ if (preface != null ) {
274+ writer. write(preface)
275+ }
276+ printer. print (xml)
258277 }
259- printer. print (xml)
260278 }
261279 }
262280}
263281
264- Node parseXml (Object path ) {
265- File xmlFile = project. file(path)
266- XmlParser xmlParser = new XmlParser (false , true , true )
267- xmlParser. setFeature(" http://apache.org/xml/features/nonvalidating/load-external-dtd" , false )
268- Node xml = xmlParser. parse(xmlFile)
269- return xml
270- }
271282
272283Pair<File , IncludedBuild > locateElasticsearchWorkspace (Gradle gradle ) {
273284 if (gradle. parent == null ) {
0 commit comments