summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java231
1 files changed, 231 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java
new file mode 100644
index 0000000..3a2ac71
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java
@@ -0,0 +1,231 @@
1/*****************************************************************************
2 * Copyright (c) 2013 Ken Gilmer, Intel Corporation
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Ken Gilmer - initial API and implementation
10 * Jessica Zhang (Intel) - Extend to support auto-fill base on src_uri value
11 * Ioana Grigoropol (Intel) - adapt class for remote support
12 *******************************************************************************/
13package org.yocto.bc.ui.wizards;
14
15import java.io.ByteArrayInputStream;
16import java.io.IOException;
17import java.io.InputStream;
18import java.io.File;
19import java.lang.reflect.InvocationTargetException;
20import java.util.ArrayList;
21
22import org.eclipse.core.resources.IContainer;
23import org.eclipse.core.resources.IFile;
24import org.eclipse.core.resources.IProject;
25import org.eclipse.core.resources.IResource;
26import org.eclipse.core.resources.IWorkspaceRoot;
27import org.eclipse.core.resources.ResourcesPlugin;
28import org.eclipse.core.runtime.CoreException;
29import org.eclipse.core.runtime.IProgressMonitor;
30import org.eclipse.core.runtime.IStatus;
31import org.eclipse.core.runtime.Path;
32import org.eclipse.core.runtime.Status;
33import org.eclipse.jface.dialogs.MessageDialog;
34import org.eclipse.jface.operation.IRunnableWithProgress;
35import org.eclipse.jface.viewers.ISelection;
36import org.eclipse.jface.viewers.IStructuredSelection;
37import org.eclipse.jface.wizard.Wizard;
38import org.eclipse.rse.core.model.IHost;
39import org.eclipse.ui.INewWizard;
40import org.eclipse.ui.IWorkbench;
41import org.eclipse.ui.IWorkbenchPage;
42import org.eclipse.ui.IWorkbenchWizard;
43import org.eclipse.ui.PartInitException;
44import org.eclipse.ui.PlatformUI;
45import org.eclipse.ui.ide.IDE;
46
47import org.yocto.bc.bitbake.BBLanguageHelper;
48import org.yocto.bc.ui.Activator;
49import org.yocto.bc.ui.model.ProjectInfo;
50import org.yocto.remote.utils.RemoteHelper;
51import org.yocto.remote.utils.YoctoCommand;
52
53public class NewBitBakeFileRecipeWizard extends Wizard implements INewWizard {
54 private NewBitBakeFileRecipeWizardPage page;
55 private ISelection selection;
56 private IHost connection;
57
58 public NewBitBakeFileRecipeWizard() {
59 super();
60 setNeedsProgressMonitor(true);
61 }
62
63 @Override
64 public void addPages() {
65 page = new NewBitBakeFileRecipeWizardPage(selection, connection);
66 addPage(page);
67 }
68
69 private void doFinish(BitbakeRecipeUIElement element, IProgressMonitor monitor) throws CoreException {
70 String fileName = element.getFile();
71 monitor.beginTask("Creating " + fileName, 2);
72 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
73 IResource resource = root.findMember(new Path(element.getContainer()));
74 if (!resource.exists() || !(resource instanceof IContainer)) {
75 throwCoreException("Container \"" + element.getContainer() + "\" does not exist.");
76 }
77 IContainer container = (IContainer) resource;
78
79 // If the extension wasn't specified, assume .bb
80 if (!fileName.endsWith(".bb") && !fileName.endsWith(".inc") && !fileName.endsWith(".conf")) {
81 fileName = fileName + ".bb";
82 }
83
84 final IFile file = container.getFile(new Path(fileName));
85 try {
86 InputStream stream = openContentStream(element);
87 if (file.exists()) {
88 file.setContents(stream, true, true, monitor);
89 } else {
90 file.create(stream, true, monitor);
91 }
92 stream.close();
93 } catch (IOException e) {
94 }
95 monitor.worked(1);
96 monitor.setTaskName("Opening file for editing...");
97 getShell().getDisplay().asyncExec(new Runnable() {
98 public void run() {
99 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
100 try {
101 IDE.openEditor(page, file, true);
102 } catch (PartInitException e) {
103 }
104 }
105 });
106 monitor.worked(1);
107 }
108
109 /**
110 * We will accept the selection in the workbench to see if we can initialize
111 * from it.
112 *
113 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
114 */
115 public void init(IWorkbench workbench, IStructuredSelection selection) {
116 this.selection = selection;
117 if (selection instanceof IStructuredSelection) {
118 Object element = selection.getFirstElement();
119
120 if (element instanceof IResource) {
121 IProject p = ((IResource)element).getProject();
122 try {
123 ProjectInfo projInfo = Activator.getProjInfo(p.getLocationURI());
124 this.connection = projInfo.getConnection();
125 } catch (CoreException e) {
126 e.printStackTrace();
127 } catch (InvocationTargetException e) {
128 e.printStackTrace();
129 } catch (InterruptedException e) {
130 e.printStackTrace();
131 }
132
133 }
134 }
135 }
136
137 /**
138 * We will initialize file contents with a sample text.
139 * @param srcuri
140 * @param author
141 * @param homepage
142 * @param license
143 * @param description
144 * @param fileName
145 * @param newPage
146 */
147
148 private InputStream openContentStream(BitbakeRecipeUIElement element) {
149
150 StringBuffer sb = new StringBuffer();
151
152 sb.append("DESCRIPTION = \"" + element.getDescription() + "\"\n");
153
154 if (element.getAuthor().length() > 0) {
155 sb.append("AUTHOR = \"" + element.getAuthor() + "\"\n");
156 }
157
158 if (element.getHomePage().length() > 0) {
159 sb.append("HOMEPAGE = \"" + element.getHomePage() + "\"\n");
160 }
161
162 if (element.getSection().length() > 0) {
163 sb.append("SECTION = \"" + element.getSection() + "\"\n");
164 }
165
166 if (element.getLicense().length() > 0) {
167 sb.append("LICENSE = \"" + element.getLicense() + "\"\n");
168 }
169
170 if (element.getChecksum().length() > 0) {
171 sb.append("LIC_FILES_CHKSUM = \"" + element.getChecksum() + "\"\n");
172 }
173
174 if (element.getSrcuri().length() > 0) {
175 sb.append("SRC_URI = \"" + element.getSrcuri() + "\"\n");
176 }
177
178 if (element.getMd5sum().length() > 0) {
179 sb.append("SRC_URI[md5sum] = \"" + element.getMd5sum() + "\"\n");
180 }
181
182 if (element.getsha256sum().length() > 0) {
183 sb.append("SRC_URI[sha256sum] = \"" + element.getsha256sum() + "\"\n");
184 }
185
186 ArrayList<String> inheritance = element.getInheritance();
187 if (!inheritance.isEmpty()) {
188 Object ia[] = inheritance.toArray();
189 String inheritance_str = "inherit ";
190 for(int i=0; i<ia.length; i++)
191 inheritance_str += ((String) ia[i]) + " ";
192 sb.append(inheritance_str);
193 }
194 sb.append("\n");
195
196 return new ByteArrayInputStream(sb.toString().getBytes());
197 }
198
199 @Override
200 public boolean performFinish() {
201 final BitbakeRecipeUIElement element = page.populateUIElement();
202
203 IRunnableWithProgress op = new IRunnableWithProgress() {
204 public void run(IProgressMonitor monitor) throws InvocationTargetException {
205 try {
206 doFinish(element, monitor);
207 RemoteHelper.handleRunCommandRemote(connection, new YoctoCommand("rm -rf " + element.getMetaDir() + "/temp", "", ""), monitor);
208 } catch (Exception e) {
209 throw new InvocationTargetException(e);
210 } finally {
211 monitor.done();
212 }
213 }
214 };
215 try {
216 getContainer().run(true, false, op);
217 } catch (InterruptedException e) {
218 return false;
219 } catch (InvocationTargetException e) {
220 Throwable realException = e.getTargetException();
221 MessageDialog.openError(getShell(), "Error", realException.getMessage());
222 return false;
223 }
224 return true;
225 }
226
227 private void throwCoreException(String message) throws CoreException {
228 IStatus status = new Status(IStatus.ERROR, "org.yocto.bc.ui", IStatus.OK, message, null);
229 throw new CoreException(status);
230 }
231}