Skip to content

Commit

Permalink
Automatically add project references to the debug classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Apr 25, 2015
1 parent f430ca2 commit 17db107
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
25 changes: 24 additions & 1 deletion Tvl.VisualStudio.Language.Java/Project/JavaProjectConfig.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
namespace Tvl.VisualStudio.Language.Java.Project
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Project;
using Tvl.VisualStudio.Shell;

using __VSDBGLAUNCHFLAGS = Microsoft.VisualStudio.Shell.Interop.__VSDBGLAUNCHFLAGS;
using __VSDBGLAUNCHFLAGS2 = Microsoft.VisualStudio.Shell.Interop.__VSDBGLAUNCHFLAGS2;
using _PersistStorageType = Microsoft.VisualStudio.Shell.Interop._PersistStorageType;
using CommandLineBuilder = Microsoft.Build.Utilities.CommandLineBuilder;
using DEBUG_LAUNCH_OPERATION = Microsoft.VisualStudio.Shell.Interop.DEBUG_LAUNCH_OPERATION;
using Directory = System.IO.Directory;
using File = System.IO.File;
using IVsDebugger2 = Microsoft.VisualStudio.Shell.Interop.IVsDebugger2;
using IVsUIShell = Microsoft.VisualStudio.Shell.Interop.IVsUIShell;
Expand Down Expand Up @@ -193,6 +196,26 @@ public override int DebugLaunch(uint grfLaunch)
workingDirectory = Path.GetFullPath(Path.Combine(this.ProjectManager.ProjectFolder, workingDirectory));
}

// Pass the project references via the CLASSPATH environment variable
List<string> classPathEntries = new List<string>();
IReferenceContainer referenceContainer = ProjectManager.GetReferenceContainer();
IList<ReferenceNode> references = referenceContainer.EnumReferences();
foreach (var referenceNode in references)
{
JarReferenceNode jarReferenceNode = referenceNode as JarReferenceNode;
if (jarReferenceNode != null)
{
if (File.Exists(jarReferenceNode.InstalledFilePath) || Directory.Exists(jarReferenceNode.InstalledFilePath))
classPathEntries.Add(jarReferenceNode.InstalledFilePath);
}
}

if (classPathEntries != null)
{
string classPath = string.Join(";", classPathEntries);
info.Environment.Add("CLASSPATH", classPath);
}

//List<string> arguments = new List<string>();
//arguments.Add(@"-agentpath:C:\dev\SimpleC\Tvl.Java.DebugHost\bin\Debug\Tvl.Java.DebugHostWrapper.dll");
////arguments.Add(@"-verbose:jni");
Expand Down Expand Up @@ -221,7 +244,7 @@ public override int DebugLaunch(uint grfLaunch)
};
info.PortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}");
info.LaunchOperation = DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
info.LaunchFlags = (__VSDBGLAUNCHFLAGS)grfLaunch;
info.LaunchFlags = (__VSDBGLAUNCHFLAGS)grfLaunch | (__VSDBGLAUNCHFLAGS)__VSDBGLAUNCHFLAGS2.DBGLAUNCH_MergeEnv;

var debugger = (IVsDebugger2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsShellDebugger));
int result = debugger.LaunchDebugTargets(info);
Expand Down
18 changes: 15 additions & 3 deletions Tvl.VisualStudio.Shell/Extensions/DebugTargetInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
namespace Tvl.VisualStudio.Shell
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.Shell.Interop;

public sealed class DebugTargetInfo
{
/// <summary>
/// This is the backing field for the <see cref="Environment"/> property.
/// </summary>
private readonly Dictionary<string, string> _environment =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public string Arguments
{
get;
Expand All @@ -18,10 +25,15 @@ public string CurrentDirectory
set;
}

public string Environment
/// <summary>
/// Gets a collection of environment variables to set for the debug process.
/// </summary>
public Dictionary<string, string> Environment
{
get;
set;
get
{
return _environment;
}
}

public string Executable
Expand Down
22 changes: 21 additions & 1 deletion Tvl.VisualStudio.Shell/Extensions/IVsDebuggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Tvl.VisualStudio.Shell
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Marshal = System.Runtime.InteropServices.Marshal;
Expand Down Expand Up @@ -35,7 +37,7 @@ public static int LaunchDebugTargets(this IVsDebugger2 debugger, params DebugTar
{
vstargets[i].bstrArg = targets[i].Arguments;
vstargets[i].bstrCurDir = targets[i].CurrentDirectory;
vstargets[i].bstrEnv = targets[i].Environment;
vstargets[i].bstrEnv = GetEnvironmentString(targets[i].Environment);
vstargets[i].bstrExe = targets[i].Executable;
vstargets[i].bstrOptions = targets[i].Options;
vstargets[i].bstrPortName = targets[i].PortName;
Expand Down Expand Up @@ -74,6 +76,24 @@ public static int LaunchDebugTargets(this IVsDebugger2 debugger, params DebugTar
}
}

private static string GetEnvironmentString(Dictionary<string, string> dictionary)
{
if (dictionary == null || dictionary.Count == 0)
return null;

StringBuilder builder = new StringBuilder();
foreach (var pair in dictionary)
{
builder.Append(pair.Key);
builder.Append('=');
builder.Append(pair.Value);
builder.Append('\0');
}

builder.Append('\0');
return builder.ToString();
}

public static int LaunchDebugTargets(this IVsDebugger2 debugger, params VsDebugTargetInfo2[] targets)
{
IntPtr ptr = IntPtr.Zero;
Expand Down

0 comments on commit 17db107

Please sign in to comment.