From 1b3623021f594e6328ea100152099b940b9b14bc Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 14:29:34 -0700 Subject: [PATCH 01/21] First draft of changes --- ...eparateApplicationPropertiesByProfile.java | 144 ++++++++++++++++++ ...ateApplicationPropertiesByProfileTest.java | 76 +++++++++ 2 files changed, 220 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java create mode 100644 src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java new file mode 100644 index 000000000..d73ca703f --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -0,0 +1,144 @@ +package org.openrewrite.java.spring; +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.intellij.lang.annotations.Language; +import org.openrewrite.*; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.properties.CreatePropertiesFile; +import org.openrewrite.properties.PropertiesVisitor; +import org.openrewrite.properties.tree.Properties; +import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; + + +@Value +@EqualsAndHashCode(callSuper = true) +public class SeparateApplicationPropertiesByProfile extends ScanningRecipe { + + @Override + public String getDisplayName() { + return "Separate application.properties by profile"; + } + + @Override + public String getDescription() { + return "Separating application.properties into separate files based on profiles."; + } + + @Override + public Accumulator getInitialValue(ExecutionContext ctx) { + return new Accumulator(); + } + + @Override + public TreeVisitor getScanner(Accumulator acc) { + return Preconditions.check( + new FindSourceFiles("**/application.properties"), + new TreeVisitor() { + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { + if (!(tree instanceof SourceFile)) return tree; + + SourceFile sourceFile = (SourceFile) tree; + String sourcePath = PathUtils.separatorsToUnix(sourceFile.getSourcePath().toString()); + + if (sourcePath.endsWith("application.properties")) + acc.existingApplicationProperties = sourceFile; + + return tree; + } + }); + } + + @Override + public Collection generate(Accumulator acc, ExecutionContext ctx) { + if (acc.existingApplicationProperties == null) return Collections.emptyList(); + + for (Map.Entry> entry : this.getNewApplicationPropertyFileInfo(acc).entrySet()) { + String fileName = entry.getKey(); + @Language("properties") String fileContent = String.join("\n", entry.getValue()); + + acc.newApplicationPropertyFiles. + add(new CreatePropertiesFile(fileName, fileContent, null). + generate(new AtomicBoolean(true), ctx). + iterator(). + next() + ); + } + + return acc.newApplicationPropertyFiles; + } + + @Override + public TreeVisitor getVisitor(Accumulator acc) { + return new PropertiesVisitor() { + @Override + public Properties visitFile(Properties.File file, ExecutionContext ctx) { + if (!file.getSourcePath().toString().equals("application.properties")) return file; + + return deleteFromApplicationProperties(file); + } + }; + } + + private Properties deleteFromApplicationProperties(Properties.File applicationProperties) { + List newContent = new ArrayList<>(); + + for (Properties.Content c : applicationProperties.getContent()) { + + if (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")))) break; + + newContent.add(c); + } + + if (applicationProperties.getContent().equals(newContent)) return applicationProperties; + + return applicationProperties.withContent(newContent); + } + + private Map> getNewApplicationPropertyFileInfo(Accumulator acc) { + if (acc.existingApplicationProperties == null) return new HashMap<>(); + + Map> map = new HashMap<>(); + String[] arr = acc.existingApplicationProperties.printAll().split("\n"); + int index = 0; + + while (index < arr.length) { + if (arr[index].equals("#---") || arr[index].equals("!---")) { + index++; + List list = this.getLinesForNewFile(arr, index); + map.put(list.get(0), list.subList(1, list.size())); + } + index++; + } + + return map; + } + + private List getLinesForNewFile(String[] arr, int index) { + List list = new ArrayList<>(); + + while (index < arr.length && !arr[index].equals("#---") && !arr[index].equals("!---")) { + if (arr[index].startsWith("spring.config.activate.on-profile=")) list.add( + 0, + "application-" + + arr[index].split("=")[1] + + ".properties"); + + else if (!arr[index].isEmpty()) list.add(arr[index]); + + index++; + } + + return list; + } + + public static class Accumulator { + @Nullable SourceFile existingApplicationProperties; + Set newApplicationPropertyFiles = new HashSet<>(); + } +} + diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java new file mode 100644 index 000000000..35631ad6e --- /dev/null +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -0,0 +1,76 @@ +package org.openrewrite.java.spring; + +import org.openrewrite.test.RewriteTest; +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; + + +public class SeparateApplicationPropertiesByProfileTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new SeparateApplicationPropertiesByProfile()); + } + + @Test + void separateProfile() { + rewriteRun( + org.openrewrite.properties.Assertions.properties( + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + !--- + spring.config.activate.on-profile=dev + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + #--- + spring.config.activate.on-profile=local + app.config.currentEnvironment=LOCAL + + + #--- + #### XX Configuration #### + spring.config.activate.on-profile=prod + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + """, + sourceSpecs -> sourceSpecs.path("application.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + 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( + null, + """ + app.config.currentEnvironment=LOCAL + """, + sourceSpecs -> sourceSpecs.path("application-local.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + #### XX Configuration #### + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + sourceSpecs -> sourceSpecs.path("application-prod.properties") + ) + ); + } +} From cb8dbe410bd4054bbde9234711ed72d9e4f38bc7 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 16:24:25 -0700 Subject: [PATCH 02/21] Added test to ensure application.properties doesn't change if no additional profiles are specified --- ...parateApplicationPropertiesByProfileTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 35631ad6e..078e80514 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -11,6 +11,22 @@ public void defaults(RecipeSpec spec) { spec.recipe(new SeparateApplicationPropertiesByProfile()); } + @Test + void noSeparateProfile() { + rewriteRun( + org.openrewrite.properties.Assertions.properties(""" + spring.application.name=Openrewrite-PR-Service + #PR-Service + + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + + """, + sourceSpecs -> sourceSpecs.path("application.properties")) + ); + } + @Test void separateProfile() { rewriteRun( From ad782692a23c7f65988b200554baaadbc28ed936 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 16:41:04 -0700 Subject: [PATCH 03/21] Used Auto Formatter --- ...eparateApplicationPropertiesByProfile.java | 8 +- ...ateApplicationPropertiesByProfileTest.java | 134 +++++++++--------- 2 files changed, 73 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index d73ca703f..34010ad20 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -1,4 +1,5 @@ package org.openrewrite.java.spring; + import lombok.EqualsAndHashCode; import lombok.Value; import org.intellij.lang.annotations.Language; @@ -7,6 +8,7 @@ import org.openrewrite.properties.CreatePropertiesFile; import org.openrewrite.properties.PropertiesVisitor; import org.openrewrite.properties.tree.Properties; + import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -89,7 +91,8 @@ private Properties deleteFromApplicationProperties(Properties.File applicationPr if (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")))) break; + ((Properties.Comment) c).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("EXCLAMATION_MARK")))) + break; newContent.add(c); } @@ -137,7 +140,8 @@ private List getLinesForNewFile(String[] arr, int index) { } public static class Accumulator { - @Nullable SourceFile existingApplicationProperties; + @Nullable + SourceFile existingApplicationProperties; Set newApplicationPropertyFiles = new HashSet<>(); } } diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 078e80514..680f75c5e 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -14,79 +14,79 @@ public void defaults(RecipeSpec spec) { @Test void noSeparateProfile() { rewriteRun( - org.openrewrite.properties.Assertions.properties(""" - spring.application.name=Openrewrite-PR-Service - #PR-Service - - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - - """, - sourceSpecs -> sourceSpecs.path("application.properties")) + org.openrewrite.properties.Assertions.properties(""" + spring.application.name=Openrewrite-PR-Service + #PR-Service + + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + + """, + sourceSpecs -> sourceSpecs.path("application.properties")) ); } @Test void separateProfile() { rewriteRun( - org.openrewrite.properties.Assertions.properties( - """ - spring.application.name=Openrewrite-PR-Service - #PR-Service - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - !--- - spring.config.activate.on-profile=dev - oauth2.clientId=9999999999999999999999 - service.domainUrl= https://this.is.my.dev.url.com - app.config.currentEnvironment=DEV - #--- - spring.config.activate.on-profile=local - app.config.currentEnvironment=LOCAL - - - #--- - #### XX Configuration #### - spring.config.activate.on-profile=prod - oauth2.clientId=77777777777777 - service.domainUrl=https://this.is.my.prod.url.com - """, - """ - spring.application.name=Openrewrite-PR-Service - #PR-Service - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - """, - sourceSpecs -> sourceSpecs.path("application.properties") - ), - org.openrewrite.properties.Assertions.properties( - null, - """ - 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( - null, - """ - app.config.currentEnvironment=LOCAL - """, - sourceSpecs -> sourceSpecs.path("application-local.properties") - ), - org.openrewrite.properties.Assertions.properties( - null, - """ - #### XX Configuration #### - oauth2.clientId=77777777777777 - service.domainUrl=https://this.is.my.prod.url.com - """, - sourceSpecs -> sourceSpecs.path("application-prod.properties") - ) + org.openrewrite.properties.Assertions.properties( + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + !--- + spring.config.activate.on-profile=dev + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + #--- + spring.config.activate.on-profile=local + app.config.currentEnvironment=LOCAL + + + #--- + #### XX Configuration #### + spring.config.activate.on-profile=prod + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + """, + sourceSpecs -> sourceSpecs.path("application.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + 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( + null, + """ + app.config.currentEnvironment=LOCAL + """, + sourceSpecs -> sourceSpecs.path("application-local.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + #### XX Configuration #### + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + sourceSpecs -> sourceSpecs.path("application-prod.properties") + ) ); } } From 75673fa4ef012e3138383fa8968ba425608a1e41 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 16:44:22 -0700 Subject: [PATCH 04/21] More Formatting --- .../SeparateApplicationPropertiesByProfile.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 34010ad20..b8e57582b 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -125,11 +125,12 @@ private List getLinesForNewFile(String[] arr, int index) { List list = new ArrayList<>(); while (index < arr.length && !arr[index].equals("#---") && !arr[index].equals("!---")) { - if (arr[index].startsWith("spring.config.activate.on-profile=")) list.add( - 0, - "application-" + - arr[index].split("=")[1] + - ".properties"); + if (arr[index].startsWith("spring.config.activate.on-profile=")) + list.add(0, + "application-" + + arr[index].split("=")[1] + + ".properties" + ); else if (!arr[index].isEmpty()) list.add(arr[index]); From 27b239899f71c57f8aa5b50ff7968469591a8586 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 16:45:43 -0700 Subject: [PATCH 05/21] More Formatting 2 --- .../java/spring/SeparateApplicationPropertiesByProfile.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index b8e57582b..e52c4be3f 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -126,11 +126,7 @@ private List getLinesForNewFile(String[] arr, int index) { while (index < arr.length && !arr[index].equals("#---") && !arr[index].equals("!---")) { if (arr[index].startsWith("spring.config.activate.on-profile=")) - list.add(0, - "application-" + - arr[index].split("=")[1] + - ".properties" - ); + list.add(0, "application-" + arr[index].split("=")[1] + ".properties"); else if (!arr[index].isEmpty()) list.add(arr[index]); From b390d3a50fb45ad264f5ab3afde5073120db86c9 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Sun, 11 Aug 2024 21:30:38 -0700 Subject: [PATCH 06/21] fixing edge case 1 --- ...eparateApplicationPropertiesByProfile.java | 131 ++++++++++++------ ...ateApplicationPropertiesByProfileTest.java | 25 ++-- 2 files changed, 100 insertions(+), 56 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index e52c4be3f..10b0a8978 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -34,38 +34,67 @@ public Accumulator getInitialValue(ExecutionContext ctx) { @Override public TreeVisitor getScanner(Accumulator acc) { - return Preconditions.check( - new FindSourceFiles("**/application.properties"), - new TreeVisitor() { - @Override - public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { - if (!(tree instanceof SourceFile)) return tree; - - SourceFile sourceFile = (SourceFile) tree; - String sourcePath = PathUtils.separatorsToUnix(sourceFile.getSourcePath().toString()); - - if (sourcePath.endsWith("application.properties")) - acc.existingApplicationProperties = sourceFile; - - return tree; - } - }); + return new TreeVisitor() { + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { + if (!(tree instanceof Properties.File)) return tree; + + Properties.File propertyFile = (Properties.File) tree; + String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString()); + + if (sourcePath.matches("application.properties")) + acc.existingApplicationProperties = propertyFile; + + if (sourcePath.matches("application-.+\\.properties")) { + String s = getNewFileContentString(propertyFile.getContent()); + acc.existingApplicationEnvProperties.put(sourcePath, s); + return null; + } + + return tree; + } + }; + } + + private String getNewFileContentString(List content) { + String fileContent = ""; + for (Properties.Content c : content) { + if (c instanceof Properties.Entry) + fileContent += ((Properties.Entry) c).getKey() + "=" + ((Properties.Entry) c).getValue().getText() + "\n"; + + else if (c instanceof Properties.Comment) + fileContent += ((Properties.Comment) c).getMessage() + "\n"; + + } + return fileContent; } @Override public Collection generate(Accumulator acc, ExecutionContext ctx) { if (acc.existingApplicationProperties == null) return Collections.emptyList(); - for (Map.Entry> entry : this.getNewApplicationPropertyFileInfo(acc).entrySet()) { + for (Map.Entry> entry : this.getNewApplicationPropertyFileInfo(acc).entrySet()) { String fileName = entry.getKey(); - @Language("properties") String fileContent = String.join("\n", entry.getValue()); - - acc.newApplicationPropertyFiles. - add(new CreatePropertiesFile(fileName, fileContent, null). - generate(new AtomicBoolean(true), ctx). - iterator(). - next() - ); + @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); + + if (acc.existingApplicationEnvProperties.containsKey(fileName)) { + fileContent += acc.existingApplicationEnvProperties.get(fileName) + "\n"; + System.out.println(fileContent); + acc.newApplicationPropertyFiles. + add((Properties.File) new CreatePropertiesFile(fileName, fileContent, true). + generate(new AtomicBoolean(true), ctx). + iterator(). + next() + ); + } + else { + acc.newApplicationPropertyFiles. + add((Properties.File) new CreatePropertiesFile(fileName, fileContent, null). + generate(new AtomicBoolean(true), ctx). + iterator(). + next() + ); + } } return acc.newApplicationPropertyFiles; @@ -87,11 +116,7 @@ private Properties deleteFromApplicationProperties(Properties.File applicationPr List newContent = new ArrayList<>(); for (Properties.Content c : applicationProperties.getContent()) { - - if (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")))) + if (isSeparator(c)) break; newContent.add(c); @@ -102,18 +127,19 @@ private Properties deleteFromApplicationProperties(Properties.File applicationPr return applicationProperties.withContent(newContent); } - private Map> getNewApplicationPropertyFileInfo(Accumulator acc) { + private Map> getNewApplicationPropertyFileInfo(Accumulator acc) { if (acc.existingApplicationProperties == null) return new HashMap<>(); - Map> map = new HashMap<>(); - String[] arr = acc.existingApplicationProperties.printAll().split("\n"); + Map> map = new HashMap<>(); + List contentList = acc.existingApplicationProperties.getContent(); int index = 0; - while (index < arr.length) { - if (arr[index].equals("#---") || arr[index].equals("!---")) { - index++; - List list = this.getLinesForNewFile(arr, index); - map.put(list.get(0), list.subList(1, list.size())); + while (index < contentList.size()) { + if (isSeparator(contentList.get(index))) { + // index++; + List newContent = getContentForNewFile(contentList, ++index); + map.put("application-" + ((Properties.Entry)newContent.get(0)).getValue().getText() + ".properties", + newContent.subList(1, newContent.size())); } index++; } @@ -121,14 +147,17 @@ private Map> getNewApplicationPropertyFileInfo(Accumulator return map; } - private List getLinesForNewFile(String[] arr, int index) { - List list = new ArrayList<>(); + private List getContentForNewFile(List contentList, int index) { + List list = new ArrayList<>(); - while (index < arr.length && !arr[index].equals("#---") && !arr[index].equals("!---")) { - if (arr[index].startsWith("spring.config.activate.on-profile=")) - list.add(0, "application-" + arr[index].split("=")[1] + ".properties"); + while (index < contentList.size() && !isSeparator(contentList.get(index))) { - else if (!arr[index].isEmpty()) list.add(arr[index]); + if (contentList.get(index) instanceof Properties.Entry && + ((Properties.Entry) contentList.get(index)).getKey().equals("spring.config.activate.on-profile")) + list.add(0, contentList.get(index)); + + else + list.add(contentList.get(index)); index++; } @@ -136,10 +165,20 @@ private List getLinesForNewFile(String[] arr, int index) { return list; } + 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"))); + } + public static class Accumulator { @Nullable - SourceFile existingApplicationProperties; - Set newApplicationPropertyFiles = new HashSet<>(); + Properties.File existingApplicationProperties; + + Set newApplicationPropertyFiles = new HashSet<>(); + Map existingApplicationEnvProperties = new HashMap<>(); } } + diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 680f75c5e..7c7a552d5 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -30,6 +30,20 @@ void noSeparateProfile() { @Test void separateProfile() { rewriteRun( + org.openrewrite.properties.Assertions.properties( + """ + line1=line1 + line2=line2 + """, + """ + oauth2.clientId=9999999999999999999999 + service.domainUrl=https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + line1=line1 + line2=line2 + """, + sourceSpecs -> sourceSpecs.path("application-dev.properties") + ), org.openrewrite.properties.Assertions.properties( """ spring.application.name=Openrewrite-PR-Service @@ -62,15 +76,6 @@ void separateProfile() { """, sourceSpecs -> sourceSpecs.path("application.properties") ), - org.openrewrite.properties.Assertions.properties( - null, - """ - 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( null, """ @@ -81,7 +86,7 @@ void separateProfile() { org.openrewrite.properties.Assertions.properties( null, """ - #### XX Configuration #### + ### XX Configuration #### oauth2.clientId=77777777777777 service.domainUrl=https://this.is.my.prod.url.com """, From 26f3c1905174a03940cea9a2f34a8ac2ed3b2a98 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 00:36:19 -0700 Subject: [PATCH 07/21] unable to append to existing application-prof.properties --- ...eparateApplicationPropertiesByProfile.java | 77 +++++----- ...ateApplicationPropertiesByProfileTest.java | 140 +++++++++--------- 2 files changed, 102 insertions(+), 115 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 10b0a8978..6209404d2 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -32,6 +32,18 @@ public Accumulator getInitialValue(ExecutionContext ctx) { return new Accumulator(); } + @Override + public TreeVisitor getVisitor(Accumulator acc) { + return new PropertiesVisitor() { + @Override + public Properties visitFile(Properties.File file, ExecutionContext ctx) { + if (!file.getSourcePath().toString().equals("application.properties")) return file; + + return deleteFromApplicationProperties(file); + } + }; + } + @Override public TreeVisitor getScanner(Accumulator acc) { return new TreeVisitor() { @@ -48,7 +60,6 @@ public TreeVisitor getScanner(Accumulator acc) { if (sourcePath.matches("application-.+\\.properties")) { String s = getNewFileContentString(propertyFile.getContent()); acc.existingApplicationEnvProperties.put(sourcePath, s); - return null; } return tree; @@ -56,19 +67,6 @@ public TreeVisitor getScanner(Accumulator acc) { }; } - private String getNewFileContentString(List content) { - String fileContent = ""; - for (Properties.Content c : content) { - if (c instanceof Properties.Entry) - fileContent += ((Properties.Entry) c).getKey() + "=" + ((Properties.Entry) c).getValue().getText() + "\n"; - - else if (c instanceof Properties.Comment) - fileContent += ((Properties.Comment) c).getMessage() + "\n"; - - } - return fileContent; - } - @Override public Collection generate(Accumulator acc, ExecutionContext ctx) { if (acc.existingApplicationProperties == null) return Collections.emptyList(); @@ -77,39 +75,32 @@ public Collection generate(Accumulator acc, ExecutionConte String fileName = entry.getKey(); @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); - if (acc.existingApplicationEnvProperties.containsKey(fileName)) { + if (acc.existingApplicationEnvProperties.containsKey(fileName)) fileContent += acc.existingApplicationEnvProperties.get(fileName) + "\n"; - System.out.println(fileContent); - acc.newApplicationPropertyFiles. - add((Properties.File) new CreatePropertiesFile(fileName, fileContent, true). - generate(new AtomicBoolean(true), ctx). - iterator(). - next() - ); - } - else { - acc.newApplicationPropertyFiles. - add((Properties.File) new CreatePropertiesFile(fileName, fileContent, null). - generate(new AtomicBoolean(true), ctx). - iterator(). - next() - ); - } + + acc.newApplicationPropertyFiles. + add(new CreatePropertiesFile(fileName, fileContent, true). + generate(new AtomicBoolean(true), ctx). + iterator(). + next() + ); + } return acc.newApplicationPropertyFiles; } - @Override - public TreeVisitor getVisitor(Accumulator acc) { - return new PropertiesVisitor() { - @Override - public Properties visitFile(Properties.File file, ExecutionContext ctx) { - if (!file.getSourcePath().toString().equals("application.properties")) return file; + private String getNewFileContentString(List content) { + String fileContent = ""; + for (Properties.Content c : content) { + if (c instanceof Properties.Entry) + fileContent += ((Properties.Entry) c).getKey() + "=" + ((Properties.Entry) c).getValue().getText() + "\n"; - return deleteFromApplicationProperties(file); - } - }; + else if (c instanceof Properties.Comment) + fileContent += ((Properties.Comment) c).getMessage() + "\n"; + + } + return fileContent; } private Properties deleteFromApplicationProperties(Properties.File applicationProperties) { @@ -138,7 +129,7 @@ private Map> getNewApplicationPropertyFileInfo( if (isSeparator(contentList.get(index))) { // index++; List newContent = getContentForNewFile(contentList, ++index); - map.put("application-" + ((Properties.Entry)newContent.get(0)).getValue().getText() + ".properties", + map.put("application-" + ((Properties.Entry) newContent.get(0)).getValue().getText() + ".properties", newContent.subList(1, newContent.size())); } index++; @@ -169,14 +160,14 @@ 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).getDelimiter().equals(Properties.Comment.Delimiter.valueOf("EXCLAMATION_MARK"))); } public static class Accumulator { @Nullable Properties.File existingApplicationProperties; - Set newApplicationPropertyFiles = new HashSet<>(); + Set newApplicationPropertyFiles = new HashSet<>(); Map existingApplicationEnvProperties = new HashMap<>(); } } diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 7c7a552d5..c41c5363e 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -14,84 +14,80 @@ public void defaults(RecipeSpec spec) { @Test void noSeparateProfile() { rewriteRun( - org.openrewrite.properties.Assertions.properties(""" - spring.application.name=Openrewrite-PR-Service - #PR-Service - - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - - """, - sourceSpecs -> sourceSpecs.path("application.properties")) + org.openrewrite.properties.Assertions.properties(""" + spring.application.name=Openrewrite-PR-Service + #PR-Service + + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + + """, + sourceSpecs -> sourceSpecs.path("application.properties")) ); } @Test void separateProfile() { rewriteRun( - org.openrewrite.properties.Assertions.properties( - """ - line1=line1 - line2=line2 - """, - """ - oauth2.clientId=9999999999999999999999 - service.domainUrl=https://this.is.my.dev.url.com - app.config.currentEnvironment=DEV - line1=line1 - line2=line2 - """, - sourceSpecs -> sourceSpecs.path("application-dev.properties") - ), - org.openrewrite.properties.Assertions.properties( - """ - spring.application.name=Openrewrite-PR-Service - #PR-Service - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - !--- - spring.config.activate.on-profile=dev - oauth2.clientId=9999999999999999999999 - service.domainUrl= https://this.is.my.dev.url.com - app.config.currentEnvironment=DEV - #--- - spring.config.activate.on-profile=local - app.config.currentEnvironment=LOCAL - - - #--- - #### XX Configuration #### - spring.config.activate.on-profile=prod - oauth2.clientId=77777777777777 - service.domainUrl=https://this.is.my.prod.url.com - """, - """ - spring.application.name=Openrewrite-PR-Service - #PR-Service - base-url.PR-services=http://my.url.com - exchange-token=1234567890 - exchange-tokens=${base-url.PR-services}/exchange-token - """, - sourceSpecs -> sourceSpecs.path("application.properties") - ), - org.openrewrite.properties.Assertions.properties( - null, - """ - app.config.currentEnvironment=LOCAL - """, - sourceSpecs -> sourceSpecs.path("application-local.properties") - ), - org.openrewrite.properties.Assertions.properties( - null, - """ - ### XX Configuration #### - oauth2.clientId=77777777777777 - service.domainUrl=https://this.is.my.prod.url.com - """, - sourceSpecs -> sourceSpecs.path("application-prod.properties") - ) + recipeSpec -> recipeSpec.cycles(3).expectedCyclesThatMakeChanges(1), + org.openrewrite.properties.Assertions.properties( + null, + """ + 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( + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + !--- + spring.config.activate.on-profile=dev + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + #--- + spring.config.activate.on-profile=local + app.config.currentEnvironment=LOCAL + + + #--- + #### XX Configuration #### + spring.config.activate.on-profile=prod + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + """, + sourceSpecs -> sourceSpecs.path("application.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + app.config.currentEnvironment=LOCAL + """, + sourceSpecs -> sourceSpecs.path("application-local.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + ### XX Configuration #### + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + sourceSpecs -> sourceSpecs.path("application-prod.properties") + ) ); } } From e1eb52036529c463fff4e8a9da0df4fdf923d627 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 00:44:03 -0700 Subject: [PATCH 08/21] testing for no application.properties --- ...ateApplicationPropertiesByProfileTest.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index c41c5363e..e80928614 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -11,6 +11,22 @@ public void defaults(RecipeSpec spec) { spec.recipe(new SeparateApplicationPropertiesByProfile()); } + @Test + void noApplicationProperties() { + rewriteRun( + org.openrewrite.properties.Assertions.properties(""" + spring.application.name=Openrewrite-PR-Service + #PR-Service + + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + + """, + sourceSpecs -> sourceSpecs.path("application-dev.properties")) + ); + } + @Test void noSeparateProfile() { rewriteRun( @@ -32,8 +48,11 @@ void separateProfile() { rewriteRun( recipeSpec -> recipeSpec.cycles(3).expectedCyclesThatMakeChanges(1), org.openrewrite.properties.Assertions.properties( - null, """ + line1=line1 + """, + """ + line1=line1 oauth2.clientId=9999999999999999999999 service.domainUrl=https://this.is.my.dev.url.com app.config.currentEnvironment=DEV From b8c6239c1404f255ac315774eb39e6e6bd472afc Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 01:05:54 -0700 Subject: [PATCH 09/21] removed cycles from tests --- .../spring/SeparateApplicationPropertiesByProfileTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index e80928614..a8aa007ec 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -46,13 +46,9 @@ void noSeparateProfile() { @Test void separateProfile() { rewriteRun( - recipeSpec -> recipeSpec.cycles(3).expectedCyclesThatMakeChanges(1), org.openrewrite.properties.Assertions.properties( + null, """ - line1=line1 - """, - """ - line1=line1 oauth2.clientId=9999999999999999999999 service.domainUrl=https://this.is.my.dev.url.com app.config.currentEnvironment=DEV From cce50195e8c744378eba6a4e21cbed33f808bd89 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 21:46:11 -0700 Subject: [PATCH 10/21] This code works! --- ...eparateApplicationPropertiesByProfile.java | 104 +++++++++++------- ...ateApplicationPropertiesByProfileTest.java | 70 +++++++++++- 2 files changed, 132 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 6209404d2..b837508cc 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -11,6 +11,8 @@ import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; +import java.util.stream.Stream; @Value @@ -24,7 +26,7 @@ public String getDisplayName() { @Override public String getDescription() { - return "Separating application.properties into separate files based on profiles."; + return "Separating application.properties into separate files based on profiles while appending to any existing application-profile.properties."; } @Override @@ -32,18 +34,6 @@ public Accumulator getInitialValue(ExecutionContext ctx) { return new Accumulator(); } - @Override - public TreeVisitor getVisitor(Accumulator acc) { - return new PropertiesVisitor() { - @Override - public Properties visitFile(Properties.File file, ExecutionContext ctx) { - if (!file.getSourcePath().toString().equals("application.properties")) return file; - - return deleteFromApplicationProperties(file); - } - }; - } - @Override public TreeVisitor getScanner(Accumulator acc) { return new TreeVisitor() { @@ -54,14 +44,14 @@ public TreeVisitor getScanner(Accumulator acc) { Properties.File propertyFile = (Properties.File) tree; String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString()); - if (sourcePath.matches("application.properties")) + if (sourcePath.matches("application.properties")) { acc.existingApplicationProperties = propertyFile; - - if (sourcePath.matches("application-.+\\.properties")) { - String s = getNewFileContentString(propertyFile.getContent()); - acc.existingApplicationEnvProperties.put(sourcePath, s); + acc.propertyFileContent = getNewApplicationPropertyFileInfo(acc); } + if (sourcePath.matches("application-.+\\.properties")) + acc.existingPropertiesFiles.put(sourcePath, propertyFile); + return tree; } }; @@ -71,36 +61,66 @@ public TreeVisitor getScanner(Accumulator acc) { public Collection generate(Accumulator acc, ExecutionContext ctx) { if (acc.existingApplicationProperties == null) return Collections.emptyList(); - for (Map.Entry> entry : this.getNewApplicationPropertyFileInfo(acc).entrySet()) { + for (Map.Entry> entry : acc.propertyFileContent.entrySet()) { String fileName = entry.getKey(); - @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); - - if (acc.existingApplicationEnvProperties.containsKey(fileName)) - fileContent += acc.existingApplicationEnvProperties.get(fileName) + "\n"; - acc.newApplicationPropertyFiles. - add(new CreatePropertiesFile(fileName, fileContent, true). - generate(new AtomicBoolean(true), ctx). - iterator(). - next() - ); + @Language("properties") + String fileContent = getNewFileContentString(entry.getValue()); + if (!acc.existingPropertiesFiles.containsKey(fileName)) { + acc.newApplicationPropertyFiles. + add(new CreatePropertiesFile(fileName, fileContent, null). + generate(new AtomicBoolean(true), ctx). + iterator(). + next() + ); + } } - return acc.newApplicationPropertyFiles; } + @Override + public TreeVisitor getVisitor(Accumulator acc) { + return new PropertiesVisitor() { + @Override + public Properties visitFile(Properties.File file, ExecutionContext ctx) { + String fileName = file.getSourcePath().toString(); + + if (fileName.equals("application.properties")) + return deleteFromApplicationProperties(file); + + if (acc.existingPropertiesFiles.containsKey(fileName) && acc.propertyFileContent.containsKey(fileName)) + return appendToExistingPropertiesFile(file, acc.propertyFileContent.get(fileName)); + + return file; + } + }; + } + private String getNewFileContentString(List content) { - String fileContent = ""; + StringBuilder fileContent = new StringBuilder(); + for (Properties.Content c : content) { if (c instanceof Properties.Entry) - fileContent += ((Properties.Entry) c).getKey() + "=" + ((Properties.Entry) c).getValue().getText() + "\n"; + fileContent. + append(((Properties.Entry) c).getKey()). + append("="). + append(((Properties.Entry) c).getValue().getText()). + append("\n"); else if (c instanceof Properties.Comment) - fileContent += ((Properties.Comment) c).getMessage() + "\n"; - + fileContent. + append(((Properties.Comment) c).getMessage()). + append("\n"); } - return fileContent; + + return fileContent.toString(); + } + + private Properties appendToExistingPropertiesFile(Properties.File propertyFile, List contentToAppend) { + return propertyFile.withContent( + Stream.concat(propertyFile.getContent().stream(), contentToAppend.stream()). + collect(Collectors.toList())); } private Properties deleteFromApplicationProperties(Properties.File applicationProperties) { @@ -122,13 +142,14 @@ private Map> getNewApplicationPropertyFileInfo( if (acc.existingApplicationProperties == null) return new HashMap<>(); Map> map = new HashMap<>(); - List contentList = acc.existingApplicationProperties.getContent(); + List applicationPropertiesContentList = acc.existingApplicationProperties.getContent(); int index = 0; - while (index < contentList.size()) { - if (isSeparator(contentList.get(index))) { - // index++; - List newContent = getContentForNewFile(contentList, ++index); + while (index < applicationPropertiesContentList.size()) { + + if (isSeparator(applicationPropertiesContentList.get(index))) { + List newContent = getContentForNewFile(applicationPropertiesContentList, ++index); + map.put("application-" + ((Properties.Entry) newContent.get(0)).getValue().getText() + ".properties", newContent.subList(1, newContent.size())); } @@ -168,7 +189,8 @@ public static class Accumulator { Properties.File existingApplicationProperties; Set newApplicationPropertyFiles = new HashSet<>(); - Map existingApplicationEnvProperties = new HashMap<>(); + Map existingPropertiesFiles = new HashMap<>(); + Map> propertyFileContent = new HashMap<>(); } } diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index a8aa007ec..8613a7b75 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -44,8 +44,76 @@ void noSeparateProfile() { } @Test - void separateProfile() { + void separateProfileWithAppend() { rewriteRun( + recipeSpec -> recipeSpec.cycles(1).expectedCyclesThatMakeChanges(1), + org.openrewrite.properties.Assertions.properties( + """ + line1=line1 + """, + """ + line1=line1 + 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( + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + !--- + spring.config.activate.on-profile=dev + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + #--- + spring.config.activate.on-profile=local + app.config.currentEnvironment=LOCAL + + + #--- + #### XX Configuration #### + spring.config.activate.on-profile=prod + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + """, + sourceSpecs -> sourceSpecs.path("application.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + app.config.currentEnvironment=LOCAL + """, + sourceSpecs -> sourceSpecs.path("application-local.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + ### XX Configuration #### + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + sourceSpecs -> sourceSpecs.path("application-prod.properties") + ) + ); + } + + @Test + void separateProfileWithoutAppend() { + rewriteRun( + recipeSpec -> recipeSpec.cycles(1).expectedCyclesThatMakeChanges(1), org.openrewrite.properties.Assertions.properties( null, """ From 780250e26b90e04d1bdabab10ffc7e2be1eace96 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 21:48:14 -0700 Subject: [PATCH 11/21] Forgot to format test file --- .../java/spring/SeparateApplicationPropertiesByProfileTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 8613a7b75..be14e1d8e 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -1,8 +1,8 @@ package org.openrewrite.java.spring; -import org.openrewrite.test.RewriteTest; import org.junit.jupiter.api.Test; import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; public class SeparateApplicationPropertiesByProfileTest implements RewriteTest { From b1e4719728a3cccf998881ef21a814b7cf1a2acc Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 21:56:47 -0700 Subject: [PATCH 12/21] More formatting --- .../spring/SeparateApplicationPropertiesByProfile.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index b837508cc..fba3361be 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -67,14 +67,12 @@ public Collection generate(Accumulator acc, ExecutionConte @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); - if (!acc.existingPropertiesFiles.containsKey(fileName)) { + if (!acc.existingPropertiesFiles.containsKey(fileName)) acc.newApplicationPropertyFiles. add(new CreatePropertiesFile(fileName, fileContent, null). generate(new AtomicBoolean(true), ctx). iterator(). - next() - ); - } + next()); } return acc.newApplicationPropertyFiles; } @@ -165,7 +163,8 @@ private List getContentForNewFile(List c 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 From 5b65012aa52701b6012f73a6f039ae81bb529a5d Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Mon, 12 Aug 2024 22:28:45 -0700 Subject: [PATCH 13/21] Fixed weird spacing issue --- .../java/spring/SeparateApplicationPropertiesByProfile.java | 1 + .../java/spring/SeparateApplicationPropertiesByProfileTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index fba3361be..b58942473 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -103,6 +103,7 @@ private String getNewFileContentString(List content) { fileContent. append(((Properties.Entry) c).getKey()). append("="). + append(((Properties.Entry) c).getValue().getPrefix()). append(((Properties.Entry) c).getValue().getText()). append("\n"); diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index be14e1d8e..fbb260028 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -118,7 +118,7 @@ void separateProfileWithoutAppend() { null, """ oauth2.clientId=9999999999999999999999 - service.domainUrl=https://this.is.my.dev.url.com + service.domainUrl= https://this.is.my.dev.url.com app.config.currentEnvironment=DEV """, sourceSpecs -> sourceSpecs.path("application-dev.properties") From 0cf74f9600c85d8e5766bdcbafb0ad59290925a2 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Tue, 13 Aug 2024 10:55:40 -0700 Subject: [PATCH 14/21] Formatting and Code Cleanup --- ...eparateApplicationPropertiesByProfile.java | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index b58942473..f982823ce 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -39,15 +39,14 @@ public TreeVisitor getScanner(Accumulator acc) { return new TreeVisitor() { @Override public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { - if (!(tree instanceof Properties.File)) return tree; + if (!(tree instanceof Properties.File)) + return tree; Properties.File propertyFile = (Properties.File) tree; String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString()); - if (sourcePath.matches("application.properties")) { - acc.existingApplicationProperties = propertyFile; - acc.propertyFileContent = getNewApplicationPropertyFileInfo(acc); - } + if (sourcePath.matches("application.properties")) + acc.propertyFileContent = getNewApplicationPropertyFileInfo(propertyFile.getContent()); if (sourcePath.matches("application-.+\\.properties")) acc.existingPropertiesFiles.put(sourcePath, propertyFile); @@ -59,22 +58,25 @@ public TreeVisitor getScanner(Accumulator acc) { @Override public Collection generate(Accumulator acc, ExecutionContext ctx) { - if (acc.existingApplicationProperties == null) return Collections.emptyList(); + if (acc.propertyFileContent.isEmpty()) + return Collections.emptyList(); + + Set newApplicationPropertiesFiles = new HashSet<>(); for (Map.Entry> entry : acc.propertyFileContent.entrySet()) { String fileName = entry.getKey(); + @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); - @Language("properties") - String fileContent = getNewFileContentString(entry.getValue()); - + // generate new application-profile.properties file if it does NOT already exist if (!acc.existingPropertiesFiles.containsKey(fileName)) - acc.newApplicationPropertyFiles. + newApplicationPropertiesFiles. add(new CreatePropertiesFile(fileName, fileContent, null). generate(new AtomicBoolean(true), ctx). iterator(). next()); } - return acc.newApplicationPropertyFiles; + + return newApplicationPropertiesFiles; } @Override @@ -87,6 +89,8 @@ public Properties visitFile(Properties.File file, ExecutionContext ctx) { if (fileName.equals("application.properties")) return deleteFromApplicationProperties(file); + // append new content if the application-profile.properties file exists AND + // it has profile configurations set in application.properties if (acc.existingPropertiesFiles.containsKey(fileName) && acc.propertyFileContent.containsKey(fileName)) return appendToExistingPropertiesFile(file, acc.propertyFileContent.get(fileName)); @@ -95,6 +99,12 @@ public Properties visitFile(Properties.File file, ExecutionContext ctx) { }; } + private Properties appendToExistingPropertiesFile(Properties.File file, List contentToAppend) { + return file.withContent( + Stream.concat(file.getContent().stream(), contentToAppend.stream()). + collect(Collectors.toList())); + } + private String getNewFileContentString(List content) { StringBuilder fileContent = new StringBuilder(); @@ -116,12 +126,6 @@ else if (c instanceof Properties.Comment) return fileContent.toString(); } - private Properties appendToExistingPropertiesFile(Properties.File propertyFile, List contentToAppend) { - return propertyFile.withContent( - Stream.concat(propertyFile.getContent().stream(), contentToAppend.stream()). - collect(Collectors.toList())); - } - private Properties deleteFromApplicationProperties(Properties.File applicationProperties) { List newContent = new ArrayList<>(); @@ -132,22 +136,20 @@ private Properties deleteFromApplicationProperties(Properties.File applicationPr newContent.add(c); } - if (applicationProperties.getContent().equals(newContent)) return applicationProperties; + if (applicationProperties.getContent().equals(newContent)) + return applicationProperties; return applicationProperties.withContent(newContent); } - private Map> getNewApplicationPropertyFileInfo(Accumulator acc) { - if (acc.existingApplicationProperties == null) return new HashMap<>(); - + private Map> getNewApplicationPropertyFileInfo(List contentList) { Map> map = new HashMap<>(); - List applicationPropertiesContentList = acc.existingApplicationProperties.getContent(); int index = 0; - while (index < applicationPropertiesContentList.size()) { + while (index < contentList.size()) { - if (isSeparator(applicationPropertiesContentList.get(index))) { - List newContent = getContentForNewFile(applicationPropertiesContentList, ++index); + if (isSeparator(contentList.get(index))) { + List newContent = getContentForNewFile(contentList, ++index); map.put("application-" + ((Properties.Entry) newContent.get(0)).getValue().getText() + ".properties", newContent.subList(1, newContent.size())); @@ -185,10 +187,6 @@ private boolean isSeparator(Properties.Content c) { } public static class Accumulator { - @Nullable - Properties.File existingApplicationProperties; - - Set newApplicationPropertyFiles = new HashSet<>(); Map existingPropertiesFiles = new HashMap<>(); Map> propertyFileContent = new HashMap<>(); } From e7398d3e7614ea30ea3dea03ec01215f9171740a Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Tue, 13 Aug 2024 11:21:19 -0700 Subject: [PATCH 15/21] Changed approach to create blank properties files and append to everything in order to simplify code --- ...eparateApplicationPropertiesByProfile.java | 70 +++---------------- ...ateApplicationPropertiesByProfileTest.java | 4 +- 2 files changed, 13 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index f982823ce..9b4f42e2c 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -2,7 +2,6 @@ import lombok.EqualsAndHashCode; import lombok.Value; -import org.intellij.lang.annotations.Language; import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.properties.CreatePropertiesFile; @@ -63,18 +62,13 @@ public Collection generate(Accumulator acc, ExecutionConte Set newApplicationPropertiesFiles = new HashSet<>(); - for (Map.Entry> entry : acc.propertyFileContent.entrySet()) { - String fileName = entry.getKey(); - @Language("properties") String fileContent = getNewFileContentString(entry.getValue()); - - // generate new application-profile.properties file if it does NOT already exist - if (!acc.existingPropertiesFiles.containsKey(fileName)) + for (Map.Entry> entry : acc.propertyFileContent.entrySet()) + if (!acc.existingPropertiesFiles.containsKey(entry.getKey())) newApplicationPropertiesFiles. - add(new CreatePropertiesFile(fileName, fileContent, null). + add(new CreatePropertiesFile(entry.getKey(), "", null). generate(new AtomicBoolean(true), ctx). iterator(). next()); - } return newApplicationPropertiesFiles; } @@ -84,17 +78,12 @@ public TreeVisitor getVisitor(Accumulator acc) { return new PropertiesVisitor() { @Override public Properties visitFile(Properties.File file, ExecutionContext ctx) { - String fileName = file.getSourcePath().toString(); - - if (fileName.equals("application.properties")) - return deleteFromApplicationProperties(file); + if (acc.propertyFileContent.isEmpty()) + return file; - // append new content if the application-profile.properties file exists AND - // it has profile configurations set in application.properties - if (acc.existingPropertiesFiles.containsKey(fileName) && acc.propertyFileContent.containsKey(fileName)) - return appendToExistingPropertiesFile(file, acc.propertyFileContent.get(fileName)); - - return file; + String fileName = file.getSourcePath().toString(); + return fileName.equals("application.properties") ? deleteFromApplicationProperties(file) : + appendToExistingPropertiesFile(file, acc.propertyFileContent.get(fileName)); } }; } @@ -105,77 +94,42 @@ private Properties appendToExistingPropertiesFile(Properties.File file, List content) { - StringBuilder fileContent = new StringBuilder(); - - for (Properties.Content c : content) { - if (c instanceof Properties.Entry) - fileContent. - append(((Properties.Entry) c).getKey()). - append("="). - append(((Properties.Entry) c).getValue().getPrefix()). - append(((Properties.Entry) c).getValue().getText()). - append("\n"); - - else if (c instanceof Properties.Comment) - fileContent. - append(((Properties.Comment) c).getMessage()). - append("\n"); - } - - return fileContent.toString(); - } - private Properties deleteFromApplicationProperties(Properties.File applicationProperties) { List newContent = new ArrayList<>(); - for (Properties.Content c : applicationProperties.getContent()) { if (isSeparator(c)) break; - newContent.add(c); } - - if (applicationProperties.getContent().equals(newContent)) - return applicationProperties; - - return applicationProperties.withContent(newContent); + return applicationProperties.getContent().equals(newContent) ? applicationProperties : + applicationProperties.withContent(newContent); } private Map> getNewApplicationPropertyFileInfo(List contentList) { Map> map = new HashMap<>(); int index = 0; - while (index < contentList.size()) { - if (isSeparator(contentList.get(index))) { List newContent = getContentForNewFile(contentList, ++index); - map.put("application-" + ((Properties.Entry) newContent.get(0)).getValue().getText() + ".properties", newContent.subList(1, newContent.size())); } index++; } - return map; } private List getContentForNewFile(List contentList, int index) { List 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")) list.add(0, contentList.get(index)); - else list.add(contentList.get(index)); - index++; } - return list; } @@ -190,6 +144,4 @@ public static class Accumulator { Map existingPropertiesFiles = new HashMap<>(); Map> propertyFileContent = new HashMap<>(); } -} - - +} \ No newline at end of file diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index fbb260028..e375675d6 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -101,7 +101,7 @@ void separateProfileWithAppend() { org.openrewrite.properties.Assertions.properties( null, """ - ### XX Configuration #### + #### XX Configuration #### oauth2.clientId=77777777777777 service.domainUrl=https://this.is.my.prod.url.com """, @@ -165,7 +165,7 @@ void separateProfileWithoutAppend() { org.openrewrite.properties.Assertions.properties( null, """ - ### XX Configuration #### + #### XX Configuration #### oauth2.clientId=77777777777777 service.domainUrl=https://this.is.my.prod.url.com """, From 35ae8c7c0d8df8bdfbc34a33c7af8717ab83935c Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Tue, 13 Aug 2024 11:53:47 -0700 Subject: [PATCH 16/21] Converted existingPropertiesFiles to Set of Strings --- .../java/spring/SeparateApplicationPropertiesByProfile.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 9b4f42e2c..61ffdf198 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -48,7 +48,7 @@ public TreeVisitor getScanner(Accumulator acc) { acc.propertyFileContent = getNewApplicationPropertyFileInfo(propertyFile.getContent()); if (sourcePath.matches("application-.+\\.properties")) - acc.existingPropertiesFiles.put(sourcePath, propertyFile); + acc.existingPropertyFilePaths.add(sourcePath); return tree; } @@ -63,7 +63,7 @@ public Collection generate(Accumulator acc, ExecutionConte Set newApplicationPropertiesFiles = new HashSet<>(); for (Map.Entry> entry : acc.propertyFileContent.entrySet()) - if (!acc.existingPropertiesFiles.containsKey(entry.getKey())) + if (!acc.existingPropertyFilePaths.contains(entry.getKey())) newApplicationPropertiesFiles. add(new CreatePropertiesFile(entry.getKey(), "", null). generate(new AtomicBoolean(true), ctx). @@ -141,7 +141,7 @@ private boolean isSeparator(Properties.Content c) { } public static class Accumulator { - Map existingPropertiesFiles = new HashMap<>(); + Set existingPropertyFilePaths = new HashSet<>(); Map> propertyFileContent = new HashMap<>(); } } \ No newline at end of file From 825d68c0938646386137a79a0efaf516258fa115 Mon Sep 17 00:00:00 2001 From: Ishaan Sharma Date: Fri, 23 Aug 2024 13:20:06 -0700 Subject: [PATCH 17/21] New properties files will go into same folder as application.properties Finds properties files that are within folders --- ...eparateApplicationPropertiesByProfile.java | 26 ++++--- ...ateApplicationPropertiesByProfileTest.java | 68 ++++++++++++++++++- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 61ffdf198..22ce52d2e 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -43,12 +43,15 @@ public TreeVisitor getScanner(Accumulator acc) { Properties.File propertyFile = (Properties.File) tree; String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString()); + String[] pathArray = sourcePath.split("/"); - if (sourcePath.matches("application.properties")) + if (sourcePath.matches("(?:.*/)?application.properties")) { + acc.pathToApplicationProperties = getPathToApplicationProperties(pathArray); acc.propertyFileContent = getNewApplicationPropertyFileInfo(propertyFile.getContent()); + } - if (sourcePath.matches("application-.+\\.properties")) - acc.existingPropertyFilePaths.add(sourcePath); + if (sourcePath.matches("(?:.*/)?application-[^/]+\\.properties")) + acc.fileNameToFilePath.put(pathArray[pathArray.length - 1], sourcePath); return tree; } @@ -63,9 +66,9 @@ public Collection generate(Accumulator acc, ExecutionConte Set newApplicationPropertiesFiles = new HashSet<>(); for (Map.Entry> entry : acc.propertyFileContent.entrySet()) - if (!acc.existingPropertyFilePaths.contains(entry.getKey())) + if (!acc.fileNameToFilePath.containsKey(entry.getKey())) newApplicationPropertiesFiles. - add(new CreatePropertiesFile(entry.getKey(), "", null). + add(new CreatePropertiesFile(acc.pathToApplicationProperties + entry.getKey(), "", null). generate(new AtomicBoolean(true), ctx). iterator(). next()); @@ -81,8 +84,10 @@ public Properties visitFile(Properties.File file, ExecutionContext ctx) { if (acc.propertyFileContent.isEmpty()) return file; - String fileName = file.getSourcePath().toString(); - return fileName.equals("application.properties") ? deleteFromApplicationProperties(file) : + String[] filePathArray = file.getSourcePath().toString().split("/"); + String fileName = filePathArray[filePathArray.length - 1]; + + return fileName.matches("application.properties") ? deleteFromApplicationProperties(file) : appendToExistingPropertiesFile(file, acc.propertyFileContent.get(fileName)); } }; @@ -133,6 +138,10 @@ private List getContentForNewFile(List c return list; } + private String getPathToApplicationProperties(String[] pathArray) { + return pathArray.length == 1 ? "" : String.join("/", Arrays.copyOfRange(pathArray, 0, pathArray.length - 1)) + "/"; + } + private boolean isSeparator(Properties.Content c) { return c instanceof Properties.Comment && ((Properties.Comment) c).getMessage().equals("---") && @@ -141,7 +150,8 @@ private boolean isSeparator(Properties.Content c) { } public static class Accumulator { - Set existingPropertyFilePaths = new HashSet<>(); + String pathToApplicationProperties = ""; + Map fileNameToFilePath = new HashMap<>(); Map> propertyFileContent = new HashMap<>(); } } \ No newline at end of file diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index e375675d6..fce86179f 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -46,7 +46,6 @@ void noSeparateProfile() { @Test void separateProfileWithAppend() { rewriteRun( - recipeSpec -> recipeSpec.cycles(1).expectedCyclesThatMakeChanges(1), org.openrewrite.properties.Assertions.properties( """ line1=line1 @@ -113,7 +112,6 @@ void separateProfileWithAppend() { @Test void separateProfileWithoutAppend() { rewriteRun( - recipeSpec -> recipeSpec.cycles(1).expectedCyclesThatMakeChanges(1), org.openrewrite.properties.Assertions.properties( null, """ @@ -173,4 +171,70 @@ void separateProfileWithoutAppend() { ) ); } + + @Test + void pathToApplicationProperties() { + rewriteRun( + org.openrewrite.properties.Assertions.properties( + """ + line1=line1 + """, + """ + line1=line1 + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + """, + sourceSpecs -> sourceSpecs.path("folder1/folder2/application-dev.properties") + ), + org.openrewrite.properties.Assertions.properties( + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + !--- + spring.config.activate.on-profile=dev + oauth2.clientId=9999999999999999999999 + service.domainUrl= https://this.is.my.dev.url.com + app.config.currentEnvironment=DEV + #--- + spring.config.activate.on-profile=local + app.config.currentEnvironment=LOCAL + + + #--- + #### XX Configuration #### + spring.config.activate.on-profile=prod + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + """ + spring.application.name=Openrewrite-PR-Service + #PR-Service + base-url.PR-services=http://my.url.com + exchange-token=1234567890 + exchange-tokens=${base-url.PR-services}/exchange-token + """, + sourceSpecs -> sourceSpecs.path("folder1/folder2/application.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + app.config.currentEnvironment=LOCAL + """, + sourceSpecs -> sourceSpecs.path("folder1/folder2/application-local.properties") + ), + org.openrewrite.properties.Assertions.properties( + null, + """ + #### XX Configuration #### + oauth2.clientId=77777777777777 + service.domainUrl=https://this.is.my.prod.url.com + """, + sourceSpecs -> sourceSpecs.path("folder1/folder2/application-prod.properties") + ) + ); + } } From 5aef7ae028b2080824fa769cc6a77ae85bd9ee17 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 11 Sep 2024 10:17:05 +0200 Subject: [PATCH 18/21] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- ...eparateApplicationPropertiesByProfile.java | 19 +++++++++++++++++-- ...ateApplicationPropertiesByProfileTest.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 22ce52d2e..12693be54 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://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 org.openrewrite.java.spring; import lombok.EqualsAndHashCode; @@ -15,7 +30,7 @@ @Value -@EqualsAndHashCode(callSuper = true) +@EqualsAndHashCode(callSuper = false) public class SeparateApplicationPropertiesByProfile extends ScanningRecipe { @Override @@ -154,4 +169,4 @@ public static class Accumulator { Map fileNameToFilePath = new HashMap<>(); Map> propertyFileContent = new HashMap<>(); } -} \ No newline at end of file +} diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index fce86179f..5987dbf3c 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://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 org.openrewrite.java.spring; import org.junit.jupiter.api.Test; From 87e7146a5f68bff838a2be642e51df635b9199c4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 11 Sep 2024 10:21:22 +0200 Subject: [PATCH 19/21] Add missing braces, language hints and apply formatter --- ...eparateApplicationPropertiesByProfile.java | 43 ++++++++------- ...ateApplicationPropertiesByProfileTest.java | 53 +++++++++++++------ 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 12693be54..5cf31a637 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -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; @@ -28,19 +28,18 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - @Value @EqualsAndHashCode(callSuper = false) public class SeparateApplicationPropertiesByProfile extends ScanningRecipe { @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 @@ -52,9 +51,10 @@ public Accumulator getInitialValue(ExecutionContext ctx) { public TreeVisitor getScanner(Accumulator acc) { return new TreeVisitor() { @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()); @@ -65,8 +65,9 @@ public TreeVisitor 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; } @@ -75,18 +76,21 @@ public TreeVisitor getScanner(Accumulator acc) { @Override public Collection generate(Accumulator acc, ExecutionContext ctx) { - if (acc.propertyFileContent.isEmpty()) + if (acc.propertyFileContent.isEmpty()) { return Collections.emptyList(); + } Set newApplicationPropertiesFiles = new HashSet<>(); - for (Map.Entry> entry : acc.propertyFileContent.entrySet()) - if (!acc.fileNameToFilePath.containsKey(entry.getKey())) + for (Map.Entry> 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; } @@ -96,8 +100,9 @@ public TreeVisitor getVisitor(Accumulator acc) { return new PropertiesVisitor() { @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]; @@ -117,8 +122,9 @@ private Properties appendToExistingPropertiesFile(Properties.File file, List 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 : @@ -143,11 +149,12 @@ private List getContentForNewFile(List c List 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; @@ -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 { diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index 5987dbf3c..ad170e5d6 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -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()); @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -127,8 +139,9 @@ 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 @@ -136,7 +149,8 @@ void separateProfileWithoutAppend() { """, sourceSpecs -> sourceSpecs.path("application-dev.properties") ), - org.openrewrite.properties.Assertions.properties( + properties( + //language=properties """ spring.application.name=Openrewrite-PR-Service #PR-Service @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 From 6c3cdd7565bbd2ca2adcb87c91ea13fe54a1d916 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 11 Sep 2024 10:43:09 +0200 Subject: [PATCH 20/21] Minor polish --- .../java/spring/SeparateApplicationPropertiesByProfile.java | 6 +++--- .../spring/SeparateApplicationPropertiesByProfileTest.java | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java index 5cf31a637..96524c70b 100644 --- a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -17,8 +17,8 @@ import lombok.EqualsAndHashCode; import lombok.Value; -import org.openrewrite.*; import org.jspecify.annotations.Nullable; +import org.openrewrite.*; import org.openrewrite.properties.CreatePropertiesFile; import org.openrewrite.properties.PropertiesVisitor; import org.openrewrite.properties.tree.Properties; @@ -60,12 +60,12 @@ public TreeVisitor getScanner(Accumulator acc) { String sourcePath = PathUtils.separatorsToUnix(propertyFile.getSourcePath().toString()); String[] pathArray = sourcePath.split("/"); - if (sourcePath.matches("(?:.*/)?application.properties")) { + if (propertyFile.getSourcePath().endsWith("application.properties")) { acc.pathToApplicationProperties = getPathToApplicationProperties(pathArray); acc.propertyFileContent = getNewApplicationPropertyFileInfo(propertyFile.getContent()); } - if (sourcePath.matches("(?:.*/)?application-[^/]+\\.properties")) { + if (propertyFile.getSourcePath().getFileName().toString().matches("application-[^/]+\\.properties")) { acc.fileNameToFilePath.put(pathArray[pathArray.length - 1], sourcePath); } diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index ad170e5d6..abfff84e3 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -40,7 +40,6 @@ void noApplicationProperties() { base-url.PR-services=http://my.url.com exchange-token=1234567890 exchange-tokens=${base-url.PR-services}/exchange-token - """, sourceSpecs -> sourceSpecs.path("application-dev.properties")) ); @@ -58,7 +57,6 @@ void noSeparateProfile() { base-url.PR-services=http://my.url.com exchange-token=1234567890 exchange-tokens=${base-url.PR-services}/exchange-token - """, sourceSpecs -> sourceSpecs.path("application.properties")) ); From 4f232974640180041c78307cde8c156c1fef9538 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 21 Sep 2024 22:32:19 +0200 Subject: [PATCH 21/21] Add test showing multi module project structure --- ...ateApplicationPropertiesByProfileTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java index abfff84e3..c1a241f78 100644 --- a/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java +++ b/src/testWithSpringBoot_2_4/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfileTest.java @@ -20,6 +20,8 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.java.Assertions.mavenProject; +import static org.openrewrite.java.Assertions.srcMainResources; import static org.openrewrite.properties.Assertions.properties; class SeparateApplicationPropertiesByProfileTest implements RewriteTest { @@ -273,4 +275,60 @@ void pathToApplicationProperties() { ) ); } + + @Test + void multiModuleProject() { + rewriteRun( + mavenProject("parent", + mavenProject("service", + srcMainResources( + properties( + """ + global.service=true + !--- + spring.config.activate.on-profile=dev + dev.service=true + """, + """ + global.service=true + """, + spec -> spec.path("application.properties") + ), + properties( + null, + """ + dev.service=true + """, + spec -> spec.path("application-dev.properties") + ) + ) + ), + mavenProject("client", + srcMainResources( + properties(""" + global.client=true + !--- + spring.config.activate.on-profile=dev + dev.client=true + """, + """ + global.client=true + """, + spec -> spec.path("application.properties") + ), + properties( + """ + dev.existing=true + """, + """ + dev.existing=true + dev.client=true + """, + spec -> spec.path("application-dev.properties") + ) + ) + ) + ) + ); + } }