2017-03-02 21:04:03 -08: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.
2016-10-10 17:13:46 -07:00
using System ;
using System.IO ;
using System.Linq ;
2024-02-05 16:27:06 -08:00
using System.Reflection ;
2018-10-29 11:26:53 -07:00
using System.Runtime.InteropServices ;
2018-11-29 21:07:50 -08:00
using FluentAssertions ;
2016-10-10 17:13:46 -07:00
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public class RepoDirectoriesProvider
{
2018-10-29 11:26:53 -07:00
public readonly static string RepoRoot ;
public readonly static string TestWorkingFolder ;
public readonly static string DotnetUnderTest ;
2018-11-29 21:07:50 -08:00
public readonly static string DotnetRidUnderTest ;
2018-12-25 19:08:40 -08:00
public readonly static string Stage2AspNetCore ;
2016-10-10 17:13:46 -07:00
2018-10-29 11:26:53 -07:00
static RepoDirectoriesProvider ( )
2016-10-10 17:13:46 -07:00
{
#if NET451
2018-10-29 11:26:53 -07:00
string directory = AppDomain . CurrentDomain . BaseDirectory ;
2016-10-10 17:13:46 -07:00
#else
2018-10-29 11:26:53 -07:00
string directory = AppContext . BaseDirectory ;
2016-10-10 17:13:46 -07:00
#endif
2018-10-29 11:26:53 -07:00
while ( directory ! = null )
{
var gitDirOrFile = Path . Combine ( directory , ".git" ) ;
if ( Directory . Exists ( gitDirOrFile ) | | File . Exists ( gitDirOrFile ) )
2016-10-10 17:13:46 -07:00
{
2018-10-29 11:26:53 -07:00
break ;
2016-10-10 17:13:46 -07:00
}
2018-10-29 11:26:53 -07:00
directory = Directory . GetParent ( directory ) ? . FullName ;
}
2016-10-10 17:13:46 -07:00
2018-10-29 11:26:53 -07:00
RepoRoot = directory ;
2016-10-10 17:13:46 -07:00
2018-10-29 11:26:53 -07:00
TestWorkingFolder = Environment . GetEnvironmentVariable ( "CORESDK_TEST_FOLDER" ) ;
if ( string . IsNullOrEmpty ( TestWorkingFolder ) )
{
TestWorkingFolder = Path . Combine ( AppContext . BaseDirectory , "Tests" ) ;
2016-10-10 17:13:46 -07:00
}
2018-10-29 11:26:53 -07:00
DotnetUnderTest = Environment . GetEnvironmentVariable ( "DOTNET_UNDER_TEST" ) ;
string dotnetExtension = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ".exe" : "" ;
if ( string . IsNullOrEmpty ( DotnetUnderTest ) )
2017-03-16 22:26:26 -05:00
{
2018-10-29 11:26:53 -07:00
if ( RepoRoot = = null )
2017-03-16 22:26:26 -05:00
{
2018-10-29 11:26:53 -07:00
DotnetUnderTest = "dotnet" + dotnetExtension ;
}
else
{
2024-02-05 16:27:06 -08:00
// https://stackoverflow.com/a/60545278/294804
var assemblyConfigurationAttribute = typeof ( RepoDirectoriesProvider ) . Assembly . GetCustomAttribute < AssemblyConfigurationAttribute > ( ) ;
string configuration = assemblyConfigurationAttribute ? . Configuration ;
2018-10-29 12:26:31 -07:00
DotnetUnderTest = Path . Combine ( RepoRoot , "artifacts" , "bin" , "redist" , configuration , "dotnet" , "dotnet" + dotnetExtension ) ;
2017-03-16 22:26:26 -05:00
}
}
2018-11-29 21:07:50 -08:00
2024-02-05 16:27:06 -08:00
string AspNetCoreDir = Path . Combine ( Path . GetDirectoryName ( DotnetUnderTest ) , "shared" , "Microsoft.AspNetCore.App" ) ;
2018-12-14 20:08:38 -08:00
if ( Directory . Exists ( AspNetCoreDir ) )
{
2024-02-05 16:27:06 -08:00
Stage2AspNetCore = Directory . EnumerateDirectories ( AspNetCoreDir ) . First ( ) ;
2018-12-14 20:08:38 -08:00
}
2018-11-29 21:07:50 -08:00
// TODO: Resolve dotnet folder even if DotnetUnderTest doesn't have full path
var sdkFolders = Directory . GetDirectories ( Path . Combine ( Path . GetDirectoryName ( DotnetUnderTest ) , "sdk" ) ) ;
sdkFolders . Length . Should ( ) . Be ( 1 , "Only one SDK folder is expected in the layout" ) ;
var sdkFolder = sdkFolders . Single ( ) ;
var versionFile = Path . Combine ( sdkFolder , ".version" ) ;
var lines = File . ReadAllLines ( versionFile ) ;
DotnetRidUnderTest = lines [ 2 ] . Trim ( ) ;
2017-03-16 22:26:26 -05:00
}
2016-10-10 17:13:46 -07:00
}
2017-03-02 20:35:20 -08:00
}