Skip to content

Commit

Permalink
Extract update info as data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Jun 28, 2023
1 parent fd51d93 commit e9006c1
Show file tree
Hide file tree
Showing 22 changed files with 770 additions and 525 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import static io.quarkus.devtools.messagewriter.MessageIcons.OUT_OF_DATE_ICON;
import static io.quarkus.devtools.messagewriter.MessageIcons.UP_TO_DATE_ICON;
import static io.quarkus.devtools.project.state.ProjectStates.resolveProjectState;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.workspace.WorkspaceModule;
import io.quarkus.bootstrap.workspace.WorkspaceModuleId;
import io.quarkus.devtools.commands.ProjectInfo;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
Expand All @@ -25,12 +22,9 @@
import io.quarkus.devtools.project.state.ModuleState;
import io.quarkus.devtools.project.state.ProjectState;
import io.quarkus.devtools.project.state.TopExtensionDependency;
import io.quarkus.devtools.project.update.PlatformInfo;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.maven.dependency.DependencyFlags;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.ExtensionOrigin;

public class ProjectInfoCommandHandler implements QuarkusCommandHandler {
Expand Down Expand Up @@ -58,13 +52,17 @@ protected static boolean logState(ProjectState projectState, boolean perModule,

final Map<ArtifactKey, PlatformInfo> providerInfo = new LinkedHashMap<>();
for (ArtifactCoords bom : projectState.getPlatformBoms()) {
providerInfo.computeIfAbsent(bom.getKey(), k -> new PlatformInfo()).imported = bom;
providerInfo.computeIfAbsent(bom.getKey(), k -> new PlatformInfo(bom, null));
}
for (TopExtensionDependency dep : projectState.getExtensions()) {
final ExtensionOrigin origin = dep.getOrigin();
if (origin != null && origin.isPlatform()) {
providerInfo.computeIfAbsent(origin.getBom().getKey(), k -> new PlatformInfo()).recommended = origin
.getBom();
providerInfo.compute(origin.getBom().getKey(), (k, v) -> {
if (v == null) {
return new PlatformInfo(null, origin.getBom());
}
return new PlatformInfo(v.getImported(), origin.getBom());
});
}
}

Expand All @@ -74,19 +72,19 @@ protected static boolean logState(ProjectState projectState, boolean perModule,
log.info("Quarkus platform BOMs:");
boolean recommendExtraImports = false;
for (PlatformInfo platform : providerInfo.values()) {
if (platform.imported == null) {
if (!platform.isImported()) {
recommendExtraImports = true;
continue;
}
final StringBuilder sb = new StringBuilder();
if (platform.recommended == null) {
if (platform.getRecommended() == null) {
if (rectify) {
sb.append(String.format(UpdateProjectCommandHandler.ITEM_FORMAT,
UpdateProjectCommandHandler.REMOVE, platform.imported.toCompactCoords()));
UpdateProjectCommandHandler.REMOVE, platform.getImported().toCompactCoords()));
recommendationsAvailable = true;
} else {
sb.append(" ");
sb.append(platform.imported.toCompactCoords());
sb.append(platform.getImported().toCompactCoords());
if (!projectState.getExtensions().isEmpty()) {
// The extension check is for modules that are aggregating modules (e.g. parent POMs)
// that import common BOMs. It's however not how it should be done.
Expand All @@ -96,27 +94,27 @@ protected static boolean logState(ProjectState projectState, boolean perModule,
}
} else if (platform.isVersionUpdateRecommended()) {
sb.append(String.format(UpdateProjectCommandHandler.ITEM_FORMAT,
rectify ? UpdateProjectCommandHandler.UPDATE : "", platform.imported.toCompactCoords()));
rectify ? UpdateProjectCommandHandler.UPDATE : "", platform.getImported().toCompactCoords()));
if (rectify) {
sb.append(platform.imported.toCompactCoords()).append(" -> ")
sb.append(platform.getImported().toCompactCoords()).append(" -> ")
.append(platform.getRecommendedVersion());
} else {
sb.append(" ").append(OUT_OF_DATE_ICON.iconOrMessage());
}
recommendationsAvailable = true;
} else {
sb.append(String.format(UpdateProjectCommandHandler.ITEM_FORMAT, "",
platform.imported.toCompactCoords()))
platform.getImported().toCompactCoords()))
.append(" ").append(UP_TO_DATE_ICON.iconOrMessage());
}
log.info(sb.toString());
}
if (rectify && recommendExtraImports) {
for (PlatformInfo platform : providerInfo.values()) {
if (platform.imported == null) {
if (platform.getImported() == null) {
log.info(String.format(UpdateProjectCommandHandler.ITEM_FORMAT,
UpdateProjectCommandHandler.ADD,
platform.recommended.toCompactCoords()));
platform.getRecommended().toCompactCoords()));
}
}
recommendationsAvailable = true;
Expand Down Expand Up @@ -299,125 +297,4 @@ private static boolean logModuleInfo(ProjectState project, ModuleState module, P
return recommendationsAvailable;
}

protected static ProjectState resolveProjectState(ApplicationModel appModel, ExtensionCatalog currentCatalog) {
final ProjectState.Builder projectBuilder = ProjectState.builder();

final Collection<ArtifactCoords> importedPlatformBoms = appModel.getPlatforms().getImportedPlatformBoms();
if (importedPlatformBoms.isEmpty()) {
return projectBuilder.build();
}

final Map<String, ExtensionProvider.Builder> extProviderBuilders = new LinkedHashMap<>(importedPlatformBoms.size());
importedPlatformBoms.forEach(bom -> {
projectBuilder.addPlatformBom(bom);
extProviderBuilders.put(ExtensionProvider.key(bom, true),
ExtensionProvider.builder().setArtifact(bom).setPlatform(true));
});

final Map<WorkspaceModuleId, ModuleState.Builder> projectModuleBuilders = new HashMap<>();
final Map<ArtifactKey, List<ModuleState.Builder>> directModuleDeps = new HashMap<>();

final WorkspaceModule appModule = appModel.getAppArtifact().getWorkspaceModule();
if (appModule != null) {
final ModuleState.Builder module = ModuleState.builder().setWorkspaceModule(appModule).setMainModule(true);
projectModuleBuilders.put(appModule.getId(), module);
appModule.getDirectDependencies()
.forEach(d -> directModuleDeps.computeIfAbsent(d.getKey(), dk -> new ArrayList<>()).add(module));

for (Dependency constraint : appModule.getDirectDependencyConstraints()) {
if (extProviderBuilders.containsKey(constraint.toCompactCoords())) {
module.addPlatformBom(constraint);
}
}

}
for (ResolvedDependency dep : appModel.getDependencies()) {
if (dep.getWorkspaceModule() != null) {
projectModuleBuilders.computeIfAbsent(dep.getWorkspaceModule().getId(), k -> {
final ModuleState.Builder module = ModuleState.builder()
.setWorkspaceModule(dep.getWorkspaceModule());
dep.getWorkspaceModule().getDirectDependencies().forEach(
d -> directModuleDeps.computeIfAbsent(d.getKey(), dk -> new ArrayList<>()).add(module));
return module;
});
}
}

final Map<ArtifactKey, TopExtensionDependency.Builder> directExtDeps = new HashMap<>();
for (ResolvedDependency dep : appModel.getDependencies()) {
if (dep.isFlagSet(DependencyFlags.TOP_LEVEL_RUNTIME_EXTENSION_ARTIFACT)) {
directExtDeps.put(dep.getKey(), TopExtensionDependency.builder().setResolvedDependency(dep)
.setTransitive(!directModuleDeps.containsKey(dep.getKey())));
} else if (dep.isRuntimeExtensionArtifact() && directModuleDeps.containsKey(dep.getKey())) {
directExtDeps.put(dep.getKey(), TopExtensionDependency.builder().setResolvedDependency(dep));
}
}

if (directExtDeps.isEmpty()) {
return projectBuilder.build();
}

currentCatalog.getExtensions().forEach(e -> {
final ArtifactKey key = e.getArtifact().getKey();
final TopExtensionDependency.Builder dep = directExtDeps.get(key);
if (dep != null) {
dep.setCatalogMetadata(e);
}
});

for (TopExtensionDependency.Builder extBuilder : directExtDeps.values()) {
final List<ModuleState.Builder> modules = directModuleDeps.getOrDefault(extBuilder.getKey(),
Collections.emptyList());
final TopExtensionDependency dep = extBuilder.setTransitive(modules.isEmpty()).build();
projectBuilder.addExtensionDependency(dep);
for (ModuleState.Builder module : modules) {
module.addExtensionDependency(dep);
}
final ExtensionProvider.Builder provider = extProviderBuilders.computeIfAbsent(dep.getProviderKey(),
k -> ExtensionProvider.builder().setOrigin(dep.getOrigin()));
provider.addExtension(dep);
}

for (ExtensionProvider.Builder builder : extProviderBuilders.values()) {
projectBuilder.addExtensionProvider(builder.build());
}

for (ModuleState.Builder builder : projectModuleBuilders.values()) {
projectBuilder.addModule(builder.build());
}

return projectBuilder.build();
}

static class PlatformInfo {
ArtifactCoords imported;
ArtifactCoords recommended;

boolean isVersionUpdateRecommended() {
return imported != null && recommended != null && !imported.getVersion().equals(recommended.getVersion());
}

String getRecommendedVersion() {
return recommended == null ? null : recommended.getVersion();
}

boolean isImported() {
return imported != null;
}

boolean isToBeImported() {
return imported == null && recommended != null;
}

ArtifactCoords getRecommendedCoords() {
return recommended == null ? imported : recommended;
}

String getRecommendedProviderKey() {
if (recommended != null) {
return ExtensionProvider.key(recommended, true);
}
return ExtensionProvider.key(imported, true);
}
}
}
Loading

0 comments on commit e9006c1

Please sign in to comment.