-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
add properties to gradle profile #9207
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
8f852ea
to
93c1f73
Compare
if (!scriptPlugin.exists()) { | ||
throw new MissingGradleProfileException(buildProfile); | ||
} | ||
addPropertyToProfile(command.property(), buildProfile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about doing directly:
addPropertyToProfile(command.property(), buildProfile); | |
addPropertyTo(command.property(), scriptPlugin); |
You've just resolved the script plugin path, there's no need to resolve it again :)
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
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:
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@murdos : I did as we agreed: (improve existing property replace). But then I thought on a solution and implemented it too check if UnknownCurrentValueException throws instead of read the gradle profile file to know if a property did not exist.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
93c1f73
to
e642e94
Compare
} | ||
|
||
private static MandatoryReplacer addNewPropertyReplacer(String gradlePropertyDeclaration) { | ||
return new MandatoryReplacer(new RegexNeedleBeforeReplacer(always(), GRADLE_PROPERTY_NEEDLE), gradlePropertyDeclaration); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
e642e94
to
92a53cd
Compare
92a53cd
to
16fa215
Compare
try { | ||
String content = Files.readString(buildGradleProfileFile); | ||
|
||
return content.contains(toCamelCasedKotlinVariable(key)); |
There was a problem hiding this comment.
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?
return content.contains(toCamelCasedKotlinVariable(key)); | |
return content.contains("val %s by extra(".formatted(toCamelCasedKotlinVariable(key)); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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())) { |
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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
See