diff options
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.java | 231 |
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 | *******************************************************************************/ | ||
13 | package org.yocto.bc.ui.wizards; | ||
14 | |||
15 | import java.io.ByteArrayInputStream; | ||
16 | import java.io.IOException; | ||
17 | import java.io.InputStream; | ||
18 | import java.io.File; | ||
19 | import java.lang.reflect.InvocationTargetException; | ||
20 | import java.util.ArrayList; | ||
21 | |||
22 | import org.eclipse.core.resources.IContainer; | ||
23 | import org.eclipse.core.resources.IFile; | ||
24 | import org.eclipse.core.resources.IProject; | ||
25 | import org.eclipse.core.resources.IResource; | ||
26 | import org.eclipse.core.resources.IWorkspaceRoot; | ||
27 | import org.eclipse.core.resources.ResourcesPlugin; | ||
28 | import org.eclipse.core.runtime.CoreException; | ||
29 | import org.eclipse.core.runtime.IProgressMonitor; | ||
30 | import org.eclipse.core.runtime.IStatus; | ||
31 | import org.eclipse.core.runtime.Path; | ||
32 | import org.eclipse.core.runtime.Status; | ||
33 | import org.eclipse.jface.dialogs.MessageDialog; | ||
34 | import org.eclipse.jface.operation.IRunnableWithProgress; | ||
35 | import org.eclipse.jface.viewers.ISelection; | ||
36 | import org.eclipse.jface.viewers.IStructuredSelection; | ||
37 | import org.eclipse.jface.wizard.Wizard; | ||
38 | import org.eclipse.rse.core.model.IHost; | ||
39 | import org.eclipse.ui.INewWizard; | ||
40 | import org.eclipse.ui.IWorkbench; | ||
41 | import org.eclipse.ui.IWorkbenchPage; | ||
42 | import org.eclipse.ui.IWorkbenchWizard; | ||
43 | import org.eclipse.ui.PartInitException; | ||
44 | import org.eclipse.ui.PlatformUI; | ||
45 | import org.eclipse.ui.ide.IDE; | ||
46 | |||
47 | import org.yocto.bc.bitbake.BBLanguageHelper; | ||
48 | import org.yocto.bc.ui.Activator; | ||
49 | import org.yocto.bc.ui.model.ProjectInfo; | ||
50 | import org.yocto.remote.utils.RemoteHelper; | ||
51 | import org.yocto.remote.utils.YoctoCommand; | ||
52 | |||
53 | public 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 | } | ||