package org.graalvm.component.installer.commands;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.graalvm.component.installer.CommandInput;
import org.graalvm.component.installer.Commands;
import static org.graalvm.component.installer.Commands.LONG_OPTION_LIST_FILES;
import static org.graalvm.component.installer.Commands.OPTION_LIST_FILES;
import org.graalvm.component.installer.CommonConstants;
import static org.graalvm.component.installer.CommonConstants.CAP_GRAALVM_VERSION;
import org.graalvm.component.installer.ComponentCollection;
import org.graalvm.component.installer.ComponentParam;
import org.graalvm.component.installer.Feedback;
import org.graalvm.component.installer.InstallerCommand;
import org.graalvm.component.installer.Version;
import org.graalvm.component.installer.model.ComponentInfo;
import org.graalvm.component.installer.model.ComponentRegistry;
public abstract class QueryCommandBase implements InstallerCommand {
protected static final Map<String, String> BASE_OPTIONS = new HashMap<>();
static {
BASE_OPTIONS.put(OPTION_LIST_FILES, "");
BASE_OPTIONS.put(LONG_OPTION_LIST_FILES, OPTION_LIST_FILES);
}
@Override
public Map<String, String> supportedOptions() {
return BASE_OPTIONS;
}
protected CommandInput input;
protected ComponentRegistry registry;
protected ComponentCollection catalog;
protected Feedback feedback;
protected boolean verbose;
protected boolean printTable;
protected boolean listFiles;
protected List<ComponentParam> componentParams = new ArrayList<>();
protected List<ComponentInfo> components = new ArrayList<>();
protected boolean simpleFormat;
public ComponentRegistry getRegistry() {
return registry;
}
public void setRegistry(ComponentRegistry registry) {
this.registry = registry;
}
public Feedback getFeedback() {
return feedback;
}
public void setFeedback(Feedback feedback) {
this.feedback = feedback;
}
public boolean isListFiles() {
return listFiles;
}
public void setListFiles(boolean listFiles) {
this.listFiles = listFiles;
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
protected ComponentCollection initRegistry() {
this.registry = input.getLocalRegistry();
if (input.optValue(Commands.OPTION_CATALOG) != null ||
input.optValue(Commands.OPTION_FOREIGN_CATALOG) != null) {
return input.getRegistry();
} else {
return registry;
}
}
@Override
public void init(CommandInput commandInput, Feedback feedBack) {
this.input = commandInput;
this.catalog = initRegistry();
this.feedback = feedBack;
listFiles = commandInput.optValue(OPTION_LIST_FILES) != null;
verbose = commandInput.optValue(Commands.OPTION_VERBOSE) != null;
printTable = !listFiles && !verbose;
processOutputFormat();
}
protected void addComponent(ComponentParam param, ComponentInfo info) {
componentParams.add(param);
components.add(info);
}
public List<ComponentInfo> getComponents() {
return components;
}
protected void printComponents() {
printHeader();
Iterator<ComponentParam> itpar = componentParams.iterator();
for (ComponentInfo info : components) {
printDetails(itpar.next(), info);
printFileList(info);
printSeparator(info);
}
}
void printHeader() {
if (simpleFormat) {
feedback.output("LIST_ComponentShortListHeader_Simple@");
return;
}
if (printTable) {
feedback.output("LIST_ComponentShortListHeader");
}
}
String val(String s) {
if (simpleFormat) {
return s == null ? "" : s;
} else {
return s == null ? feedback.l10n("LIST_MetadataUnknown") : s;
}
}
protected String shortenComponentId(ComponentInfo info) {
return registry.shortenComponentId(info);
}
@SuppressWarnings("unused")
void printDetails(ComponentParam param, ComponentInfo info) {
String org;
URL u = info.getRemoteURL();
if (simpleFormat) {
org = u == null ? "" : u.toString();
} else if (u == null) {
org = "";
} else if (u.getProtocol().equals("file")) {
try {
org = new File(u.toURI()).getAbsolutePath();
} catch (URISyntaxException ex) {
org = u.toString();
}
} else {
org = u.getHost();
}
if (printTable) {
String fmt = simpleFormat ? "LIST_ComponentShortList_Simple@" : "LIST_ComponentShortList";
String line = String.format(feedback.l10n(fmt),
shortenComponentId(info), info.getVersion().displayString(), info.getName(), org, info.getId(), info.getStability().displayName(feedback));
feedback.verbatimOut(line, false);
} else {
String fmt = simpleFormat ? "LIST_ComponentBasicInfo_Simple@" : "LIST_ComponentBasicInfo";
feedback.output(fmt,
shortenComponentId(info), info.getVersion().displayString(), info.getName(),
findRequiredGraalVMVersion(info), u == null ? "" : u, info.getId(), info.getStability().displayName(feedback));
}
}
protected String findRequiredGraalVMVersion(ComponentInfo info) {
String s = info.getRequiredGraalValues().get(CAP_GRAALVM_VERSION);
if (s == null) {
return val(s);
}
Version v = Version.fromString(s);
return v.displayString();
}
void printFileList(ComponentInfo info) {
if (!listFiles) {
return;
}
List<String> files = new ArrayList<>(info.getPaths());
feedback.output(simpleFormat ? "LIST_ComponentFilesHeader_Simple@" : "LIST_ComponentFilesHeader", files.size());
Collections.sort(files);
for (String s : files) {
feedback.verbatimOut(s, false);
}
if (simpleFormat) {
feedback.output("LIST_ComponentFilesEnd@");
}
}
void printSeparator(@SuppressWarnings("unused") ComponentInfo info) {
if (simpleFormat || printTable) {
return;
}
feedback.verbatimOut("", true);
}
void processOutputFormat() {
if (Boolean.TRUE.toString().equals(System.getProperty(CommonConstants.SYSPROP_SIMPLE_OUTPUT))) {
simpleFormat = true;
}
}
}