Programmatically list open projects in an eclipse workspace from outside an eclipse

I want to write a Gradle plugin that can check the eclipse workspace directory and iterate over open projects in the workspace and locate each of them.

Something like

Workspace workspace = EclipseUtils.parseWorkspace("c:/myEclipseWorkspace"); Collection<Project> projects = workspace.getProjects(); for (Project project : projects) { System.out.println(String.format("name=%s, location=%s, open=%s", project.getName(), project.getLocation(), project.isOpen())); } 

I looked at my workspace and see some .location files under c:\myEclipseWorkspace\.metadata\.plugins\org.eclipse.core.resources\.projects\

But these files are a custom binary format. enter image description here

Is there an eclipse API I can call to parse them? Or some other solution for iterating open projects in the workspace.

Please note that I want to do this from outside to outshine and NOT inside the eclipse plugin.

+8
java eclipse parsing workspace
Nov 20 '15 at 10:04
source share
2 answers

Reading a private description for location

Since you write in Java, reuse the Eclipse code from your external location.

i.e. Pull the key code part out of org.eclipse.core.resources.ResourcesPlugin . Start with the org.eclipse.core.resources.ResourcesPlugin.getWorkspace() function and then go to org.eclipse.core.resources.IWorkspaceRoot.getProjects()

The code above reads the project description here: org.eclipse.core.internal.resources.LocalMetaArea.readPrivateDescription(IProject, ProjectDescription) and is called from org.eclipse.core.internal.localstore.FileSystemResourceManager.read(IProject, boolean) , which has some default location logic.

It's a joy of the EPL, as long as your new EPL program / feature, you can reuse the core Eclipse code to do new and wonderful things.

Reading workspace state to get open / close status

When reading the status of the workspace, you go to the ElementTree data structures. Reading this without using the ElementTree classes is probably unrealistic. Using ElementTree classes without full OSGi is probably unrealistic. I provide the following notes to help you on your journey.

Work back:

  • ICoreConstants.M_OPEN is a flag indicating that the project is open or closed (set to open, cleaning closed)
  • M_OPEN is checked when Project.isOpen() is called
  • Flags at runtime are in ResourceInfo.flags
  • Flags are loaded by ResourceInfo.readFrom() , called from SaveManager.readElement()
  • DataInput input passed to readElement from a tree of items stored in the workspace metadirectory in .metadata/.plugins/org.eclipse.core.resources/.root/<id>.tree . The specific version (id) of the file used is recorded in a secure table .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
  • A secure table is part of the internal state of SaveManager stored in MasterTable
+3
Nov 20 '15 at 10:24
source share

I was able to parse the file using this as a reference

  protected Location parseLocation(File locationFile) throws IOException { String name = locationFile.getParentFile().getName(); DataInputStream in = new DataInputStream(new FileInputStream(locationFile)); try { in.skip(ILocalStoreConstants.BEGIN_CHUNK.length); String path = in.readUTF(); int numRefs = in.readInt(); String[] refNames = new String[numRefs]; for (int i = 0; i < numRefs; ++ i) { refNames[i] = in.readUTF(); } in.skipBytes(ILocalStoreConstants.END_CHUNK.length); return new Location(name, path, refNames); } finally { in.close(); } } 

Unfortunately, this does not seem to be able to determine if a project is closed or not. Any pointers to getting a closed flag would be much appreciated

+1
Nov 20 '15 at 16:26
source share



All Articles