aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/actions/AbstractBitbakeCommandAction.java
blob: 484c8a6bb8d9966c5145d9c96ddfa84762106ad5 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * 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:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui.actions;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
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.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.openembedded.bc.bitbake.BBLanguageHelper;
import org.openembedded.bc.bitbake.BBSession;
import org.openembedded.bc.bitbake.ICommandResponseHandler;
import org.openembedded.bc.ui.Activator;
import org.openembedded.bc.ui.builder.BitbakeCommanderNature;


public abstract class AbstractBitbakeCommandAction implements IWorkbenchWindowActionDelegate {

	private class CommandJob extends Job {

		public CommandJob() {
			super(getJobTitle());
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			String cmds[] = getCommands();
			return execCommands(cmds, monitor);
		}

	}
	protected IAction action;
	protected IFile recipe;
	protected BBSession bbs;

	private Color commandColor, responseColor, errorColor;
	private boolean errorOccurred = false;

	public AbstractBitbakeCommandAction() {
		commandColor = JFaceResources.getColorRegistry().get(JFacePreferences.ACTIVE_HYPERLINK_COLOR);
		responseColor = JFaceResources.getColorRegistry().get(JFacePreferences.HYPERLINK_COLOR);
		errorColor = JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR);
	}

	private void checkEnabled(IFile file) {
		try {
			if (file.getFileExtension() == null || !file.getFileExtension().equals(BBLanguageHelper.BITBAKE_RECIPE_FILE_EXTENSION)) {
				action.setEnabled(false);
				return;
			}

			IProject project = file.getProject();
			if (!(project.hasNature(BitbakeCommanderNature.NATURE_ID))) {
				action.setEnabled(false);
				return;
			}

			bbs = Activator.getBBSession(project.getLocationURI().getPath());

			if (bbs != null) {
				recipe = file;
				action.setEnabled(true);
			}

		} catch (CoreException e) {
			action.setEnabled(false);
			e.printStackTrace();
		} catch (Exception e) {
			action.setEnabled(false);
			e.printStackTrace();
		}
	}

	public void dispose() {
	}

	/**
	 * Execute array of commands with bitbake and put output in console.
	 * 
	 * @param cmds
	 * @param monitor
	 * @return
	 */
	protected IStatus execCommands(String[] cmds, final IProgressMonitor monitor) {
		MessageConsole mc = bbs.getConsole();
		final MessageConsoleStream cmd = mc.newMessageStream();		
		final MessageConsoleStream out = mc.newMessageStream();
		final MessageConsoleStream err = mc.newMessageStream();
		
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			
			@Override
			public void run() {
				cmd.setColor(commandColor);
				err.setColor(errorColor);
			}
		});
		
		
		try {
			for (int i = 0; i < cmds.length; ++i) {
				cmd.println(cmds[i]);
				monitor.subTask(cmds[i]);
				bbs.getShell().execute(cmds[i], new ICommandResponseHandler() {

					public void response(String line, boolean isError) {
						if (monitor.isCanceled()) {
							cmd.println("Interrupting process by user request.");
							bbs.getShell().interrupt();
						}
						
						if (isError) {
							err.println(line);
							errorOccurred();
						} else if (line.startsWith("ERROR:")) {
							err.println(line);
						} else {
							out.println(line);
						}
					}
				});
			}
		} catch (IOException e) {
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
		} finally {
			try {
				if (errorOccurred) {
					cmd.println("At least one error occured while executing this command.  Check output for more details.");
				}
				cmd.close();
				out.close();
				err.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return Status.OK_STATUS;
	}

	protected void errorOccurred() {
		errorOccurred = true;
	}

	/**
	 * Return the command to be executed.
	 * 
	 * @return
	 */
	public abstract String[] getCommands();

	public Job getJob() {
		return new CommandJob();
	}

	/**
	 * Return the title of the job.
	 * 
	 * @return
	 */
	public abstract String getJobTitle();

	public void init(IWorkbenchWindow window) {
	}

	public void run(IAction action) {
		Job job = getJob();
		job.schedule();
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.action = action;
		if (selection instanceof IStructuredSelection) {
			Object sel = ((IStructuredSelection) selection).getFirstElement();

			if (sel instanceof IFile) {
				checkEnabled((IFile) sel);
				return;
			}
		}

		action.setEnabled(false);
	}
	
	/**
	 * @param path Path to recipe file
	 * @return The recipe name that bitbake will understand, based on a full path to a recipe file.
	 */
	protected static String getRecipeFromIFile(IFile path) {
		String bbRecipeExtension = ".bb";
		if (!path.getName().endsWith(bbRecipeExtension))
			throw new RuntimeException("File is not a bitbake recipe: " + path.getName());
		
		//Extract the filename without the extension.
		String name = path.getName().substring(0, path.getName().length() - bbRecipeExtension.length());
		
		String [] nvp = name.split("_");
		
		if (nvp.length == 0)
			throw new RuntimeException("Unable to parse recipe name from filename: " + name);
		
		//No version information embedded in the filename
		if (nvp.length == 1)
			return nvp[0];
		
		//Use bitbake's convention for specifying the version with a "-"
		if (nvp.length == 2)
			return nvp[0] + "-" + nvp[1];
		
		//Unknown format, just return the name
		return nvp[0];
	}

}