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 ;
2017-03-16 22:26:26 -05:00
using System.Xml.Linq ;
2016-10-15 18:34:03 -07:00
using Microsoft.DotNet.PlatformAbstractions ;
2016-10-10 17:13:46 -07:00
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public class RepoDirectoriesProvider
{
private static string s_repoRoot ;
2017-03-16 22:26:26 -05:00
private static string s_buildRid ;
2016-10-10 17:13:46 -07:00
private string _artifacts ;
2018-04-02 12:37:25 -07:00
private string _dotnetRoot ;
2016-10-10 17:13:46 -07:00
private string _builtDotnet ;
private string _nugetPackages ;
private string _stage2Sdk ;
2017-04-07 16:36:23 -07:00
private string _stage2WithBackwardsCompatibleRuntimesDirectory ;
2016-10-10 17:13:46 -07:00
private string _testPackages ;
2017-08-29 17:59:34 -07:00
private string _testWorkingFolder ;
2016-10-10 17:13:46 -07:00
public static string RepoRoot
{
get
{
if ( ! string . IsNullOrEmpty ( s_repoRoot ) )
{
return s_repoRoot ;
}
#if NET451
string directory = AppDomain . CurrentDomain . BaseDirectory ;
#else
string directory = AppContext . BaseDirectory ;
#endif
2017-06-19 10:57:38 -07:00
while ( directory ! = null )
2016-10-10 17:13:46 -07:00
{
2017-06-19 10:57:38 -07:00
var gitDirOrFile = Path . Combine ( directory , ".git" ) ;
if ( Directory . Exists ( gitDirOrFile ) | | File . Exists ( gitDirOrFile ) )
{
break ;
}
directory = Directory . GetParent ( directory ) ? . FullName ;
2016-10-10 17:13:46 -07:00
}
if ( directory = = null )
{
throw new Exception ( "Cannot find the git repository root" ) ;
}
s_repoRoot = directory ;
return s_repoRoot ;
}
}
2017-03-20 11:32:49 -05:00
public static string BuildRid
2017-03-16 22:26:26 -05:00
{
get
{
if ( string . IsNullOrEmpty ( s_buildRid ) )
{
2017-09-01 14:04:52 -07:00
var buildInfoPath = Path . Combine ( RepoRoot , "bin" , "obj" , "BuildInfo.props" ) ;
2017-03-16 22:26:26 -05:00
var root = XDocument . Load ( buildInfoPath ) . Root ;
var ns = root . Name . Namespace ;
s_buildRid = root
. Elements ( ns + "PropertyGroup" )
. Elements ( ns + "Rid" )
. FirstOrDefault ( )
? . Value ;
if ( string . IsNullOrEmpty ( s_buildRid ) )
{
throw new InvalidOperationException ( $"Could not find a property named 'Rid' in {buildInfoPath}" ) ;
}
}
return s_buildRid ;
}
}
2016-10-10 17:13:46 -07:00
public string Artifacts = > _artifacts ;
public string BuiltDotnet = > _builtDotnet ;
2018-04-02 12:37:25 -07:00
public string DotnetRoot = > _dotnetRoot ;
2016-10-10 17:13:46 -07:00
public string NugetPackages = > _nugetPackages ;
public string Stage2Sdk = > _stage2Sdk ;
2017-04-07 16:36:23 -07:00
public string Stage2WithBackwardsCompatibleRuntimesDirectory = > _stage2WithBackwardsCompatibleRuntimesDirectory ;
2016-10-10 17:13:46 -07:00
public string TestPackages = > _testPackages ;
2017-08-29 17:59:34 -07:00
public string TestWorkingFolder = > _testWorkingFolder ;
2016-10-10 17:13:46 -07:00
public RepoDirectoriesProvider (
string artifacts = null ,
string builtDotnet = null ,
string nugetPackages = null ,
string corehostPackages = null ,
2017-08-29 17:59:34 -07:00
string corehostDummyPackages = null )
2016-10-10 17:13:46 -07:00
{
2017-08-29 17:59:34 -07:00
// Ideally this wouldn't be hardcoded, so that you could use stage n to build stage n + 1, and then use stage n + 1 to run tests
int previousStage = 2 ;
2017-08-28 16:38:55 -07:00
_artifacts = artifacts ? ? Path . Combine ( RepoRoot ,
2017-09-01 14:04:52 -07:00
"bin" ,
2017-08-29 17:59:34 -07:00
previousStage . ToString ( ) ,
2017-08-28 16:38:55 -07:00
BuildRid ) ;
2016-10-10 17:13:46 -07:00
_builtDotnet = builtDotnet ? ? Path . Combine ( _artifacts , "intermediate" , "sharedFrameworkPublish" ) ;
2018-04-02 12:37:25 -07:00
_dotnetRoot = Path . Combine ( _artifacts , "dotnet" ) ;
2016-10-27 18:46:43 -07:00
_nugetPackages = nugetPackages ? ? Path . Combine ( RepoRoot , ".nuget" , "packages" ) ;
2017-06-20 22:05:44 -07:00
_stage2Sdk = Directory
2017-08-09 22:30:20 -07:00
. EnumerateDirectories ( Path . Combine ( _artifacts , "dotnet" , "sdk" ) )
2017-06-20 22:05:44 -07:00
. First ( d = > ! d . Contains ( "NuGetFallbackFolder" ) ) ;
2017-04-07 16:36:23 -07:00
_stage2WithBackwardsCompatibleRuntimesDirectory =
2017-08-09 22:30:20 -07:00
Path . Combine ( _artifacts , "dotnetWithBackwardsCompatibleRuntimes" ) ;
_testPackages = Environment . GetEnvironmentVariable ( "TEST_PACKAGES" ) ;
if ( string . IsNullOrEmpty ( _testPackages ) )
{
2018-03-30 11:35:19 -07:00
_testPackages = Path . Combine ( _artifacts , "test" , "packages" ) ;
2017-08-09 22:30:20 -07:00
}
2016-10-27 18:46:43 -07:00
2017-08-29 17:59:34 -07:00
_testWorkingFolder = Path . Combine ( RepoRoot ,
2017-09-01 14:04:52 -07:00
"bin" ,
2017-08-29 17:59:34 -07:00
( previousStage + 1 ) . ToString ( ) ,
BuildRid ,
"test" ) ;
2016-10-27 18:46:43 -07:00
}
2016-10-10 17:13:46 -07:00
}
2017-03-02 20:35:20 -08:00
}