Skip to content

Commit

Permalink
FIXED : update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vpc committed Sep 15, 2019
1 parent 1fdc7ca commit 4763f5e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public Iterator<NutsId> findVersionsImplFilesFolders(NutsId id, NutsIdFilter idF
FilesFoldersApi.Item[] all = FilesFoldersApi.getFilesAndFolders(false,true,artifactUrl, session.getSession());
List<NutsId> n = new ArrayList<>();
for (FilesFoldersApi.Item s : all) {
if(s.isFolder() && s.getName().equals("LATEST")){
continue;
}
String versionFilesUrl = artifactUrl + "/" + s.getName();
FilesFoldersApi.Item[] versionFiles = FilesFoldersApi.getFilesAndFolders(true,false,versionFilesUrl, session.getSession());
boolean validVersion = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
package net.vpc.app.nuts.core.impl.def.wscommands;

import net.vpc.app.nuts.*;
import net.vpc.app.nuts.core.impl.def.config.NutsWorkspaceConfigRuntime;
import net.vpc.app.nuts.core.impl.def.repos.DefaultNutsInstalledRepository;
import net.vpc.app.nuts.core.wscommands.AbstractNutsUpdateCommand;
import net.vpc.app.nuts.core.DefaultNutsUpdateResult;
import net.vpc.app.nuts.core.DefaultNutsWorkspaceUpdateResult;
import net.vpc.app.nuts.core.NutsExtensionListHelper;
import net.vpc.app.nuts.core.impl.def.config.NutsWorkspaceConfigApi;
import net.vpc.app.nuts.core.spi.NutsWorkspaceExt;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -37,6 +31,17 @@
* @author vpc
*/
public class DefaultNutsUpdateCommand extends AbstractNutsUpdateCommand {
private Comparator<NutsId> LATEST_VERSION_FIRST = (x, y) -> -x.getVersion().compareTo(y.getVersion());
private Comparator<NutsId> DEFAULT_THEN_LATEST_VERSION_FIRST = (x, y) -> {
DefaultNutsInstalledRepository rr = NutsWorkspaceExt.of(ws).getInstalledRepository();
int xi = rr.isDefaultVersion(x) ? 0 : 1;
int yi = rr.isDefaultVersion(y) ? 0 : 1;
int v = Integer.compare(xi, yi);
if (v != 0) {
return v;
}
return -x.getVersion().compareTo(y.getVersion());
};

public static final Logger LOG = Logger.getLogger(DefaultNutsUpdateCommand.class.getName());

Expand Down Expand Up @@ -128,8 +133,6 @@ private Set<NutsId> getRegularIds() {
@Override
public NutsUpdateCommand checkUpdates() {
NutsWorkspaceExt dws = NutsWorkspaceExt.of(ws);
// NutsWorkspaceCurrentConfig actualBootConfig = ws.config().current();
// NutsWorkspaceCurrentConfig jsonBootConfig = getConfigManager().getBootContext();
NutsSession session = NutsWorkspaceUtils.validateSession(ws, this.getSession());
Map<String, NutsUpdateResult> allUpdates = new LinkedHashMap<>();
Map<String, NutsUpdateResult> extUpdates = new LinkedHashMap<>();
Expand Down Expand Up @@ -268,11 +271,11 @@ protected NutsUpdateResult checkRegularUpdate(NutsId id) {
DefaultNutsUpdateResult r = new DefaultNutsUpdateResult();
r.setId(id.getShortNameId());
boolean shouldUpdateDefault = false;
NutsId d0Id = ws.search().id(id).setSession(searchSession).installed().setOptional(false).failFast(false).defaultVersions()
NutsId d0Id = ws.search().id(id).session(searchSession).installed().setOptional(false).failFast(false).defaultVersions()
.getResultIds().first();
if (d0Id == null) {
// may be the id is not default!
d0Id = ws.search().id(id).setSession(searchSession).installed().setOptional(false).failFast(false).latest()
d0Id = ws.search().id(id).session(searchSession).installed().setOptional(false).failFast(false).latest()
.getResultIds().first();
if (d0Id != null) {
shouldUpdateDefault = true;
Expand All @@ -281,19 +284,19 @@ protected NutsUpdateResult checkRegularUpdate(NutsId id) {
if (d0Id == null) {
throw new NutsIllegalArgumentException(ws, id + " is not yet installed to be updated.");
}
NutsDefinition d0 = fetch0().id(d0Id).setSession(searchSession).installed().setOptional(false).failFast(false)
NutsDefinition d0 = fetch0().id(d0Id).session(searchSession).installed().setOptional(false).failFast(false)
.getResultDefinition();
if (d0 == null) {
throw new NutsIllegalArgumentException(ws, d0Id + " installation is broken and cannot be updated.");
}
//search latest parse
NutsId d1Id = ws.search().id(d0Id.getShortNameId()).setSession(searchSession)
NutsId d1Id = ws.search().id(d0Id.getShortNameId()).session(searchSession)
.failFast(false)
.anyWhere()
.latest()
.getResultIds().first();
//then fetch its definition!
NutsDefinition d1 = d1Id == null ? null : latestOnlineDependencies(fetch0().id(d1Id).setSession(searchSession))
NutsDefinition d1 = d1Id == null ? null : latestOnlineDependencies(fetch0().id(d1Id).session(searchSession))
.failFast(false)
.getResultDefinition();
r.setLocal(d0);
Expand Down Expand Up @@ -349,29 +352,6 @@ private void applyResult(NutsWorkspaceUpdateResult result) {
throw new NutsUserCancelException(ws);
}
NutsWorkspaceConfigManagerExt wcfg = NutsWorkspaceConfigManagerExt.of(ws.config());
// NutsWorkspaceCurrentConfig actualBootConfig = ws.config().getContext(net.vpc.app.nuts.NutsBootContextType.RUNTIME);
// NutsWorkspaceConfigApi aconfig = null;
// NutsWorkspaceConfigRuntime rconfig = null;
// if (apiUpdate != null && apiUpdate.isUpdateAvailable() && !apiUpdate.isUpdateApplied()) {
// aconfig = wcfg.getStoredConfigApi(apiUpdate.getAvailable().getId().getVersion().toString());
//// NutsWorkspaceExt.of(ws).deployBoot(getValidSession(), apiUpdate.getAvailable().getId(), false);
// ((DefaultNutsUpdateResult) apiUpdate).setUpdateApplied(true);
// traceSingleUpdate(apiUpdate);
// requireSave = true;
// }
// if (runtimeUpdate != null && runtimeUpdate.isUpdateAvailable() && !runtimeUpdate.isUpdateApplied()) {
//// NutsWorkspaceExt.of(ws).deployBoot(getValidSession(), runtimeUpdate.getAvailable().getId(), true);
// if (aconfig == null) {
// aconfig = wcfg.getStoredConfigApi(runtimeUpdate.getAvailable().getApiId().getVersion().toString());
// }
// aconfig.setRuntimeId(runtimeUpdate.getAvailable().getId().getLongName());
//
// rconfig = wcfg.getStoredConfigRuntime();
// rconfig.setDependencies(Arrays.stream(runtimeUpdate.getDependencies()).map(NutsId::getLongName).collect(Collectors.joining(";")));
// ((DefaultNutsUpdateResult) runtimeUpdate).setUpdateApplied(true);
// traceSingleUpdate(runtimeUpdate);
// requireSave = true;
// }
boolean apiUpdateAvailable = apiUpdate != null && apiUpdate.getAvailable() != null && !apiUpdate.isUpdateApplied();
boolean runtimeUpdateAvailable = runtimeUpdate != null && runtimeUpdate.getAvailable() != null && !runtimeUpdate.isUpdateApplied();
boolean apiUpdateApplicable = apiUpdateAvailable && !apiUpdate.isUpdateApplied();
Expand Down Expand Up @@ -507,22 +487,26 @@ public NutsUpdateResult checkCoreUpdate(NutsId id, String bootApiVersion, NutsSe
if (confId != null) {
oldId = confId;
}
try {
oldFile = fetch0().id(oldId).session(searchSession).online().getResultDefinition();
} catch (NutsNotFoundException ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
if(oldId!=null) {
try {
oldFile = fetch0().id(oldId).session(searchSession).online().getResultDefinition();
} catch (NutsNotFoundException ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
}
}
try {
newId = ws.search()
.addId(oldFile != null ? oldFile.getId().builder().setVersion("").build().toString() : NutsConstants.Ids.NUTS_RUNTIME)
NutsSearchCommand se = ws.search()
.addId(oldFile != null ? oldFile.getId().builder().version("").build().toString() : NutsConstants.Ids.NUTS_RUNTIME)
.runtime()
.targetApiVersion(bootApiVersion)
.frozenIds(getFrozenIds())
.latest()
.anyWhere()
.session(searchSession)
.getResultIds().first();
.sort(LATEST_VERSION_FIRST)
;
newId = se.getResultIds().first();
newFile = newId == null ? null : latestOnlineDependencies(fetch0().id(newId))
.session(searchSession)
.failFast(false)
Expand All @@ -533,55 +517,41 @@ public NutsUpdateResult checkCoreUpdate(NutsId id, String bootApiVersion, NutsSe
}
break;
}
case "companion":
case "extension": {
try {
oldId = ws.search().id(id).setEffective(true).setSession(searchSession)
.offline().getResultIds().first();
oldFile = fetch0().id(oldId).setSession(searchSession).getResultDefinition();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
}
try {
newId = ws.search().session(searchSession).addId(id)
.extensions()
.targetApiVersion(bootApiVersion)
.frozenIds(getFrozenIds())
.anyWhere()
.failFast(false)
.getResultIds().first();
newFile = newId == null ? null : latestOnlineDependencies(fetch0().session(searchSession).id(newId))
.online()
.getResultDefinition();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
}
}
case "companion": {
try {
oldId = ws.search().id(id).setEffective(true).setSession(searchSession)
.offline().getResultIds().first();
oldFile = fetch0().id(oldId).setSession(searchSession).getResultDefinition();
oldId = ws.search().id(id).effective().session(searchSession)
.installed().sort(DEFAULT_THEN_LATEST_VERSION_FIRST).failFast(false).getResultIds().first();
if (oldId != null) {
oldFile = fetch0().id(oldId).session(searchSession).getResultDefinition();
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
}
try {
newId = ws.search().session(searchSession).addId(id)
.companion()
NutsSearchCommand se = ws.search().session(searchSession).addId(id)
.targetApiVersion(bootApiVersion)
.frozenIds(getFrozenIds())
.anyWhere()
.failFast(false)
.getResultIds().first();
.latest()
.sort(LATEST_VERSION_FIRST);
if (type.equals("extension")) {
se.extensions();
} else if (type.equals("companion")) {
se.companion();
}
newId = se.getResultIds().first();

newFile = newId == null ? null : latestOnlineDependencies(fetch0().session(searchSession).id(newId))
.online()
.getResultDefinition();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error " + ex, ex);
//ignore
}
break;
}
}
//compare canonical forms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class NutsIdGraph {
private static final Logger LOG=Logger.getLogger(NutsIdGraph.class.getName());

private NutsIdGraphContext context = new NutsIdGraphContext();
private NutsIdGraphContext context;

private final Set<NutsId> visited = new LinkedHashSet<>();
private final Set<NutsIdNode> wildIds = new LinkedHashSet<>();
Expand All @@ -55,6 +55,7 @@ public class NutsIdGraph {
public NutsIdGraph(NutsSession session, boolean failFast) {
this.session = session;
this.failFast = failFast;
context=new NutsIdGraphContext(session);
}

private void reset(){
Expand Down Expand Up @@ -119,6 +120,7 @@ private <T> void fixConflicts() {
for (NutsIdNode nutsId : wildIds.values()) {
try {
NutsId nutsId1 = session.workspace().fetch().id(nutsId.id).setSession(session).getResultId();
context.getNutsIdInfo(nutsId.id,true).refTo=context.getNutsIdInfo(nutsId1,true);
toaddOk.add(new NutsIdNode(nutsId1, nutsId.path, nutsId.filter, session));
} catch (NutsNotFoundException ex) {
NutsDependency dep = session.workspace().dependency().builder().id(nutsId.id).build();
Expand Down Expand Up @@ -182,7 +184,7 @@ private NutsId uniformNutsId(NutsId id) {
public NutsId[] collect(Collection<NutsId> ids, Collection<NutsId> exclude) {
Set<NutsIdInfo> collected = new HashSet<>();
for (NutsId id : ids) {
visit(context.getNutsIdInfo(id, true), collected);
visit(context.getNutsIdInfoResult(id), collected);
}
Set<NutsId> excludeSet = new HashSet<>();
if (exclude != null) {
Expand Down Expand Up @@ -296,6 +298,7 @@ private void push0(Collection<NutsIdNode> ids) {
}
}
} else {
processed++;
wildIds.add(curr.id);
}
}
Expand Down Expand Up @@ -348,6 +351,7 @@ public static class NutsIdInfo {
private Set<NutsIdNode> nodes = new HashSet<>();
public List<NutsIdInfo> input = new ArrayList<>();
public List<NutsIdInfo> output = new ArrayList<>();
public NutsIdInfo refTo;

public NutsIdInfo(NutsId id) {
this.id = id;
Expand Down Expand Up @@ -421,6 +425,11 @@ public static class NutsIdGraphContext {
private Set<NutsIdNode> nodes = new HashSet<>();
private Map<NutsId, NutsIdInfo> nutsIds = new HashMap<>();
private Map<String, SimpleNutsIdInfo> snutsIds = new HashMap<>();
private NutsSession session;

public NutsIdGraphContext(NutsSession session) {
this.session = session;
}

SimpleNutsIdInfo getSimpleNutsIdInfo(NutsId id, boolean create) {
String ii = id.getShortName();
Expand All @@ -434,6 +443,17 @@ SimpleNutsIdInfo getSimpleNutsIdInfo(NutsId id, boolean create) {
return p;
}

NutsIdInfo getNutsIdInfoResult(NutsId id) {
NutsIdInfo p = getNutsIdInfo(id, false);
if(p==null){
throw new NutsNotFoundException(session.workspace(),id);
}
if(p.refTo!=null){
return p.refTo;
}
return p;
}

NutsIdInfo getNutsIdInfo(NutsId id, boolean create) {
NutsId ii = cleanup(id);
NutsIdInfo p = nutsIds.get(ii);
Expand Down
6 changes: 5 additions & 1 deletion nuts/src/main/java/net/vpc/app/nuts/NutsSearchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public interface NutsSearchCommand extends NutsWorkspaceCommand {
NutsSearchCommand setSorted(boolean sort);

/**
* latest version only
* search must return only latest versions for each artifact id
*
* @return {@code this} instance
*/
Expand Down Expand Up @@ -582,6 +582,10 @@ public interface NutsSearchCommand extends NutsWorkspaceCommand {

boolean isMain();

/**
* true if search must return only latest versions for each artifact id
* @return true if search must return only latest versions for each artifact id
*/
boolean isLatest();

NutsFetchCommand toFetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public boolean saveFile(Path filePath, String content, boolean force) throws IOE
fileContent = new String(Files.readAllBytes(filePath));
}
if (force || !content.trim().equals(fileContent.trim())) {
Files.createDirectories(filePath.getParent());
Files.write(filePath, content.getBytes());
return true;
}
Expand Down Expand Up @@ -85,6 +86,7 @@ public boolean addFileLine(Path filePath, String commentLine, String goodLine, b
updatedBashrc = true;
}
if (force || updatedBashrc) {
Files.createDirectories(filePath.getParent());
Files.write(filePath, (String.join("\n", lines) + "\n").getBytes());
}
return updatedBashrc;
Expand Down Expand Up @@ -114,6 +116,7 @@ public boolean removeFileLine(Path filePath, String commentLine, boolean force)
updatedBashrc = true;
}
if (force || updatedBashrc) {
Files.createDirectories(filePath.getParent());
Files.write(filePath, (String.join("\n", lines) + "\n").getBytes());
}
return updatedBashrc;
Expand Down

0 comments on commit 4763f5e

Please sign in to comment.