Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CreateAssemblyGenerator.exe by CreateAssemblyGenerator.ps1 #68

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Build/Build.ssmssqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@
<AssociatedConnUserName />
<FullPath>ChangeSigningKey_ReadMe.txt</FullPath>
</FileNode>
<FileNode Name="CreateAssemblyGenerator.exe">
<FileNode Name="CreateAssemblyGenerator.ps1">
<AssociatedConnectionMoniker />
<AssociatedConnSrvName />
<AssociatedConnUserName />
<FullPath>CreateAssemblyGenerator.exe</FullPath>
<FullPath>CreateAssemblyGenerator.ps1</FullPath>
</FileNode>
<FileNode Name="Install the tSQLt build.docx">
<AssociatedConnectionMoniker />
Expand Down
Binary file removed Build/CreateAssemblyGenerator.exe
Binary file not shown.
111 changes: 111 additions & 0 deletions Build/CreateAssemblyGenerator.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
param([Parameter(Mandatory = $true)][string]$dllPath,
[Parameter(Mandatory = $true)][string]$templatePath,
[Parameter(Mandatory = $true)][string]$assemblyPattern,
[string]$thumbPrintPattern,
[int]$maxLength = 0,
[string]$separator = "'+$( [Environment]::NewLine )'")

$ErrorActionPreference = 'Stop'

class Generator
{
static
[string]
GetHexStringFromBytes([byte[]]$dllBytes)
{
$stringBuilder = [System.Text.StringBuilder]::new('0x', $dllBytes.Length * 2 + 2)

foreach ($dllByte in $dllBytes)
{
$stringBuilder.Append($dllByte.ToString('X2'))
}

return $stringBuilder.ToString()
}

static
[byte[]]
GetBytesFromDll([string]$dllPath)
{
return (Get-Content -Path $dllPath -AsByteStream -Raw)
}

static
[string]
GetTemplateFile([string]$templatePath)
{
return (Get-Content -Path $templatePath -Raw)
}

static
[string]
GetTemplate([string]$templatePath,
[string]$assemblyPattern,
[string]$thumbPrintPattern)
{
$template = [Generator]::GetTemplateFile($templatePath)
if ($template -notmatch $assemblyPattern)
{
throw "The specified template file [" + $templatePath + "] did not contain the specified assembly pattern [" + $assemblyPattern + "]!"
}

if (![string]::IsNullOrEmpty($thumbPrintPattern) -and $template -notmatch $thumbPrintPattern)
{
throw "The specified template file [" + $templatePath + "] did not contain the specified thumbprint pattern [" + $thumbPrintPattern + "]!"
}
return $template
}

static
[byte[]]
GetAssemblyBytes([string]$dllPath)
{
$bytesFromDll = [Generator]::GetBytesFromDll($dllPath)
if ($bytesFromDll.Length -eq 0)
{
throw "The DLL specified [" + $dllPath + "] for generating the CREATE ASSEMBLY statement was empty"
}
return $bytesFromDll
}

static
[string]
Wrap([string]$hexString,
[int] $maxLength,
[string] $separator)
{
if ($maxLength -le 0 -or $hexString.Length -le $maxLength)
{
return $hexString
}

$separator = $separator -replace '\\n', [Environment]::NewLine
$stringBuilder = [System.Text.StringBuilder]::new($hexString.Length + [int][Math]::Floor($hexString.Length / $maxLength) * $separator.Length)

$stringBuilder.Append($hexString, 0, $maxLength);
for ($i = $maxLength; $i -lt $hexString.Length; $i += $maxLength) {
$stringBuilder.Append($separator)
$stringBuilder.Append($hexString, $i,[Math]::Min($maxLength, $hexString.Length - $i))
}

return $stringBuilder.ToString()
}
}

$template = [Generator]::GetTemplate($templatePath, $assemblyPattern, $thumbPrintPattern)
$assemblyBytes = [Generator]::GetAssemblyBytes($dllPath)

if (![string]::IsNullOrEmpty($thumbPrintPattern))
{
$thumbPrint = [Generator]::GetHexStringFromBytes([System.Reflection.Assembly]::Load($assemblyBytes).GetName().GetPublicKeyToken())
$template = $template -replace $thumbPrintPattern, $thumbPrint
}

if (![string]::IsNullOrEmpty($assemblyPattern))
{
$hexString = [Generator]::GetHexStringFromBytes($assemblyBytes)
$assembly = [Generator]::Wrap($hexString, $maxLength, $separator)
$template = $template -replace $assemblyPattern, $assembly
}

Write-Host $template
102 changes: 81 additions & 21 deletions Build/tSQLt.build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
<project name="tSQLt" default="all" basedir=".">
<description>Build tSQLt</description>

<condition property="powershell.exe" value="powershell" else="pwsh">
<os family="windows"/>
</condition>

<target name="validate.parameters" description="">
<!-- no parameters at this point-->
<fail message="Parameter '${powershell.exe}' is required">
<condition>
<not>
<and>
<isset property="${powershell.exe}"/>
</and>
</not>
</condition>
</fail>
</target>

<target
Expand Down Expand Up @@ -112,24 +124,56 @@

<target name="package.construct.tsqlt">
<echo message="Generating CREATE ASSEMBLY statement."/>
<exec executable="cmd" dir="." failonerror="true" output="temp/CreateAssembly.sql">
<arg value="/c"/>
<arg value="CreateAssemblyGenerator.exe output/tSQLtCLR/tSQLtCLR.dll ../Source/tSQLtCLR.mdl.sql 0x000000 &quot;&quot; 200 &quot;'+\n'&quot;"/>
</exec>
<exec executable="cmd" dir="." failonerror="true" >
<arg value="/c"/>
<arg value="type temp\CreateAssembly.sql"/>
<exec executable="${powershell.exe}" dir="." failonerror="true" output="temp/CreateAssembly.sql" logError="true">
<arg value="-ExecutionPolicy"/>
<arg value="RemoteSigned"/>
<arg value="-File"/>
<arg value="CreateAssemblyGenerator.ps1"/>
<arg value="-dllPath"/>
<arg value="output/tSQLtCLR/tSQLtCLR.dll"/>
<arg value="-templatePath"/>
<arg value="../Source/tSQLtCLR.mdl.sql"/>
<arg value="-assemblyPattern"/>
<arg value="0x000000"/>
<arg value="-thumbprintPattern"/>
<arg value=""/>
<arg value="-maxLength"/>
<arg value="200"/>
<arg value="-separator"/>
<arg value="'+\n'"/>
</exec>
<concat>
<filelist dir="." files="temp\CreateAssembly.sql"/>
<filterchain>
<prefixlines prefix=" "/>
</filterchain>
</concat>

<echo message="Generating GetAssemblyKeyBytes function."/>
<exec executable="cmd" dir="." failonerror="true" output="output/tSQLt.Private_GetAssemblyKeyBytes.sql">
<arg value="/c"/>
<arg value="CreateAssemblyGenerator.exe output/tSQLtCLR/tSQLtAssemblyKey.dll ../Source/tSQLt.Private_GetAssemblyKeyBytes.mdl.sql 0x000000 0x000001 200 &quot;+\n0x&quot;"/>
</exec>
<exec executable="cmd" dir="." failonerror="true" >
<arg value="/c"/>
<arg value="type output\tSQLt.Private_GetAssemblyKeyBytes.sql"/>
<exec executable="${powershell.exe}" dir="." failonerror="true" output="output/tSQLt.Private_GetAssemblyKeyBytes.sql" logError="true">
<arg value="-ExecutionPolicy"/>
<arg value="RemoteSigned"/>
<arg value="-File"/>
<arg value="CreateAssemblyGenerator.ps1"/>
<arg value="-dllPath"/>
<arg value="output/tSQLtCLR/tSQLtAssemblyKey.dll"/>
<arg value="-templatePath"/>
<arg value="../Source/tSQLt.Private_GetAssemblyKeyBytes.mdl.sql"/>
<arg value="-assemblyPattern"/>
<arg value="0x000000"/>
<arg value="-thumbprintPattern"/>
<arg value="0x000001"/>
<arg value="-maxLength"/>
<arg value="200"/>
<arg value="-separator"/>
<arg value="+\n0x"/>
</exec>
<concat>
<filelist dir="." files="output/tSQLt.Private_GetAssemblyKeyBytes.sql"/>
<filterchain>
<prefixlines prefix=" "/>
</filterchain>
</concat>

<exec executable="cmd" dir="." failonerror="true">
<arg value="/c"/>
Expand Down Expand Up @@ -291,15 +335,31 @@

<target name="package.test.files">
<echo message="scripting tSQLtTestUtilCLR.dll" />
<exec executable="cmd" dir="." failonerror="true" output="temp/CreateTestUtilAssembly.sql">
<arg value="/c"/>
<arg value="CreateAssemblyGenerator.exe output/tSQLtCLR/tSQLtTestUtilCLR.dll ../TestUtil/tSQLtTestUtilCLR.mdl.sql 0x000000"/>
<exec executable="${powershell.exe}" dir="." failonerror="true" output="temp/CreateTestUtilAssembly.sql" logError="true">
<arg value="-ExecutionPolicy"/>
<arg value="RemoteSigned"/>
<arg value="-File"/>
<arg value="CreateAssemblyGenerator.ps1"/>
<arg value="-dllPath"/>
<arg value="output/tSQLtCLR/tSQLtTestUtilCLR.dll"/>
<arg value="-templatePath"/>
<arg value="../TestUtil/tSQLtTestUtilCLR.mdl.sql"/>
<arg value="-assemblyPattern"/>
<arg value="0x000000"/>
</exec>

<echo message="scripting UnsignedEmpty.dll" />
<exec executable="cmd" dir="." failonerror="true" output="temp/GetUnsignedEmptyBytes.sql">
<arg value="/c"/>
<arg value="CreateAssemblyGenerator.exe output/tSQLtCLR/UnsignedEmpty.dll ../TestUtil/GetUnsignedEmptyBytes.mdl.sql 0x000000"/>
<exec executable="${powershell.exe}" dir="." failonerror="true" output="temp/GetUnsignedEmptyBytes.sql" logError="true">
<arg value="-ExecutionPolicy"/>
<arg value="RemoteSigned"/>
<arg value="-File"/>
<arg value="CreateAssemblyGenerator.ps1"/>
<arg value="-dllPath"/>
<arg value="output/tSQLtCLR/UnsignedEmpty.dll"/>
<arg value="-templatePath"/>
<arg value="../TestUtil/GetUnsignedEmptyBytes.mdl.sql"/>
<arg value="-assemblyPattern"/>
<arg value="0x000000"/>
</exec>

<echo message="Building TestUtil.sql" />
Expand Down