diff options
author | Ken Gilmer <kgilmer@gmail.com> | 2010-05-18 21:53:22 -0400 |
---|---|---|
committer | Ken Gilmer <kgilmer@gmail.com> | 2010-05-18 21:53:22 -0400 |
commit | a7e84830627e50adac5c81ae4dad69aa350933fb (patch) | |
tree | 219aa86f4d5f03b29d7bc56f8cdba2702d67082c /org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java | |
parent | e27a6dcce13930bfa711a6e72992597a8d1d07c5 (diff) | |
download | eclipsetools-a7e84830627e50adac5c81ae4dad69aa350933fb.tar.gz |
org.openembedded.bc.ui: initial commit
Diffstat (limited to 'org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java')
-rw-r--r-- | org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java b/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java new file mode 100644 index 0000000..5b2b0a7 --- /dev/null +++ b/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/InstallJob.java @@ -0,0 +1,170 @@ +/** + * + */ +package org.openembedded.bc.ui.wizards.install; + +import java.io.BufferedReader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.commands.ParameterValueConversionException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.openembedded.bc.bitbake.ICommandResponseHandler; +import org.openembedded.bc.bitbake.ShellSession; +import org.openembedded.bc.ui.Activator; + + +class InstallJob extends Job { + + private final Map mod; + private UICommandResponseHandler cmdOut; + private boolean errorOccurred = false; + private String errorMessage = ""; + + public InstallJob(Map model, ProgressPage progressPage) { + super("Install OpenEmbedded"); + mod = model; + cmdOut = new UICommandResponseHandler(progressPage); + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + BufferedReader reader = new BufferedReader(new StringReader( + (String) mod.get(FlavorPage.INSTALL_SCRIPT))); + String line = null; + Map vars = loadVariables(); + + try { + ShellSession shell = new ShellSession(ShellSession.SHELL_TYPE_BASH, + null, null, null); + while ((line = reader.readLine()) != null && !errorOccurred) { + line = line.trim(); + if (line.length() > 0 && !line.startsWith("#")) { + line = substitute(line, vars); + cmdOut.printCmd(line); + System.out.println("Running: " + line); + shell.execute(line, cmdOut); + } else if (line.startsWith("#")) { + cmdOut.printDialog(line.substring(1).trim()); + } + } + + if (errorOccurred) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to install OpenEmbedded: " + errorMessage); + } + } catch (Exception e) { + e.printStackTrace(); + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to install OpenEmbedded", e); + } + + return Status.OK_STATUS; + } + + private Map loadVariables() { + return (Map) mod.get(OptionsPage.OPTION_MAP); + } + + /** + * Return a string with variable substitutions in place. + * + * @param expression + * @return Input string with any substitutions from this file. + * @throws ParameterValueConversionException + */ + public static String substitute(String expression, Map mo) + throws ParameterValueConversionException { + + List vars; + int literals = 0; + + while ((vars = parseVars(expression)).size() > literals) { + for (Iterator i = vars.iterator(); i.hasNext();) { + String varName = (String) i.next(); + String varToken = "${" + varName + "}"; + + if (mo.containsKey(varName)) { + expression = expression.replace(varToken, (String) mo + .get(varName)); + } else if (System.getProperty(varName) != null) { + expression = expression.replace(varToken, System + .getProperty(varName)); + } else if (varName.toUpperCase().equals("HOME")) { + expression = expression.replace(varToken, System + .getProperty("user.home")); + } else { + //throw new ParameterValueConversionException( + // "Unable to match parameter: " + expression); + // Leave the value, treat as a literal. + literals++; + } + } + } + + return expression; + } + + /** + * + * @param line + * @return A list of variables in $[variable name] format. + */ + public static List parseVars(String line) { + List l = new ArrayList(); + + int i = 0; + + while ((i = line.indexOf("${", i)) > -1) { + int i2 = line.indexOf("}", i); + + String var = line.subSequence(i + 2, i2).toString().trim(); + + if (var.length() > 0 && !l.contains(var)) { + l.add(var); + } + i++; + } + + return l; + } + + private class UICommandResponseHandler implements ICommandResponseHandler { + + private final ProgressPage progressPage; + + public UICommandResponseHandler(ProgressPage progressPage) { + this.progressPage = progressPage; + } + + public void printDialog(String msg) { + progressPage.printDialog(msg); + } + + public void response(String line, boolean isError) { + if (isError) { + progressPage.printLine(line, ProgressPage.PRINT_ERR); + //errorOccurred = true; + //errorMessage = line; + } else { + progressPage.printLine(line, ProgressPage.PRINT_OUT); + if (line.endsWith("!!OTEWIZARDSTOP")) { + errorOccurred = true; + errorMessage = line; + } + } + } + + public void printCmd(String cmd) { + progressPage.printLine(cmd, ProgressPage.PRINT_CMD); + } + + } + +}
\ No newline at end of file |