Search in sources :

Example 6 with CommandLine

use of aQute.lib.getopt.CommandLine 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 7 with CommandLine

use of aQute.lib.getopt.CommandLine in project bnd by bndtools.

the class Main method _platform.

/**
	 * Show the platform info.
	 * 
	 * @param opts
	 * @throws IOException
	 * @throws Exception
	 */
@Description("Show platform information")
public void _platform(PlatformOptions opts) throws IOException, Exception {
    CommandLine cli = opts._command();
    List<String> cmds = opts._arguments();
    if (cmds.isEmpty()) {
        if (opts.verbose()) {
            Justif j = new Justif(80, 30, 40, 50, 60);
            jpm.getPlatform().report(j.formatter());
            out.append(j.wrap());
        } else
            out.println(jpm.getPlatform().getName());
    } else {
        String execute = cli.execute(jpm.getPlatform(), cmds.remove(0), cmds);
        if (execute != null) {
            out.append(execute);
        }
    }
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Justif(aQute.lib.justif.Justif) Description(aQute.lib.getopt.Description)

Example 8 with CommandLine

use of aQute.lib.getopt.CommandLine 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 9 with CommandLine

use of aQute.lib.getopt.CommandLine in project bnd by bndtools.

the class Main method run.

/**
	 * Main entry for the command line
	 * 
	 * @param args
	 * @throws Exception
	 */
public void run(String[] args) throws Exception {
    CommandLine cl = new CommandLine(this);
    ExtList<String> list = new ExtList<String>(args);
    String help = cl.execute(this, "jpm", list);
    check();
    if (help != null)
        err.println(help);
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) ExtList(aQute.lib.collections.ExtList)

Example 10 with CommandLine

use of aQute.lib.getopt.CommandLine in project bnd by bndtools.

the class Main method _init.

@Description("Install jpm on the current system")
public void _init(InitOptions opts) throws Exception {
    if (!jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    jpm.getPlatform().init();
    try {
        String s = System.getProperty("java.class.path");
        if (s == null) {
            error("Cannot initialize because not clear what the command jar is from java.class.path: %s", s);
            return;
        }
        String[] parts = s.split(File.pathSeparator);
        s = parts[0];
        try {
            File f = new File(s).getAbsoluteFile();
            if (f.exists()) {
                CommandLine cl = new CommandLine(this);
                String help = cl.execute(this, "install", Arrays.asList("-fl", f.getAbsolutePath()));
                if (help != null) {
                    error(help);
                    return;
                }
                String completionInstallResult = jpm.getPlatform().installCompletion(this);
                if (completionInstallResult != null)
                    logger.debug("{}", completionInstallResult);
                settings.put(JPM_CONFIG_BIN, jpm.getBinDir().getAbsolutePath());
                settings.put(JPM_CONFIG_HOME, jpm.getHomeDir().getAbsolutePath());
                settings.save();
                if (jpm.getPlatform().hasPost()) {
                    Command cmd = new Command();
                    cmd.add(new File(jpm.getBinDir(), "jpm").getAbsolutePath());
                    cmd.add("_postinstall");
                    cmd.setTimeout(5, TimeUnit.SECONDS);
                    StringBuffer stdout = new StringBuffer();
                    StringBuffer stderr = new StringBuffer();
                    System.out.println("post : " + cmd);
                    int result = cmd.execute(stdout, stderr);
                    if (result != 0) {
                        Justif j = new Justif(80, 5, 10, 20);
                        if (stdout.length() != 0)
                            j.formatter().format("stdout: $-%n", stdout);
                        if (stderr.length() != 0)
                            j.formatter().format("stderr: $-%n", stderr);
                        error("Failed to run platform init, exit code %s.%n%s", result, j.wrap());
                    } else
                        out.append(stdout);
                }
                out.println("Home dir      " + jpm.getHomeDir());
                out.println("Bin  dir      " + jpm.getBinDir());
            } else
                error("Cannot find the jpm jar from %s", f);
        } catch (InvocationTargetException e) {
            exception(e.getTargetException(), "Could not install jpm, %s", e.getTargetException());
            if (isExceptions())
                e.printStackTrace();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Justif(aQute.lib.justif.Justif) Command(aQute.libg.command.Command) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Description(aQute.lib.getopt.Description)

Aggregations

CommandLine (aQute.lib.getopt.CommandLine)19 Description (aQute.lib.getopt.Description)10 File (java.io.File)8 Justif (aQute.lib.justif.Justif)6 DistroOptions (aQute.bnd.main.RemoteCommand.DistroOptions)4 RemoteOptions (aQute.bnd.main.RemoteCommand.RemoteOptions)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 Formatter (java.util.Formatter)3 Workspace (aQute.bnd.build.Workspace)2 Domain (aQute.bnd.osgi.Domain)2 ExtList (aQute.lib.collections.ExtList)2 ReporterAdapter (aQute.libg.reporter.ReporterAdapter)2 Sed (aQute.libg.sed.Sed)2 Reporter (aQute.service.reporter.Reporter)2 RandomAccessFile (java.io.RandomAccessFile)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 Resource (org.osgi.resource.Resource)2 Project (aQute.bnd.build.Project)1