-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require gradle-consitent-versions idea plugin for all projects using …
…GCV (#1272) Require gradle-consitent-versions idea plugin for all projects using GCV
- Loading branch information
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: improvement | ||
improvement: | ||
description: Require gradle-consitent-versions idea plugin for all projects using | ||
GCV | ||
links: | ||
- https://github.com/palantir/gradle-consistent-versions/pull/1272 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
apply plugin: 'java-gradle-plugin' | ||
apply plugin: 'groovy' | ||
apply plugin: 'com.palantir.external-publish-jar' |
71 changes: 71 additions & 0 deletions
71
...-versions-idea/src/main/groovy/com/palantir/gradle/versions/ConfigureIdeaPluginXml.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.gradle.versions | ||
|
||
import groovy.xml.XmlNodePrinter | ||
import groovy.xml.XmlParser | ||
import org.gradle.internal.impldep.com.google.common.collect.ImmutableMap | ||
import org.xml.sax.SAXException | ||
|
||
import javax.xml.parsers.ParserConfigurationException | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
|
||
class ConfigureIdeaPluginXml { | ||
static void updateIdeaXmlFile(File configurationFile, String minVersion, boolean createIfAbsent) { | ||
Node rootNode; | ||
if (configurationFile.isFile()) { | ||
try { | ||
rootNode = new XmlParser().parse(configurationFile); | ||
} catch (IOException | SAXException | ParserConfigurationException e) { | ||
throw new RuntimeException("Couldn't parse existing configuration file: " + configurationFile, e); | ||
} | ||
} else { | ||
if (!createIfAbsent) { | ||
return; | ||
} | ||
rootNode = new Node(null, "project", ImmutableMap.of("version", "4")); | ||
} | ||
|
||
configureExternalDependencies(rootNode, minVersion) | ||
|
||
try (BufferedWriter writer = Files.newBufferedWriter(configurationFile.toPath(), StandardCharsets.UTF_8); | ||
PrintWriter printWriter = new PrintWriter(writer)) { | ||
XmlNodePrinter nodePrinter = new XmlNodePrinter(printWriter); | ||
nodePrinter.setPreserveWhitespace(true); | ||
nodePrinter.print(rootNode); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write back to configuration file: " + configurationFile, e); | ||
} | ||
} | ||
|
||
static void configureExternalDependencies(Node rootNode, String minVersion) { | ||
def externalDependencies = matchOrCreateChild(rootNode, 'component', [name: 'ExternalDependencies']) | ||
matchOrCreateChild(externalDependencies, 'plugin', [ 'id': 'gradle-consistent-versions' ], ['min-version' : minVersion]) | ||
} | ||
|
||
private static Node matchOrCreateChild(Node base, String name, Map keyAttributes = [:], Map otherAttributes = [:]) { | ||
Node node = base[name].find { it.attributes().entrySet().containsAll(keyAttributes.entrySet()) } as Node | ||
if (Optional.ofNullable(node).isEmpty()) { | ||
return base.appendNode(name, keyAttributes + otherAttributes) | ||
} else { | ||
node.attributes().clear() | ||
node.attributes().putAll(keyAttributes + otherAttributes) | ||
} | ||
return node | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/palantir/gradle/versions/RequireConsistentVersionsIdeaPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.gradle.versions; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class RequireConsistentVersionsIdeaPlugin implements Plugin<Project> { | ||
private static final Logger log = LoggerFactory.getLogger(RequireConsistentVersionsIdeaPlugin.class); | ||
private static final String MIN_IDEA_PLUGIN_VERSION = "0.9.0"; | ||
|
||
@Override | ||
public final void apply(Project project) { | ||
|
||
if (!Boolean.getBoolean("idea.active")) { | ||
return; | ||
} | ||
|
||
configureIntelliJImport(project); | ||
} | ||
|
||
private static void configureIntelliJImport(Project project) { | ||
// Note: we tried using 'org.jetbrains.gradle.plugin.idea-ext' and afterSync triggers, but these are currently | ||
// very hard to manage as the tasks feel disconnected from the Sync operation, and you can't remove them once | ||
// you've added them. For that reason, we accept that we have to resolve this configuration at | ||
// configuration-time, but only do it when part of an IDEA import. | ||
ConfigureIdeaPluginXml.updateIdeaXmlFile( | ||
project.file(".idea/externalDependencies.xml"), MIN_IDEA_PLUGIN_VERSION, true); | ||
} | ||
} |