2016-02-02 10:04:50 -08:00
using System ;
using System.IO ;
using System.Runtime.InteropServices ;
2016-03-08 19:32:31 -08:00
using System.Security.Cryptography ;
2016-02-02 10:04:50 -08:00
2016-04-25 13:17:46 -07:00
using Microsoft.DotNet.Cli.Build.Framework ;
2016-02-02 10:04:50 -08:00
namespace Microsoft.DotNet.Cli.Build
{
public static class Utils
{
public static void CleanNuGetTempCache ( )
{
// Clean NuGet Temp Cache on Linux (seeing some issues on Linux)
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) & & Directory . Exists ( "/tmp/NuGet" ) )
{
Directory . Delete ( "/tmp/NuGet" , recursive : true ) ;
}
}
public static string GetOSName ( )
{
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
return "win" ;
}
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
{
return "osx" ;
}
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
{
throw new NotImplementedException ( ) ;
}
else
{
throw new PlatformNotSupportedException ( ) ;
}
}
2016-03-08 19:32:31 -08:00
// Generate a Version 5 (SHA1 Name Based) Guid from a name.
public static Guid GenerateGuidFromName ( string name )
{
// Any fixed GUID will do for a namespace.
Guid namespaceId = new Guid ( "28F1468D-672B-489A-8E0C-7C5B3030630C" ) ;
using ( SHA1 hasher = SHA1 . Create ( ) )
{
var nameBytes = System . Text . Encoding . UTF8 . GetBytes ( name ? ? string . Empty ) ;
var namespaceBytes = namespaceId . ToByteArray ( ) ;
SwapGuidByteOrder ( namespaceBytes ) ;
var streamToHash = new byte [ namespaceBytes . Length + nameBytes . Length ] ;
Array . Copy ( namespaceBytes , streamToHash , namespaceBytes . Length ) ;
Array . Copy ( nameBytes , 0 , streamToHash , namespaceBytes . Length , nameBytes . Length ) ;
var hashResult = hasher . ComputeHash ( streamToHash ) ;
var res = new byte [ 16 ] ;
Array . Copy ( hashResult , res , res . Length ) ;
unchecked { res [ 6 ] = ( byte ) ( 0x50 | ( res [ 6 ] & 0x0F ) ) ; }
unchecked { res [ 8 ] = ( byte ) ( 0x40 | ( res [ 8 ] & 0x3F ) ) ; }
SwapGuidByteOrder ( res ) ;
return new Guid ( res ) ;
}
}
// Do a byte order swap, .NET GUIDs store multi byte components in little
// endian.
private static void SwapGuidByteOrder ( byte [ ] b )
{
Swap ( b , 0 , 3 ) ;
Swap ( b , 1 , 2 ) ;
Swap ( b , 5 , 6 ) ;
Swap ( b , 7 , 8 ) ;
}
private static void Swap ( byte [ ] b , int x , int y )
{
byte t = b [ x ] ;
b [ x ] = b [ y ] ;
b [ y ] = t ;
}
2016-03-15 14:06:54 -07:00
public static void DeleteDirectory ( string path )
{
if ( Directory . Exists ( path ) )
{
string [ ] files = Directory . GetFiles ( path , "*" , SearchOption . AllDirectories ) ;
foreach ( string file in files )
{
File . SetAttributes ( file , FileAttributes . Normal ) ;
File . Delete ( file ) ;
}
2016-04-01 21:52:08 -07:00
var retry = 5 ;
while ( retry > = 0 )
{
try
{
Directory . Delete ( path , true ) ;
return ;
}
catch ( IOException ex )
{
if ( retry = = 0 )
{
throw ;
}
System . Threading . Thread . Sleep ( 200 ) ;
retry - - ;
}
}
2016-03-15 14:06:54 -07:00
}
}
2016-03-15 17:01:50 -07:00
2016-03-16 17:54:44 -07:00
public static void CopyDirectoryRecursively ( string path , string destination , bool keepParentDir = false )
2016-03-15 17:01:50 -07:00
{
2016-03-16 17:54:44 -07:00
if ( keepParentDir )
{
path = path . TrimEnd ( Path . DirectorySeparatorChar ) ;
destination = Path . Combine ( destination , Path . GetFileName ( path ) ) ;
Directory . CreateDirectory ( destination ) ;
}
2016-03-15 17:01:50 -07:00
foreach ( var file in Directory . GetFiles ( path , "*" , SearchOption . AllDirectories ) )
{
string destFile = file . Replace ( path , destination ) ;
Directory . CreateDirectory ( Path . GetDirectoryName ( destFile ) ) ;
File . Copy ( file , destFile , true ) ;
}
}
2016-04-25 13:17:46 -07:00
public static string GetSharedFrameworkVersionFileContent ( BuildTargetContext c )
{
string SharedFrameworkNugetVersion = c . BuildContext . Get < string > ( "SharedFrameworkNugetVersion" ) ;
return $@"{c.BuildContext[" CommitHash "]}{Environment.NewLine}{SharedFrameworkNugetVersion}{Environment.NewLine}" ;
}
public static string GetCliVersionFileContent ( BuildTargetContext c )
{
var buildVersion = c . BuildContext . Get < BuildVersion > ( "BuildVersion" ) ;
var version = buildVersion . NuGetVersion ;
return $@"{c.BuildContext[" CommitHash "]}{Environment.NewLine}{version}{Environment.NewLine}" ;
}
2016-02-02 10:04:50 -08:00
}
}