Merge pull request #1140 from PowerShell/vors/typegen

TypeGen enhancements
This commit is contained in:
Sergei Vorobev
2016-06-17 15:05:48 -07:00
committed by GitHub
5 changed files with 122 additions and 3 deletions
+41
View File
@@ -27,6 +27,7 @@ function Start-PSBuild {
[switch]$Restore,
[string]$Output,
[switch]$ResGen,
[switch]$TypeGen,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$Publish,
@@ -195,6 +196,13 @@ function Start-PSBuild {
}
}
# handle TypeGen
if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs"))
{
log "Run TypeGen (generating CorePsTypeCatalog.cs)"
Start-TypeGen
}
try {
# Relative paths do not work well if cwd is not changed to project
Push-Location $Options.Top
@@ -868,6 +876,39 @@ function Send-GitDiffToSd {
}
}
function Start-TypeGen
{
[CmdletBinding()]
param()
if (!$IsWindows)
{
throw "Start-TypeGen is not supported on non-windows. Use src/TypeCatalogGen/build.sh instead"
}
Push-Location "$PSScriptRoot/src/TypeCatalogParser"
try
{
dotnet restore -v Warning
dotnet run
}
finally
{
Pop-Location
}
Push-Location "$PSScriptRoot/src/TypeCatalogGen"
try
{
dotnet restore -v Warning
dotnet run ../Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs powershell.inc
}
finally
{
Pop-Location
}
}
function Start-ResGen
{
[CmdletBinding()]
@@ -0,0 +1,76 @@
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Globalization;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Cryptography;
namespace System.Management.Automation
{
internal static class AssemblyMetadataHelper
{
/// <summary>
/// Construct the strong assembly name from metadata
/// </summary>
internal static string GetAssemblyStrongName(MetadataReader metadataReader)
{
AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition();
string asmName = metadataReader.GetString(assemblyDefinition.Name);
string asmVersion = assemblyDefinition.Version.ToString();
string asmCulture = metadataReader.GetString(assemblyDefinition.Culture);
asmCulture = (asmCulture == string.Empty) ? "neutral" : asmCulture;
AssemblyHashAlgorithm hashAlgorithm = assemblyDefinition.HashAlgorithm;
BlobHandle blobHandle = assemblyDefinition.PublicKey;
BlobReader blobReader = metadataReader.GetBlobReader(blobHandle);
string publicKeyTokenString = "null";
// Extract public key token only if PublicKey exists in the metadata
if (blobReader.Length > 0)
{
byte[] publickey = blobReader.ReadBytes(blobReader.Length);
HashAlgorithm hashImpl = null;
switch (hashAlgorithm)
{
case AssemblyHashAlgorithm.Sha1:
hashImpl = SHA1.Create();
break;
case AssemblyHashAlgorithm.MD5:
hashImpl = MD5.Create();
break;
case AssemblyHashAlgorithm.Sha256:
hashImpl = SHA256.Create();
break;
case AssemblyHashAlgorithm.Sha384:
hashImpl = SHA384.Create();
break;
case AssemblyHashAlgorithm.Sha512:
hashImpl = SHA512.Create();
break;
default:
throw new NotSupportedException();
}
byte[] publicKeyHash = hashImpl.ComputeHash(publickey);
byte[] publicKeyTokenBytes = new byte[8];
// Note that, the low 8 bytes of the hash of public key in reverse order is the public key tokens.
for (int i = 1; i <= 8; i++)
{
publicKeyTokenBytes[i - 1] = publicKeyHash[publicKeyHash.Length - i];
}
// Convert bytes to hex format strings in lower case.
publicKeyTokenString = BitConverter.ToString(publicKeyTokenBytes).Replace("-", string.Empty).ToLowerInvariant();
}
string strongAssemblyName = string.Format(CultureInfo.InvariantCulture,
"{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
asmName, asmVersion, asmCulture, publicKeyTokenString);
return strongAssemblyName;
}
}
}
@@ -1,3 +1,4 @@
{
"monad/src/CoreCLR/CorePsAssemblyLoadContext.cs": "CoreCLR/CorePsAssemblyLoadContext.cs"
"monad/src/CoreCLR/CorePsAssemblyLoadContext.cs": "CoreCLR/CorePsAssemblyLoadContext.cs",
"monad/src/CoreCLR/AssemblyMetadataHelper.cs": "CoreCLR/AssemblyMetadataHelper.cs"
}
@@ -25,7 +25,9 @@
},
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027",
"System.Runtime.Loader": "4.0.0-rc2-24027"
"System.Runtime.Loader": "4.0.0-rc2-24027",
"System.Reflection.Metadata": "1.1.0",
"System.Security.Cryptography.Algorithms": "4.1.0-rc2-24027"
}
}
}
-1
View File
@@ -1,2 +1 @@
powershell.inc
CorePsTypeCatalog.cs