Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve coordination between java-versions and idea ipr #2012

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-2012.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Improve coordination between java-versions and idea ipr allowing project
iprs to be successfully imported
links:
- https://github.com/palantir/gradle-baseline/pull/2012
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ package com.palantir.baseline.plugins

import com.google.common.collect.ImmutableMap
import com.palantir.baseline.IntellijSupport
import com.palantir.baseline.extensions.BaselineJavaVersionExtension
import com.palantir.baseline.extensions.BaselineJavaVersionsExtension
import com.palantir.baseline.util.GitUtils
import groovy.transform.CompileStatic
import groovy.xml.XmlUtil
import org.gradle.api.JavaVersion
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.plugins.ide.idea.model.IdeaLanguageLevel
import org.gradle.plugins.ide.idea.model.IdeaModule
import org.gradle.plugins.ide.idea.model.Jdk

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened with the imports here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a formatter for groovy that automatically cleans up imports.

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -58,6 +66,7 @@ class BaselineIdea extends AbstractBaselinePlugin {
// Configure Idea module
IdeaModel ideaModuleModel = project.extensions.getByType(IdeaModel)
moveProjectReferencesToEnd(ideaModuleModel)
updateModuleLanguageVersion(ideaModuleModel, project)

// If someone renames a project, leftover {ipr,iml,ipr} files may still exist on disk and
// confuse users, so we proactively clean them up. Intentionally using an Action<Task> to allow up-to-dateness.
Expand Down Expand Up @@ -87,6 +96,7 @@ class BaselineIdea extends AbstractBaselinePlugin {
ideaRootModel.project.ipr.withXml {XmlProvider provider ->
Node node = provider.asNode()
addCodeStyle(node)
setRootJavaVersions(node)
addCopyright(node)
addCheckstyle(node)
addEclipseFormat(node)
Expand Down Expand Up @@ -177,6 +187,51 @@ class BaselineIdea extends AbstractBaselinePlugin {
})
}

private void setRootJavaVersions(Node node) {
BaselineJavaVersionsExtension versions = project.getExtensions().findByType(BaselineJavaVersionsExtension.class)
if (versions != null) {
updateCompilerConfiguration(node, versions)
updateProjectRootManager(node, versions)
}
}

private void updateCompilerConfiguration(Node node, BaselineJavaVersionsExtension versions) {
Node compilerConfiguration = node.component.find { it.'@name' == 'CompilerConfiguration' }
Node bytecodeTargetLevel = GroovyXmlUtils.matchOrCreateChild(compilerConfiguration, "bytecodeTargetLevel")
JavaLanguageVersion defaultBytecodeVersion = versions.libraryTarget().get()
bytecodeTargetLevel.attributes().put("target", defaultBytecodeVersion.toString())
project.allprojects.forEach({ project ->
BaselineJavaVersionExtension version = project.getExtensions().findByType(BaselineJavaVersionExtension.class)
if (version != null && version.target().get().asInt() != defaultBytecodeVersion.asInt()) {
bytecodeTargetLevel.appendNode("module", ImmutableMap.of(
"name", project.getName(),
"target", version.target().get().toString()))
}
})
}

private void updateProjectRootManager(Node node, BaselineJavaVersionsExtension versions) {
Node projectRootManager = node.component.find { it.'@name' == 'ProjectRootManager' }
int featureRelease = versions.distributionTarget().get().asInt()
JavaVersion javaVersion = JavaVersion.toVersion(featureRelease)
projectRootManager.attributes().put("project-jdk-name", featureRelease)
projectRootManager.attributes().put("languageLevel", new IdeaLanguageLevel(javaVersion).getLevel())
}

private static void updateModuleLanguageVersion(IdeaModel ideaModel, Project currentProject) {
ideaModel.module.iml.withXml { XmlProvider provider ->
// Extension must be checked lazily within the transformer
BaselineJavaVersionExtension version = currentProject.extensions.findByType(BaselineJavaVersionExtension.class)
if (version != null) {
int featureRelease = version.target().get().asInt()
JavaVersion javaVersion = JavaVersion.toVersion(featureRelease)
Node node = provider.asNode()
Node newModuleRootManager = node.component.find { it.'@name' == 'NewModuleRootManager' }
newModuleRootManager.attributes().put("LANGUAGE_LEVEL", new IdeaLanguageLevel(javaVersion).getLevel())
}
}
}

/**
* Extracts copyright headers from Baseline directory and adds them to Idea project XML node.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,27 @@ class BaselineIdeaIntegrationTest extends AbstractPluginTest {
def deps = new XmlSlurper().parse(externalDepsSettingsFile)
deps.component.find { it.@name == "ExternalDependencies" }
}

def 'Idea files use versions derived from the baseline-java-versions plugin'() {
when:
buildFile << standardBuildFile
buildFile << """
apply plugin: 'com.palantir.baseline-java-versions'
javaVersions {
libraryTarget = 11
distributionTarget = 15
runtime = 17
}
""".stripIndent()

then:
with('idea').build()
def rootIpr = Files.asCharSource(new File(projectDir, projectDir.name + ".ipr"), Charsets.UTF_8).read()
rootIpr.contains('languageLevel="JDK_15"')
rootIpr.contains('project-jdk-name="15"')
rootIpr.contains('<bytecodeTargetLevel target="11">')
rootIpr.contains("<module name=\"${projectDir.name}\" target=\"15\"/>")
def rootIml = Files.asCharSource(new File(projectDir, projectDir.name + ".iml"), Charsets.UTF_8).read()
rootIml.contains('LANGUAGE_LEVEL="JDK_15')
}
}