Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Fixes for launcher working directory.

2002-11-21 Alain Magloire

        * src/../internal/ui/WorkingDirectoryBlock.java (setDefaultWorkingDir):
        Set the workspace directory default to be the Project.
        * src/.../internal/LocalCLaunchConfigurationDelegate.java (launch):
        Use the new function getWorkingDirectory(), it does more check by
        calling verifyWorkingDirectory().
        * src/.../AbstractCLaunchDelegate.java (getEnvironmentArray):
        implemented.
        (getWorkingDirectory): New method, call verifyWorkingDirectory();
        (verifyWorkingDirectory): New method, does more checking.
        (getWorkingDir): Deprecated and calls getWorkingDirectory().
        (getWorkingDirectoryPath): New method.
        (getCProject): Now static.
        (getProjectName): Now static

Index: src/org/eclipse/cdt/launch/AbstractCLaunchDelegate.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/AbstractCLaunchDelegate.java,v
retrieving revision 1.9
diff -u -r1.9 AbstractCLaunchDelegate.java
--- src/org/eclipse/cdt/launch/AbstractCLaunchDelegate.java	22 Nov 2002 01:58:17 -0000	1.9
+++ src/org/eclipse/cdt/launch/AbstractCLaunchDelegate.java	22 Nov 2002 04:27:07 -0000
@@ -19,13 +19,16 @@
 import org.eclipse.cdt.debug.core.CDebugCorePlugin;
 import org.eclipse.cdt.debug.core.ICDebugConfiguration;
 import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin;
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
 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.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.ILaunch;
@@ -39,9 +42,22 @@
 		throws CoreException;
 
 	protected String[] getEnvironmentArray(ILaunchConfiguration config) {
-		//		Map env = configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ENVIROMENT_MAP, (Map) null);
-		// TODO create env array;
-		return null;
+		Map env = null;
+		try {
+			env = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ENVIROMENT_MAP, (Map) null);
+		} catch (CoreException e) {
+		}
+		if (env == null) {
+			return new String[0];
+		}
+		String[] array = new String[env.size()];
+		Iterator entries = env.entrySet().iterator();
+		Entry entry;
+		for (int i = 0; entries.hasNext() && i < array.length; i++) {
+			entry = (Entry) entries.next();
+			array[i] = ((String) entry.getKey()) + ((String) entry.getValue());
+		}
+		return array;
 	}
 
 	protected Properties getEnvironmentProperty(ILaunchConfiguration config) {
@@ -63,19 +79,39 @@
 		return prop;
 	}
 
-	protected File getWorkingDir(ILaunchConfiguration config) throws CoreException {
+	/**
+	 * Returns the working directory specified by
+	 * the given launch configuration, or <code>null</code> if none.
+	 * 
+	 * @deprecated Should use getWorkingDirectory()
+	 * @param configuration launch configuration
+	 * @return the working directory specified by the given 
+	 *  launch configuration, or <code>null</code> if none
+	 * @exception CoreException if unable to retrieve the attribute
+	 */
+	public File getWorkingDir(ILaunchConfiguration configuration) throws CoreException {
+		return getWorkingDirectory(configuration);
+	}
+
+	/**
+	 * Returns the working directory specified by
+	 * the given launch configuration, or <code>null</code> if none.
+	 * 
+	 * @param configuration launch configuration
+	 * @return the working directory specified by the given 
+	 *  launch configuration, or <code>null</code> if none
+	 * @exception CoreException if unable to retrieve the attribute
+	 */
+	public File getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
+		return verifyWorkingDirectory(configuration);
+	}
+
+	protected IPath getWorkingDirectoryPath(ILaunchConfiguration config) throws CoreException {
 		String path = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String) null);
-		if (path == null) {
-			return null;
+		if (path != null) {
+			return new Path(path);
 		}
-		File dir = new File(path);
-		if (!dir.isDirectory()) {
-			abort(
-				"Specified working directory does not exist or is not a directory",
-				null,
-				ICDTLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
-		}
-		return dir;
+		return null;
 	}
 
 	/**
@@ -102,7 +138,7 @@
 	abstract protected String getPluginID();
 
 
-	public ICProject getCProject(ILaunchConfiguration configuration) throws CoreException {
+	public static ICProject getCProject(ILaunchConfiguration configuration) throws CoreException {
 		String projectName = getProjectName(configuration);
 		if (projectName != null) {
 			projectName = projectName.trim();
@@ -117,7 +153,7 @@
 		return null;
 	}
 
-	public String getProjectName(ILaunchConfiguration configuration) throws CoreException {
+	public static String getProjectName(ILaunchConfiguration configuration) throws CoreException {
 		return configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
 	}
 
@@ -222,6 +258,45 @@
 			abort("Program file does not exist", null, ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
 		}
 		return projectPath.getLocation();
+	}
+
+	/**
+	 * Verifies the working directory specified by the given 
+	 * launch configuration exists, and returns the working
+	 * directory, or <code>null</code> if none is specified.
+	 * 
+	 * @param configuration launch configuration
+	 * @return the working directory specified by the given 
+	 *  launch configuration, or <code>null</code> if none
+	 * @exception CoreException if unable to retrieve the attribute
+	 */	
+	public File verifyWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
+		IPath path = getWorkingDirectoryPath(configuration);
+		if (path == null) {
+			// default working dir is the project if this config has a project
+			ICProject cp = getCProject(configuration);
+			if (cp != null) {
+				IProject p = cp.getProject();
+				return p.getLocation().toFile();
+			}
+		} else {
+			if (path.isAbsolute()) {
+				File dir = new File(path.toOSString());
+				if (dir.isDirectory()) {
+					return dir;
+				} else {
+					abort("Working directory does not exist", null, ICDTLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
+				}
+			} else {
+				IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
+				if (res instanceof IContainer && res.exists()) {
+					return res.getLocation().toFile();
+				} else {
+					abort("Working directory does not exist", null, ICDTLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
+				}
+			}
+		}
+		return null;		
 	}
 
 	private static class ArgumentParser {
Index: src/org/eclipse/cdt/launch/internal/LocalCLaunchConfigurationDelegate.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/internal/LocalCLaunchConfigurationDelegate.java,v
retrieving revision 1.18
diff -u -r1.18 LocalCLaunchConfigurationDelegate.java
--- src/org/eclipse/cdt/launch/internal/LocalCLaunchConfigurationDelegate.java	1 Nov 2002 18:03:59 -0000	1.18
+++ src/org/eclipse/cdt/launch/internal/LocalCLaunchConfigurationDelegate.java	22 Nov 2002 04:27:07 -0000
@@ -80,9 +80,9 @@
 					dsession = debugConfig.getDebugger().createLaunchSession(config, exe);
 					ICDIRuntimeOptions opt = dsession.getRuntimeOptions();
 					opt.setArguments(getProgramArgumentsArray(config));
-					File wd = getWorkingDir(config);
+					File wd = getWorkingDirectory(config);
 					if (wd != null) {
-						opt.setWorkingDirectory(wd.toString());
+						opt.setWorkingDirectory(wd.getAbsolutePath());
 					}
 					opt.setEnvironment(getEnvironmentProperty(config));
 					ICDITarget dtarget = dsession.getTargets()[0];
@@ -127,7 +127,11 @@
 				abort("Failed Launching CDI Debugger", e, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
 			}
 		} else {
-			Process process = exec(commandArray, getEnvironmentArray(config), getWorkingDir(config));
+			File wd = getWorkingDirectory(config);
+			if (wd == null) {
+				wd = new File(System.getProperty("user.home", ".")); //NON-NLS-1;
+			}
+			Process process = exec(commandArray, getEnvironmentArray(config), wd);
 			DebugPlugin.getDefault().newProcess(launch, process, renderProcessLabel(commandArray[0]));
 		}
 		monitor.done();
Index: src/org/eclipse/cdt/launch/internal/ui/WorkingDirectoryBlock.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/internal/ui/WorkingDirectoryBlock.java,v
retrieving revision 1.1
diff -u -r1.1 WorkingDirectoryBlock.java
--- src/org/eclipse/cdt/launch/internal/ui/WorkingDirectoryBlock.java	13 Aug 2002 04:05:02 -0000	1.1
+++ src/org/eclipse/cdt/launch/internal/ui/WorkingDirectoryBlock.java	22 Nov 2002 04:27:07 -0000
@@ -1,6 +1,8 @@
 package org.eclipse.cdt.launch.internal.ui;
 import java.io.File;
 
+import org.eclipse.cdt.core.model.ICProject;
+import org.eclipse.cdt.launch.AbstractCLaunchDelegate;
 import org.eclipse.cdt.launch.ICDTLaunchConfigurationConstants;
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IResource;
@@ -246,12 +248,15 @@
 	 * Sets the default working directory
 	 */
 	protected void setDefaultWorkingDir() {
-			ILaunchConfiguration config = getLaunchConfiguration();
+		ILaunchConfiguration config = getLaunchConfiguration();
 		if (config != null) {
-//			IJavaProject javaProject = JavaRuntime.getJavaProject(config);
-			Object cProject = null;
+			ICProject cProject = null;
+			try {
+				cProject = AbstractCLaunchDelegate.getCProject(config);
+			} catch (CoreException e) {
+			}
 			if (cProject != null) {
-//				fWorkspaceDirText.setText(cProject.getPath().makeRelative().toOSString());
+				fWorkspaceDirText.setText(cProject.getPath().makeRelative().toOSString());
 				fLocalDirButton.setSelection(false);
 				fWorkspaceDirButton.setSelection(true);
 				return;



Back to the top