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

add properties to gradle profile #9207

Merged

Conversation

renanfranca
Copy link
Contributor

Copy link

codecov bot commented Mar 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e178946) to head (1a4f0c5).
Report is 3 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##                main     #9207   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity      2928      2940   +12     
===========================================
  Files            733       734    +1     
  Lines          12649     12681   +32     
  Branches         256       257    +1     
===========================================
+ Hits           12649     12681   +32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@renanfranca renanfranca requested a review from murdos March 13, 2024 13:47
@renanfranca renanfranca force-pushed the 8628-add-properties-to-gradle-profile branch from 8f852ea to 93c1f73 Compare March 13, 2024 14:27
if (!scriptPlugin.exists()) {
throw new MissingGradleProfileException(buildProfile);
}
addPropertyToProfile(command.property(), buildProfile);
Copy link
Contributor

Choose a reason for hiding this comment

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

How about doing directly:

Suggested change
addPropertyToProfile(command.property(), buildProfile);
addPropertyTo(command.property(), scriptPlugin);

You've just resolved the script plugin path, there's no need to resolve it again :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks 😁!

public MissingGradleProfileException(BuildProfileId profileId) {
super(
badRequest(GradleDependencyErrorKey.MISSING_PROFILE).message(
"Your gradle project does not contain the profile-%s.gradle.kts file".formatted(profileId)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Your gradle project does not contain the profile-%s.gradle.kts file".formatted(profileId)
"Your gradle project does not contain any "profile-%s.gradle.kts" precompiled script plugin".formatted(profileId)

}

private void addProperty(BuildProperty property, String buildGradleProfileFilePath) {
String gradlePropertyFormatted = convertToKotlinFormat(property);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
String gradlePropertyFormatted = convertToKotlinFormat(property);
String gradlePropertyDeclaration = propertyKotlinDeclaration(property);

return "val %s by extra(\"%s\")".formatted(convertToKotlinFormat(property.key()), property.value().get());
}

private String convertToKotlinFormat(PropertyKey key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This naming seems more explicit:

Suggested change
private String convertToKotlinFormat(PropertyKey key) {
private String toCamelCasedKotlinVariable(PropertyKey key) {


private void addProperty(BuildProperty property, String buildGradleProfileFilePath) {
String gradlePropertyFormatted = convertToKotlinFormat(property);
Optional<String> propertyLine = readPropertyFrom(convertToKotlinFormat(property.key()), buildGradleProfileFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you really need read the existing file?
Using a regexp like val %s by extra\\(([^)])\)".formatted(convertToKotlinFormat(property.key()) could probably work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review! Interesting, I will try it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am thinking here, let me share with you. What I need to know is if the property already exists to use a RegexReplacer instead of the RegexBefore. So I need to read the file to know if the property exists, but I dont need anymore to seek for the exact line if I use this Regex. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yeah, you're probably right. Let's keep it that way then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@renanfranca : I'm not convinced by e642e94 : relying on (a runtime) exception to adjust behavior is not a good idea, and is not really robust.
A runtime exception is not an explicit API: if FileReplacer changes its behavior and throw a different exception, or stop throwing this exception, that would break GradleCommandHandler unexpectedly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the careful look and constructive review, I totally agree with you. I am going to drop this commit now 👍

@@ -32,6 +32,9 @@ error.missing-java-build-configuration-files.detail=Aucun fichier de configurati
error.invalid-toml-version-catalog-file.title=Catalogue de version Gradle TOML invalide
error.invalid-toml-version-catalog-file.detail=Le fichier gradle/libs.versions.toml est invalide

error.missing-gradle-profile.title=Profil Gradle manquant
error.missing-gradle-profile.detail=Votre projet gradle manque du fichier de profil cible
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
error.missing-gradle-profile.detail=Votre projet gradle manque du fichier de profil cible
error.missing-gradle-profile.detail=Votre projet Gradle ne contient pas de fichier correspondant au profil

@renanfranca renanfranca force-pushed the 8628-add-properties-to-gradle-profile branch from 93c1f73 to e642e94 Compare March 14, 2024 14:08
@renanfranca renanfranca requested a review from murdos March 14, 2024 14:39
}

private static MandatoryReplacer addNewPropertyReplacer(String gradlePropertyDeclaration) {
return new MandatoryReplacer(new RegexNeedleBeforeReplacer(always(), GRADLE_PROPERTY_NEEDLE), gradlePropertyDeclaration);
Copy link
Contributor

Choose a reason for hiding this comment

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

The API would be more explicit if the parameter would be the BuildProperty, and if you were doing that (calling method convertToKotlinFormat()):
return new MandatoryReplacer(new RegexNeedleBeforeReplacer(always(), GRADLE_PROPERTY_NEEDLE), convertToKotlinFormat(property));

@@ -308,22 +320,14 @@ private String toCamelCasedKotlinVariable(PropertyKey key) {
}

@ExcludeFromGeneratedCodeCoverage(reason = "The exception handling is hard to test and an implementation detail")
private Optional<String> readPropertyFrom(String gradlePropertyFormatted, Path buildGradleProfileFile) {
private Boolean propertyExistsFrom(String gradlePropertyFormatted, Path buildGradleProfileFile) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use primitive boolean: that removes the 3rd possible value: null

Copy link
Contributor

Choose a reason for hiding this comment

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

Also here the API would be clearer if you use the BuildProperty as parameter rather than String gradlePropertyFormatted

@renanfranca renanfranca force-pushed the 8628-add-properties-to-gradle-profile branch from e642e94 to 92a53cd Compare March 15, 2024 10:17
@renanfranca renanfranca force-pushed the 8628-add-properties-to-gradle-profile branch from 92a53cd to 16fa215 Compare March 15, 2024 10:23
@renanfranca renanfranca requested a review from murdos March 15, 2024 10:43
murdos
murdos previously approved these changes Mar 15, 2024
try {
String content = Files.readString(buildGradleProfileFile);

return content.contains(toCamelCasedKotlinVariable(key));
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be more robust to verify the variable declaration, rather than just the variable appearance, no?

Suggested change
return content.contains(toCamelCasedKotlinVariable(key));
return content.contains("val %s by extra(".formatted(toCamelCasedKotlinVariable(key));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right! Only the key could find a name out of context.


return content.contains(toCamelCasedKotlinVariable(key));
} catch (IOException e) {
throw GeneratorException.technicalError("Error writing pom: " + e.getMessage(), e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Exception message should be updated, there's no pom involved :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, copy and paste is a hell 😅


private void addPropertyTo(BuildProperty property, File buildGradleFile) {
MandatoryReplacer replacer;
if (propertyExistsFrom(property.key(), buildGradleFile.toPath())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 The code is now clean and easy to read and understand :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks murdos 🎉! I have to use Value Objects as much as I can 😁

… and rename parameter for a better description
@renanfranca renanfranca merged commit 5737c6f into jhipster:main Mar 15, 2024
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants