Skip to content

Commit

Permalink
Require gradle-consitent-versions idea plugin for all projects using …
Browse files Browse the repository at this point in the history
…GCV (#1272)

Require gradle-consitent-versions idea plugin for all projects using GCV
  • Loading branch information
FinlayRJW authored Nov 19, 2024
1 parent 48a276e commit 3a4313e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ allprojects {
}

dependencies {
implementation project(':gradle-consistent-versions-idea')
implementation 'com.google.guava:guava'
implementation 'com.netflix.nebula:nebula-dependency-recommender'
implementation 'com.palantir.gradle.failure-reports:gradle-failure-reports-exceptions'
Expand Down Expand Up @@ -123,6 +124,13 @@ gradlePlugin {
description = displayName
tags.set(['versions'])
}
requireConsistentVersionsIdeaPlugin {
id = 'com.palantir.consistent-versions-require-idea-plugin'
implementationClass = 'com.palantir.gradle.versions.RequireConsistentVersionsIdeaPlugin'
displayName = 'Plugin to ensure that the intellij plugin is added to the externalDependencies.xml.'
description = displayName
tags.set(['versions'])
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1272.v2.yml
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
3 changes: 3 additions & 0 deletions gradle-consistent-versions-idea/build.gradle
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'
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
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ buildscript {
}
apply plugin: 'com.palantir.jdks.settings'
rootProject.name = 'gradle-consistent-versions'

include 'gradle-consistent-versions-idea'
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public final void apply(Project project) {
project.getPluginManager().apply(VersionsPropsPlugin.class);
project.getPluginManager().apply(GetVersionPlugin.class);
project.getPluginManager().apply(VersionsPropsIdeaPlugin.class);
project.getPluginManager().apply(RequireConsistentVersionsIdeaPlugin.class);

project.allprojects(proj -> {
proj.getPluginManager().withPlugin("java", _plugin -> {
Expand Down
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);
}
}

0 comments on commit 3a4313e

Please sign in to comment.