Skip to content
This repository was archived by the owner on Sep 17, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<ProjectReference Include="..\RoslynCodeTaskFactory\RoslynCodeTaskFactory.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/RoslynCodeTaskFactory/CodeTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Xml;
using System.Xml.Linq;

Expand Down Expand Up @@ -160,6 +162,8 @@ public TaskPropertyInfo[] GetTaskParameters()
/// <inheritdoc cref="ITaskFactory.Initialize"/>
public bool Initialize(string taskName, IDictionary<string, TaskPropertyInfo> parameterGroup, string taskBody, IBuildEngine taskFactoryLoggingHost)
{
WaitForDebuggerIfConfigured();

_log = new TaskLoggingHelper(taskFactoryLoggingHost, taskName)
{
TaskResources = Strings.ResourceManager,
Expand Down Expand Up @@ -807,5 +811,27 @@ private bool TryCompileInMemoryAssembly(IBuildEngine buildEngine, TaskInfo taskI
}
}
}

/// <summary>
/// Waits for a user to attach a debugger.
/// </summary>
private void WaitForDebuggerIfConfigured()
{
if (!String.Equals(Environment.GetEnvironmentVariable("ROSLYNCODETASKFACTORY_DEBUG"), "1"))
{
return;
}

Process currentProcess = Process.GetCurrentProcess();

Console.WriteLine(Strings.CodeTaskFactory_WaitingForDebugger, currentProcess.MainModule.FileName, currentProcess.Id);

while (!Debugger.IsAttached)
{
Thread.Sleep(200);
}

Debugger.Break();
}
}
}
53 changes: 40 additions & 13 deletions src/RoslynCodeTaskFactory/Internal/ManagedCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Linq;

namespace RoslynCodeTaskFactory.Internal
{
internal abstract class ManagedCompiler : ToolTask
{
private static readonly string DotnetCliPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");

private readonly Lazy<string> _executablePath;

protected ManagedCompiler()
{
_executablePath = new Lazy<string>(() =>
{
string pathToBuildTools = ToolLocationHelper.GetPathToBuildTools(ToolLocationHelper.CurrentToolsVersion, DotNetFrameworkArchitecture.Bitness32);

Func<string>[] possibleLocations =
{
// Standard MSBuild and legacy .NET Core
() => Path.Combine(pathToBuildTools, "Roslyn", ToolName),
// Legacy .NET Core
() => Path.Combine(pathToBuildTools, "Roslyn", Path.ChangeExtension(ToolName, ".dll")),
// .NET Core 2.0
() => Path.Combine(pathToBuildTools, "Roslyn", "bincore", Path.ChangeExtension(ToolName, ".dll")),
};

return possibleLocations.Select(possibleLocation => possibleLocation()).FirstOrDefault(File.Exists);
}, isThreadSafe: true);
}

public bool? Deterministic { get; set; }

public bool? NoConfig { get; set; }
Expand All @@ -26,6 +51,8 @@ internal abstract class ManagedCompiler : ToolTask

public bool? UseSharedCompilation { get; set; }

protected bool IsDotnetCli => !String.IsNullOrWhiteSpace(DotnetCliPath);

protected internal virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
{
commandLine.AppendPlusOrMinusSwitch("/deterministic", Deterministic);
Expand All @@ -45,6 +72,13 @@ protected override string GenerateCommandLineCommands()
{
CommandLineBuilderExtension commandLineBuilder = new CommandLineBuilderExtension();

if (IsDotnetCli)
{
commandLineBuilder.AppendFileNameIfNotNull(_executablePath.Value);

commandLineBuilder.AppendTextUnquoted(" ");
}

AddCommandLineCommands(commandLineBuilder);

return commandLineBuilder.ToString();
Expand All @@ -57,19 +91,12 @@ protected override string GenerateFullPathToTool()
return ToolExe;
}

string pathToBuildTools = ToolLocationHelper.GetPathToBuildTools(ToolLocationHelper.CurrentToolsVersion, DotNetFrameworkArchitecture.Bitness32);

if (pathToBuildTools != null)
if (IsDotnetCli)
{
string toolMSBuildLocation = Path.Combine(pathToBuildTools, "Roslyn", ToolName);

if (File.Exists(toolMSBuildLocation))
{
return toolMSBuildLocation;
}
return DotnetCliPath;
}

return null;
return _executablePath.Value;
}

protected override string GenerateResponseFileCommands()
Expand Down
9 changes: 9 additions & 0 deletions src/RoslynCodeTaskFactory/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/RoslynCodeTaskFactory/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,7 @@
<value>MSB3428: You must specify source code within the Code element or a path to a file containing source code.</value>
<comment>{StrBegin="MSB3428: "}</comment>
</data>
<data name="CodeTaskFactory_WaitingForDebugger" xml:space="preserve">
<value>Waiting for debugger to attach ({0} PID {1}). Press enter to continue...</value>
</data>
</root>