Search in sources :

Example 1 with Sed

use of aQute.libg.sed.Sed in project bnd by bndtools.

the class bnd method _bash.

@Description("Generate autocompletion file for bash")
public void _bash(Options options) throws Exception {
    File tmp = File.createTempFile("bnd-completion", ".tmp");
    tmp.deleteOnExit();
    try {
        IO.copy(getClass().getResource("bnd-completion.bash"), tmp);
        Sed sed = new Sed(tmp);
        sed.setBackup(false);
        Reporter r = new ReporterAdapter();
        CommandLine c = new CommandLine(r);
        Map<String, Method> commands = c.getCommands(this);
        StringBuilder sb = new StringBuilder();
        for (String commandName : commands.keySet()) {
            sb.append(" " + commandName);
        }
        sb.append(" help");
        sed.replace("%listCommands%", sb.toString().substring(1));
        sed.doIt();
        IO.copy(tmp, out);
    } finally {
        IO.delete(tmp);
    }
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Sed(aQute.libg.sed.Sed) ReporterAdapter(aQute.libg.reporter.ReporterAdapter) Reporter(aQute.service.reporter.Reporter) Method(java.lang.reflect.Method) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 2 with Sed

use of aQute.libg.sed.Sed in project bnd by bndtools.

the class Platform method parseCompletion.

public void parseCompletion(Object target, File f) throws Exception {
    IO.copy(getClass().getResource("unix/jpm-completion.bash"), f);
    Sed sed = new Sed(f);
    sed.setBackup(false);
    Reporter r = new ReporterAdapter();
    CommandLine c = new CommandLine(r);
    Map<String, Method> commands = c.getCommands(target);
    StringBuilder sb = new StringBuilder();
    for (String commandName : commands.keySet()) {
        sb.append(" " + commandName);
    }
    sb.append(" help");
    sed.replace("%listJpmCommands%", sb.toString().substring(1));
    sed.doIt();
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Sed(aQute.libg.sed.Sed) ReporterAdapter(aQute.libg.reporter.ReporterAdapter) Reporter(aQute.service.reporter.Reporter) Method(java.lang.reflect.Method)

Example 3 with Sed

use of aQute.libg.sed.Sed in project bnd by bndtools.

the class Project method replace.

boolean replace(File f, String pattern, String replacement) throws IOException {
    final Macro macro = getReplacer();
    Sed sed = new Sed(new Replacer() {

        public String process(String line) {
            return macro.process(line);
        }
    }, f);
    sed.replace(pattern, replacement);
    return sed.doIt() > 0;
}
Also used : Sed(aQute.libg.sed.Sed) Macro(aQute.bnd.osgi.Macro) Replacer(aQute.libg.sed.Replacer)

Example 4 with Sed

use of aQute.libg.sed.Sed in project bnd by bndtools.

the class Platform method process.

protected void process(String resource, CommandData data, String path, Map<String, String> map, String... extra) throws Exception {
    File file = new File(path);
    copy(getClass().getResourceAsStream(resource), file);
    Sed sed = new Sed(file);
    sed.setBackup(false);
    if (data.title == null || data.title.trim().length() == 0)
        data.title = data.name;
    //
    // Allow commands to be done in java or javaw
    //
    sed.replace("%java%", data.windows ? "javaw" : "java");
    for (Field key : data.getClass().getFields()) {
        Object value = key.get(data);
        if (value == null) {
            value = "";
        }
        // executed as one command and thus logged as one command
        if ("epilog".equals(key.getName()) || "prolog".equals(key.getName())) {
            String s = (String) value;
            if (s != null && s.trim().length() > 0) {
                value = "(" + s + ")";
            }
        }
        String v = "" + value;
        v = v.replace("\\", "\\\\");
        sed.replace("%" + key.getName() + "%", v);
    }
    ExtList<String> deps = new ExtList<String>();
    for (byte[] dependency : data.dependencies) {
        ArtifactData d = jpm.get(dependency);
        deps.add(d.file);
    }
    for (String x : extra) {
        deps.add(x);
    }
    String classpath = deps.join(File.pathSeparator);
    sed.replace("%classpath%", classpath.replace("\\", "\\\\"));
    if (map != null) {
        StringBuilder sb = new StringBuilder();
        String del = "-D";
        for (Map.Entry<String, String> e : map.entrySet()) {
            sed.replace("%" + e.getKey() + "%", e.getValue());
            sb.append(del).append(e.getKey()).append("=\"").append(e.getValue()).append("\"");
            del = " -D";
        }
        sed.replace("%defines%", sb.toString());
    } else {
        sed.replace("%defines%", "");
    }
    sed.doIt();
}
Also used : Field(java.lang.reflect.Field) Sed(aQute.libg.sed.Sed) ExtList(aQute.lib.collections.ExtList) File(java.io.File) Map(java.util.Map) ArtifactData(aQute.jpm.lib.ArtifactData)

Aggregations

Sed (aQute.libg.sed.Sed)4 CommandLine (aQute.lib.getopt.CommandLine)2 ReporterAdapter (aQute.libg.reporter.ReporterAdapter)2 Reporter (aQute.service.reporter.Reporter)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Macro (aQute.bnd.osgi.Macro)1 ArtifactData (aQute.jpm.lib.ArtifactData)1 ExtList (aQute.lib.collections.ExtList)1 Description (aQute.lib.getopt.Description)1 Replacer (aQute.libg.sed.Replacer)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1