dotnet-installer/src/dotnet/commands/dotnet-msbuild/MSBuildForwardingApp.cs

110 lines
3.7 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
2016-09-22 19:11:08 -05:00
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using System.Diagnostics;
2016-09-22 19:11:08 -05:00
namespace Microsoft.DotNet.Tools.MSBuild
{
public class MSBuildForwardingApp
{
internal const string TelemetrySessionIdEnvironmentVariableName = "DOTNET_CLI_TELEMETRY_SESSIONID";
private const string MSBuildExeName = "MSBuild.dll";
private const string SdksDirectoryName = "Sdks";
private readonly ForwardingApp _forwardingApp;
private readonly Dictionary<string, string> _msbuildRequiredEnvironmentVariables =
new Dictionary<string, string>
{
{ "MSBuildExtensionsPath", AppContext.BaseDirectory },
{ "CscToolExe", GetRunCscPath() },
{ "MSBuildSDKsPath", GetMSBuildSDKsPath() }
};
private readonly IEnumerable<string> _msbuildRequiredParameters =
new List<string> { "/m", "/v:m" };
2017-02-14 11:38:01 -08:00
public MSBuildForwardingApp(IEnumerable<string> argsToForward, string msbuildPath = null)
{
if (Telemetry.CurrentSessionId != null)
{
2016-11-01 09:29:19 -07:00
try
{
Type loggerType = typeof(MSBuildLogger);
argsToForward = argsToForward
.Concat(new[]
{
$"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}"
});
2016-11-01 09:29:19 -07:00
}
catch (Exception)
{
// Exceptions during telemetry shouldn't cause anything else to fail
}
}
_forwardingApp = new ForwardingApp(
2017-02-14 11:38:01 -08:00
msbuildPath ?? GetMSBuildExePath(),
_msbuildRequiredParameters.Concat(argsToForward.Select(Escape)),
environmentVariables: _msbuildRequiredEnvironmentVariables);
}
public ProcessStartInfo GetProcessStartInfo()
{
return _forwardingApp
.WithEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, Telemetry.CurrentSessionId)
.GetProcessStartInfo();
}
public int Execute()
{
return GetProcessStartInfo().Execute();
}
private static string Escape(string arg) =>
2017-02-01 10:09:54 -08:00
// this is a workaround for https://github.com/Microsoft/msbuild/issues/1622
(arg.StartsWith("/p:RestoreSources=", StringComparison.OrdinalIgnoreCase)) ?
2017-02-01 10:09:54 -08:00
arg.Replace(";", "%3B")
.Replace("://", ":%2F%2F") :
arg;
private static string GetMSBuildExePath()
{
return Path.Combine(
AppContext.BaseDirectory,
MSBuildExeName);
}
private static string GetMSBuildSDKsPath()
{
var envMSBuildSDKsPath = Environment.GetEnvironmentVariable("MSBuildSDKsPath");
if (envMSBuildSDKsPath != null)
{
return envMSBuildSDKsPath;
}
return Path.Combine(
AppContext.BaseDirectory,
SdksDirectoryName);
}
private static string GetRunCscPath()
{
var scriptExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".cmd" : ".sh";
return Path.Combine(AppContext.BaseDirectory, "Roslyn", $"RunCsc{scriptExtension}");
}
}
}