Search in sources :

Example 1 with V2_AsciiTable

use of de.vandermeer.asciitable.v2.V2_AsciiTable in project walkmod-core by walkmod.

the class PrintPluginsCommand method execute.

public void execute() {
    if (help) {
        command.usage("init");
    } else {
        try {
            WalkModFacade facade = new WalkModFacade(OptionsBuilder.options());
            Configuration cfg = facade.getConfiguration();
            Collection<PluginConfig> installedPlugins = null;
            if (cfg != null) {
                installedPlugins = cfg.getPlugins();
            }
            URL searchURL = new URL(MVN_SEARCH_URL);
            InputStream is = null;
            Map<String, Boolean> installedList = new LinkedHashMap<String, Boolean>();
            Map<String, String> pluginsList = new LinkedHashMap<String, String>();
            Map<String, String> pluginsURLs = new LinkedHashMap<String, String>();
            try {
                is = searchURL.openStream();
                String content = readInputStreamAsString(is);
                DefaultJSONParser parser = new DefaultJSONParser(content);
                JSONObject object = parser.parseObject();
                parser.close();
                JSONArray artifactList = (object.getJSONObject("response")).getJSONArray("docs");
                for (int i = 0; i < artifactList.size(); i++) {
                    JSONObject artifact = artifactList.getJSONObject(i);
                    String artifactId = artifact.getString("a");
                    if (artifactId.startsWith("walkmod-") && artifactId.endsWith("-plugin")) {
                        String groupId = artifact.getString("g");
                        if (!"org.walkmod.maven.plugins".equals(groupId)) {
                            String latestVersion = artifact.getString("latestVersion");
                            String pom = artifactId + "-" + latestVersion + ".pom";
                            String directory = groupId.replaceAll("\\.", "/");
                            URL artifactDetailsURL = new URL(ARTIFACT_DETAILS_URL + directory + "/" + artifactId + "/" + latestVersion + "/" + pom);
                            InputStream projectIs = null;
                            try {
                                projectIs = artifactDetailsURL.openStream();
                            } catch (Exception e) {
                            }
                            if (projectIs != null) {
                                try {
                                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                                    Document doc = dBuilder.parse(projectIs);
                                    NodeList nList = doc.getElementsByTagName("description");
                                    String description = "unavailable description";
                                    if (nList.getLength() == 1) {
                                        description = nList.item(0).getTextContent();
                                    }
                                    String id = "";
                                    if (!groupId.equals("org.walkmod")) {
                                        id = groupId + ":";
                                    }
                                    id += artifactId.substring("walkmod-".length(), artifactId.length() - "-plugin".length());
                                    if (Character.isLowerCase(description.charAt(0))) {
                                        description = Character.toUpperCase(description.charAt(0)) + description.substring(1, description.length());
                                    }
                                    if (!description.endsWith(".")) {
                                        description = description + ".";
                                    }
                                    pluginsList.put(id, description);
                                    nList = doc.getChildNodes().item(0).getChildNodes();
                                    int max = nList.getLength();
                                    String url = "unavailable url";
                                    for (int j = 0; j < max; j++) {
                                        String name = nList.item(j).getNodeName();
                                        if (name.equals("url")) {
                                            url = nList.item(j).getTextContent();
                                            j = max;
                                        }
                                    }
                                    pluginsURLs.put(id, url);
                                    PluginConfig equivalentPluginCfg = new PluginConfigImpl();
                                    equivalentPluginCfg.setGroupId(groupId);
                                    equivalentPluginCfg.setArtifactId(artifactId);
                                    boolean isInstalled = (installedPlugins != null && installedPlugins.contains(equivalentPluginCfg));
                                    installedList.put(id, isInstalled);
                                } finally {
                                    projectIs.close();
                                }
                            }
                        }
                    }
                }
            } finally {
                is.close();
            }
            Set<String> keys = pluginsList.keySet();
            List<String> sortedKeys = new LinkedList<String>(keys);
            Collections.sort(sortedKeys);
            at = new V2_AsciiTable();
            at.addRule();
            at.addRow("PLUGIN NAME (ID)", "RDY", "DESCRIPTION");
            at.addStrongRule();
            for (String key : sortedKeys) {
                String installed = "";
                if (installedList.get(key)) {
                    installed = "*";
                }
                at.addRow(key, installed, pluginsList.get(key) + "\n\nURL:" + pluginsURLs.get(key));
                at.addRule();
            }
        } catch (Exception e) {
            throw new WalkModException("Invalid plugins URL", e);
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(org.walkmod.conf.entities.Configuration) Document(org.w3c.dom.Document) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModException(org.walkmod.exceptions.WalkModException) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) JSONArray(com.alibaba.fastjson.JSONArray) WalkModException(org.walkmod.exceptions.WalkModException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PluginConfig(org.walkmod.conf.entities.PluginConfig) JSONObject(com.alibaba.fastjson.JSONObject) DocumentBuilder(javax.xml.parsers.DocumentBuilder) WalkModFacade(org.walkmod.WalkModFacade) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 2 with V2_AsciiTable

use of de.vandermeer.asciitable.v2.V2_AsciiTable in project walkmod-core by walkmod.

the class PrintProvidersCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        jcommander.usage("modules");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options());
        Configuration cfg = facade.getConfiguration();
        if (cfg == null) {
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
        }
        at = new V2_AsciiTable();
        at.addRule();
        at.addRow("CONFIGURATION PROVIDERS", "PARAMETERS");
        at.addRule();
        if (cfg != null) {
            Collection<ProviderConfig> providers = cfg.getProviderConfigurations();
            if (providers != null) {
                for (ProviderConfig provider : providers) {
                    Map<String, Object> params = provider.getParameters();
                    if (params == null) {
                        at.addRow(provider.getType(), "");
                    } else {
                        Set<String> keys = params.keySet();
                        int i = 0;
                        for (String key : keys) {
                            if (i == 0) {
                                at.addRow(provider.getType(), params.get(key).toString());
                            } else {
                                at.addRow("", params.get(key).toString());
                            }
                            i++;
                        }
                    }
                    at.addRule();
                }
            }
        }
        at.addRule();
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) WalkModFacade(org.walkmod.WalkModFacade)

Example 3 with V2_AsciiTable

use of de.vandermeer.asciitable.v2.V2_AsciiTable in project walkmod-core by walkmod.

the class InspectCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("inspect");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().offline(offline));
        List<BeanDefinition> beans = facade.inspectPlugin(new PluginConfigImpl(pluginId.get(0)));
        List<String> validTypesList = Arrays.asList("String", "JSONObject", "JSONArray");
        if (beans != null) {
            at = new V2_AsciiTable();
            at.addRule();
            at.addRow("TYPE NAME (ID)", "CATEGORY", "PROPERTIES", "DESCRIPTION");
            at.addStrongRule();
            for (BeanDefinition bean : beans) {
                List<PropertyDefinition> properties = bean.getProperties();
                if (properties == null || properties.isEmpty()) {
                    at.addRow(bean.getType(), bean.getCategory(), "", bean.getDescription());
                } else {
                    int i = 0;
                    for (PropertyDefinition pd : properties) {
                        if (validTypesList.contains(pd.getType())) {
                            String label = pd.getName() + ":" + pd.getType();
                            if (pd.getDefaultValue() != null && pd.getDefaultValue().length() != 0) {
                                label = label + " (" + pd.getDefaultValue() + ")";
                            }
                            if (i == 0) {
                                at.addRow(bean.getType(), bean.getCategory(), label, bean.getDescription());
                            } else {
                                at.addRow("", "", label, "");
                            }
                            i++;
                        }
                    }
                }
                at.addRule();
            }
        }
    }
}
Also used : V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade) BeanDefinition(org.walkmod.conf.entities.BeanDefinition) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) PropertyDefinition(org.walkmod.conf.entities.PropertyDefinition)

Example 4 with V2_AsciiTable

use of de.vandermeer.asciitable.v2.V2_AsciiTable in project walkmod-core by walkmod.

the class PrintChainsCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("chains");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().configurationFile(configurationFile).build());
        Configuration cfg = facade.getConfiguration();
        at = new V2_AsciiTable();
        at.addRule();
        at.addRow("CHAIN", "READER PATH", "WRITER PATH", "TRANSFORMATIONS");
        at.addStrongRule();
        if (cfg == null) {
            at.addRule();
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
        }
        if (cfg != null) {
            Collection<ChainConfig> chains = cfg.getChainConfigs();
            if (chains != null) {
                for (ChainConfig cc : chains) {
                    List<TransformationConfig> transformations = cc.getWalkerConfig().getTransformations();
                    int numTransformations = transformations.size();
                    int numReaderIncludesExcludes = 0;
                    int includesLength = 0;
                    String[] excludes = cc.getReaderConfig().getExcludes();
                    if (excludes != null) {
                        numReaderIncludesExcludes = excludes.length;
                    }
                    String[] includes = cc.getReaderConfig().getIncludes();
                    if (includes != null) {
                        includesLength = includes.length;
                        numReaderIncludesExcludes += includes.length;
                    }
                    int limit = numReaderIncludesExcludes + 1;
                    if (numTransformations > numReaderIncludesExcludes) {
                        limit = numTransformations;
                    }
                    int includesExcludesWriterLength = 0;
                    int includesWriterLength = 0;
                    String[] excludesWriter = cc.getWriterConfig().getExcludes();
                    if (excludesWriter != null) {
                        includesExcludesWriterLength += excludesWriter.length;
                        if (excludesWriter.length + 1 > limit) {
                            limit = excludesWriter.length + 1;
                        }
                    }
                    String[] includesWriter = cc.getWriterConfig().getIncludes();
                    if (includesWriter != null) {
                        includesExcludesWriterLength += includesWriter.length;
                        includesWriterLength = includesWriter.length;
                        if (includesWriter.length + 1 > limit) {
                            limit = includesWriter.length + 1;
                        }
                    }
                    for (int i = 0; i < limit; i++) {
                        TransformationConfig next = null;
                        String type = "";
                        if (i < numTransformations) {
                            next = transformations.get(i);
                            type = "- " + next.getType();
                        }
                        if (i == 0) {
                            at.addRow(cc.getName(), cc.getReaderConfig().getPath(), cc.getWriterConfig().getPath(), type);
                        } else {
                            String readerWildcard = "";
                            if (i - 1 < includesLength) {
                                readerWildcard = "> " + includes[i - 1];
                            } else {
                                if (i - 1 < numReaderIncludesExcludes) {
                                    readerWildcard = "< " + excludes[i - 1 + includesLength];
                                }
                            }
                            String writerWildcard = "";
                            if (includesWriter != null && i - 1 < includesWriter.length) {
                                writerWildcard = "> " + includesWriter[i - 1];
                            } else if (i - 1 < includesExcludesWriterLength) {
                                writerWildcard = "< " + excludesWriter[i - 1 + includesWriterLength];
                            }
                            at.addRow("", readerWildcard, writerWildcard, type);
                        }
                    }
                    at.addRule();
                }
            }
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Example 5 with V2_AsciiTable

use of de.vandermeer.asciitable.v2.V2_AsciiTable in project walkmod-core by walkmod.

the class AsciiTableWidth method getColumnWidths.

@Override
public int[] getColumnWidths(V2_AsciiTable table) {
    int cols = table.getColumnCount();
    int[] resultWidths = new int[cols];
    // apply min width settings
    System.arraycopy(minWidths, 0, resultWidths, 0, minWidths.length > cols ? cols : minWidths.length);
    // iterate over all rows
    for (V2_Row row : table.getTable()) {
        if (row instanceof ContentRow) {
            ContentRow crow = (ContentRow) row;
            Object[] cells = crow.getColumns();
            // iterate over all cells in the row
            for (int i = 0; i < cells.length; i++) {
                String[] lines = ArrayTransformations.PROCESS_CONTENT(cells[i]);
                if (lines != null) {
                    // measuring the width of each line within a cell
                    for (String line : lines) {
                        int lineWidth = line.length() + 2 * crow.getPadding()[i];
                        if (lineWidth > maxWidth) {
                            lineWidth = maxWidth;
                        }
                        if (lineWidth > resultWidths[i]) {
                            int maxWidth = (maxWidths.length > i) ? maxWidths[i] : 0;
                            if (maxWidth < 1 || lineWidth < maxWidth) {
                                resultWidths[i] = lineWidth;
                            } else {
                                resultWidths[i] = maxWidth;
                            }
                        }
                    }
                }
            }
        }
    }
    return resultWidths;
}
Also used : ContentRow(de.vandermeer.asciitable.v2.row.ContentRow) V2_Row(de.vandermeer.asciitable.v2.row.V2_Row)

Aggregations

V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)7 WalkModFacade (org.walkmod.WalkModFacade)6 Configuration (org.walkmod.conf.entities.Configuration)5 ChainConfig (org.walkmod.conf.entities.ChainConfig)2 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)2 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)1 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 RenderedTable (de.vandermeer.asciitable.v2.RenderedTable)1 V2_AsciiTableRenderer (de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer)1 ContentRow (de.vandermeer.asciitable.v2.row.ContentRow)1 V2_Row (de.vandermeer.asciitable.v2.row.V2_Row)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1