2929import java .net .URI ;
3030import java .nio .charset .Charset ;
3131import java .util .List ;
32+ import java .util .Optional ;
3233import java .util .function .Consumer ;
3334import java .util .stream .Collectors ;
3435import javax .xml .parsers .ParserConfigurationException ;
@@ -47,23 +48,35 @@ public void apply(Project rootProject) {
4748 "May only apply com.palantir.java-format-idea to the root project" );
4849
4950 rootProject .getPlugins ().apply (PalantirJavaFormatProviderPlugin .class );
50-
5151 rootProject .getPluginManager ().withPlugin ("idea" , ideaPlugin -> {
5252 Configuration implConfiguration =
5353 rootProject .getConfigurations ().getByName (PalantirJavaFormatProviderPlugin .CONFIGURATION_NAME );
5454
55- configureLegacyIdea (rootProject , implConfiguration );
56- configureIntelliJImport (rootProject , implConfiguration );
55+ Optional <Configuration > nativeImplConfiguration = maybeGetNativeImplConfiguration (rootProject );
56+
57+ configureLegacyIdea (rootProject , implConfiguration , nativeImplConfiguration );
58+ configureIntelliJImport (rootProject , implConfiguration , nativeImplConfiguration );
5759 });
5860 }
5961
60- private static void configureLegacyIdea (Project project , Configuration implConfiguration ) {
62+ private static Optional <Configuration > maybeGetNativeImplConfiguration (Project rootProject ) {
63+ return NativeImageFormatProviderPlugin .shouldUseNativeImage (rootProject )
64+ ? Optional .of (rootProject
65+ .getConfigurations ()
66+ .getByName (NativeImageFormatProviderPlugin .NATIVE_CONFIGURATION_NAME ))
67+ : Optional .empty ();
68+ }
69+
70+ private static void configureLegacyIdea (
71+ Project project , Configuration implConfiguration , Optional <Configuration > nativeImplConfiguration ) {
6172 IdeaModel ideaModel = project .getExtensions ().getByType (IdeaModel .class );
6273 ideaModel .getProject ().getIpr ().withXml (xmlProvider -> {
6374 // this block is lazy
6475 List <URI > uris =
6576 implConfiguration .getFiles ().stream ().map (File ::toURI ).collect (Collectors .toList ());
66- ConfigureJavaFormatterXml .configureJavaFormat (xmlProvider .asNode (), uris );
77+ Optional <URI > nativeUri =
78+ nativeImplConfiguration .map (conf -> conf .getSingleFile ().toURI ());
79+ ConfigureJavaFormatterXml .configureJavaFormat (xmlProvider .asNode (), uris , nativeUri );
6780 ConfigureJavaFormatterXml .configureExternalDependencies (xmlProvider .asNode ());
6881 });
6982
@@ -72,7 +85,8 @@ private static void configureLegacyIdea(Project project, Configuration implConfi
7285 });
7386 }
7487
75- private static void configureIntelliJImport (Project project , Configuration implConfiguration ) {
88+ private static void configureIntelliJImport (
89+ Project project , Configuration implConfiguration , Optional <Configuration > nativeImplConfiguration ) {
7690 // Note: we tried using 'org.jetbrains.gradle.plugin.idea-ext' and afterSync triggers, but these are currently
7791 // very hard to manage as the tasks feel disconnected from the Sync operation, and you can't remove them once
7892 // you've added them. For that reason, we accept that we have to resolve this configuration at
@@ -84,9 +98,12 @@ private static void configureIntelliJImport(Project project, Configuration implC
8498 List <URI > uris =
8599 implConfiguration .getFiles ().stream ().map (File ::toURI ).collect (Collectors .toList ());
86100
101+ Optional <URI > nativeImageUri =
102+ nativeImplConfiguration .map (conf -> conf .getSingleFile ().toURI ());
103+
87104 createOrUpdateIdeaXmlFile (
88105 project .file (".idea/palantir-java-format.xml" ),
89- node -> ConfigureJavaFormatterXml .configureJavaFormat (node , uris ));
106+ node -> ConfigureJavaFormatterXml .configureJavaFormat (node , uris , nativeImageUri ));
90107 createOrUpdateIdeaXmlFile (
91108 project .file (".idea/externalDependencies.xml" ),
92109 node -> ConfigureJavaFormatterXml .configureExternalDependencies (node ));
@@ -95,7 +112,7 @@ private static void configureIntelliJImport(Project project, Configuration implC
95112
96113 // Still configure legacy idea if using intellij import
97114 updateIdeaXmlFileIfExists (project .file (project .getName () + ".ipr" ), node -> {
98- ConfigureJavaFormatterXml .configureJavaFormat (node , uris );
115+ ConfigureJavaFormatterXml .configureJavaFormat (node , uris , nativeImageUri );
99116 ConfigureJavaFormatterXml .configureExternalDependencies (node );
100117 });
101118 updateIdeaXmlFileIfExists (project .file (project .getName () + ".iws" ), node -> {
0 commit comments