summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/InvokeSyncAction.java
blob: f39d5fdb57d115d96313289d5089455d0c747a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.ide.actions;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.eclipse.cdt.core.CommandLauncher;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.cdt.internal.autotools.core.AutotoolsNewMakeGenerator;
import org.eclipse.cdt.internal.autotools.ui.actions.InvokeAction;
import org.eclipse.cdt.internal.autotools.ui.actions.InvokeMessages;

import org.yocto.sdk.ide.YoctoSDKPlugin;

@SuppressWarnings("restriction")
public class InvokeSyncAction extends InvokeAction {
	protected void executeLocalConsoleCommand(final IConsole console, final String actionName, final String command,
			final String[] argumentList, final IPath execDir, final String password) throws CoreException, IOException {
		
		String errMsg = null;
		IProject project = getSelectedContainer().getProject();
		// Get a build console for the project
		ConsoleOutputStream consoleOutStream = console.getOutputStream();
		// FIXME: we want to remove need for ManagedBuilderManager, but how do we
		// get environment variables.
		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
		IConfiguration cfg = info.getDefaultConfiguration();

		StringBuffer buf = new StringBuffer();
		String[] consoleHeader = new String[3];

		consoleHeader[0] = actionName;
		consoleHeader[1] = cfg.getName();
		consoleHeader[2] = project.getName();
		buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$	//$NON-NLS-2$
		String invokeMsg = InvokeMessages.getFormattedString("InvokeAction.console.message", //$NON-NLS-1$
				new String[]{actionName, execDir.toString()}); //$NON-NLS-1$
		buf.append(invokeMsg);
		buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$	//$NON-NLS-2$
		buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$	//$NON-NLS-2$
		consoleOutStream.write(buf.toString().getBytes());
		consoleOutStream.flush();
		
		ArrayList<String> additionalEnvs = new ArrayList<String>();
		String strippedCommand = AutotoolsNewMakeGenerator.stripEnvVars(command, additionalEnvs);
		// Get a launcher for the config command
		CommandLauncher launcher = new CommandLauncher();
		// Set the environment
		IEnvironmentVariable variables[] = ManagedBuildManager
				.getEnvironmentVariableProvider().getVariables(cfg, true);
		String[] env = null;
		ArrayList<String> envList = new ArrayList<String>();
		if (variables != null) {
			for (int i = 0; i < variables.length; i++) {
				envList.add(variables[i].getName()
						+ "=" + variables[i].getValue()); //$NON-NLS-1$
			}
			// add any additional environment variables specified ahead of script
			if (additionalEnvs.size() > 0)
				envList.addAll(additionalEnvs); 
			env = (String[]) envList.toArray(new String[envList.size()]);
		}
		OutputStream stdout = consoleOutStream;
		OutputStream stderr = consoleOutStream;

		launcher.showCommand(true);
		// Run the shell script via shell command.
		Process proc = launcher.execute(new Path(strippedCommand), argumentList, env,
					execDir, new NullProgressMonitor());

		if (proc != null) {
			// Close the input of the process since we will never write to it
			OutputStream out = proc.getOutputStream();
			if (!password.isEmpty()) {
				out.write(password.getBytes());
				out.write("\n".getBytes());
			}
			out.close();
			
			if (launcher.waitAndRead(stdout, stderr) != CommandLauncher.OK) {
				errMsg = launcher.getErrorMessage();
			}
		} else {
			errMsg = launcher.getErrorMessage();
		}
		
		if (errMsg != null)
			YoctoSDKPlugin.logErrorMessage(errMsg);	
	}	
}