diff --git a/changelog/@unreleased/pr-2015.v2.yml b/changelog/@unreleased/pr-2015.v2.yml new file mode 100644 index 000000000..232aa94c8 --- /dev/null +++ b/changelog/@unreleased/pr-2015.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: It's no longer necessary to import an ipr before using gradle integration, + code styles are imported correctly out of the box. + links: + - https://github.com/palantir/gradle-baseline/pull/2015 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineIdea.groovy b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineIdea.groovy index 5e4a5fdc7..1c7b13e0b 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineIdea.groovy +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineIdea.groovy @@ -152,6 +152,11 @@ class BaselineIdea extends AbstractBaselinePlugin { private void addCodeStyleIntellijImport() { def ideaStyleFile = project.file("${configDir}/idea/intellij-java-palantir-style.xml") + // This runs eagerly, so the file might not exist if we haven't run `baselineUpdateConfig` yet. + // Thus, don't do anything if the file is not there yet. + if (!ideaStyleFile.isFile()) { + return + } def ideaStyle = new XmlParser().parse(ideaStyleFile) .component @@ -176,10 +181,15 @@ class BaselineIdea extends AbstractBaselinePlugin { project.file(".idea/codeStyles/Project.xml"), { def codeScheme = GroovyXmlUtils.matchOrCreateChild(it, "code_scheme", [name: 'Project']) - // Just add the default configuration nodes on top of whatever nodes already exist - // We could be better about this, but IDEA will mostly resolve the duplicates here for us - ideaStyleSettings.value.option.forEach { - codeScheme.append(it) + codeScheme.attributes().putIfAbsent("version", 173) + def javaCodeStyleSettings = GroovyXmlUtils.matchOrCreateChild(codeScheme, "JavaCodeStyleSettings") + // Avoid re-adding duplicate options to the project. This allows users to override settings based + // on preference. + ideaStyleSettings.value.option.forEach { ideaStyleSetting -> + def settingName = ideaStyleSetting.attributes().get("name") + if (settingName != null && javaCodeStyleSettings["option"].find { it.attributes().get("name") == settingName } == null) { + javaCodeStyleSettings.append(ideaStyleSetting) + } } }, { diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineIdeaIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineIdeaIntegrationTest.groovy index 35f148619..f562b5b28 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineIdeaIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineIdeaIntegrationTest.groovy @@ -141,7 +141,8 @@ class BaselineIdeaIntegrationTest extends AbstractPluginTest { def ideaStyleSettings = new File(projectDir, ".idea/codeStyles/Project.xml").text ideaStyleSettings.startsWith('<component name="ProjectCodeStyleConfiguration">') - ideaStyleSettings.contains('<code_scheme name="Project">') + ideaStyleSettings.contains('<code_scheme name="Project" version="173">') + ideaStyleSettings.contains('<JavaCodeStyleSettings>') ideaStyleSettings.contains('<option name="ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION" value="true"/>') ideaStyleSettings.endsWith(""" </code_scheme>