Skip to content

Commit

Permalink
Fix for GAL-259, CLI, Including, excluding packages and transitive de…
Browse files Browse the repository at this point in the history
…pendency issues
  • Loading branch information
jfdenise committed Feb 25, 2019
1 parent 6d910b1 commit 069b51d
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -42,13 +42,18 @@ protected void runCommand(PmCommandInvocation invoc, State session) throws IOExc
}

public FeaturePackConfig getProvisionedFP(PmSession session) throws CommandExecutionException {
ProducerSpec channel = getProducer(session);
if (channel == null) {
ProducerSpec producer = getProducer(session);
if (producer == null) {
return null;
}
ProvisioningConfig config = session.getState().getConfig();
for (FeaturePackConfig dep : config.getFeaturePackDeps()) {
if (dep.getLocation().getProducer().equals(channel)) {
if (dep.getLocation().getProducer().equals(producer)) {
return dep;
}
}
for (FeaturePackConfig dep : config.getTransitiveDeps()) {
if (dep.getLocation().getProducer().equals(producer)) {
return dep;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,7 +19,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.aesh.command.option.Argument;
import org.jboss.galleon.universe.FeaturePackLocation;
import org.jboss.galleon.universe.FeaturePackLocation.ProducerSpec;
Expand Down Expand Up @@ -137,11 +136,10 @@ public FeaturePackConfig getProvisionedFP(PmSession session) throws CommandExecu
String orig = getPackage().substring(0, i);
String name = getPackage().substring(i + 1);
FeaturePackLocation.FPID fpid = null;
for (Entry<String, FeatureContainer> entry : session.getContainer().getFullDependencies().entrySet()) {
FeatureContainer container = entry.getValue();
FeatureContainer container = session.getContainer().getFullDependencies().get(orig);
if (container != null) {
if (container.getAllPackages().containsKey(Identity.fromString(orig, name))) {
fpid = container.getFPID();
break;
}
}
if (fpid == null) {
Expand All @@ -153,9 +151,17 @@ public FeaturePackConfig getProvisionedFP(PmSession session) throws CommandExecu
break;
}
}
}
if (config == null) {
throw new CommandExecutionException("No feature pack found for " + getPackage());
if (config == null) {
// reset buildID
FeaturePackLocation noBuildLocation = new FeaturePackLocation(fpid.getUniverse(), fpid.getProducer().getName(),
null, null, null);
for (FeaturePackConfig c : session.getState().getConfig().getTransitiveDeps()) {
if (c.getLocation().equals(c.getLocation().hasBuild() ? fpid.getLocation() : noBuildLocation)) {
config = c;
break;
}
}
}
}
return config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -59,6 +59,13 @@ protected List<String> getItems(PmCompleterInvocation completerInvocation) {
}
}
}
for (FeaturePackConfig fc : completerInvocation.getPmSession().getState().getConfig().getTransitiveDeps()) {
for (String pkg : cmd.getTargetedPackages(fc)) {
if (!packages.contains(pkg)) {
packages.add(pkg);
}
}
}
} else {
for (String pkg : cmd.getTargetedPackages(fp)) {
if (!packages.contains(pkg)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public static Map<FeaturePackConfig, String> getIncludedPackages(PmSession sessi
packages.put(c, pkg);
}
}
for (FeaturePackConfig c : session.getState().getConfig().getTransitiveDeps()) {
if (c.getIncludedPackages().contains(pkg)) {
packages.put(c, pkg);
}
}
} else {
if (config.getIncludedPackages().contains(pkg)) {
packages.put(config, pkg);
Expand All @@ -78,6 +83,11 @@ public static Map<FeaturePackConfig, String> getExcludedPackages(PmSession sessi
packages.put(c, pkg);
}
}
for (FeaturePackConfig c : session.getState().getConfig().getTransitiveDeps()) {
if (c.getExcludedPackages().contains(pkg)) {
packages.put(c, pkg);
}
}
} else {
if (config.getExcludedPackages().contains(pkg)) {
packages.put(config, pkg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -34,6 +34,11 @@ public boolean isActivated(ParsedCommand command) {
return true;
}
}
for (FeaturePackConfig cf : getSession().getState().getConfig().getTransitiveDeps()) {
if (!cmd.getTargetedPackages(cf).isEmpty()) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -38,7 +38,15 @@ public class StateExcludePackageCommand extends AbstractPackageCommand {
@Override
protected void runCommand(PmCommandInvocation invoc, State session, FeaturePackConfig config) throws IOException, ProvisioningException, CommandExecutionException {
try {
session.excludePackage(invoc.getPmSession(), PackagesUtil.getPackage(invoc.getPmSession(), config.getLocation().getFPID(), getPackage()), config);
int i = getPackage().indexOf("/");
String name = getPackage().substring(i + 1);
// If the config is null, it means that the package exists in a transitive dependency
// but no transitive dependency has been created yet.
if (config == null) {
session.excludePackageFromNewTransitive(invoc.getPmSession(), getProducer(invoc.getPmSession()), name);
} else {
session.excludePackage(invoc.getPmSession(), name, config);
}
} catch (Exception ex) {
throw new CommandExecutionException(invoc.getPmSession(), CliErrors.excludeFailed(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -38,7 +38,15 @@ public class StateIncludePackageCommand extends AbstractPackageCommand {
@Override
protected void runCommand(PmCommandInvocation invoc, State session, FeaturePackConfig config) throws IOException, ProvisioningException, CommandExecutionException {
try {
session.includePackage(invoc.getPmSession(), PackagesUtil.getPackage(invoc.getPmSession(), config.getLocation().getFPID(), getPackage()), config);
int i = getPackage().indexOf("/");
String name = getPackage().substring(i + 1);
// If the config is null, it means that the package exists in a transitive dependency
// but no transitive dependency has been created yet.
if (config == null) {
session.includePackageInNewTransitive(invoc.getPmSession(), getProducer(invoc.getPmSession()), name);
} else {
session.includePackage(invoc.getPmSession(), name, config);
}
} catch (Exception ex) {
throw new CommandExecutionException(invoc.getPmSession(), CliErrors.includeFailed(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.jboss.galleon.cli.model.state;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

Expand All @@ -28,6 +30,7 @@
import org.jboss.galleon.config.ConfigId;
import org.jboss.galleon.config.FeaturePackConfig;
import org.jboss.galleon.config.ProvisioningConfig;
import org.jboss.galleon.universe.FeaturePackLocation.ProducerSpec;

/**
*
Expand Down Expand Up @@ -84,7 +87,7 @@ private abstract class AbstractAction<T> implements State.Action {

private final Map<FeaturePackConfig, T> cf;
private final Map<FeaturePackLocation.FPID, Integer> indexes = new HashMap<>();

private final List<FeaturePackConfig> transitives = new ArrayList<>();
AbstractAction(Map<FeaturePackConfig, T> cf) {
this.cf = cf;
}
Expand All @@ -98,14 +101,32 @@ public void doAction(ProvisioningConfig current, ProvisioningConfig.Builder buil
boolean doit = doAction(fpBuilder, entry.getValue());
// this complexity is due to the fact that some fp could already have the configuration included/excluded/...
if (doit) {
int index = builder.getFeaturePackDepIndex(entry.getKey().getLocation());
indexes.put(entry.getKey().getLocation().getFPID(), index);
builder.removeFeaturePackDep(entry.getKey().getLocation());
builder.addFeaturePackDep(index, fpBuilder.build());
FeaturePackConfig cfg = fpBuilder.build();
if (cfg.isTransitive()) {
builder.removeTransitiveDep(cfg.getLocation().getFPID());
// Do not add back empty transitive
if (cfg.getLocation().getBuild() != null || !isEmptyConfig(cfg)) {
builder.addFeaturePackDep(cfg);
}
transitives.add(entry.getKey());
} else {
int index = builder.getFeaturePackDepIndex(entry.getKey().getLocation());
indexes.put(entry.getKey().getLocation().getFPID(), index);
builder.removeFeaturePackDep(entry.getKey().getLocation());
builder.addFeaturePackDep(index, fpBuilder.build());
}
}
}
}

private boolean isEmptyConfig(FeaturePackConfig cfg) {
return !cfg.hasDefinedConfigs() && !cfg.hasExcludedConfigs()
&& !cfg.hasExcludedPackages() && !cfg.hasFullModelsExcluded()
&& !cfg.hasFullModelsIncluded() && !cfg.hasIncludedConfigs()
&& !cfg.hasIncludedPackages() && !cfg.hasPatches() && cfg.isInheritPackages()
&& cfg.isInheritConfigs();
}

@Override
public void undoAction(ProvisioningConfig.Builder builder) throws ProvisioningException {
for (Entry<FeaturePackConfig, T> entry : cf.entrySet()) {
Expand All @@ -116,6 +137,13 @@ public void undoAction(ProvisioningConfig.Builder builder) throws ProvisioningEx
builder.addFeaturePackDep(index, entry.getKey());
}
}
for (FeaturePackConfig t : transitives) {
// Empty transitive are not added, so could no more exist
if (builder.hasTransitiveFeaturePackDep(t.getLocation().getProducer())) {
builder.removeTransitiveDep(t.getLocation().getFPID());
}
builder.addFeaturePackDep(t);
}
}

}
Expand Down Expand Up @@ -241,6 +269,51 @@ protected boolean doAction(FeaturePackConfig.Builder fpBuilder, String pkg) thro
}
}

private class ExcludePackageFromNewTransitiveAction implements State.Action {

private final String pkg;
private final FeaturePackLocation loc;
ExcludePackageFromNewTransitiveAction(ProducerSpec producer, String pkg) {
this.pkg = pkg;
// New transitive are created without build ID.
loc = FeaturePackLocation.fromString(producer.toString());
}

@Override
public void doAction(ProvisioningConfig current, ProvisioningConfig.Builder builder) throws ProvisioningException {
FeaturePackConfig config = FeaturePackConfig.transitiveBuilder(loc).excludePackage(pkg).build();
builder.addFeaturePackDep(config);
}

@Override
public void undoAction(ProvisioningConfig.Builder builder) throws ProvisioningException {
builder.removeTransitiveDep(loc.getFPID());
}
}

private class IncludePackageInNewTransitiveAction implements State.Action {

private final String pkg;
private final FeaturePackLocation loc;

IncludePackageInNewTransitiveAction(ProducerSpec producer, String pkg) {
this.pkg = pkg;
// New transitive are created without build ID.
loc = FeaturePackLocation.fromString(producer.toString());
}

@Override
public void doAction(ProvisioningConfig current, ProvisioningConfig.Builder builder) throws ProvisioningException {
FeaturePackConfig config = FeaturePackConfig.transitiveBuilder(loc).includePackage(pkg).build();
builder.addFeaturePackDep(config);
}

@Override
public void undoAction(ProvisioningConfig.Builder builder) throws ProvisioningException {
builder.removeTransitiveDep(loc.getFPID());
}
}

private class RemoveIncludedPackageAction extends AbstractAction<String> {

RemoveIncludedPackageAction(Map<FeaturePackConfig, String> cf) {
Expand Down Expand Up @@ -326,4 +399,12 @@ State.Action excludePackage(String pkg, FeaturePackConfig cf) throws Provisionin
State.Action removeExcludedPackage(Map<FeaturePackConfig, String> cf) throws ProvisioningDescriptionException, ProvisioningException, IOException {
return new RemoveExcludedPackageAction(cf);
}

State.Action excludePackageFromNewTransitive(ProducerSpec producer, String pkg) {
return new ExcludePackageFromNewTransitiveAction(producer, pkg);
}

State.Action includePackageInNewTransitive(ProducerSpec producer, String pkg) {
return new IncludePackageInNewTransitiveAction(producer, pkg);
}
}
11 changes: 11 additions & 0 deletions cli/src/main/java/org/jboss/galleon/cli/model/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jboss.galleon.runtime.FeaturePackRuntime;
import org.jboss.galleon.runtime.ProvisioningRuntime;
import org.jboss.galleon.runtime.ProvisioningRuntimeBuilder;
import org.jboss.galleon.universe.FeaturePackLocation.ProducerSpec;
import org.jboss.galleon.xml.ProvisioningXmlParser;
import org.jboss.galleon.xml.ProvisioningXmlWriter;

Expand Down Expand Up @@ -250,6 +251,16 @@ public void defineConfiguration(PmSession pmSession, ConfigId id) throws Provisi
config = pushState(action, pmSession);
}

public void excludePackageFromNewTransitive(PmSession pmSession, ProducerSpec producer, String pkg) throws ProvisioningException, IOException {
Action action = fpProvisioning.excludePackageFromNewTransitive(producer, pkg);
config = pushState(action, pmSession);
}

public void includePackageInNewTransitive(PmSession pmSession, ProducerSpec producer, String pkg) throws ProvisioningException, IOException {
Action action = fpProvisioning.includePackageInNewTransitive(producer, pkg);
config = pushState(action, pmSession);
}

public void export(Path file) throws Exception {
ProvisioningXmlWriter.getInstance().write(config, file);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -152,6 +152,10 @@ public boolean hasFeaturePackDep(ProducerSpec producer) {
return fpDeps.containsKey(producer);
}

public boolean hasTransitiveFeaturePackDep(ProducerSpec producer) {
return transitiveDeps.containsKey(producer);
}

public boolean hasFeaturePackDeps() {
return !fpDeps.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class CliTestUtils {
public static final String PRODUCER1 = "producer1";
public static final String PRODUCER2 = "producer2";
public static final String PRODUCER3 = "producer3";
public static final String PRODUCER4 = "producer4";
public static final String UNIVERSE_NAME = "cli-test-universe";

public static ProvisioningConfig getConfig(Path dir) throws ProvisioningException {
Expand Down
Loading

0 comments on commit 069b51d

Please sign in to comment.