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.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.graalvm.component.installer.Commands;
import org.graalvm.component.installer.MetadataException;
import org.graalvm.component.installer.Version;
import org.graalvm.component.installer.model.ComponentInfo;
public class ListInstalledCommand extends QueryCommandBase {
private List<String> expressions = Collections.emptyList();
private Pattern filterPattern;
public List<String> getExpressions() {
return expressions;
}
public void setExpressions(List<String> expressions) {
this.expressions = expressions;
}
@Override
public Map<String, String> supportedOptions() {
Map<String, String> m = new HashMap<>(super.supportedOptions());
m.put(Commands.OPTION_CATALOG, "");
m.put(Commands.LONG_OPTION_CATALOG, Commands.OPTION_CATALOG);
m.put(Commands.OPTION_URLS, "X");
m.put(Commands.OPTION_FILES, "X");
return m;
}
private void makeRegularExpression() {
StringBuilder sb = new StringBuilder();
for (String s : expressions) {
if (sb.length() > 0) {
sb.append("|");
}
sb.append(Pattern.quote(s)).append("$");
}
if (sb.length() == 0) {
sb.append(".*");
}
filterPattern = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
}
List<String> findComponentIds() {
Collection<String> comps = catalog.getComponentIDs();
List<String> ids = new ArrayList<>(comps.size());
for (String s : comps) {
if (filterPattern.matcher(s).find()) {
ids.add(s);
}
}
Collections.sort(ids);
return ids;
}
protected String acceptExpression(String expr) {
return expr;
}
@Override
public int execute() throws IOException {
if (input.optValue(Commands.OPTION_HELP) != null) {
feedback.output("LIST_Help");
return 0;
}
init(input, feedback);
expressions = new ArrayList<>();
while (input.hasParameter()) {
String s = input.nextParameter();
if (s == null || s.isEmpty()) {
continue;
}
String accepted = acceptExpression(s);
if (accepted != null) {
expressions.add(accepted);
}
}
if (process()) {
printComponents();
}
return 0;
}
protected Version.Match getVersionFilter() {
return input.getLocalRegistry().getGraalVersion().match(Version.Match.Type.INSTALLABLE);
}
protected List<ComponentInfo> filterDisplayedVersions(@SuppressWarnings("unused") String id, Collection<ComponentInfo> infos) {
List<ComponentInfo> ordered = new ArrayList<>(infos);
Collections.sort(ordered, ComponentInfo.reverseVersionComparator(input.getLocalRegistry().getManagementStorage()));
return ordered;
}
boolean process() {
makeRegularExpression();
List<String> ids = findComponentIds();
List<MetadataException> exceptions = new ArrayList<>();
if (ids.isEmpty()) {
if (!simpleFormat) {
feedback.message("LIST_NoComponentsFound");
}
return false;
}
Version.Match versionFilter = getVersionFilter();
for (String id : ids) {
try {
List<ComponentInfo> infos = new ArrayList<>(catalog.loadComponents(id, versionFilter, listFiles));
if (infos != null) {
for (ComponentInfo ci : filterDisplayedVersions(id, infos)) {
addComponent(null, ci);
}
}
} catch (MetadataException ex) {
exceptions.add(ex);
}
}
if (components.isEmpty()) {
if (!simpleFormat) {
feedback.message("LIST_NoComponentsFound");
}
return false;
}
if (!simpleFormat && !exceptions.isEmpty()) {
feedback.error("LIST_ErrorInComponentMetadata", null);
for (Exception e : exceptions) {
feedback.error("LIST_ErrorInComponentMetadataItem", e, e.getLocalizedMessage());
}
}
return true;
}
}