简体   繁体   English

从 Eclipse 外部以编程方式列出 Eclipse 工作区中打开的项目

[英]Programmatically list open projects in an eclipse workspace from outside of eclipse

I want to write a Gradle plugin which can inspect an eclipse workspace directory and iterate over the open projects within the workspace and determine the location of each.我想编写一个 Gradle 插件,它可以检查 eclipse 工作区目录并遍历工作区中的打开项目并确定每个项目的位置。

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've looked at my workspace and can see some .location files under c:\\myEclipseWorkspace\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\我查看了我的工作区,可以在c:\\myEclipseWorkspace\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\下看到一些.location文件

But these files are a custom binary format但是这些文件是自定义的二进制格式在此处输入图片说明

Is there an eclipse API that I can invoke to parse these?是否有我可以调用的 Eclipse API 来解析这些? Or some other solution to iterate the open projects in a workspace.或者其他一些解决方案来迭代工作区中的打开项目。

Please note that I want to do this externally to eclipse and NOT within an eclipse plugin.请注意,我想在 eclipse 外部执行此操作,而不是在 eclipse 插件中执行此操作。

Reading the Private Description to Obtain Location阅读私人描述以获取位置

Since you are writing in Java, then reuse the Eclipse code from your external location.由于您是用 Java 编写的,因此可以重用外部位置的 Eclipse 代码。

ie Pull out some of the key code from org.eclipse.core.resources.ResourcesPlugin .即从org.eclipse.core.resources.ResourcesPlugin拉出一些关键代码。 Start with the impl of org.eclipse.core.resources.ResourcesPlugin.getWorkspace() and then work your way to org.eclipse.core.resources.IWorkspaceRoot.getProjects()org.eclipse.core.resources.ResourcesPlugin.getWorkspace()的 impl 开始,然后按照自己的方式工作到org.eclipse.core.resources.IWorkspaceRoot.getProjects()

The above code reads the project description here: org.eclipse.core.internal.resources.LocalMetaArea.readPrivateDescription(IProject, ProjectDescription) and that is called from org.eclipse.core.internal.localstore.FileSystemResourceManager.read(IProject, boolean) which has some logic about default locations.上面的代码在这里读取项目描述: org.eclipse.core.internal.resources.LocalMetaArea.readPrivateDescription(IProject, ProjectDescription)并且从org.eclipse.core.internal.localstore.FileSystemResourceManager.read(IProject, boolean)调用它有一些关于默认位置的逻辑。

This is the joy of EPL, as long as your new program/feature is EPL you can reuse Eclipse's core code to do new and wonderful things.这就是 EPL 的乐趣,只要您的新程序/特性是 EPL,您就可以重用 Eclipse 的核心代码来做新奇的事情。

Reading Workspace State to Obtain Open/Close State读取工作区状态以获取打开/关闭状态

When reading workspace state, you are moving into the ElementTree data structures.读取工作区状态时,您正在进入ElementTree数据结构。 Reading this without using the ElementTree classes is probably unrealistic.在不使用 ElementTree 类的情况下阅读本文可能是不现实的。 Using the ElementTree classes without full OSGi is probably unrealistic.在没有完整 OSGi 的情况下使用 ElementTree 类可能是不现实的。 I provide the following notes to help you on your way.我提供以下说明以帮助您。

Working backwards:向后工作:

  • ICoreConstants.M_OPEN is the flag value indicating project is open or closed (set for open, clear for closed) ICoreConstants.M_OPEN是表示项目打开或关闭的标志值(设置为打开,清除为关闭)
  • M_OPEN is tested when Project.isOpen() is called调用Project.isOpen()时测试 M_OPEN
  • The flags at runtime are in ResourceInfo.flags运行时的标志在ResourceInfo.flags
  • The flags are loaded by ResourceInfo.readFrom() called from SaveManager.readElement()这些标志由从SaveManager.readElement()调用的ResourceInfo.readFrom()加载
  • The DataInput input passed to readElement is from the Element Tree stored in the workspace meta directory in .metadata/.plugins/org.eclipse.core.resources/.root/<id>.tree .传递给readElementDataInput input来自存储在.metadata/.plugins/org.eclipse.core.resources/.root/<id>.tree的工作空间元目录中的元素树。 The specific version (id) of the file to use is recorded in the safe table .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources要使用的文件的特定版本(id)记录在安全表.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
  • The safe table is part of the SaveManager 's internal state stored in a MasterTable安全表是部分SaveManager存储在内部的状态MasterTable

I've managed 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 doesn't seem to be able to detect if a project is closed or not.不幸的是,这似乎无法检测项目是否已关闭。 Any pointers on getting the closed flag would be much appreciated任何有关获取关闭标志的指针将不胜感激

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM