2016-10-27 18:46:43 -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 ;
using System.Linq ;
2016-10-31 16:22:10 -07:00
using System.Reflection ;
2016-10-27 18:46:43 -07:00
using System.Runtime.CompilerServices ;
2016-10-31 16:22:10 -07:00
using System.Text ;
2016-10-27 18:46:43 -07:00
using System.Threading.Tasks ;
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.Tools.Common ;
namespace Microsoft.DotNet.TestFramework
{
public class TestAssetInstance
{
private TestAssetInfo _testAssetInfo ;
private DirectoryInfo _root ;
public DirectoryInfo Root
{
get
{
return _root ;
}
}
public TestAssetInstance ( TestAssetInfo testAssetInfo , DirectoryInfo root )
{
_testAssetInfo = testAssetInfo ;
_root = root ;
if ( root . Exists )
{
root . Delete ( recursive : true ) ;
}
root . Create ( ) ;
}
public TestAssetInstance WithSourceFiles ( )
{
var filesToCopy = _testAssetInfo . GetSourceFiles ( ) ;
CopyFiles ( filesToCopy ) ;
return this ;
}
public TestAssetInstance WithRestoreFiles ( )
{
var filesToCopy = _testAssetInfo . GetRestoreFiles ( ) ;
CopyFiles ( filesToCopy ) ;
return this ;
}
public TestAssetInstance WithBuildFiles ( )
{
var filesToCopy = _testAssetInfo . GetBuildFiles ( ) ;
CopyFiles ( filesToCopy ) ;
return this ;
}
2016-10-31 16:22:10 -07:00
public TestAssetInstance WithNuGetConfig ( string nugetCache )
{
var thisAssembly = typeof ( TestAssetInstance ) . GetTypeInfo ( ) . Assembly ;
var newNuGetConfigPath = Path . Combine ( Root . FullName , "NuGet.config" ) ;
using ( var resource = thisAssembly . GetManifestResourceStream ( "NuGet.template.config" ) )
{
var streamReader = new StreamReader ( resource ) ;
var content = streamReader . ReadToEnd ( ) ;
content = content . Replace ( "$fullpath$" , nugetCache ) ;
using ( var newNuGetConfig = new FileStream ( newNuGetConfigPath , FileMode . Create , FileAccess . Write ) )
{
var contentBytes = new UTF8Encoding ( true ) . GetBytes ( content ) ;
newNuGetConfig . Write ( contentBytes , 0 , contentBytes . Length ) ;
}
}
return this ;
}
2016-10-27 18:46:43 -07:00
private void CopyFiles ( IEnumerable < FileInfo > filesToCopy )
{
foreach ( var file in filesToCopy )
{
var relativePath = file . FullName . Substring ( _testAssetInfo . Root . FullName . Length + 1 ) ;
var newPath = Path . Combine ( Root . FullName , relativePath ) ;
var newFile = new FileInfo ( newPath ) ;
PathUtility . EnsureDirectory ( newFile . Directory . FullName ) ;
file . CopyTo ( newPath ) ;
}
}
}
}