package bndtools.launch.util;

import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;

import aQute.bnd.build.Project;
import aQute.bnd.build.Run;
import bndtools.central.Central;
import bndtools.launch.LaunchConstants;

public final class LaunchUtils {

    private LaunchUtils() {}

    public static IResource getTargetResource(ILaunchConfiguration configuration) throws CoreException {
        String target = configuration.getAttribute(LaunchConstants.ATTR_LAUNCH_TARGET, (String) null);
        if (target == null || target.length() == 0) {
            return null;
        }

        IResource targetResource = ResourcesPlugin.getWorkspace().getRoot().findMember(target);
        if (targetResource == null)
            return null;

        return targetResource;
    }

    public static String getLaunchProjectName(IResource launchResource) {
        String result;

        IProject project = launchResource.getProject();
        Project bnd;
        try {
            bnd = Central.getWorkspace().getProject(project.getName());
        } catch (Exception e) {
            bnd = null;
        }

        result = (bnd != null) ? bnd.getName() : Project.BNDCNF;
        return result;
    }

    public static Project getBndProject(ILaunchConfiguration configuration) throws Exception {
        IResource targetResource = getTargetResource(configuration);
        if (targetResource == null)
            throw new IllegalArgumentException("Bnd launch target was not specified, or does not exist.", null);

        return getBndProject(targetResource);
    }

    public static Run getBndProject(IResource targetResource) throws Exception {
        File targetFile = targetResource.getLocation().toFile();
        return Run.createRun(Central.getWorkspaceIfPresent(), targetFile);
    }
}

@@ -45,16 +45,17 @@
 o.File;%0A
+%0A
 import o
@@ -346,16 +346,50 @@
 ld.Run;%0A
+import aQute.bnd.build.Workspace;%0A
 import b
@@ -1895,138 +1895,1396 @@
     
-File targetFile = targetResource.getLocation().toFile();%0A        return Run.createRun(Central.getWorkspaceIfPresent(), targetFile)
+Run run;%0A%0A        Workspace ws = Central.getWorkspaceIfPresent();%0A        if (targetResource.getType() == IResource.PROJECT) %7B%0A            // This is a bnd project --%3E find the bnd.bnd file%0A            IProject project = (IProject) targetResource;%0A            if (ws == null)%0A                throw new Exception(String.format(%22Cannot load bnd project for directory %25s: no bnd workspace found%22, project.getLocation()));%0A            File bndFile = project.getFile(Project.BNDFILE).getLocation().toFile();%0A            if (bndFile == null %7C%7C !bndFile.isFile())%0A                throw new Exception(String.format(%22Failed to load bnd project for directory %25s: %25s does not exist or is not a file.%22, project.getLocation(), Project.BNDFILE));%0A%0A            run = Run.createRun(ws, bndFile);%0A        %7D else if (targetResource.getType() == IResource.FILE) %7B%0A            // This is file, use directly%0A            File file = targetResource.getLocation().toFile();%0A            if (file == null %7C%7C !file.isFile())%0A                throw new Exception(String.format(%22Failed to create bnd launch configuration: %25s does not exist or is not a file.%22, file));%0A            run = Run.createRun(ws, file);%0A        %7D else %7B%0A            throw new Exception(String.format(%22Cannot create a bnd launch configuration for %25s: not a project or file resource.%22, targetResource.getLocation()));%0A        %7D%0A        return run
 ;%0A  

Fix #1190: Can't Run from bnd.bnd file