diff options
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/InstallWizard.java')
-rw-r--r-- | plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/InstallWizard.java | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/InstallWizard.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/InstallWizard.java new file mode 100644 index 0000000..f2cb1da --- /dev/null +++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/InstallWizard.java | |||
@@ -0,0 +1,404 @@ | |||
1 | package org.yocto.bc.ui.wizards.install; | ||
2 | |||
3 | import java.io.BufferedReader; | ||
4 | import java.io.File; | ||
5 | import java.io.FileInputStream; | ||
6 | import java.io.FileDescriptor; | ||
7 | import java.io.InputStream; | ||
8 | import java.io.IOException; | ||
9 | import java.io.InputStreamReader; | ||
10 | import java.io.OutputStream; | ||
11 | import java.io.Writer; | ||
12 | import java.util.ArrayList; | ||
13 | import java.util.Hashtable; | ||
14 | import java.util.Map; | ||
15 | import java.util.regex.Matcher; | ||
16 | import java.util.regex.Pattern; | ||
17 | import java.lang.reflect.InvocationTargetException; | ||
18 | import java.net.URL; | ||
19 | |||
20 | import org.eclipse.core.runtime.IProgressMonitor; | ||
21 | import org.eclipse.core.runtime.IStatus; | ||
22 | import org.eclipse.core.runtime.NullProgressMonitor; | ||
23 | import org.eclipse.core.runtime.Path; | ||
24 | import org.eclipse.core.runtime.Status; | ||
25 | import org.eclipse.jface.operation.IRunnableWithProgress; | ||
26 | import org.eclipse.jface.viewers.IStructuredSelection; | ||
27 | import org.eclipse.jface.wizard.IWizardPage; | ||
28 | import org.eclipse.jface.wizard.WizardPage; | ||
29 | |||
30 | import org.eclipse.ui.PlatformUI; | ||
31 | import org.eclipse.ui.IWorkbench; | ||
32 | import org.eclipse.ui.IWorkbenchWizard; | ||
33 | import org.eclipse.ui.IWorkbenchPage; | ||
34 | import org.eclipse.ui.IWorkbenchWindow; | ||
35 | import org.eclipse.ui.console.IConsoleConstants; | ||
36 | import org.eclipse.ui.console.MessageConsole; | ||
37 | import org.eclipse.ui.console.MessageConsoleStream; | ||
38 | import org.eclipse.ui.console.ConsolePlugin; | ||
39 | import org.eclipse.ui.console.IConsoleManager; | ||
40 | import org.eclipse.ui.console.IConsole; | ||
41 | import org.eclipse.ui.console.IConsoleView; | ||
42 | import org.eclipse.ui.progress.IProgressService; | ||
43 | |||
44 | import org.yocto.bc.bitbake.ICommandResponseHandler; | ||
45 | import org.yocto.bc.bitbake.ShellSession; | ||
46 | import org.yocto.bc.ui.Activator; | ||
47 | import org.yocto.bc.ui.model.ProjectInfo; | ||
48 | import org.yocto.bc.ui.wizards.FiniteStateWizard; | ||
49 | |||
50 | import org.yocto.bc.ui.wizards.newproject.BBConfigurationInitializeOperation; | ||
51 | import org.yocto.bc.ui.wizards.newproject.CreateBBCProjectOperation; | ||
52 | |||
53 | /** | ||
54 | * A wizard for installing a fresh copy of an OE system. | ||
55 | * | ||
56 | * @author kgilmer | ||
57 | * | ||
58 | * A Wizard for creating a fresh Yocto bitbake project and new poky build tree from git | ||
59 | * | ||
60 | * @modified jzhang | ||
61 | * | ||
62 | */ | ||
63 | public class InstallWizard extends FiniteStateWizard implements | ||
64 | IWorkbenchWizard { | ||
65 | |||
66 | static final String KEY_PINFO = "KEY_PINFO"; | ||
67 | protected static final String OPTION_MAP = "OPTION_MAP"; | ||
68 | protected static final String INSTALL_SCRIPT = "INSTALL_SCRIPT"; | ||
69 | protected static final String INSTALL_DIRECTORY = "Install Directory"; | ||
70 | protected static final String INIT_SCRIPT = "Init Script"; | ||
71 | |||
72 | protected static final String PROJECT_NAME = "Project Name"; | ||
73 | protected static final String DEFAULT_INIT_SCRIPT = "oe-init-build-env"; | ||
74 | protected static final String DEFAULT_INSTALL_DIR = "~/yocto"; | ||
75 | |||
76 | protected static final String GIT_CLONE = "Git Clone"; | ||
77 | public static final String VALIDATION_FILE = DEFAULT_INIT_SCRIPT; | ||
78 | |||
79 | private Map model; | ||
80 | private MessageConsole myConsole; | ||
81 | |||
82 | public InstallWizard() { | ||
83 | this.model = new Hashtable(); | ||
84 | model.put(INSTALL_DIRECTORY, DEFAULT_INSTALL_DIR); | ||
85 | model.put(INIT_SCRIPT, DEFAULT_INIT_SCRIPT); | ||
86 | |||
87 | setWindowTitle("Yocto Project BitBake Commander"); | ||
88 | setNeedsProgressMonitor(true); | ||
89 | |||
90 | myConsole = findConsole("Yocto Project Console"); | ||
91 | IWorkbench wb = PlatformUI.getWorkbench(); | ||
92 | IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); | ||
93 | IWorkbenchPage page = win.getActivePage(); | ||
94 | String id = IConsoleConstants.ID_CONSOLE_VIEW; | ||
95 | try { | ||
96 | IConsoleView view = (IConsoleView) page.showView(id); | ||
97 | view.display(myConsole); | ||
98 | } catch (Exception e) { | ||
99 | e.printStackTrace(); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | private MessageConsole findConsole(String name) { | ||
104 | ConsolePlugin plugin = ConsolePlugin.getDefault(); | ||
105 | IConsoleManager conMan = plugin.getConsoleManager(); | ||
106 | IConsole[] existing = conMan.getConsoles(); | ||
107 | for (int i = 0; i < existing.length; i++) | ||
108 | if (name.equals(existing[i].getName())) | ||
109 | return (MessageConsole) existing[i]; | ||
110 | // no console found, so create a new one | ||
111 | MessageConsole myConsole = new MessageConsole(name, null); | ||
112 | conMan.addConsoles(new IConsole[] { myConsole }); | ||
113 | return myConsole; | ||
114 | } | ||
115 | |||
116 | public InstallWizard(IStructuredSelection selection) { | ||
117 | model = new Hashtable(); | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * @Override public IWizardPage getNextPage(IWizardPage page) { if (page | ||
122 | * instanceof WelcomePage) { if (model.containsKey(WelcomePage.ACTION_USE)) | ||
123 | * { return bbcProjectPage; } } else if (page instanceof ProgressPage) { | ||
124 | * return bitbakePage; } | ||
125 | * | ||
126 | * if (super.getNextPage(page) != null) { System.out.println("next page: " + | ||
127 | * super.getNextPage(page).getClass().getName()); } else { | ||
128 | * System.out.println("end page"); } | ||
129 | * | ||
130 | * return super.getNextPage(page); } | ||
131 | * | ||
132 | * @Override public boolean canFinish() { System.out.println("can finish: " | ||
133 | * + super.canFinish()); return super.canFinish(); } | ||
134 | */ | ||
135 | @Override | ||
136 | public void addPages() { | ||
137 | addPage(new OptionsPage(model)); | ||
138 | } | ||
139 | |||
140 | @Override | ||
141 | public Map getModel() { | ||
142 | return model; | ||
143 | } | ||
144 | |||
145 | @Override | ||
146 | public boolean performFinish() { | ||
147 | BCCommandResponseHandler cmdOut = new BCCommandResponseHandler( | ||
148 | myConsole); | ||
149 | |||
150 | WizardPage page = (WizardPage) getPage("Options"); | ||
151 | page.setPageComplete(true); | ||
152 | Map options = (Map) model; | ||
153 | String install_dir = ""; | ||
154 | if (options.containsKey(INSTALL_DIRECTORY)) { | ||
155 | install_dir = (String) options.get(INSTALL_DIRECTORY); | ||
156 | } | ||
157 | |||
158 | try { | ||
159 | if (((Boolean)options.get(GIT_CLONE)).booleanValue()) { | ||
160 | String []git_clone_cmd = {"git", "clone", "--progress", "git://git.pokylinux.org/poky.git", install_dir}; | ||
161 | final Pattern pattern = Pattern.compile("^Receiving objects:\\s*(\\d+)%.*"); | ||
162 | |||
163 | this.getContainer().run(true,true, | ||
164 | new LongtimeRunningTask("Checking out Yocto git repository", | ||
165 | git_clone_cmd, null, null, | ||
166 | cmdOut, | ||
167 | new ICalculatePercentage() { | ||
168 | public float calWorkloadDone(String info) throws IllegalArgumentException { | ||
169 | Matcher m=pattern.matcher(info.trim()); | ||
170 | if(m.matches()) { | ||
171 | return new Float(m.group(1)) / 100; | ||
172 | }else { | ||
173 | throw new IllegalArgumentException(); | ||
174 | } | ||
175 | } | ||
176 | } | ||
177 | ) | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | if (!cmdOut.hasError()) { | ||
182 | |||
183 | String initPath = install_dir + "/" | ||
184 | + (String) options.get(INIT_SCRIPT); | ||
185 | String prjName = (String) options.get(PROJECT_NAME); | ||
186 | ProjectInfo pinfo = new ProjectInfo(); | ||
187 | pinfo.setInitScriptPath(initPath); | ||
188 | pinfo.setLocation(install_dir); | ||
189 | pinfo.setName(prjName); | ||
190 | |||
191 | ConsoleWriter cw = new ConsoleWriter(); | ||
192 | this.getContainer().run(false, false, | ||
193 | new BBConfigurationInitializeOperation(pinfo, cw)); | ||
194 | |||
195 | myConsole.newMessageStream().println(cw.getContents()); | ||
196 | |||
197 | model.put(InstallWizard.KEY_PINFO, pinfo); | ||
198 | Activator.putProjInfo(pinfo.getRootPath(), pinfo); | ||
199 | |||
200 | this.getContainer().run(false, false, | ||
201 | new CreateBBCProjectOperation(pinfo)); | ||
202 | return true; | ||
203 | } | ||
204 | } catch (Exception e) { | ||
205 | Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, | ||
206 | IStatus.ERROR, e.getMessage(), e)); | ||
207 | this.getContainer().getCurrentPage().setDescription( | ||
208 | "Failed to create project: " + e.getMessage()); | ||
209 | } | ||
210 | return false; | ||
211 | } | ||
212 | |||
213 | public void init(IWorkbench workbench, IStructuredSelection selection) { | ||
214 | } | ||
215 | |||
216 | private interface ICalculatePercentage { | ||
217 | public float calWorkloadDone(String info) throws IllegalArgumentException; | ||
218 | } | ||
219 | |||
220 | private class LongtimeRunningTask implements IRunnableWithProgress { | ||
221 | private String []cmdArray; | ||
222 | private String []envp; | ||
223 | private File dir; | ||
224 | private ICommandResponseHandler handler; | ||
225 | private Process p; | ||
226 | private String taskName; | ||
227 | static public final int TOTALWORKLOAD=100; | ||
228 | private int reported_workload; | ||
229 | ICalculatePercentage cal; | ||
230 | |||
231 | public LongtimeRunningTask(String taskName, | ||
232 | String []cmdArray, String []envp, File dir, | ||
233 | ICommandResponseHandler handler, | ||
234 | ICalculatePercentage calculator) { | ||
235 | this.taskName=taskName; | ||
236 | this.cmdArray=cmdArray; | ||
237 | this.envp=envp; | ||
238 | this.dir=dir; | ||
239 | this.handler=handler; | ||
240 | this.p=null; | ||
241 | this.cal=calculator; | ||
242 | } | ||
243 | |||
244 | private void reportProgress(IProgressMonitor monitor,String info) { | ||
245 | if(cal == null) { | ||
246 | monitor.worked(1); | ||
247 | }else { | ||
248 | float percentage; | ||
249 | try { | ||
250 | percentage=cal.calWorkloadDone(info); | ||
251 | } catch (IllegalArgumentException e) { | ||
252 | //can't get percentage | ||
253 | return; | ||
254 | } | ||
255 | int delta=(int) (TOTALWORKLOAD * percentage - reported_workload); | ||
256 | if( delta > 0 ) { | ||
257 | monitor.worked(delta); | ||
258 | reported_workload += delta; | ||
259 | } | ||
260 | } | ||
261 | } | ||
262 | |||
263 | synchronized public void run(IProgressMonitor monitor) | ||
264 | throws InvocationTargetException, InterruptedException { | ||
265 | |||
266 | boolean cancel=false; | ||
267 | reported_workload=0; | ||
268 | |||
269 | try { | ||
270 | monitor.beginTask(taskName, TOTALWORKLOAD); | ||
271 | |||
272 | p=Runtime.getRuntime().exec(cmdArray,envp,dir); | ||
273 | BufferedReader inbr = new BufferedReader(new InputStreamReader(p.getInputStream())); | ||
274 | BufferedReader errbr = new BufferedReader(new InputStreamReader(p.getErrorStream())); | ||
275 | String info; | ||
276 | while (!cancel) { | ||
277 | if(monitor.isCanceled()) | ||
278 | { | ||
279 | cancel=true; | ||
280 | throw new InterruptedException("User Cancelled"); | ||
281 | } | ||
282 | |||
283 | info=null; | ||
284 | //reading stderr | ||
285 | while (errbr.ready()) { | ||
286 | info=errbr.readLine(); | ||
287 | //some application using stderr to print out information | ||
288 | handler.response(info, false); | ||
289 | } | ||
290 | //reading stdout | ||
291 | while (inbr.ready()) { | ||
292 | info=inbr.readLine(); | ||
293 | handler.response(info, false); | ||
294 | } | ||
295 | |||
296 | //report progress | ||
297 | if(info!=null) | ||
298 | reportProgress(monitor,info); | ||
299 | |||
300 | //check if exit | ||
301 | try { | ||
302 | int exitValue=p.exitValue(); | ||
303 | if (exitValue != 0) { | ||
304 | handler.response( | ||
305 | taskName + " failed with the return value " + new Integer(exitValue).toString(), | ||
306 | true); | ||
307 | } | ||
308 | break; | ||
309 | }catch (IllegalThreadStateException e) { | ||
310 | } | ||
311 | |||
312 | Thread.sleep(500); | ||
313 | } | ||
314 | } catch (IOException e) { | ||
315 | throw new InvocationTargetException(e); | ||
316 | } finally { | ||
317 | monitor.done(); | ||
318 | if (p != null ) { | ||
319 | p.destroy(); | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | |||
325 | private class BCCommandResponseHandler implements ICommandResponseHandler { | ||
326 | private MessageConsoleStream myConsoleStream; | ||
327 | private Boolean errorOccured = false; | ||
328 | |||
329 | public BCCommandResponseHandler(MessageConsole console) { | ||
330 | try { | ||
331 | this.myConsoleStream = console.newMessageStream(); | ||
332 | } catch (Exception e) { | ||
333 | e.printStackTrace(); | ||
334 | } | ||
335 | } | ||
336 | |||
337 | public void printDialog(String msg) { | ||
338 | try { | ||
339 | myConsoleStream.println(msg); | ||
340 | } catch (Exception e) { | ||
341 | e.printStackTrace(); | ||
342 | } | ||
343 | } | ||
344 | |||
345 | public Boolean hasError() { | ||
346 | return errorOccured; | ||
347 | } | ||
348 | |||
349 | public void response(String line, boolean isError) { | ||
350 | try { | ||
351 | if (isError) { | ||
352 | myConsoleStream.println(line); | ||
353 | errorOccured = true; | ||
354 | } else { | ||
355 | myConsoleStream.println(line); | ||
356 | } | ||
357 | } catch (Exception e) { | ||
358 | e.printStackTrace(); | ||
359 | } | ||
360 | } | ||
361 | |||
362 | public void printCmd(String cmd) { | ||
363 | try { | ||
364 | myConsoleStream.println(cmd); | ||
365 | } catch (Exception e) { | ||
366 | e.printStackTrace(); | ||
367 | } | ||
368 | } | ||
369 | } | ||
370 | |||
371 | private class ConsoleWriter extends Writer { | ||
372 | |||
373 | private StringBuffer sb; | ||
374 | |||
375 | public ConsoleWriter() { | ||
376 | sb = new StringBuffer(); | ||
377 | } | ||
378 | |||
379 | @Override | ||
380 | public void close() throws IOException { | ||
381 | } | ||
382 | |||
383 | public String getContents() { | ||
384 | return sb.toString(); | ||
385 | } | ||
386 | |||
387 | @Override | ||
388 | public void flush() throws IOException { | ||
389 | } | ||
390 | |||
391 | @Override | ||
392 | public void write(char[] cbuf, int off, int len) throws IOException { | ||
393 | // txtConsole.getText().concat(new String(cbuf)); | ||
394 | sb.append(cbuf); | ||
395 | } | ||
396 | |||
397 | @Override | ||
398 | public void write(String str) throws IOException { | ||
399 | sb.append(str); | ||
400 | } | ||
401 | |||
402 | } | ||
403 | |||
404 | } | ||