forked from EasyHook/EasyHook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
342 lines (276 loc) · 9.39 KB
/
build.ps1
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
param(
[ValidateSet("vs2013", "vs2015", "vs2017", "nupkg-only")]
[Parameter(Position = 0)]
[string] $Target = "vs2015",
[Parameter(Position = 1)]
[string] $AssemblyVersion = "2.8.0.0"
)
$WorkingDir = split-path -parent $MyInvocation.MyCommand.Definition
$EasyHookSln = Join-Path $WorkingDir 'EasyHook.sln'
$VSInstallationPath = $null
function Write-Diagnostic
{
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $Message
)
Write-Host
Write-Host $Message -ForegroundColor Green
Write-Host
}
function DownloadNuget()
{
$Nuget = Join-Path $env:LOCALAPPDATA .\nuget\NuGet.exe
if(-not (Test-Path $Nuget))
{
$Client = New-Object System.Net.WebClient;
$Client.DownloadFile('http://nuget.org/nuget.exe', $Nuget);
}
}
function RestoreNugetPackages()
{
$Nuget = Join-Path $env:LOCALAPPDATA .\nuget\NuGet.exe
Get-Location
. $Nuget restore EasyHook.sln
. $Nuget install MSBuildTasks -Version 1.5.0.196
. $Nuget install vswhere -Version 2.1.3
}
# https://github.com/jbake/Powershell_scripts/blob/master/Invoke-BatchFile.ps1
function Invoke-BatchFile
{
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path,
[Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true)]
[string]$Parameters
)
$tempFile = [IO.Path]::GetTempFileName()
cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" "
Get-Content $tempFile | Foreach-Object {
if ($_ -match "^(.*?)=(.*)$")
{
Set-Content "env:\$($matches[1])" $matches[2]
}
}
Remove-Item $tempFile
}
function Die
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Message
)
Write-Host
Write-Error $Message
exit 1
}
function Warn
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Message
)
Write-Host
Write-Host $Message -ForegroundColor Yellow
Write-Host
}
function TernaryReturn
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[bool] $Yes,
[Parameter(Position = 1, ValueFromPipeline = $true)]
$Value,
[Parameter(Position = 2, ValueFromPipeline = $true)]
$Value2
)
if($Yes) {
return $Value
}
$Value2
}
function Msvs
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Sln = $null,
[ValidateSet('v120', 'v140', 'v141')]
[Parameter(Position = 1, ValueFromPipeline = $true)]
[string] $Toolchain,
[Parameter(Position = 2, ValueFromPipeline = $true)]
[ValidateSet('netfx3.5-Debug', 'netfx3.5-Release', 'netfx4-Debug', 'netfx4-Release')]
[string] $Configuration,
[Parameter(Position = 3, ValueFromPipeline = $true)]
[ValidateSet('Win32', 'x64')]
[string] $Platform
)
Write-Diagnostic "Targeting $Toolchain using configuration $Configuration on platform $Platform"
$VisualStudioVersion = $null
$VXXCommonTools = $null
switch -Exact ($Toolchain) {
'v120' {
$MSBuildExe = join-path -path (Get-ItemProperty "HKLM:\software\Microsoft\MSBuild\ToolsVersions\12.0").MSBuildToolsPath -childpath "msbuild.exe"
$MSBuildExe = $MSBuildExe -replace "Framework64", "Framework"
$VisualStudioVersion = '12.0'
$VXXCommonTools = Join-Path $VSInstallationPath '.\vc'
}
'v140' {
$MSBuildExe = join-path -path (Get-ItemProperty "HKLM:\software\Microsoft\MSBuild\ToolsVersions\14.0").MSBuildToolsPath -childpath "msbuild.exe"
$MSBuildExe = $MSBuildExe -replace "Framework64", "Framework"
$VisualStudioVersion = '14.0'
$VXXCommonTools = Join-Path $VSInstallationPath '.\vc'
}
'v141' {
$MSBuildExe = join-path $VSInstallationPath ".\MSBuild\15.0\Bin\msbuild.exe"
$VisualStudioVersion = '15.0'
$VXXCommonTools = Join-Path $VSInstallationPath '.\vc\auxiliary\build'
}
}
if ($VXXCommonTools -eq $null -or (-not (Test-Path($VXXCommonTools)))) {
Die 'Error unable to find any visual studio environment'
}
$VCVarsAll = Join-Path $VXXCommonTools vcvarsall.bat
if (-not (Test-Path $VCVarsAll)) {
Die "Unable to find $VCVarsAll"
}
# Only configure build environment once
if($env:EASYHOOK_BUILD_IS_BOOTSTRAPPED -eq $null) {
Invoke-BatchFile $VCVarsAll $Platform
$env:EASYHOOK_BUILD_IS_BOOTSTRAPPED = $true
}
$Arch = TernaryReturn ($Platform -eq 'x64') 'x64' 'win32'
$Arguments = @(
"$Sln",
"/t:rebuild",
"/tv:$VisualStudioVersion",
"/p:VisualStudioVersion=$VisualStudioVersion",
"/p:Configuration=$Configuration",
"/p:Platform=$Arch",
"/p:PlatformToolset=$Toolchain",
"/p:PackageDir=.\Deploy;FX35BuildDir=.\Build\netfx3.5-Release\x64;FX4BuildDir=.\Build\netfx4-Release\x64",
"/verbosity:quiet"
)
#$StartInfo = New-Object System.Diagnostics.ProcessStartInfo
#$StartInfo.FileName = $MSBuildExe
#$StartInfo.Arguments = $Arguments
#$StartInfo.EnvironmentVariables.Clear()
#Get-ChildItem -Path env:* | ForEach-Object {
# $StartInfo.EnvironmentVariables.Add($_.Name, $_.Value)
#}
#$StartInfo.UseShellExecute = $false
#$StartInfo.CreateNoWindow = $false
#$StartInfo.RedirectStandardError = $true
#$StartInfo.RedirectStandardOutput = $true
#$Process = New-Object System.Diagnostics.Process
#$Process.StartInfo = $startInfo
#$Process.Start()
#$stdout = $Process.StandardOutput.ReadToEnd()
#$stderr = $Process.StandardError.ReadToEnd()
#$Process.WaitForExit()
#if($Process.ExitCode -ne 0)
#{
# Write-Host "stdout: $stdout"
# Write-Host "stderr: $stderr"
# Die "Build failed"
#}
&$MSBuildExe $Arguments
if (!$?) {
Die "Build failed"
}
}
function FindVS
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Toolchain
)
$VSWhere = ".\vswhere.2.1.3\tools\vswhere.exe"
# use vswhere to locate toolchain dir
if ($Toolchain -eq 'v120') {
$script:VSInstallationPath = . $VSWhere -legacy -version "[12.0,13.0)" -property installationPath
}
if ($Toolchain -eq 'v140') {
$script:VSInstallationPath = . $VSWhere -legacy -version "[14.0,15.0)" -property installationPath
}
if ($Toolchain -eq 'v141') {
$script:VSInstallationPath = . $VSWhere -version "[15.0,16.0)" -property installationPath
}
}
function VSX
{
param(
[ValidateSet('v120', 'v140', 'v141')]
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Toolchain
)
FindVS "$Toolchain"
if ($script:VSInstallationPath -eq $null) {
Warn "Toolchain $Toolchain is not installed on your development machine, skipping build."
Return
}
Write-Diagnostic "Visual Studio Installation path: $VSInstallationPath"
Write-Diagnostic "Starting build targeting toolchain $Toolchain"
Msvs "$EasyHookSln" "$Toolchain" 'netfx3.5-Release' 'x64'
Msvs "$EasyHookSln" "$Toolchain" 'netfx3.5-Release' 'Win32'
Msvs "$EasyHookSln" "$Toolchain" 'netfx4-Release' 'x64'
Msvs "$EasyHookSln" "$Toolchain" 'netfx4-Release' 'Win32'
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx3.5-Debug;Platform=x64" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx3.5-Debug;Platform=Win32" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx3.5-Release;Platform=x64" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx3.5-Release;Platform=Win32" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx4-Debug;Platform=x64" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx4-Debug;Platform=Win32" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx4-Release;Platform=x64" />
# <MSBuild Projects="EasyHook.sln" Properties="Configuration=netfx4-Release;Platform=Win32" />
Write-Diagnostic "Finished build targeting toolchain $Toolchain"
}
function WriteAssemblyVersionForFile
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $File
)
#$Regex = 'public const string AssemblyVersion = "(.*)"';
$Regex = '\[assembly: AssemblyVersion\("(.*)"\)\]'
$AssemblyInfo = Get-Content $File
$NewString = $AssemblyInfo -replace $Regex, "[assembly: AssemblyVersion(""$AssemblyVersion"")]"
$NewString | Set-Content $File -Encoding UTF8
}
function WriteAssemblyVersion
{
param()
$Filename = Join-Path $WorkingDir EasyHook\Properties\AssemblyInfo.cs
Write-Diagnostic "$Filename"
WriteAssemblyVersionForFile "$Filename"
$Filename = Join-Path $WorkingDir EasyLoad\Properties\AssemblyInfo.cs
WriteAssemblyVersionForFile "$Filename"
}
function Nupkg
{
}
Write-Diagnostic "EasyHook version = $AssemblyVersion"
DownloadNuget
RestoreNugetPackages
WriteAssemblyVersion
switch -Exact ($Target)
{
"nupkg-only"
{
Nupkg
}
"vs2013"
{
VSX v120
Nupkg
}
"vs2015"
{
VSX v140
Nupkg
}
"vs2017"
{
VSX v141
Nupkg
}
}