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 | 215 |
1 files changed, 215 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..14b268b --- /dev/null +++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/NewBitBakeFileRecipeWizard.java | |||
@@ -0,0 +1,215 @@ | |||
1 | /***************************************************************************** | ||
2 | * Copyright (c) 2009 Ken Gilmer | ||
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 | *******************************************************************************/ | ||
12 | package org.yocto.bc.ui.wizards; | ||
13 | |||
14 | import java.io.ByteArrayInputStream; | ||
15 | import java.io.IOException; | ||
16 | import java.io.InputStream; | ||
17 | import java.io.File; | ||
18 | import java.lang.reflect.InvocationTargetException; | ||
19 | import java.util.ArrayList; | ||
20 | |||
21 | import org.eclipse.core.resources.IContainer; | ||
22 | import org.eclipse.core.resources.IFile; | ||
23 | import org.eclipse.core.resources.IResource; | ||
24 | import org.eclipse.core.resources.IWorkspaceRoot; | ||
25 | import org.eclipse.core.resources.ResourcesPlugin; | ||
26 | import org.eclipse.core.runtime.CoreException; | ||
27 | import org.eclipse.core.runtime.IProgressMonitor; | ||
28 | import org.eclipse.core.runtime.IStatus; | ||
29 | import org.eclipse.core.runtime.Path; | ||
30 | import org.eclipse.core.runtime.Status; | ||
31 | import org.eclipse.jface.dialogs.MessageDialog; | ||
32 | import org.eclipse.jface.operation.IRunnableWithProgress; | ||
33 | import org.eclipse.jface.viewers.ISelection; | ||
34 | import org.eclipse.jface.viewers.IStructuredSelection; | ||
35 | import org.eclipse.jface.wizard.Wizard; | ||
36 | import org.eclipse.ui.INewWizard; | ||
37 | import org.eclipse.ui.IWorkbench; | ||
38 | import org.eclipse.ui.IWorkbenchPage; | ||
39 | import org.eclipse.ui.IWorkbenchWizard; | ||
40 | import org.eclipse.ui.PartInitException; | ||
41 | import org.eclipse.ui.PlatformUI; | ||
42 | import org.eclipse.ui.ide.IDE; | ||
43 | |||
44 | import org.yocto.bc.bitbake.BBLanguageHelper; | ||
45 | |||
46 | public class NewBitBakeFileRecipeWizard extends Wizard implements INewWizard { | ||
47 | private NewBitBakeFileRecipeWizardPage page; | ||
48 | private ISelection selection; | ||
49 | |||
50 | public NewBitBakeFileRecipeWizard() { | ||
51 | super(); | ||
52 | setNeedsProgressMonitor(true); | ||
53 | } | ||
54 | |||
55 | @Override | ||
56 | public void addPages() { | ||
57 | page = new NewBitBakeFileRecipeWizardPage(selection); | ||
58 | addPage(page); | ||
59 | } | ||
60 | |||
61 | private void doFinish(BitbakeRecipeUIElement element, IProgressMonitor monitor) throws CoreException { | ||
62 | String fileName = element.getFile(); | ||
63 | monitor.beginTask("Creating " + fileName, 2); | ||
64 | IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); | ||
65 | IResource resource = root.findMember(new Path(element.getContainer())); | ||
66 | if (!resource.exists() || !(resource instanceof IContainer)) { | ||
67 | throwCoreException("Container \"" + element.getContainer() + "\" does not exist."); | ||
68 | } | ||
69 | IContainer container = (IContainer) resource; | ||
70 | |||
71 | // If the extension wasn't specified, assume .bb | ||
72 | if (!fileName.endsWith(".bb") && !fileName.endsWith(".inc") && !fileName.endsWith(".conf")) { | ||
73 | fileName = fileName + ".bb"; | ||
74 | } | ||
75 | |||
76 | final IFile file = container.getFile(new Path(fileName)); | ||
77 | try { | ||
78 | InputStream stream = openContentStream(element); | ||
79 | if (file.exists()) { | ||
80 | file.setContents(stream, true, true, monitor); | ||
81 | } else { | ||
82 | file.create(stream, true, monitor); | ||
83 | } | ||
84 | stream.close(); | ||
85 | } catch (IOException e) { | ||
86 | } | ||
87 | monitor.worked(1); | ||
88 | monitor.setTaskName("Opening file for editing..."); | ||
89 | getShell().getDisplay().asyncExec(new Runnable() { | ||
90 | public void run() { | ||
91 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | ||
92 | try { | ||
93 | IDE.openEditor(page, file, true); | ||
94 | } catch (PartInitException e) { | ||
95 | } | ||
96 | } | ||
97 | }); | ||
98 | monitor.worked(1); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * We will accept the selection in the workbench to see if we can initialize | ||
103 | * from it. | ||
104 | * | ||
105 | * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) | ||
106 | */ | ||
107 | public void init(IWorkbench workbench, IStructuredSelection selection) { | ||
108 | this.selection = selection; | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * We will initialize file contents with a sample text. | ||
113 | * @param srcuri | ||
114 | * @param author | ||
115 | * @param homepage | ||
116 | * @param license | ||
117 | * @param description | ||
118 | * @param fileName | ||
119 | * @param newPage | ||
120 | */ | ||
121 | |||
122 | private InputStream openContentStream(BitbakeRecipeUIElement element) { | ||
123 | |||
124 | StringBuffer sb = new StringBuffer(); | ||
125 | |||
126 | sb.append("DESCRIPTION = \"" + element.getDescription() + "\"\n"); | ||
127 | |||
128 | if (element.getAuthor().length() > 0) { | ||
129 | sb.append("AUTHOR = \"" + element.getAuthor() + "\"\n"); | ||
130 | } | ||
131 | |||
132 | if (element.getHomePage().length() > 0) { | ||
133 | sb.append("HOMEPAGE = \"" + element.getHomePage() + "\"\n"); | ||
134 | } | ||
135 | |||
136 | if (element.getSection().length() > 0) { | ||
137 | sb.append("SECTION = \"" + element.getSection() + "\"\n"); | ||
138 | } | ||
139 | |||
140 | if (element.getLicense().length() > 0) { | ||
141 | sb.append("LICENSE = \"" + element.getLicense() + "\"\n"); | ||
142 | } | ||
143 | |||
144 | if (element.getChecksum().length() > 0) { | ||
145 | sb.append("LIC_FILES_CHKSUM = \"" + element.getChecksum() + "\"\n"); | ||
146 | } | ||
147 | |||
148 | if (element.getSrcuri().length() > 0) { | ||
149 | sb.append("SRC_URI = \"" + element.getSrcuri() + "\"\n"); | ||
150 | } | ||
151 | |||
152 | if (element.getMd5sum().length() > 0) { | ||
153 | sb.append("SRC_URI[md5sum] = \"" + element.getMd5sum() + "\"\n"); | ||
154 | } | ||
155 | |||
156 | if (element.getsha256sum().length() > 0) { | ||
157 | sb.append("SRC_URI[sha256sum] = \"" + element.getsha256sum() + "\"\n"); | ||
158 | } | ||
159 | |||
160 | ArrayList<String> inheritance = element.getInheritance(); | ||
161 | if (!inheritance.isEmpty()) { | ||
162 | Object ia[] = inheritance.toArray(); | ||
163 | String inheritance_str = "inherit "; | ||
164 | for(int i=0; i<ia.length; i++) | ||
165 | inheritance_str += ((String) ia[i]) + " "; | ||
166 | sb.append(inheritance_str); | ||
167 | } | ||
168 | sb.append("\n"); | ||
169 | |||
170 | return new ByteArrayInputStream(sb.toString().getBytes()); | ||
171 | } | ||
172 | |||
173 | @Override | ||
174 | public boolean performFinish() { | ||
175 | final BitbakeRecipeUIElement element = page.getUIElement(); | ||
176 | |||
177 | IRunnableWithProgress op = new IRunnableWithProgress() { | ||
178 | public void run(IProgressMonitor monitor) throws InvocationTargetException { | ||
179 | try { | ||
180 | doFinish(element, monitor); | ||
181 | File temp_dir = new File(element.getMetaDir() + "/temp"); | ||
182 | if (temp_dir.exists()) { | ||
183 | File working_dir = new File(element.getMetaDir()); | ||
184 | |||
185 | String rm_cmd = "rm -rf temp"; | ||
186 | final Process process = Runtime.getRuntime().exec(rm_cmd, null, working_dir); | ||
187 | int returnCode = process.waitFor(); | ||
188 | if (returnCode != 0) { | ||
189 | throw new Exception("Failed to clean up the temp dir"); | ||
190 | } | ||
191 | } | ||
192 | } catch (Exception e) { | ||
193 | throw new InvocationTargetException(e); | ||
194 | } finally { | ||
195 | monitor.done(); | ||
196 | } | ||
197 | } | ||
198 | }; | ||
199 | try { | ||
200 | getContainer().run(true, false, op); | ||
201 | } catch (InterruptedException e) { | ||
202 | return false; | ||
203 | } catch (InvocationTargetException e) { | ||
204 | Throwable realException = e.getTargetException(); | ||
205 | MessageDialog.openError(getShell(), "Error", realException.getMessage()); | ||
206 | return false; | ||
207 | } | ||
208 | return true; | ||
209 | } | ||
210 | |||
211 | private void throwCoreException(String message) throws CoreException { | ||
212 | IStatus status = new Status(IStatus.ERROR, "org.yocto.bc.ui", IStatus.OK, message, null); | ||
213 | throw new CoreException(status); | ||
214 | } | ||
215 | } \ No newline at end of file | ||