Skip to content

Commit

Permalink
Add missing braces, language hints and apply formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 11, 2024
1 parent 5aef7ae commit 87e7146
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.jspecify.annotations.Nullable;
import org.openrewrite.properties.CreatePropertiesFile;
import org.openrewrite.properties.PropertiesVisitor;
import org.openrewrite.properties.tree.Properties;
Expand All @@ -28,19 +28,18 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Value
@EqualsAndHashCode(callSuper = false)
public class SeparateApplicationPropertiesByProfile extends ScanningRecipe<SeparateApplicationPropertiesByProfile.Accumulator> {

@Override
public String getDisplayName() {
return "Separate application.properties by profile";
return "Separate `application.properties` by profile";
}

@Override
public String getDescription() {
return "Separating application.properties into separate files based on profiles while appending to any existing application-profile.properties.";
return "Separating `application.properties` into separate files based on profiles while appending to any existing `application-profile.properties`.";
}

@Override
Expand All @@ -52,9 +51,10 @@ public Accumulator getInitialValue(ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
if (!(tree instanceof Properties.File))
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (!(tree instanceof Properties.File)) {
return tree;
}

Properties.File propertyFile = (Properties.File) tree;
String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString());
Expand All @@ -65,8 +65,9 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
acc.propertyFileContent = getNewApplicationPropertyFileInfo(propertyFile.getContent());
}

if (sourcePath.matches("(?:.*/)?application-[^/]+\\.properties"))
if (sourcePath.matches("(?:.*/)?application-[^/]+\\.properties")) {
acc.fileNameToFilePath.put(pathArray[pathArray.length - 1], sourcePath);
}

return tree;
}
Expand All @@ -75,18 +76,21 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {

@Override
public Collection<? extends SourceFile> generate(Accumulator acc, ExecutionContext ctx) {
if (acc.propertyFileContent.isEmpty())
if (acc.propertyFileContent.isEmpty()) {
return Collections.emptyList();
}

Set<SourceFile> newApplicationPropertiesFiles = new HashSet<>();

for (Map.Entry<String, List<Properties.Content>> entry : acc.propertyFileContent.entrySet())
if (!acc.fileNameToFilePath.containsKey(entry.getKey()))
for (Map.Entry<String, List<Properties.Content>> entry : acc.propertyFileContent.entrySet()) {
if (!acc.fileNameToFilePath.containsKey(entry.getKey())) {
newApplicationPropertiesFiles.
add(new CreatePropertiesFile(acc.pathToApplicationProperties + entry.getKey(), "", null).
generate(new AtomicBoolean(true), ctx).
iterator().
next());
}
}

return newApplicationPropertiesFiles;
}
Expand All @@ -96,8 +100,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new PropertiesVisitor<ExecutionContext>() {
@Override
public Properties visitFile(Properties.File file, ExecutionContext ctx) {
if (acc.propertyFileContent.isEmpty())
if (acc.propertyFileContent.isEmpty()) {
return file;
}

String[] filePathArray = file.getSourcePath().toString().split("/");
String fileName = filePathArray[filePathArray.length - 1];
Expand All @@ -117,8 +122,9 @@ private Properties appendToExistingPropertiesFile(Properties.File file, List<Pro
private Properties deleteFromApplicationProperties(Properties.File applicationProperties) {
List<Properties.Content> newContent = new ArrayList<>();
for (Properties.Content c : applicationProperties.getContent()) {
if (isSeparator(c))
if (isSeparator(c)) {
break;
}
newContent.add(c);
}
return applicationProperties.getContent().equals(newContent) ? applicationProperties :
Expand All @@ -143,11 +149,12 @@ private List<Properties.Content> getContentForNewFile(List<Properties.Content> c
List<Properties.Content> list = new ArrayList<>();
while (index < contentList.size() && !isSeparator(contentList.get(index))) {
if (contentList.get(index) instanceof Properties.Entry &&
((Properties.Entry) contentList.get(index)).getKey().equals
("spring.config.activate.on-profile"))
((Properties.Entry) contentList.get(index)).getKey().equals
("spring.config.activate.on-profile")) {
list.add(0, contentList.get(index));
else
} else {
list.add(contentList.get(index));
}
index++;
}
return list;
Expand All @@ -159,9 +166,9 @@ private String getPathToApplicationProperties(String[] pathArray) {

private boolean isSeparator(Properties.Content c) {
return c instanceof Properties.Comment &&
((Properties.Comment) c).getMessage().equals("---") &&
((((Properties.Comment) c).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("HASH_TAG"))) ||
((Properties.Comment) c).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("EXCLAMATION_MARK")));
((Properties.Comment) c).getMessage().equals("---") &&
((((Properties.Comment) c).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("HASH_TAG"))) ||
((Properties.Comment) c).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("EXCLAMATION_MARK")));
}

public static class Accumulator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package org.openrewrite.java.spring;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.properties.Assertions.properties;

public class SeparateApplicationPropertiesByProfileTest implements RewriteTest {
class SeparateApplicationPropertiesByProfileTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new SeparateApplicationPropertiesByProfile());
Expand All @@ -29,7 +31,9 @@ public void defaults(RecipeSpec spec) {
@Test
void noApplicationProperties() {
rewriteRun(
org.openrewrite.properties.Assertions.properties("""
properties(
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -45,7 +49,9 @@ void noApplicationProperties() {
@Test
void noSeparateProfile() {
rewriteRun(
org.openrewrite.properties.Assertions.properties("""
properties(
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -58,13 +64,16 @@ void noSeparateProfile() {
);
}

@DocumentExample
@Test
void separateProfileWithAppend() {
rewriteRun(
org.openrewrite.properties.Assertions.properties(
properties(
//language=properties
"""
line1=line1
""",
//language=properties
"""
line1=line1
oauth2.clientId=9999999999999999999999
Expand All @@ -73,7 +82,8 @@ void separateProfileWithAppend() {
""",
sourceSpecs -> sourceSpecs.path("application-dev.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -96,6 +106,7 @@ void separateProfileWithAppend() {
oauth2.clientId=77777777777777
service.domainUrl=https://this.is.my.prod.url.com
""",
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -105,15 +116,16 @@ void separateProfileWithAppend() {
""",
sourceSpecs -> sourceSpecs.path("application.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
"""
app.config.currentEnvironment=LOCAL
""",
sourceSpecs -> sourceSpecs.path("application-local.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
#### XX Configuration ####
oauth2.clientId=77777777777777
Expand All @@ -127,16 +139,18 @@ void separateProfileWithAppend() {
@Test
void separateProfileWithoutAppend() {
rewriteRun(
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
oauth2.clientId=9999999999999999999999
service.domainUrl= https://this.is.my.dev.url.com
app.config.currentEnvironment=DEV
""",
sourceSpecs -> sourceSpecs.path("application-dev.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -159,6 +173,7 @@ void separateProfileWithoutAppend() {
oauth2.clientId=77777777777777
service.domainUrl=https://this.is.my.prod.url.com
""",
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -168,15 +183,17 @@ void separateProfileWithoutAppend() {
""",
sourceSpecs -> sourceSpecs.path("application.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
app.config.currentEnvironment=LOCAL
""",
sourceSpecs -> sourceSpecs.path("application-local.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
#### XX Configuration ####
oauth2.clientId=77777777777777
Expand All @@ -190,10 +207,12 @@ void separateProfileWithoutAppend() {
@Test
void pathToApplicationProperties() {
rewriteRun(
org.openrewrite.properties.Assertions.properties(
properties(
//language=properties
"""
line1=line1
""",
//language=properties
"""
line1=line1
oauth2.clientId=9999999999999999999999
Expand All @@ -202,7 +221,8 @@ void pathToApplicationProperties() {
""",
sourceSpecs -> sourceSpecs.path("folder1/folder2/application-dev.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -225,6 +245,7 @@ void pathToApplicationProperties() {
oauth2.clientId=77777777777777
service.domainUrl=https://this.is.my.prod.url.com
""",
//language=properties
"""
spring.application.name=Openrewrite-PR-Service
#PR-Service
Expand All @@ -234,15 +255,17 @@ void pathToApplicationProperties() {
""",
sourceSpecs -> sourceSpecs.path("folder1/folder2/application.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
app.config.currentEnvironment=LOCAL
""",
sourceSpecs -> sourceSpecs.path("folder1/folder2/application-local.properties")
),
org.openrewrite.properties.Assertions.properties(
properties(
null,
//language=properties
"""
#### XX Configuration ####
oauth2.clientId=77777777777777
Expand Down

0 comments on commit 87e7146

Please sign in to comment.