Skip to content

Commit

Permalink
add new updates from sb2 to sb3, export applied directly from updateF…
Browse files Browse the repository at this point in the history
…ile method (#342)
  • Loading branch information
juancgalvis authored Feb 13, 2023
1 parent 80fa00c commit d063455
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 86 deletions.
9 changes: 7 additions & 2 deletions src/main/java/co/com/bancolombia/factory/ModuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,14 @@ public final <T extends Validation> void runValidations(Class<T>... validations)
}
}

public void updateFile(String path, FileUpdater updater) throws IOException {
public boolean updateFile(String path, FileUpdater updater) throws IOException {
String content = readFile(path);
addFile(path, updater.update(content));
String newContent = updater.update(content);
boolean changed = !content.equals(newContent);
if (changed) {
addFile(path, newContent);
}
return changed;
}

public Release getLatestRelease() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import co.com.bancolombia.factory.ModuleBuilder;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

Expand All @@ -11,17 +10,14 @@ public class UpdateUtils {

public static boolean appendIfNotContains(
ModuleBuilder builder, String file, String contains, String toAdd) throws IOException {
AtomicBoolean applied = new AtomicBoolean(false);
builder.updateFile(
return builder.updateFile(
file,
content -> {
if (!content.contains(contains)) {
applied.getAndSet(true);
return content + toAdd;
}
return content;
});
return applied.get();
}

public static void updateVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
import static co.com.bancolombia.Constants.MainFiles.MAIN_GRADLE;

import co.com.bancolombia.factory.ModuleBuilder;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.SneakyThrows;

public class UpgradeMainGradle {

@SneakyThrows
public boolean up(ModuleBuilder builder, String match, String validation, String value) {
AtomicBoolean applied = new AtomicBoolean(false);
builder.updateFile(
MAIN_GRADLE,
content -> {
String res = UpdateUtils.appendValidate(content, match, validation, value);
if (!content.equals(res)) {
applied.set(true);
}
return res;
});
return applied.get();
return builder.updateFile(
MAIN_GRADLE, content -> UpdateUtils.appendValidate(content, match, validation, value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import co.com.bancolombia.utils.Utils;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.SneakyThrows;

public class UpgradeY2022M05D03 implements UpgradeAction {
Expand Down Expand Up @@ -38,18 +37,7 @@ public String description() {

@SneakyThrows
private boolean applyUpdate(ModuleBuilder builder, String file) {
AtomicBoolean applied = new AtomicBoolean(false);
String regex = "['\\\"](software\\.amazon\\.awssdk:)((?!(bom)).*)(:.+)['\\\"]";
String regexExists = "['\\\"](software\\.amazon\\.awssdk:)(.+)['\\\"]";
builder.updateFile(
file,
content -> {
String result = Utils.replaceExpression(content, regex, "'$1$2'");
if (!content.matches(regexExists)) {
applied.set(true);
}
return result;
});
return applied.get();
return builder.updateFile(file, content -> Utils.replaceExpression(content, regex, "'$1$2'"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import co.com.bancolombia.utils.Utils;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

public class UpgradeY2022M05D05 implements UpgradeAction {
@Override
@SuppressWarnings("unchecked")
public boolean up(ModuleBuilder builder) {
String name = builder.getProject().getRootProject().getName();
AtomicBoolean applied = new AtomicBoolean(false);
try {
builder.updateFile(
DOCKERFILE,
content -> {
String newContent = Utils.replaceExpression(content, "app.jar", name + ".jar");
if (!content.equals(newContent)) {
applied.set(true);
}
return newContent;
});
return builder.updateFile(
DOCKERFILE, content -> Utils.replaceExpression(content, "app.jar", name + ".jar"));
} catch (IOException e) {
builder.getProject().getLogger().debug("Error reading Dockerfile", e);
}
return applied.get();
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.upgrades.UpdateUtils;
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.SneakyThrows;

public class UpgradeY2023M01D12 implements UpgradeAction {
Expand All @@ -23,17 +22,8 @@ public class UpgradeY2023M01D12 implements UpgradeAction {
@Override
@SneakyThrows
public boolean up(ModuleBuilder builder) {
AtomicBoolean applied = new AtomicBoolean(false);
builder.updateFile(
SETTINGS_GRADLE,
content -> {
String res = UpdateUtils.appendValidate(content, MATCH, VALIDATION, VALUE);
if (!content.equals(res)) {
applied.set(true);
}
return res;
});
return applied.get();
return builder.updateFile(
SETTINGS_GRADLE, content -> UpdateUtils.appendValidate(content, MATCH, VALIDATION, VALUE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ public boolean up(ModuleBuilder builder) {

private void apply(ModuleBuilder builder, File file, AtomicBoolean applied, Logger logger) {
try {
builder.updateFile(
file.getAbsolutePath(),
content -> {
String res = UpdateUtils.replace(content, "javax.persistence", "jakarta.persistence");
if (!content.equals(res)) {
logger.debug("javax to jakarta applied in: ${}", file.getName());
applied.set(true);
}
return res;
});
boolean appliedItem =
builder.updateFile(
file.getAbsolutePath(),
content -> {
String res =
UpdateUtils.replace(content, "javax.persistence", "jakarta.persistence");
res = UpdateUtils.replace(res, "javax.jms", "jakarta.jms");
res = UpdateUtils.replace(res, "com.ibm.mq.jms", "com.ibm.mq.jakarta.jms");
res =
UpdateUtils.replace(
res, "com.ibm.msg.client.jms", "com.ibm.msg.client.jakarta.jms");
res =
UpdateUtils.replace(
res, "com.ibm.msg.client.wmq", "com.ibm.msg.client.jakarta.wmq");
return res;
});
if (appliedItem) {
logger.debug("javax to jakarta applied in: ${}", file.getName());
applied.set(true);
}
} catch (IOException e) {
logger.warn("Error applying javax to jakarta persistence", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import co.com.bancolombia.factory.upgrades.UpdateUtils;
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.SneakyThrows;

public class UpgradeY2023M02D08Redis implements UpgradeAction {
Expand All @@ -23,27 +22,21 @@ public boolean up(ModuleBuilder builder) {

if (builder.getProject().file(repository).getAbsoluteFile().exists()
&& builder.getProject().file(repositoryAdapter).getAbsoluteFile().exists()) {
AtomicBoolean applied = new AtomicBoolean(false);
apply(builder, repository, applied);
apply(builder, repositoryAdapter, applied);
builder.appendDependencyToModule(
"redis", "implementation 'jakarta.persistence:jakarta.persistence-api'");
return applied.get();
return apply(builder, repository) | apply(builder, repositoryAdapter);
}
return false;
}

private void apply(ModuleBuilder builder, String file, AtomicBoolean applied) throws IOException {
private boolean apply(ModuleBuilder builder, String file) throws IOException {
String prev = "org.springframework.data.repository.CrudRepository";
String next = "org.springframework.data.keyvalue.repository.KeyValueRepository";
builder.updateFile(
return builder.updateFile(
file,
content -> {
String res = UpdateUtils.replace(content, prev, next);
res = UpdateUtils.replace(res, "CrudRepository", "KeyValueRepository");
if (!content.equals(res)) {
applied.set(true);
}
return res;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package co.com.bancolombia.factory.upgrades.actions;

import static co.com.bancolombia.Constants.MainFiles.APP_BUILD_GRADLE;
import static co.com.bancolombia.Constants.MainFiles.MAIN_GRADLE;

import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.upgrades.UpdateUtils;
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import lombok.SneakyThrows;

public class UpgradeY2023M02D13Gradle implements UpgradeAction {

@Override
@SneakyThrows
public boolean up(ModuleBuilder builder) {
return builder.updateFile(
MAIN_GRADLE,
content -> {
String res =
UpdateUtils.replace(
content,
"tasks.withType(JavaCompile) {",
"tasks.withType(JavaCompile).configureEach {");
res =
UpdateUtils.replace(
res,
"task jacocoMergedReport(type: JacocoReport) {",
"tasks.register('jacocoMergedReport', JacocoReport) {");
res =
UpdateUtils.replace(
res,
"reportsDirectory = file(\"$buildDir/reports\")",
"reportsDirectory.set(file(\"$buildDir/reports\"))");
return res;
})
| builder.updateFile(
APP_BUILD_GRADLE,
content ->
UpdateUtils.replace(
content,
"task explodedJar(type: Copy) {",
"tasks.register('explodedJar', Copy) {"));
}

@Override
public String name() {
return "2.4.10->3.0.0";
}

@Override
public String description() {
return "Update gradle tasks";
}
}
4 changes: 2 additions & 2 deletions src/main/java/co/com/bancolombia/task/UpdateProjectTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void updateProject() throws IOException, CleanException {
if (git == BooleanOption.TRUE && CommandUtils.getDefault().hasGitPendingChanges(logger)) {
getTextOutputFactory()
.create(UpdateProjectTask.class)
.style(StyledTextOutput.Style.Error)
.append(
.style(StyledTextOutput.Style.Info)
.println(
"You have changes pending to be committed, please commit your changes before run this task"
+ " or pass the '--git false' flag");
return;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/co/com/bancolombia/utils/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public boolean hasGitPendingChanges(Logger logger) {
try {
Process process = rt.exec(GIT_STATUS);
String output = processCommandOutput(process);
return !output.contains("nothing to commit");
boolean hasPending =
!(output.contains("nothing to commit") || output.contains("nada para hacer commit"));
if (hasPending) {
logger.lifecycle(output);
}
return hasPending;
} catch (IOException e) {
logger.warn("Could not run git verification", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {
runtimeOnly('org.springframework.boot:spring-boot-devtools')
}

task explodedJar(type: Copy) {
tasks.register('explodedJar', Copy) {
with jar
into "${buildDir}/exploded"
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/structure/root/main.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jacoco {
reportsDirectory = file("$buildDir/reports")
}

task jacocoMergedReport(type: JacocoReport) {
tasks.register('jacocoMergedReport', JacocoReport) {
dependsOn = subprojects.jacocoTestReport
additionalSourceDirs.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
Expand All @@ -92,7 +92,7 @@ cobertura {
test.finalizedBy(project.tasks.cobertura)
{{/cobertura}}

tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
options.compilerArgs = [
'-Amapstruct.suppressGeneratorTimestamp=true'
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void shouldNotAppendWhenContains() throws IOException {
// Act
boolean applied = UpdateUtils.appendIfNotContains(builder, file, check, toAdd);
// Assert
verify(builder, times(2)).addFile(file, currentContent);
verify(builder, times(1)).addFile(file, currentContent);
assertFalse(applied);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void shouldUpdate() {
boolean applied = updater.up(builder);
// Assert
assertTrue(applied);
verify(builder, times(2))
verify(builder, times(1))
.addFile(BUILD_GRADLE, "\t\tcleanArchitectureVersion = '" + release.getTagName() + "'\n");
verify(builder).addFile(GRADLE_PROPERTIES, "systemProp.version=" + release.getTagName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ public class AnUpdate implements UpgradeAction {
@Override
public boolean up(ModuleBuilder builder) {
try {
builder.updateFile("build.gradle", content -> content + "modified");
return builder.updateFile("build.gradle", content -> content + "modified");
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}

@Override
Expand Down

0 comments on commit d063455

Please sign in to comment.