-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathCreateAssemblyGenerator.ps1
More file actions
111 lines (94 loc) · 3.42 KB
/
CreateAssemblyGenerator.ps1
File metadata and controls
111 lines (94 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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