2016-06-17 16:16:09 -07:00
|
|
|
|
// 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;
|
2016-10-29 12:08:52 -07:00
|
|
|
|
using System.Linq;
|
2016-10-31 16:16:39 -07:00
|
|
|
|
using System.Reflection;
|
2016-09-23 10:40:25 -07:00
|
|
|
|
using System.Runtime.InteropServices;
|
2016-09-22 19:11:08 -05:00
|
|
|
|
using Microsoft.DotNet.Cli;
|
2016-11-15 11:56:39 -08:00
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
2016-06-17 16:16:09 -07:00
|
|
|
|
|
2016-09-22 19:11:08 -05:00
|
|
|
|
namespace Microsoft.DotNet.Tools.MSBuild
|
2016-06-17 16:16:09 -07:00
|
|
|
|
{
|
|
|
|
|
public class MSBuildForwardingApp
|
|
|
|
|
{
|
2016-10-31 16:16:39 -07:00
|
|
|
|
internal const string TelemetrySessionIdEnvironmentVariableName = "DOTNET_CLI_TELEMETRY_SESSIONID";
|
|
|
|
|
|
2016-11-23 23:54:44 -08:00
|
|
|
|
private const string MSBuildExeName = "MSBuild.dll";
|
|
|
|
|
|
2016-12-04 22:31:58 -08:00
|
|
|
|
private const string SdksDirectoryName = "Sdks";
|
2016-10-29 12:08:52 -07:00
|
|
|
|
|
2016-06-17 16:16:09 -07:00
|
|
|
|
private readonly ForwardingApp _forwardingApp;
|
|
|
|
|
|
2016-10-29 12:08:52 -07:00
|
|
|
|
private readonly Dictionary<string, string> _msbuildRequiredEnvironmentVariables =
|
|
|
|
|
new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "MSBuildExtensionsPath", AppContext.BaseDirectory },
|
2016-11-23 23:54:44 -08:00
|
|
|
|
{ "CscToolExe", GetRunCscPath() },
|
|
|
|
|
{ "MSBuildSDKsPath", GetMSBuildSDKsPath() }
|
2016-10-29 12:08:52 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly IEnumerable<string> _msbuildRequiredParameters =
|
2016-11-15 11:56:39 -08:00
|
|
|
|
new List<string> { "/m", "/v:m" };
|
2016-10-29 12:08:52 -07:00
|
|
|
|
|
2016-08-10 23:45:30 -07:00
|
|
|
|
public MSBuildForwardingApp(IEnumerable<string> argsToForward)
|
2016-06-17 16:16:09 -07:00
|
|
|
|
{
|
2016-10-31 16:16:39 -07:00
|
|
|
|
if (Telemetry.CurrentSessionId != null)
|
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Type loggerType = typeof(MSBuildLogger);
|
|
|
|
|
|
2017-01-31 16:47:41 -08:00
|
|
|
|
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
|
|
|
|
|
}
|
2016-10-31 16:16:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-17 16:16:09 -07:00
|
|
|
|
_forwardingApp = new ForwardingApp(
|
|
|
|
|
GetMSBuildExePath(),
|
2017-02-01 14:12:34 -08:00
|
|
|
|
_msbuildRequiredParameters.Concat(argsToForward.Select(Escape)),
|
2016-10-29 12:08:52 -07:00
|
|
|
|
environmentVariables: _msbuildRequiredEnvironmentVariables);
|
2016-06-17 16:16:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Execute()
|
|
|
|
|
{
|
2016-10-31 16:16:39 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Environment.SetEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, Telemetry.CurrentSessionId);
|
|
|
|
|
|
|
|
|
|
return _forwardingApp.Execute();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Environment.SetEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, null);
|
|
|
|
|
}
|
2016-06-17 16:16:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 11:56:39 -08:00
|
|
|
|
internal static CommandOption AddVerbosityOption(CommandLineApplication app)
|
|
|
|
|
{
|
2016-12-16 21:09:58 -08:00
|
|
|
|
return app.Option("-v|--verbosity", LocalizableStrings.VerbosityOptionDescription, CommandOptionType.SingleValue);
|
2016-11-15 11:56:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 16:47:41 -08:00
|
|
|
|
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
|
2017-01-31 16:47:41 -08:00
|
|
|
|
(arg.StartsWith("/p:RestoreSources=", StringComparison.OrdinalIgnoreCase)) ?
|
2017-02-01 10:09:54 -08:00
|
|
|
|
arg.Replace(";", "%3B")
|
|
|
|
|
.Replace("://", ":%2F%2F") :
|
2017-01-31 16:47:41 -08:00
|
|
|
|
arg;
|
|
|
|
|
|
2016-06-17 16:16:09 -07:00
|
|
|
|
private static string GetMSBuildExePath()
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(
|
|
|
|
|
AppContext.BaseDirectory,
|
2016-11-23 23:54:44 -08:00
|
|
|
|
MSBuildExeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetMSBuildSDKsPath()
|
|
|
|
|
{
|
2016-12-01 14:45:55 -08:00
|
|
|
|
var envMSBuildSDKsPath = Environment.GetEnvironmentVariable("MSBuildSDKsPath");
|
|
|
|
|
|
|
|
|
|
if (envMSBuildSDKsPath != null)
|
|
|
|
|
{
|
|
|
|
|
return envMSBuildSDKsPath;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 23:54:44 -08:00
|
|
|
|
return Path.Combine(
|
|
|
|
|
AppContext.BaseDirectory,
|
2016-12-04 22:31:58 -08:00
|
|
|
|
SdksDirectoryName);
|
2016-06-17 16:16:09 -07:00
|
|
|
|
}
|
2016-09-21 15:24:54 -07:00
|
|
|
|
|
|
|
|
|
private static string GetRunCscPath()
|
|
|
|
|
{
|
2016-09-23 10:40:25 -07:00
|
|
|
|
var scriptExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".cmd" : ".sh";
|
2017-01-05 14:56:11 -08:00
|
|
|
|
return Path.Combine(AppContext.BaseDirectory, "Roslyn", $"RunCsc{scriptExtension}");
|
2016-09-21 15:24:54 -07:00
|
|
|
|
}
|
2016-06-17 16:16:09 -07:00
|
|
|
|
}
|
|
|
|
|
}
|