Skip to content

Commit

Permalink
merged with 858
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Sep 20, 2023
2 parents 002fb76 + 306ec47 commit 31724f8
Show file tree
Hide file tree
Showing 94 changed files with 1,673 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2021 - 2022 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.springframework.sbm.shell;

import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.engine.recipe.UserInteractions;
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
import org.springframework.sbm.parsers.RewriteExecutionContext;
import org.springframework.sbm.project.parser.ProjectContextInitializer;
import org.springframework.sbm.spring.migration.actions.InitDataSourceAfterJpaInitAction;
import org.springframework.sbm.test.ProjectContextFileSystemTestSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.springframework.core.io.Resource;
import org.springframework.sbm.engine.commands.ScanCommand;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
import org.springframework.sbm.parsers.RewriteExecutionContext;
import org.springframework.sbm.project.parser.ProjectContextInitializer;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.springframework.sbm;

import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
import org.springframework.sbm.parsers.RewriteExecutionContext;
import org.springframework.sbm.scopes.annotations.ScanScope;

import java.util.function.Supplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

import org.jetbrains.annotations.NotNull;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Scope;
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -165,11 +163,40 @@ private boolean isDeclaredInProject(ParentDeclaration parentPomDeclaration) {

private boolean noOtherPomDependsOn(BuildFile buildFile) {
return !this.modules.stream()
.anyMatch(module -> module.getBuildFile().getRequestedDependencies().stream().anyMatch(d -> d.getCoordinates().equals(buildFile.getCoordinates())));
.anyMatch(module -> module.getBuildFile().getRequestedDependencies().stream().anyMatch(d -> d.getGav().equals(buildFile.getCoordinates())));
}

public boolean isSingleModuleApplication() {
return modules.size() == 1;
}

/**
* Find all modules with a declared dependency on the module with given GAV.
* @return modules that have a dependency to the given module and the scope of this dependency.
*/
public Map<Scope, List<Module>> findModulesWithDeclaredDependencyTo(String gav) {
Map<Scope, List<Module>> dependantModules = new HashMap<>();
for (Module m : this.modules) {
Optional<Dependency> declaredDependency = m.getBuildFile().findRequestedDependency(gav);
if (declaredDependency.isPresent()) {
Dependency dependency = declaredDependency.get();
String scopeStr = dependency.getScope();
Scope scope = scopeStr != null ? Scope.valueOf(uppercaseFirstChar(scopeStr)) : Scope.Compile;
if(dependantModules.containsKey(scope)) {
dependantModules.get(scope).add(m);
} else {
List<Module> modules = new ArrayList<>();
modules.add(m);
dependantModules.put(scope, modules);
}
}
}
return dependantModules;
}

// TODO: provide in common util class
String uppercaseFirstChar(String name) {
if (name.isEmpty()) return name;
return Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package org.springframework.sbm.build.api;

import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;

import org.springframework.sbm.project.resource.ProjectResource;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand All @@ -40,6 +42,11 @@ public interface BuildFile extends ProjectResource {
*/
List<Dependency> getRequestedDependencies();

/**
* Searches for a requested dependency matching the given GAV.
*/
Optional<Dependency> findRequestedDependency(String gav);

/**
* Returns any available dependency (declared or transitive) with given scope.
*/
Expand Down Expand Up @@ -98,12 +105,18 @@ public interface BuildFile extends ProjectResource {

List<Path> getResolvedDependenciesPaths();

Map<Scope, Set<Path>> getResolvedDependenciesMap();

/**
* Returns the classpath for given {@code scope}.
* target/classes and target/test-classes are included.
*/
Set<Path> getClasspath(Scope scope);

boolean hasPlugin(Plugin plugin);

void addPlugin(Plugin plugin);

List<Path> getClasspath();

List<Path> getSourceFolders();

List<Path> getTestSourceFolders();
Expand Down Expand Up @@ -176,4 +189,22 @@ public interface BuildFile extends ProjectResource {

Optional<Plugin> findPlugin(String groupId, String artifactId);

/**
* Returns GAV of groupId:artifactId:version of this build file.
*/
default String getGav() {
return getGroupId() + ":" + getArtifactId() + ":" + getVersion();
}

/**
* Searches for the dependency matching the given gav.
*/
Optional<Dependency> findDeclaredDependency(String gav);

/**
* Returns the declared dependency matching the given gav and throw exception if it doesn't exist.
* @throws IllegalStateException when no dependency with given gav exists.
*/
Dependency getDeclaredDependency(String gav);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
package org.springframework.sbm.build.api;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openrewrite.maven.tree.Scope;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RequiredArgsConstructor
@Getter
public class DependenciesChangedEvent {

private final List<Path> resolvedDependencies;
/**
* Event published when new dependencies were added to a {@link BuildFile}.
* A listener can then use the information to recompile affected java source files.
*
* @author Fabian Krueger
*/
public record DependenciesChangedEvent(OpenRewriteMavenBuildFile openRewriteMavenBuildFile, Map<Scope, Set<Path>> resolvedDependencies) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.core.lang.Nullable;
import lombok.*;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.semver.LatestRelease;

import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -99,7 +100,7 @@ private String tagString(String name, String value) {
/**
* @return the dependency coordinates as {@code 'groupId:artifactId:version'}
*/
public String getCoordinates() {
public String getGav() {
return getGroupId() + ":" + getArtifactId() + ":" + getVersion();
}

Expand All @@ -125,4 +126,8 @@ public static Dependency fromCoordinates(String coordinate) {
throw new IllegalArgumentException("Expected dependency in format groupid:artifactid[:version], but it is: " + coordinate);
}
}

public String getEffectiveScope() {
return scope == null ? Scope.Compile.name() : scope;
}
}
Loading

0 comments on commit 31724f8

Please sign in to comment.