Search in sources :

Example 76 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class Main method _jpm.

/**
	 * Initialize the repository and other global vars.
	 * 
	 * @param opts the options
	 * @throws IOException
	 */
@Description("Just Another Package Manager for Java (\"jpm help jpm\" to see a list of global options)")
public void _jpm(JpmOptions opts) throws IOException {
    try {
        setExceptions(opts.exceptions());
        setTrace(opts.trace());
        setPedantic(opts.pedantic());
        Platform platform = Platform.getPlatform(this, opts.os());
        if (opts.base() != null)
            base = IO.getFile(base, opts.base());
        if (opts.settings() != null) {
            settings = new Settings(opts.settings());
            logger.debug("Using settings file: {}", opts.settings());
        } else {
            settings = new Settings(platform.getConfigFile());
            logger.debug("Using settings file: {}", platform.getConfigFile());
        }
        File homeDir;
        File binDir;
        String home = settings.get(JPM_CONFIG_HOME);
        String bin = settings.get(JPM_CONFIG_BIN);
        if (opts.home() != null) {
            logger.debug("home set");
            homeDir = IO.getFile(base, opts.home());
            binDir = new File(homeDir, "bin");
        } else if (opts.user()) {
            logger.debug("user set");
            homeDir = platform.getLocal();
            binDir = new File(homeDir, "bin");
        } else if (!opts.global() && home != null) {
            logger.debug("global or in settings");
            homeDir = new File(home);
            binDir = new File(bin);
        } else {
            logger.debug("default");
            homeDir = platform.getGlobal();
            binDir = platform.getGlobalBinDir();
        }
        logger.debug("home={}, bin={}", homeDir, binDir);
        if (opts.bindir() != null) {
            logger.debug("bindir set");
            binDir = new File(opts.bindir());
            if (!binDir.isAbsolute())
                binDir = new File(base, opts.bindir());
            binDir = binDir.getAbsoluteFile();
        } else if (bin != null && !opts.user() && !opts.global()) {
            binDir = new File(bin);
        }
        logger.debug("home={}, bin={}", homeDir, binDir);
        url = opts.library();
        if (url == null)
            url = settings.get("library.url");
        jpm = new JustAnotherPackageManager(this, platform, homeDir, binDir);
        platform.setJpm(jpm);
        jpm.setLibrary(url == null ? null : new URI(url));
        try {
            this.options = opts;
            if (opts.xtesting())
                jpm.setUnderTest();
            CommandLine handler = opts._command();
            List<String> arguments = opts._arguments();
            if (arguments.isEmpty()) {
                Justif j = new Justif();
                Formatter f = j.formatter();
                handler.help(f, this);
                err.println(j.wrap());
            } else {
                String cmd = arguments.remove(0);
                String help = handler.execute(this, cmd, arguments);
                if (help != null) {
                    err.println(help);
                }
            }
            if (options.width() > 0)
                this.width = options.width();
        } finally {
            jpm.close();
        }
    } catch (InvocationTargetException t) {
        Throwable tt = t;
        while (tt instanceof InvocationTargetException) tt = ((InvocationTargetException) tt).getTargetException();
        exception(tt, "%s", tt);
    } catch (Throwable t) {
        exception(t, "Failed %s", t);
    } finally {
        // Check if we need to wait for it to finish
        if (opts.key()) {
            System.out.println("Hit a key to continue ...");
            System.in.read();
        }
    }
    if (!check(opts.failok())) {
        System.exit(getErrors().size());
    }
}
Also used : JustAnotherPackageManager(aQute.jpm.lib.JustAnotherPackageManager) CommandLine(aQute.lib.getopt.CommandLine) Platform(aQute.jpm.platform.Platform) Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) URI(java.net.URI) Settings(aQute.lib.settings.Settings) InvocationTargetException(java.lang.reflect.InvocationTargetException) Description(aQute.lib.getopt.Description)

Example 77 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class Main method _update.

@Description("Perform updates for installed commands and services")
public void _update(UpdateOptions opts) throws Exception {
    if (!jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    ArrayList<String> refs = new ArrayList<String>();
    for (CommandData data : jpm.getCommands()) {
        refs.add(data.name);
    }
    for (ServiceData data : jpm.getServices()) {
        refs.add(data.name);
    }
    ArrayList<UpdateMemo> notFound = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<UpdateMemo> upToDate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<UpdateMemo> toUpdate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<CommandData> datas = new ArrayList<CommandData>();
    if (opts._arguments().size() == 0) {
        datas.addAll(jpm.getCommands());
        datas.addAll(jpm.getServices());
    } else {
        for (String pattern : opts._arguments()) {
            Glob glob = new Glob(pattern);
            for (String name : refs) {
                if (glob.matcher(name).matches()) {
                    CommandData data = jpm.getCommand(name);
                    if (data == null) {
                        Service service = jpm.getService(name);
                        if (service != null) {
                            data = service.getServiceData();
                        }
                    }
                    if (data != null) {
                        datas.add(data);
                    }
                }
            }
        }
    }
    for (CommandData data : datas) {
        jpm.listUpdates(notFound, upToDate, toUpdate, data, opts.staged());
    }
    if (opts.all() || opts._arguments().size() > 0) {
        for (UpdateMemo memo : toUpdate) {
            jpm.update(memo);
        }
        out.format("%d command(s) updated%n", toUpdate.size());
    } else {
        Justif justif = new Justif(100, 20, 50);
        StringBuilder sb = new StringBuilder();
        Formatter f = new Formatter(sb);
        if (upToDate.size() > 0) {
            f.format("Up to date:%n");
            for (UpdateMemo memo : upToDate) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service) \t0- %s%n", memo.current.name, memo.current.version);
                } else {
                    f.format(" - %s \t0- %s%n", memo.current.name, memo.current.version);
                }
            }
            f.format("%n");
        }
        if (toUpdate.size() > 0) {
            f.format("Update available:%n");
            for (UpdateMemo memo : toUpdate) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service) \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
                } else {
                    f.format(" - %s \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
                }
            }
            f.format("%n");
        }
        if (notFound.size() > 0) {
            if (opts.staged()) {
                f.format("Information not found (local install ?):%n");
            } else {
                f.format("Information not found (try including staging versions with the --staged (-s) flag)%n");
            }
            for (UpdateMemo memo : notFound) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service)%n", memo.current.name);
                } else {
                    f.format(" - %s%n", memo.current.name);
                }
            }
        }
        if (toUpdate.size() > 0) {
            f.format("%nIn order to apply all possible updates, run jpm update again with the --all (or -a) flag.%n");
        }
        f.flush();
        justif.wrap(sb);
        out.println(sb.toString());
        f.close();
    }
}
Also used : JustAnotherPackageManager(aQute.jpm.lib.JustAnotherPackageManager) Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter) ArrayList(java.util.ArrayList) UpdateMemo(aQute.jpm.lib.JustAnotherPackageManager.UpdateMemo) Service(aQute.jpm.lib.Service) ServiceData(aQute.jpm.lib.ServiceData) Glob(aQute.libg.glob.Glob) CommandData(aQute.jpm.lib.CommandData) Description(aQute.lib.getopt.Description)

Example 78 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class Main method printPrograms.

// void print(Iterable<RevisionRef> revisions) {
// for (RevisionRef r : revisions) {
// out.printf("%-40s %s %s\n", jpm.getCoordinates(r),
// Hex.toHexString(r._id), (r.description == null ? ""
// : r.description));
// }
// }
void printPrograms(Iterable<? extends Program> programs) {
    Justif j = new Justif(120, 40, 42, 100);
    StringBuilder sb = new StringBuilder();
    try (Formatter f = new Formatter(sb)) {
        for (Program p : programs) {
            if (p.groupId.equals(Library.OSGI_GROUP) || p.groupId.equals(Library.SHA_GROUP))
                f.format("%s", p.artifactId);
            else
                f.format("%s:%s", p.groupId, p.artifactId);
            f.format("\t0-\t1");
            if (p.wiki != null && p.wiki.text != null)
                sb.append(p.wiki.text.replace('\n', '\f'));
            else if (p.last != null) {
                if (p.last.description != null)
                    sb.append(p.last.description.replace('\n', '\f'));
            }
            f.format("%n");
        }
        j.wrap(sb);
        out.println(sb);
    }
}
Also used : Program(aQute.service.library.Library.Program) Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter)

Example 79 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class Main method print.

private void print(List<CommandData> commands) {
    Justif j = new Justif(width, tabs);
    Formatter f = j.formatter();
    for (CommandData command : commands) {
        f.format("%s\t1%s%n", command.name, Strings.display(command.description, command.title));
    }
    out.append(j.wrap());
}
Also used : Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter) CommandData(aQute.jpm.lib.CommandData)

Example 80 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class Platform method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    try (Formatter formatter = new Formatter(sb)) {
        formatter.format("Name                %s%n", getName());
        formatter.format("Local               %s%n", getLocal());
        formatter.format("Global              %s%n", getGlobal());
        return formatter.toString();
    }
}
Also used : Formatter(java.util.Formatter)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6