author | Eugene Ostroukhov <eugeneo@symbian.org> |
Thu, 18 Mar 2010 11:56:59 -0700 | |
changeset 276 | f2f4a1259de8 |
parent 2 | e4420d2515f1 |
child 355 | 8726e95bcbba |
permissions | -rw-r--r-- |
2 | 1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style license that can be |
|
3 |
// found in the LICENSE file. |
|
4 |
||
5 |
package org.chromium.debug.core.util; |
|
6 |
||
7 |
import java.io.ByteArrayInputStream; |
|
8 |
import java.io.File; |
|
9 |
import java.net.URI; |
|
10 |
||
11 |
import org.chromium.debug.core.ChromiumDebugPlugin; |
|
12 |
import org.chromium.debug.core.efs.ChromiumScriptFileSystem; |
|
13 |
import org.eclipse.core.filesystem.EFS; |
|
14 |
import org.eclipse.core.filesystem.IFileStore; |
|
15 |
import org.eclipse.core.resources.IContainer; |
|
16 |
import org.eclipse.core.resources.IFile; |
|
17 |
import org.eclipse.core.resources.IProject; |
|
18 |
import org.eclipse.core.resources.IProjectDescription; |
|
19 |
import org.eclipse.core.resources.IWorkspace; |
|
20 |
import org.eclipse.core.resources.ResourceAttributes; |
|
21 |
import org.eclipse.core.resources.ResourcesPlugin; |
|
22 |
import org.eclipse.core.runtime.CoreException; |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
23 |
import org.eclipse.core.runtime.IPath; |
2 | 24 |
import org.eclipse.core.runtime.IProgressMonitor; |
25 |
import org.eclipse.core.runtime.IStatus; |
|
26 |
import org.eclipse.core.runtime.Status; |
|
27 |
import org.eclipse.core.runtime.jobs.Job; |
|
28 |
import org.eclipse.swt.widgets.Display; |
|
29 |
import org.eclipse.ui.IWorkbench; |
|
30 |
import org.eclipse.ui.IWorkbenchWindow; |
|
31 |
import org.eclipse.ui.PartInitException; |
|
32 |
import org.eclipse.ui.PlatformUI; |
|
33 |
||
34 |
/** |
|
35 |
* A utility for interaction with the Eclipse workspace. |
|
36 |
*/ |
|
37 |
public class ChromiumDebugPluginUtil { |
|
38 |
||
39 |
public static final String CHROMIUM_EXTENSION = "chromium"; //$NON-NLS-1$ |
|
40 |
||
41 |
public static final String JS_DEBUG_PROJECT_NATURE = "org.chromium.debug.core.jsnature"; //$NON-NLS-1$ |
|
42 |
||
43 |
public static final String CHROMIUM_EXTENSION_SUFFIX = "." + CHROMIUM_EXTENSION; //$NON-NLS-1$ |
|
44 |
||
45 |
private static final String PROJECT_EXPLORER_ID = "org.eclipse.ui.navigator.ProjectExplorer"; //$NON-NLS-1$ |
|
46 |
||
47 |
/** |
|
48 |
* Brings up the "Project Explorer" view in the active workbench window. |
|
49 |
*/ |
|
50 |
public static void openProjectExplorerView() { |
|
51 |
Display.getDefault().asyncExec(new Runnable() { |
|
52 |
public void run() { |
|
53 |
IWorkbench workbench = PlatformUI.getWorkbench(); |
|
54 |
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); |
|
55 |
if (window == null) { |
|
56 |
if (workbench.getWorkbenchWindowCount() == 1) { |
|
57 |
window = workbench.getWorkbenchWindows()[0]; |
|
58 |
} |
|
59 |
} |
|
60 |
if (window != null) { |
|
61 |
try { |
|
62 |
window.getActivePage().showView(PROJECT_EXPLORER_ID); |
|
63 |
} catch (PartInitException e) { |
|
64 |
// ignore |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
}); |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Creates an empty workspace project with the name starting with the given projectNameBase. |
|
73 |
* Created project is guaranteed to be new in EFS, but workspace may happen to |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
74 |
* already have project with such url (left uncleaned from previous runs). Such project |
2 | 75 |
* silently gets deleted. |
76 |
* @param projectNameBase project name template |
|
77 |
* @return the newly created project, or {@code null} if the creation failed |
|
78 |
*/ |
|
79 |
public static IProject createEmptyProject(String projectNameBase) { |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
80 |
ProjectCheckData projectProject; |
2 | 81 |
try { |
82 |
for (int uniqueNumber = 0; ; uniqueNumber++) { |
|
83 |
String projectNameTry; |
|
84 |
if (uniqueNumber == 0) { |
|
85 |
projectNameTry = projectNameBase; |
|
86 |
} else { |
|
87 |
projectNameTry = projectNameBase + " (" + uniqueNumber + ")"; //$NON-NLS-1$ //$NON-NLS-2$ |
|
88 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
89 |
projectProject = checkProjectName(projectNameTry); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
90 |
if (projectProject != null) { |
2 | 91 |
break; |
92 |
} |
|
93 |
} |
|
94 |
} catch (CoreException e) { |
|
95 |
ChromiumDebugPlugin.log(e); |
|
96 |
return null; |
|
97 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
98 |
IProject project = projectProject.getProject(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
99 |
if (project.exists()) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
100 |
try { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
101 |
project.delete(true, null); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
102 |
} catch (CoreException e) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
103 |
ChromiumDebugPlugin.log(e); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
104 |
return null; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
105 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
106 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
107 |
|
2 | 108 |
IProjectDescription description = |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
109 |
ResourcesPlugin.getWorkspace().newProjectDescription(project.getName()); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
110 |
description.setLocationURI(projectProject.getProjectUri()); |
2 | 111 |
description.setNatureIds(new String[] { JS_DEBUG_PROJECT_NATURE }); |
112 |
try { |
|
113 |
||
114 |
project.create(description, null); |
|
115 |
project.open(null); |
|
116 |
||
117 |
return project; |
|
118 |
} catch (CoreException e) { |
|
119 |
ChromiumDebugPlugin.log(e); |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
120 |
return null; |
2 | 121 |
} |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
122 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
123 |
|
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
124 |
private interface ProjectCheckData { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
125 |
IProject getProject(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
126 |
URI getProjectUri(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
127 |
} |
2 | 128 |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
129 |
/** |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
130 |
* Checks whether debug virtual project can be created. |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
131 |
* @param projectNameTry desired project name |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
132 |
* @return project project with parameters data or null if project with desired name cannot be |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
133 |
* created |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
134 |
*/ |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
135 |
private static ProjectCheckData checkProjectName(String projectNameTry) throws CoreException { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
136 |
final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectNameTry); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
137 |
if (project.exists()) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
138 |
URI projectURI = project.getLocationURI(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
139 |
if (!ChromiumScriptFileSystem.isChromiumDebugURI(projectURI)) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
140 |
// This is not our project. Do not touch it. |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
141 |
return null; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
142 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
143 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
144 |
IPath newPath = project.getFullPath(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
145 |
final URI projectUriTry = ChromiumScriptFileSystem.getFileStoreUri(newPath); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
146 |
IFileStore projectStore = EFS.getStore(projectUriTry); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
147 |
if (projectStore.fetchInfo().exists()) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
148 |
return null; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
149 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
150 |
return new ProjectCheckData() { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
151 |
public IProject getProject() { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
152 |
return project; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
153 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
154 |
public URI getProjectUri() { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
155 |
return projectUriTry; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
156 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
157 |
}; |
2 | 158 |
} |
159 |
||
160 |
||
161 |
/** |
|
162 |
* Removes virtual project which was created for debug session. Does its job |
|
163 |
* asynchronously. |
|
164 |
*/ |
|
165 |
public static void deleteVirtualProjectAsync(final IProject debugProject) { |
|
166 |
Job job = new Job("Remove virtual project") { |
|
167 |
@Override |
|
168 |
protected IStatus run(IProgressMonitor monitor) { |
|
169 |
URI projectUri = debugProject.getLocationURI(); |
|
170 |
try { |
|
171 |
IFileStore projectStore = EFS.getStore(projectUri); |
|
172 |
if (projectStore.fetchInfo().exists()) { |
|
173 |
projectStore.delete(EFS.NONE, null); |
|
174 |
} |
|
175 |
debugProject.delete(true, null); |
|
176 |
} catch (CoreException e) { |
|
177 |
ChromiumDebugPlugin.log(e); |
|
178 |
return new Status(IStatus.ERROR, ChromiumDebugPlugin.PLUGIN_ID, |
|
179 |
"Failed to delete virtual project"); |
|
180 |
} |
|
181 |
return Status.OK_STATUS; |
|
182 |
} |
|
183 |
}; |
|
184 |
||
185 |
job.schedule(); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* @param projectName to check for existence |
|
190 |
* @return whether the project named projectName exists. |
|
191 |
*/ |
|
192 |
public static boolean projectExists(String projectName) { |
|
193 |
IWorkspace ws = ResourcesPlugin.getWorkspace(); |
|
194 |
IProject proj = ws.getRoot().getProject(projectName); |
|
195 |
return proj.exists(); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Creates an empty file with the given filename in the given project. |
|
200 |
* |
|
201 |
* @param project to create the file in |
|
202 |
* @param filename the base file name to create (will be sanitized for |
|
203 |
* illegal chars and, in the case of a name clash, suffixed with "(N)") |
|
204 |
* @return the result of IFile.getName(), or {@code null} if the creation |
|
205 |
* has failed |
|
206 |
*/ |
|
207 |
public static IFile createFile(IProject project, String filename) { |
|
208 |
String patchedName = new File(filename).getName().replace('?', '_'); // simple name |
|
209 |
String uniqueName = patchedName; |
|
210 |
||
211 |
// TODO(apavlov): refactor this? |
|
212 |
for (int i = 1; i < 1000; ++i) { |
|
213 |
String filePathname = uniqueName + CHROMIUM_EXTENSION_SUFFIX; |
|
214 |
IFile file = project.getFile(filePathname); |
|
215 |
||
216 |
if (file.exists()) { |
|
217 |
uniqueName = new StringBuilder(patchedName) |
|
218 |
.append(" (") //$NON-NLS-1$ |
|
219 |
.append(i) |
|
220 |
.append(')') |
|
221 |
.toString(); |
|
222 |
} else { |
|
223 |
try { |
|
224 |
file.create(new ByteArrayInputStream("".getBytes()), false, null); //$NON-NLS-1$ |
|
225 |
} catch (CoreException e) { |
|
226 |
ChromiumDebugPlugin.log(e); |
|
227 |
return null; |
|
228 |
} |
|
229 |
return file; |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
// Can we have 1000 same-named files? |
|
234 |
return null; |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Writes data into a resource with the given resourceName residing in the |
|
239 |
* source folder of the given project. The previous file content is lost. |
|
240 |
* Temporarily resets the "read-only" file attribute if one is present. |
|
241 |
* |
|
242 |
* @param file to set contents for |
|
243 |
* @param data to write into the file |
|
244 |
* @throws CoreException |
|
245 |
*/ |
|
246 |
public static void writeFile(IFile file, String data) throws CoreException { |
|
247 |
if (file != null && file.exists()) { |
|
248 |
ResourceAttributes resourceAttributes = file.getResourceAttributes(); |
|
249 |
if (resourceAttributes.isReadOnly()) { |
|
250 |
resourceAttributes.setReadOnly(false); |
|
251 |
file.setResourceAttributes(resourceAttributes); |
|
252 |
} |
|
253 |
file.setContents(new ByteArrayInputStream(data.getBytes()), IFile.FORCE, null); |
|
254 |
resourceAttributes.setReadOnly(true); |
|
255 |
file.setResourceAttributes(resourceAttributes); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
public static boolean isInteger(String value) { |
|
260 |
try { |
|
261 |
Integer.parseInt(value); |
|
262 |
return true; |
|
263 |
} catch (NumberFormatException e) { |
|
264 |
return false; |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* The container where the script sources should be put. |
|
270 |
* |
|
271 |
* @param project where the launch configuration stores the scripts |
|
272 |
* @return the script source container |
|
273 |
*/ |
|
274 |
public static IContainer getSourceContainer(IProject project) { |
|
275 |
return project; |
|
276 |
} |
|
277 |
||
278 |
private ChromiumDebugPluginUtil() { |
|
279 |
// not instantiable |
|
280 |
} |
|
281 |
} |