package org.graalvm.component.installer.commands;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.graalvm.component.installer.CommandInput;
import org.graalvm.component.installer.Commands;
import org.graalvm.component.installer.CommonConstants;
import org.graalvm.component.installer.ComponentCollection;
import org.graalvm.component.installer.Feedback;
import org.graalvm.component.installer.Version;
import org.graalvm.component.installer.model.ComponentInfo;
public class AvailableCommand extends ListInstalledCommand {
private Version.Match vmatch;
@Override
public Map<String, String> supportedOptions() {
Map<String, String> opts = new HashMap<>(super.supportedOptions());
opts.put(Commands.OPTION_ALL, "");
opts.put(Commands.LONG_OPTION_ALL, Commands.OPTION_ALL);
opts.put(Commands.OPTION_VERSION, "s");
opts.put(Commands.LONG_OPTION_VERSION, Commands.OPTION_VERSION);
opts.put(Commands.OPTION_CATALOG, "X");
opts.put(Commands.OPTION_FOREIGN_CATALOG, "s");
opts.put(Commands.LONG_OPTION_FOREIGN_CATALOG, Commands.OPTION_FOREIGN_CATALOG);
opts.put(Commands.OPTION_USE_EDITION, "s");
opts.put(Commands.LONG_OPTION_USE_EDITION, Commands.OPTION_USE_EDITION);
opts.put(Commands.OPTION_SHOW_CORE, "");
opts.put(Commands.LONG_OPTION_SHOW_CORE, Commands.OPTION_SHOW_CORE);
opts.put(Commands.OPTION_SHOW_UPDATES, "");
opts.put(Commands.LONG_OPTION_SHOW_UPDATES, Commands.OPTION_SHOW_UPDATES);
return opts;
}
@Override
protected ComponentCollection initRegistry() {
super.initRegistry();
if (showUpdates) {
input.getRegistry().setAllowDistUpdate(true);
}
return input.getRegistry();
}
@Override
public void init(CommandInput commandInput, Feedback feedBack) {
String v = commandInput.optValue(Commands.OPTION_VERSION);
if (v != null) {
vmatch = Version.versionFilter(v);
}
showUpdates = commandInput.hasOption(Commands.OPTION_SHOW_UPDATES) || commandInput.hasOption(Commands.OPTION_ALL);
showCore = commandInput.hasOption(Commands.OPTION_SHOW_CORE) | showUpdates | commandInput.hasOption(Commands.OPTION_USE_EDITION);
super.init(commandInput, feedBack);
}
private boolean showUpdates;
private boolean showCore;
@Override
public int execute() throws IOException {
if (input.optValue(Commands.OPTION_HELP) != null) {
feedback.output("AVAILABLE_Help");
return 0;
}
return super.execute();
}
private boolean defaultFilter = true;
@Override
protected List<ComponentInfo> filterDisplayedVersions(String id, Collection<ComponentInfo> infos) {
if (input.optValue(Commands.OPTION_ALL) != null) {
return super.filterDisplayedVersions(id, infos);
}
Set<Version> seen = new HashSet<>();
Collection<ComponentInfo> filtered = new ArrayList<>();
Version.Match compatibleFilter = getRegistry().getGraalVersion().match(Version.Match.Type.COMPATIBLE);
if (defaultFilter) {
List<ComponentInfo> sorted = new ArrayList<>(infos);
Collections.sort(sorted, ComponentInfo.versionComparator().reversed());
for (ComponentInfo ci : sorted) {
if (!showUpdates && !compatibleFilter.test(ci.getVersion())) {
continue;
}
if (CommonConstants.GRAALVM_CORE_PREFIX.equals(ci.getId()) && !showCore) {
continue;
}
filtered.add(ci);
break;
}
} else {
for (ComponentInfo ci : infos) {
if (seen.add(ci.getVersion().installVersion())) {
filtered.add(ci);
}
}
}
return super.filterDisplayedVersions(id, filtered);
}
@Override
protected String acceptExpression(String expr) {
if (vmatch != null) {
return super.acceptExpression(expr);
}
Version.Match vm = Version.versionFilter(expr);
if (vm == null) {
vmatch = getRegistry().getGraalVersion().match(Version.Match.Type.INSTALLABLE);
defaultFilter = true;
return expr;
} else {
defaultFilter = false;
vmatch = vm;
return null;
}
}
@Override
protected Version.Match getVersionFilter() {
return vmatch == null ? super.getVersionFilter() : vmatch;
}
}