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-10-19 12:43:21 -07:00
private const string s_msbuildExeName = "MSBuild.dll" ;
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 } ,
{ "CscToolExe" , GetRunCscPath ( ) }
} ;
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 ) ;
2016-11-02 13:09:50 -07:00
argsToForward = argsToForward . Concat ( new [ ]
{
2016-11-02 14:43:21 -07:00
$"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}"
2016-11-02 13:09:50 -07:00
} ) ;
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 ( ) ,
2016-10-29 12:08:52 -07:00
_msbuildRequiredParameters . Concat ( argsToForward ) ,
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 )
{
return app . Option ( "-v|--verbosity" , "Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]" , CommandOptionType . SingleValue ) ;
}
2016-06-17 16:16:09 -07:00
private static string GetMSBuildExePath ( )
{
return Path . Combine (
AppContext . BaseDirectory ,
s_msbuildExeName ) ;
}
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" ;
2016-09-21 15:24:54 -07:00
return Path . Combine ( AppContext . BaseDirectory , $"RunCsc{scriptExtension}" ) ;
}
2016-06-17 16:16:09 -07:00
}
}