diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-23 16:59:14 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-23 16:59:14 +0000 |
commit | 9e3ccaeb444382abe38ffc5a14f2e62a95b6432c (patch) | |
tree | 5b14dbfc8c494e75a3af9026cafaaf8419c1ae9e /sandbox/rfeng/eclipse-workspace/src/main/java | |
parent | 73fd85c3daabbd085621a85e926cd2c7159b3121 (diff) |
An eclipse plugin that can run in headless mode to configure the eclipse workspace using command line
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@926662 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/rfeng/eclipse-workspace/src/main/java')
2 files changed, 372 insertions, 0 deletions
diff --git a/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/Activator.java b/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/Activator.java new file mode 100644 index 0000000000..c0095686f8 --- /dev/null +++ b/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/Activator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.eclipse.workspace; + +import org.eclipse.core.runtime.Plugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends Plugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.apache.tuscany.eclipse.workspace"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/WorkspaceConfigurator.java b/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/WorkspaceConfigurator.java new file mode 100644 index 0000000000..fcdd484939 --- /dev/null +++ b/sandbox/rfeng/eclipse-workspace/src/main/java/org/apache/tuscany/eclipse/workspace/WorkspaceConfigurator.java @@ -0,0 +1,312 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.eclipse.workspace; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.jobs.IJobChangeEvent; +import org.eclipse.core.runtime.jobs.IJobChangeListener; +import org.eclipse.core.runtime.jobs.JobChangeAdapter; +import org.eclipse.equinox.app.IApplication; +import org.eclipse.equinox.app.IApplicationContext; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.pde.internal.core.PDECore; +import org.eclipse.pde.internal.core.target.TargetDefinitionPersistenceHelper; +import org.eclipse.pde.internal.core.target.provisional.ITargetHandle; +import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService; +import org.eclipse.pde.internal.core.target.provisional.LoadTargetDefinitionJob; +import org.xml.sax.SAXException; + +@SuppressWarnings("restriction") +public class WorkspaceConfigurator implements IApplication { + private static final String SOURCE_ROOT = "source.root"; + private static final String TARGET_DEFINITION = "target.definition"; + private static final String MAVEN_LOCAL_REPO = "maven.repo"; + private static final String M2_REPO = "M2_REPO"; + + private IWorkspace workspace; + private final Object lock = new Object(); + + public Object start(final IApplicationContext appcontext) throws Exception { + IProgressMonitor monitor = createMonitor(); + final String[] args = (String[])appcontext.getArguments().get(IApplicationContext.APPLICATION_ARGS); + setM2REPOClassPathVariable(args, monitor); + setTargetPlatform(args); + + // Wait for the target platform job is done + synchronized (lock) { + lock.wait(); + } + configureWorkspace(args, monitor); + return EXIT_OK; + } + + private void setTargetPlatform(String args[]) throws FileNotFoundException, CoreException, + ParserConfigurationException, SAXException, IOException { + String targetFile = getOptionValue(args, TARGET_DEFINITION); + if (targetFile == null) { + return; + } + File tf = new File(targetFile).getAbsoluteFile(); + print("Setting target platform: " + tf); + ITargetPlatformService tps = + (ITargetPlatformService)PDECore.getDefault().acquireService(ITargetPlatformService.class.getName()); + ITargetHandle th = tps.getTarget(tf.toURI()); + FileInputStream is = new FileInputStream(tf); + try { + TargetDefinitionPersistenceHelper.initFromXML(th.getTargetDefinition(), is); + } finally { + is.close(); + } + IJobChangeListener jobListener = new JobChangeAdapter() { + + @Override + public void done(IJobChangeEvent event) { + super.done(event); + synchronized (lock) { + print("Target platform is configured: " + event.getResult()); + lock.notifyAll(); + } + } + + }; + LoadTargetDefinitionJob.load(th.getTargetDefinition(), jobListener); + } + + private IProgressMonitor createMonitor() { + return new NullProgressMonitor() { + + @Override + public void beginTask(String name, int totalWork) { + } + + @Override + public void done() { + // System.out.println(); + } + + @Override + public void worked(int work) { + // System.out.print("."); + } + }; + } + + private IPath setM2REPOClassPathVariable(String args[], IProgressMonitor monitor) throws JavaModelException { + IPath var = JavaCore.getClasspathVariable(M2_REPO); + if (var == null) { + String localRepo = getOptionValue(args, MAVEN_LOCAL_REPO); + if (localRepo == null) { + String home = System.getProperty("user.home"); + localRepo = home + File.separator + ".m2" + File.separator + "repository"; + } + File repo = new File(localRepo); + IPath path = new Path(repo.getAbsolutePath()); + JavaCore.setClasspathVariable(M2_REPO, path, monitor); + return path; + } + return var; + } + + private void configureWorkspace(final String[] args, IProgressMonitor monitor) throws Exception, CoreException { + this.workspace = ResourcesPlugin.getWorkspace(); + + String sourceRootArg = getOptionValue(args, SOURCE_ROOT); + File sourceRoot = null; + if (sourceRootArg != null) { + sourceRoot = new File(sourceRootArg); + } + + configureJDT(); + + if (sourceRoot != null) { + importProjects(sourceRoot, monitor); + } + + // cleanProjects(monitor); + buildWorkspace(monitor); + + workspace.save(true, monitor); + } + + private String getOptionValue(final String[] args, String option) { + String value = null; + if (args != null) { + List<String> argList = Arrays.asList(args); + int index = argList.indexOf("-" + option); + if (index != -1 && argList.size() - 1 >= index + 1) { + value = argList.get(index + 1); + } + } + return value; + } + + public void stop() { + this.workspace = null; + } + + private void importProjects(File sourceRoot, final IProgressMonitor monitor) throws Exception { + IWorkspaceRoot workspaceRoot = workspace.getRoot(); + + loadProjects(sourceRoot, monitor); + + for (IProject p : workspaceRoot.getProjects()) { + if (!p.isOpen()) { + // Open the project + p.open(monitor); + } else { + // Refresh the project + p.refreshLocal(IResource.DEPTH_INFINITE, monitor); + } + if (p.getFile(".project") == null) { + print("Deleting project: " + p.getName()); + p.delete(false, true, monitor); + } + } + + } + + @SuppressWarnings("unchecked") + private void configureJDT() { + Hashtable<Object, Object> options = JavaCore.getOptions(); + options.put(JavaCore.CORE_CIRCULAR_CLASSPATH, JavaCore.ERROR); + options.put(JavaCore.CORE_INCOMPLETE_CLASSPATH, JavaCore.WARNING); + JavaCore.setOptions(options); + + IVMInstall vminstall = JavaRuntime.getDefaultVMInstall(); + if (!vminstall.getName().equals("Default JRE")) { + vminstall.setName("Default JRE"); + } + } + + private void loadProjects(File sourceRoot, final IProgressMonitor monitor) throws CoreException { + if (sourceRoot.isDirectory()) { + File dotProject = new File(sourceRoot, ".project"); + if (dotProject.exists() && dotProject.isFile()) { + IPath path = new Path(dotProject.getAbsolutePath()); + IProjectDescription project = workspace.loadProjectDescription(path); + IProject p = workspace.getRoot().getProject(project.getName()); + if (!p.exists()) { + print("Importing project: " + project.getName()); + p.create(project, monitor); + } + } + } else { + return; + } + + File[] dirs = sourceRoot.listFiles(); + if (dirs == null) { + return; + } + + for (int i = 0; i < dirs.length; i++) { + if (dirs[i].isDirectory()) { + loadProjects(dirs[i], monitor); + } + } + } + + private void buildWorkspace(IProgressMonitor monitor) throws Exception { + print("Running a full build..."); + workspace.build(IncrementalProjectBuilder.FULL_BUILD, monitor); + + reportErrors(); + } + + private void reportErrors() throws CoreException { + int errorCount = 0; + print(); + IProject[] projects = workspace.getRoot().getProjects(); + HashMap<String, List<IMarker>> errors = new HashMap<String, List<IMarker>>(); + for (IProject p : projects) { + IMarker[] markers = p.findMarkers(null, true, IResource.DEPTH_INFINITE); + for (int m = 0; m < markers.length; m++) { + IMarker marker = markers[m]; + int severity = marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO); + if (severity == IMarker.SEVERITY_ERROR) { + List<IMarker> list = errors.get(p.getName()); + if (list == null) { + list = new ArrayList<IMarker>(); + errors.put(p.getName(), list); + } + list.add(marker); + reportError(marker); + errorCount++; + } + } + } + + Iterator<String> it = errors.keySet().iterator(); + while (it.hasNext()) { + String project = it.next(); + List<IMarker> list = errors.get(project); + print("Project " + project + " has " + list.size() + " errors."); + } + print(); + + if (errorCount > 0) { + print("The build is completed with " + errorCount + " errors."); + } else { + print("The build is completed successfully."); + } + print(); + } + + private void reportError(IMarker marker) throws CoreException { + print("Error: " + marker.getResource() + + ":" + + marker.getAttribute(IMarker.LINE_NUMBER, 0) + + ": " + + marker.getAttribute(IMarker.MESSAGE)); + } + + private void print(Object... msgs) { + for (Object msg : msgs) { + System.out.println(msg); + } + } +} |