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

Feature flagged native image palantir-java-format #1200

Merged
merged 74 commits into from
Feb 20, 2025
Merged
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
52712d6
add benchmarks
crogoz Jan 22, 2025
217a7b0
.
crogoz Jan 22, 2025
0618efc
.
crogoz Jan 22, 2025
fa35c61
spotlessApply
crogoz Jan 23, 2025
ef7b081
test?
crogoz Jan 24, 2025
25d20ea
some nativeImage
crogoz Jan 27, 2025
e5e8492
execute permissions
crogoz Jan 27, 2025
a73f12f
executable
crogoz Jan 27, 2025
5de0181
working transforms
crogoz Jan 28, 2025
c475f83
add replacement reflect classes for native-image
crogoz Jan 30, 2025
07a2d0e
.
crogoz Jan 30, 2025
b1119e2
bump gradle-jdks version
crogoz Jan 30, 2025
0fbf044
Add generated changelog entries
svc-changelog Jan 30, 2025
782f075
Add generated changelog entries
svc-changelog Jan 30, 2025
25bce25
natieImage
crogoz Jan 30, 2025
6d346d9
Merge branch 'cr/graal-1' of github.com:palantir/palantir-java-format…
crogoz Jan 30, 2025
2a2d086
spotless
crogoz Jan 31, 2025
320c77e
gradlew
crogoz Jan 31, 2025
d1323a4
Merge branch 'cr/graal-1' into cr/use-graal-image
crogoz Jan 31, 2025
12f2b1d
compatibility
crogoz Jan 31, 2025
4c058df
Merge branch 'cr/graal-1' into cr/use-graal-image
crogoz Jan 31, 2025
b9a2cc5
OS as part of classifier
crogoz Jan 31, 2025
cbccc19
configurable
crogoz Feb 3, 2025
2b0d8f9
fix tests
crogoz Feb 3, 2025
ce67ada
no log line
crogoz Feb 3, 2025
0f01291
no logging
crogoz Feb 3, 2025
ddad367
use gradle utils
crogoz Feb 4, 2025
f7fe10c
publishing
crogoz Feb 10, 2025
374729d
cleanup circle configs
crogoz Feb 10, 2025
09cd140
configurations
crogoz Feb 11, 2025
6fd3877
cleanup
crogoz Feb 11, 2025
7e0c6c7
cleanup
crogoz Feb 11, 2025
bb5c552
Merge branch 'cr/graal-1' into cr/use-graal-image
crogoz Feb 11, 2025
e37e8e3
wip
crogoz Feb 11, 2025
be60611
wip
crogoz Feb 11, 2025
7113b17
wip
crogoz Feb 12, 2025
760922c
spotless
crogoz Feb 12, 2025
1191756
different plugin
crogoz Feb 12, 2025
ca6969b
Merge branch 'cr/graal-1' into cr/use-graal-image
crogoz Feb 12, 2025
a399b9e
.
crogoz Feb 12, 2025
1336054
wip
crogoz Feb 12, 2025
b0a1950
using gradle-utils
crogoz Feb 12, 2025
975ba5d
separate project
crogoz Feb 12, 2025
65bc162
Merge branch 'develop' into cr/graal-1
crogoz Feb 12, 2025
23b563f
name
crogoz Feb 12, 2025
8db364f
keep :
crogoz Feb 12, 2025
22a9f7d
fix path
crogoz Feb 13, 2025
8456625
publication
crogoz Feb 13, 2025
0825b1b
osExtension
crogoz Feb 13, 2025
561286d
change
crogoz Feb 13, 2025
c492501
circleci check publishLocal
crogoz Feb 13, 2025
4e1e128
glibc or musl
crogoz Feb 13, 2025
ecbc2ed
only bin or exe
crogoz Feb 13, 2025
cc3a0f2
publishing
crogoz Feb 14, 2025
adf33ac
if else CI
crogoz Feb 14, 2025
76ea3fc
fix getEnv
crogoz Feb 14, 2025
c8bc7a5
fix
crogoz Feb 14, 2025
3a8850d
typo
crogoz Feb 14, 2025
6b9fa1f
old circle-all job
crogoz Feb 14, 2025
072f22c
Merge branch 'cr/graal-1' into cr/use-graal-image
crogoz Feb 14, 2025
fa45c1e
fix native
crogoz Feb 14, 2025
67ab915
fix
crogoz Feb 17, 2025
0f28770
Merge branch 'develop' into cr/use-graal-image
crogoz Feb 17, 2025
c9e4c0f
.
crogoz Feb 17, 2025
fbf59e7
gradle utils
crogoz Feb 17, 2025
0d39ef0
spotless
crogoz Feb 17, 2025
25330df
formatDiff
crogoz Feb 18, 2025
0937083
cleanup
crogoz Feb 18, 2025
53484ad
cleanup
crogoz Feb 18, 2025
d47bc98
cleanup
crogoz Feb 18, 2025
7e40b3a
cleanup
crogoz Feb 18, 2025
1119558
.
crogoz Feb 19, 2025
3baf58a
transform
crogoz Feb 19, 2025
1f084a1
no usenativeImageClasspath
crogoz Feb 20, 2025
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
Prev Previous commit
Next Next commit
working transforms
crogoz committed Jan 28, 2025
commit 5de0181db4c5af45534ccdd22a27a9697406e04d
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* (c) Copyright 2025 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.javaformat.gradle;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.artifacts.transform.InputArtifact;
import org.gradle.api.artifacts.transform.TransformAction;
import org.gradle.api.artifacts.transform.TransformOutputs;
import org.gradle.api.artifacts.transform.TransformParameters;
import org.gradle.api.file.FileSystemLocation;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Provider;

public abstract class ExecutableTransform implements TransformAction<TransformParameters.None> {

private static Logger logger = Logging.getLogger(ExecutableTransform.class);

@InputArtifact
public abstract Provider<FileSystemLocation> getInputArtifact();

@Override
public void transform(TransformOutputs outputs) {
File inputFile = getInputArtifact().get().getAsFile();
makeFileExecutable(inputFile.toPath());
Copy link
Contributor

Choose a reason for hiding this comment

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

This is modifying the executable in the cache, no? Really we should copy it first to not modify it.

Copy link
Contributor

Choose a reason for hiding this comment

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

This has other strange effects - like when I run with a publishToMavenLocald executable, it changes the file in the maven local ~/.m2 repo

logger.info("Output is the input file: {}", inputFile);
outputs.file(inputFile);
logger.info("Output is the input file: {}", outputs);
}

private static void makeFileExecutable(Path pathToExe) {
try {
Set<PosixFilePermission> existingPermissions = Files.getPosixFilePermissions(pathToExe);
Files.setPosixFilePermissions(
pathToExe,
Stream.concat(
existingPermissions.stream(),
Stream.of(
PosixFilePermission.OWNER_EXECUTE,
PosixFilePermission.GROUP_EXECUTE,
PosixFilePermission.OTHERS_EXECUTE))
.collect(Collectors.toSet()));
logger.info("Marking is the input file as executable: {}", pathToExe);
} catch (IOException e) {
throw new RuntimeException("Failed to set execute permissions on native-image", e);
}
}

interface Parameters extends TransformParameters {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused I think

}
Original file line number Diff line number Diff line change
@@ -18,27 +18,33 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.Optional;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;

public final class PalantirJavaFormatProviderPlugin implements Plugin<Project> {

static final String CONFIGURATION_NAME = "palantirJavaFormat";
static final String CONFIGURATION_NAME_NATIVE = "palantirJavaFormatNative";

@Override
public void apply(Project rootProject) {
Preconditions.checkState(
rootProject == rootProject.getRootProject(),
"May only apply com.palantir.java-format-provider to the root project");
Boolean legacyFormatter = Optional.ofNullable(rootProject.findProperty("palantir.legacy.formatter"))
.map(value -> Boolean.getBoolean((String) value))
.orElse(false);

Configuration _configuration = rootProject.getConfigurations().create(CONFIGURATION_NAME, conf -> {
conf.setDescription("Internal configuration for resolving the palantir-java-format implementation");
conf.setVisible(false);
conf.setCanBeConsumed(false);
Configuration configuration = rootProject.getConfigurations().create(CONFIGURATION_NAME);
Copy link
Contributor

Choose a reason for hiding this comment

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

.register and put the method calls inside the register action

configuration.setDescription("Internal configuration for resolving the palantir-java-format implementation");
configuration.setVisible(false);
configuration.setCanBeConsumed(false);
configuration.setCanBeResolved(true);

conf.defaultDependencies(deps -> {
if (legacyFormatter) {
configuration.defaultDependencies(deps -> {
deps.add(rootProject
.getDependencies()
.create(ImmutableMap.of(
@@ -49,15 +55,8 @@ public void apply(Project rootProject) {
"version",
JavaFormatExtension.class.getPackage().getImplementationVersion())));
});
});

Configuration configuration2 = rootProject.getConfigurations().create(CONFIGURATION_NAME_NATIVE, conf -> {
conf.setDescription("Internal configuration for resolving the palantir-java-format-native implementation");
conf.setVisible(false);
conf.setCanBeConsumed(false);
conf.setCanBeResolved(true);

conf.defaultDependencies(deps -> {
} else {
configuration.defaultDependencies(deps -> {
deps.add(rootProject
.getDependencies()
.create(ImmutableMap.of(
@@ -72,8 +71,17 @@ public void apply(Project rootProject) {
"ext",
"sh")));
});
});
configuration
.getAttributes()
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, "executable-nativeImage");
rootProject.getDependencies().registerTransform(ExecutableTransform.class, transformSpec -> {
transformSpec.getFrom().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, "sh");
transformSpec
.getTo()
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, "executable-nativeImage");
});
}

rootProject.getExtensions().create("palantirJavaFormat", JavaFormatExtension.class, configuration2);
rootProject.getExtensions().create("palantirJavaFormat", JavaFormatExtension.class, configuration);
}
}
Original file line number Diff line number Diff line change
@@ -31,7 +31,8 @@
final class SpotlessInterop {
private static Logger logger = Logging.getLogger(SpotlessInterop.class);

private SpotlessInterop() {}
private SpotlessInterop() {
}

static void addSpotlessJavaStep(Project project, String configurationName) {
SpotlessExtension spotlessExtension = project.getExtensions().getByType(SpotlessExtension.class);
@@ -50,7 +51,7 @@ static FormatterStep addSpotlessJavaFormatStep(Project project, String configura
} else {
logger.info("Using the native-image palantir-java-formatter");
return NativePalantirJavaFormatStep.create(
project.getRootProject().getConfigurations().getByName("palantirJavaFormatNative"));
project.getRootProject().getConfigurations().getByName(configurationName));
}
}
}
Original file line number Diff line number Diff line change
@@ -24,22 +24,15 @@
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

public class NativePalantirJavaFormatStep {
private static Logger logger = Logging.getLogger(NativePalantirJavaFormatStep.class);

private NativePalantirJavaFormatStep() {
}
private NativePalantirJavaFormatStep() {}

private static final String NAME = "palantir-java-format";

@@ -50,7 +43,6 @@ public static FormatterStep create(Configuration configuration) {
() -> {
File execFile = configuration.getSingleFile();
logger.info("Using native-image at {}", configuration.getSingleFile());
makeFileExecutable(execFile.toPath());
return new State(FileSignature.signAsSet(execFile));
},
State::createFormat);
@@ -77,22 +69,4 @@ FormatterFunc.Closeable createFormat() {
return FormatterFunc.Closeable.of(runner, this::format);
}
}

private static void makeFileExecutable(Path pathToExe) {
try {
Set<PosixFilePermission> existingPermissions =
Files.getPosixFilePermissions(pathToExe);
Files.setPosixFilePermissions(
pathToExe,
Stream.concat(
existingPermissions.stream(),
Stream.of(
PosixFilePermission.OWNER_EXECUTE,
PosixFilePermission.GROUP_EXECUTE,
PosixFilePermission.OTHERS_EXECUTE))
.collect(Collectors.toSet()));
} catch (IOException e) {
throw new RuntimeException("Failed to set execute permissions on native-image", e);
}
}
}
4 changes: 1 addition & 3 deletions palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
@@ -166,9 +166,7 @@ publishing {
extension="sh"
}
artifactId = 'palantir-java-format-native'

}
}
}