这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/co/com/bancolombia/task/InternalTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import co.com.bancolombia.task.annotations.CATask;
import co.com.bancolombia.utils.SonarCheck;
import co.com.bancolombia.utils.Utils;
import co.com.bancolombia.utils.offline.UpdateProjectDependencies;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;

@CATask(name = "internalTask", shortcut = "it", description = "Run non final user task")
public class InternalTask extends AbstractCleanArchitectureDefaultTask {
private Action action = Action.SONARCHECK;
private Action action = Action.SONAR_CHECK;

@Option(option = "action", description = "Set task action to run")
public void setAction(Action action) {
Expand All @@ -23,14 +27,27 @@ public List<Action> getInputOptions() {
return Arrays.asList(Action.values());
}

@Getter @Internal private boolean success = false;

@Override
public void execute() throws IOException {
if (Objects.requireNonNull(action) == Action.SONARCHECK) {
SonarCheck.parse(getProject());
switch (Objects.requireNonNull(action)) {
case SONAR_CHECK:
SonarCheck.parse(getProject());
break;
case UPDATE_DEPENDENCIES:
String basePath = getProject().getProjectDir().toString();
List<String> files = Utils.getAllFilesWithExtension(basePath, builder.isKotlin());
logger.lifecycle(
"Updating project dependencies from root {} in files \n {}", basePath, files);
UpdateProjectDependencies.builder().withFiles(files).build().run();
break;
}
success = true;
}

public enum Action {
SONARCHECK
SONAR_CHECK,
UPDATE_DEPENDENCIES
}
}
3 changes: 2 additions & 1 deletion src/main/java/co/com/bancolombia/task/UpdateProjectTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public void execute() throws IOException, CleanException {
return;
}
// Add specific parameters for UpgradeActions
String basePath = getProject().getProjectDir().toString();
builder.addParam(DEPENDENCIES_TO_UPDATE, dependencies);
builder.addParam(FILES_TO_UPDATE, Utils.getAllFilesWithExtension(builder.isKotlin()));
builder.addParam(FILES_TO_UPDATE, Utils.getAllFilesWithExtension(basePath, builder.isKotlin()));
UpgradeFactory factory = new UpgradeFactory();
factory.buildModule(builder);
builder.persist();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/co/com/bancolombia/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ public static String replaceExpression(String content, String regex, String repl
return content.replaceAll(regex, replaceValue);
}

public static List<String> getAllFilesWithExtension(boolean isKotlin) throws IOException {
public static List<String> getAllFilesWithExtension(String base, boolean isKotlin)
throws IOException {
String extension = isKotlin ? "gradle.kts" : "gradle";
List<String> paths;
try (Stream<Path> walk = Files.walk(Paths.get("."))) {
try (Stream<Path> walk = Files.walk(Paths.get(base))) {
paths =
walk.filter(p -> !Files.isDirectory(p))
.map(Path::toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import lombok.Builder;
import lombok.Setter;
import lombok.SneakyThrows;

@Setter
@Builder(setterPrefix = "with")
public class UpdateProjectDependencies {
public static final String BUILD_GRADLE_FILE = "build.gradle";

@Builder.Default private ExternalOperations operations = OperationsProvider.real();
@Builder.Default private String constantsPath = BUILD_GRADLE_FILE;
@Builder.Default private ExternalOperations operations = OperationsProvider.fromDefault();
@Builder.Default private List<String> files = List.of(BUILD_GRADLE_FILE);

public static UpdateProjectDependencies ofDefaults() {
return UpdateProjectDependencies.builder().build();
}

public void run() throws IOException {
File buildGradle = Paths.get(constantsPath).toFile();
files.forEach(this::updateDependency);
}

@SneakyThrows
private void updateDependency(String file) {
File buildGradle = Paths.get(file).toFile();
String content = FileUtils.readFileAsString(buildGradle, null);
content =
Utils.findExpressions(content, "['\"].+:.+:[^\\$].+['\"]").stream()
Expand Down Expand Up @@ -53,6 +60,15 @@ public void run() throws IOException {
Utils.replaceExpression(current, release.toRegex(), release.toString()),
(current, next) -> next);

if (content.contains("wrapper")) {
Optional<String> dep = operations.getGradleWrapperFromFile();
if (dep.isPresent()) {
content =
Utils.replaceExpression(
content, "gradleVersion\\s*=\\s*.*", "gradleVersion = '" + dep.get() + "'");
}
}

FileUtils.writeContentToFile(content, buildGradle);
}
}
18 changes: 17 additions & 1 deletion src/test/java/co/com/bancolombia/task/InternalTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Objects;
import org.gradle.api.Project;
Expand Down Expand Up @@ -65,13 +67,27 @@ void shouldParseDependencyReport() throws IOException {
assertTrue(appService.file(SonarCheck.OUTPUT).exists());
}

@Test
void shouldRunUpdateAllDependencies() throws IOException {
// Arrange
Files.write(
Paths.get(TEST_DIR, "gradle.properties"),
"simulateRest=true".getBytes(),
StandardOpenOption.APPEND);
task.setAction(InternalTask.Action.UPDATE_DEPENDENCIES);
// Act
task.execute();
// Assert
assertTrue(task.isSuccess());
}

@Test
void shouldGetOptions() {
// Arrange
// Act
List<InternalTask.Action> options = task.getInputOptions();
// Assert
assertEquals(1, options.size());
assertEquals(InternalTask.Action.values().length, options.size());
}

private String readResourceFile(String location) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/co/com/bancolombia/utils/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ void shouldReplaceExpression() {

@Test
void shouldGetAllFilesWithExtension() throws IOException {
List<String> result = Utils.getAllFilesWithExtension(true);
List<String> result = Utils.getAllFilesWithExtension(".", true);
assertEquals(true, result.stream().allMatch(s -> s.endsWith("gradle.kts")));
result = Utils.getAllFilesWithExtension(false);
result = Utils.getAllFilesWithExtension(".", false);
assertEquals(true, result.stream().allMatch(s -> s.endsWith("gradle")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +36,7 @@ public void setup() throws IOException {
task =
UpdateProjectDependencies.builder()
.withOperations(operations)
.withConstantsPath(TEST_PATH)
.withFiles(List.of(TEST_PATH))
.build();
}

Expand Down