Search in sources :

Example 91 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project bndtools by bndtools.

the class Central method toPath.

public static IPath toPath(File file) throws Exception {
    IPath result = null;
    File absolute = file.getCanonicalFile();
    IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
    IFile[] candidates = wsroot.findFilesForLocationURI(absolute.toURI());
    if (candidates != null && candidates.length > 0) {
        result = candidates[0].getFullPath();
    } else {
        String workspacePath = getWorkspace().getBase().getAbsolutePath();
        String absolutePath = absolute.getPath();
        if (absolutePath.startsWith(workspacePath))
            result = new Path(absolutePath.substring(workspacePath.length()));
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 92 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project bndtools by bndtools.

the class BuiltBundleIndexer method builtBundles.

@Override
public void builtBundles(final IProject project, IPath[] paths) {
    IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
    final URI workspaceRootUri = wsroot.getLocationURI();
    Set<File> files = new HashSet<File>();
    for (IPath path : paths) {
        try {
            IFile ifile = wsroot.getFile(path);
            IPath location = ifile.getLocation();
            if (location != null)
                files.add(location.toFile());
        } catch (IllegalArgumentException e) {
            System.err.println("### Error processing path: " + path);
            e.printStackTrace();
        }
    }
    // Generate the index file
    File indexFile;
    try {
        Project model = Central.getProject(project);
        File target = model.getTarget();
        indexFile = new File(target, INDEX_FILENAME);
        IFile indexPath = wsroot.getFile(Central.toPath(indexFile));
        // Create the indexer and add ResourceAnalyzers from plugins
        RepoIndex indexer = new RepoIndex(logAdapter);
        List<ResourceAnalyzer> analyzers = Central.getWorkspace().getPlugins(ResourceAnalyzer.class);
        for (ResourceAnalyzer analyzer : analyzers) {
            indexer.addAnalyzer(analyzer, null);
        }
        // Use an analyzer to add a marker capability to workspace resources
        indexer.addAnalyzer(new ResourceAnalyzer() {

            @Override
            public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
                Capability cap = new Builder().setNamespace("bndtools.workspace").addAttribute("bndtools.workspace", workspaceRootUri.toString()).addAttribute("project.path", project.getFullPath().toString()).buildCapability();
                capabilities.add(cap);
            }
        }, null);
        Map<String, String> config = new HashMap<String, String>();
        config.put(ResourceIndexer.REPOSITORY_NAME, project.getName());
        config.put(ResourceIndexer.ROOT_URL, project.getLocation().toFile().toURI().toString());
        config.put(ResourceIndexer.PRETTY, "true");
        try (OutputStream output = IO.outputStream(indexFile)) {
            indexer.index(files, output, config);
        }
        indexPath.refreshLocal(IResource.DEPTH_ZERO, null);
        if (indexPath.exists())
            indexPath.setDerived(true, null);
    } catch (Exception e) {
        logger.logError(MessageFormat.format("Failed to generate index file for bundles in project {0}.", project.getName()), e);
        return;
    }
    // Parse the index and add to the workspace repository
    try (InputStream input = IO.stream(indexFile)) {
        WorkspaceR5Repository workspaceRepo = Central.getWorkspaceR5Repository();
        workspaceRepo.loadProjectIndex(project, input, project.getLocation().toFile().toURI());
    } catch (Exception e) {
        logger.logError("Failed to update workspace index.", e);
    }
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) Builder(org.osgi.service.indexer.Builder) OutputStream(java.io.OutputStream) URI(java.net.URI) WorkspaceR5Repository(bndtools.central.WorkspaceR5Repository) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) Capability(org.osgi.service.indexer.Capability) InputStream(java.io.InputStream) Resource(org.osgi.service.indexer.Resource) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) Project(aQute.bnd.build.Project) Requirement(org.osgi.service.indexer.Requirement) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IFile(org.eclipse.core.resources.IFile) File(java.io.File) RepoIndex(org.osgi.service.indexer.impl.RepoIndex)

Aggregations

IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)92 IPath (org.eclipse.core.runtime.IPath)41 IProject (org.eclipse.core.resources.IProject)31 IFile (org.eclipse.core.resources.IFile)30 IResource (org.eclipse.core.resources.IResource)29 CoreException (org.eclipse.core.runtime.CoreException)25 File (java.io.File)19 IWorkspace (org.eclipse.core.resources.IWorkspace)15 Path (org.eclipse.core.runtime.Path)14 IFolder (org.eclipse.core.resources.IFolder)12 IJavaProject (org.eclipse.jdt.core.IJavaProject)11 ArrayList (java.util.ArrayList)10 IContainer (org.eclipse.core.resources.IContainer)10 IOException (java.io.IOException)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 URI (java.net.URI)5 IProjectDescription (org.eclipse.core.resources.IProjectDescription)5 IStatus (org.eclipse.core.runtime.IStatus)5 Project (aQute.bnd.build.Project)4 Workspace (aQute.bnd.build.Workspace)4