Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Unified Diff: editor/tools/plugins/com.google.dart.tools.deploy/src/com/google/dart/tools/ui/theme/preferences/TemporaryProject.java

Issue 11364134: Merge libv1. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.tools.deploy/src/com/google/dart/tools/ui/theme/preferences/TemporaryProject.java
diff --git a/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/test/util/TestProject.java b/editor/tools/plugins/com.google.dart.tools.deploy/src/com/google/dart/tools/ui/theme/preferences/TemporaryProject.java
similarity index 66%
copy from editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/test/util/TestProject.java
copy to editor/tools/plugins/com.google.dart.tools.deploy/src/com/google/dart/tools/ui/theme/preferences/TemporaryProject.java
index 8ca387ad30e8a0528ddc73cc8d9db5b584dfecca..6c128999be5bc94b761c8369a6db8470ccfc378f 100644
--- a/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/test/util/TestProject.java
+++ b/editor/tools/plugins/com.google.dart.tools.deploy/src/com/google/dart/tools/ui/theme/preferences/TemporaryProject.java
@@ -11,16 +11,12 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
-
-package com.google.dart.tools.core.test.util;
+package com.google.dart.tools.ui.theme.preferences;
import com.google.common.io.CharStreams;
import com.google.dart.compiler.util.apache.StringUtils;
import com.google.dart.tools.core.DartCore;
import com.google.dart.tools.core.analysis.AnalysisServer;
-import com.google.dart.tools.core.analysis.AnalysisTestUtilities;
-import com.google.dart.tools.core.index.NotifyCallback;
-import com.google.dart.tools.core.internal.index.impl.InMemoryIndex;
import com.google.dart.tools.core.internal.model.PackageLibraryManagerProvider;
import com.google.dart.tools.core.model.CompilationUnit;
import com.google.dart.tools.core.model.DartProject;
@@ -38,97 +34,98 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.jobs.IJobManager;
-import org.eclipse.core.runtime.jobs.Job;
import java.io.ByteArrayInputStream;
-import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
-import java.util.concurrent.CountDownLatch;
/**
- * Helper for creating, manipulating and disposing temporary {@link DartProject}.
+ * Helper for creating, manipulating and disposing a temporary {@link DartProject}. This exists in
+ * the workspace for a short time but the user should never see it.
*/
-public class TestProject {
+public class TemporaryProject {
+
+ private static final String DEFAULT_NAME = "CodeColoringSample";
+
+ static void deleteProject(IProject project) throws CoreException {
+ final int MAX_FAILURES = 10;
+
+ int failureCount = 0;
- /**
- * Wait for auto-build notification to occur, that is for the auto-build to finish.
- */
- public static void waitForAutoBuild() {
while (true) {
try {
- IJobManager jobManager = Job.getJobManager();
- jobManager.wakeUp(ResourcesPlugin.FAMILY_AUTO_BUILD);
- jobManager.wakeUp(ResourcesPlugin.FAMILY_AUTO_BUILD);
- jobManager.wakeUp(ResourcesPlugin.FAMILY_AUTO_BUILD);
- jobManager.join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
- break;
- } catch (Throwable e) {
- }
- }
- AnalysisTestUtilities.waitForAnalysis();
- try {
- final CountDownLatch latch = new CountDownLatch(1);
- InMemoryIndex.getInstance().notify(new NotifyCallback() {
- @Override
- public void done() {
- latch.countDown();
+ project.delete(true, true, null);
+ return;
+ } catch (CoreException ce) {
+ failureCount++;
+
+ if (failureCount >= MAX_FAILURES) {
+ throw ce;
}
- });
- latch.await();
- } catch (Throwable e) {
+ Runtime.getRuntime().gc();
+ Runtime.getRuntime().runFinalization();
+ }
}
}
private final IProject project;
-
private final DartProject dartProject;
/**
- * Creates new {@link DartProject} with name "Test".
+ * Creates new {@link DartProject} with default name.
*/
- public TestProject() throws Exception {
- this("Test");
+ public TemporaryProject() throws CoreException {
+ this(DEFAULT_NAME);
}
/**
* Creates new {@link DartProject} with given name.
*/
- public TestProject(final String projectName) throws Exception {
+ public TemporaryProject(final String name) throws CoreException {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
+ final String projectName = findUniqueName(root, name);
project = root.getProject(projectName);
workspace.run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
// delete project
if (project.exists()) {
- TestUtilities.deleteProject(project);
+ deleteProject(project);
}
// create project
- {
- project.create(null);
- project.open(null);
- }
+ project.create(null);
+ project.open(null);
// set nature
- {
- IProjectDescription description = workspace.newProjectDescription(projectName);
- description.setNatureIds(new String[] {DartCore.DART_PROJECT_NATURE});
- project.setDescription(description, null);
- }
+ IProjectDescription description = workspace.newProjectDescription(projectName);
+ description.setNatureIds(new String[] {DartCore.DART_PROJECT_NATURE});
+ project.setDescription(description, null);
}
}, null);
// remember DartProject
dartProject = DartCore.create(project);
}
+ public IFolder createFolder(String path) throws Exception {
+ String[] parts = StringUtils.split(path, "/");
+ IContainer container = project;
+ for (String part : parts) {
+ IFolder folder = container.getFolder(new Path(part));
+ if (!folder.exists()) {
+ folder.create(true, true, null);
+ }
+ container = folder;
+ }
+ return (IFolder) container;
+ }
+
/**
* Disposes allocated resources and deletes project.
*/
- public void dispose() throws Exception {
- // notify AnalysisServer
+ public void dispose() throws CoreException {
+ // TODO(messick): Do we need to notify AnalysisServer?
{
IPath location = project.getLocation();
if (location != null) {
@@ -136,8 +133,6 @@ public class TestProject {
server.discard(location.toFile());
}
}
- // we need to close, because in the other case DelteProcessor for some reason closes it,
- // but at the time when we create (!!!) new project
try {
if (project.exists()) {
project.close(null);
@@ -145,20 +140,7 @@ public class TestProject {
} catch (Throwable e) {
}
// do dispose
- TestUtilities.deleteProject(project);
- }
-
- public IFolder createFolder(String path) throws Exception {
- String[] parts = StringUtils.split(path, "/");
- IContainer container = project;
- for (String part : parts) {
- IFolder folder = container.getFolder(new Path(part));
- if (!folder.exists()) {
- folder.create(true, true, null);
- }
- container = folder;
- }
- return (IFolder) container;
+ deleteProject(project);
}
/**
@@ -169,8 +151,7 @@ public class TestProject {
}
/**
- * @return the {@link CompilationUnit} on given path, not <code>null</code>, but may be not
- * existing.
+ * @return the {@link IFile} on given path, not <code>null</code>, but may not exist.
*/
public IFile getFile(String path) {
return project.getFile(new Path(path));
@@ -207,7 +188,7 @@ public class TestProject {
/**
* Creates or updates {@link IFile} with content of the given {@link InputStream}.
*/
- public IFile setFileContent(String path, InputStream stream) throws Exception {
+ public IFile setFileContent(String path, InputStream stream) throws CoreException {
IFile file = getFile(path);
if (file.exists()) {
file.setContents(stream, true, false, null);
@@ -215,21 +196,13 @@ public class TestProject {
file.create(stream, true, null);
file.setCharset("UTF-8", null);
}
- // notify AnalysisServer
- {
- AnalysisServer server = PackageLibraryManagerProvider.getDefaultAnalysisServer();
- File javaFile = file.getLocation().toFile();
- server.scan(javaFile, 5000);
- server.changed(javaFile);
- }
- // done
return file;
}
/**
* Creates or updates with {@link String} content of the {@link IFile}.
*/
- public IFile setFileContent(String path, String content) throws Exception {
+ public IFile setFileContent(String path, String content) throws IOException, CoreException {
byte[] bytes = content.getBytes("UTF-8");
InputStream stream = new ByteArrayInputStream(bytes);
return setFileContent(path, stream);
@@ -238,8 +211,23 @@ public class TestProject {
/**
* Creates or updates {@link CompilationUnit} at given path.
*/
- public CompilationUnit setUnitContent(String path, String content) throws Exception {
+ public CompilationUnit setUnitContent(String path, String content) throws IOException,
+ CoreException {
IFile file = setFileContent(path, content);
- return (CompilationUnit) DartCore.create(file);
+ CompilationUnit unit = (CompilationUnit) DartCore.create(file);
+ return unit;
+ }
+
+ private String findUniqueName(IWorkspaceRoot root, String initialName) {
+ String name = initialName;
+ int n = 0;
+ while (true) {
+ IProject p = root.getProject(name);
+ if (!p.exists()) {
+ return name;
+ }
+ n += 1;
+ name = initialName + String.valueOf(n);
+ }
}
}

Powered by Google App Engine
This is Rietveld 408576698