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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
# When using System.IO.Compression.ZipFile.CreateFromDirectory in PowerShell, it still uses backslashes in the zip paths
# despite this https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/mitigation-ziparchiveentry-fullname-path-separator
# Based upon post by Seth Jackson https://sethjackson.github.io/2016/12/17/path-separators/
#
# PowerShell 5 (WMF5) & 6
# Using class Keyword https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Classes
#
# https://gist.github.com/lantrix/738ebfa616d5222a8b1db947793bc3fc
#
####################################
### Fix Zip slash ###
####################################
Add-Type -AssemblyName System.Text.Encoding
Add-Type -AssemblyName System.IO.Compression.FileSystem
class FixedEncoder : System.Text.UTF8Encoding {
FixedEncoder() : base($true) { }
[byte[]] GetBytes([string] $s)
{
$s = $s.Replace("\", "/");
return ([System.Text.UTF8Encoding]$this).GetBytes($s);
}
}
function wixCleanUpElement([System.Xml.XmlElement]$element, [string]$rootPath)
{
$files = Get-ChildItem -Path $rootPath -File -Force -ErrorAction SilentlyContinue
$folders = Get-ChildItem -Path $rootPath -Directory -Force -ErrorAction SilentlyContinue
# Removing not existant files
foreach($e in $element.Component)
{
$found = $false
foreach($file in $files)
{
if($e.File.Source.EndsWith("\$($file.Name)")){
$found = $true
break
}
}
if ($found -eq $false){
# File not found on this directory, remove element
Write-Host("WIX: File '$($e.File.Source)' does not exist anymore, removing...")
$e.ParentNode.RemoveChild($e) | Out-Null
}
}
# Adding not existant files
foreach($file in $files)
{
$found = $false
foreach($e in $element.Component)
{
if($null -ne $e.File.Source -and $e.File.Source.EndsWith("\$($file.Name)")){
$found = $true
break
}
}
if ($found -eq $false){
# File not found on manifest, add element
$guid = [guid]::NewGuid().ToString().ToUpper()
$guidNoSeparator = $guid.Replace('-', '')
$filePath = $file.FullName.Substring($msiSourceFiles.Length)
while ($filePath.StartsWith('\'))
{
$filePath = $filePath.Remove(0, 1)
}
Write-Host("WIX: File '$filePath' does not exist on manifest, adding with GUID: $guid")
$xmlComponent = $element.OwnerDocument.CreateElement("Component", $element.OwnerDocument.DocumentElement.NamespaceURI)
$xmlComponent.SetAttribute("Id", "owc$guidNoSeparator")
$xmlComponent.SetAttribute("Guid", $guid)
$xmlFile = $element.OwnerDocument.CreateElement("File", $element.OwnerDocument.DocumentElement.NamespaceURI)
$xmlFile.SetAttribute("Id", "owf$guidNoSeparator")
$xmlFile.SetAttribute("Source", "`$(var.SourceDir)\$filePath")
$xmlFile.SetAttribute("KeyPath", 'yes')
$xmlComponent.AppendChild($xmlFile) | Out-Null
$element.AppendChild($xmlComponent) | Out-Null
}
}
# Removing not existant folters
foreach($e in $element.Directory)
{
$found = $false
foreach($folder in $folders)
{
if($e.Name -eq $folder.Name){
$found = $true
break
}
}
if ($found -eq $false){
# Directory not found on this directory, remove element
Write-Host("WIX: Directory '$($e.Name)' does not exist anymore, removing...")
$e.ParentNode.RemoveChild($e) | Out-Null
}
}
# Adding not existant directories
foreach($folder in $folders)
{
$foundDirectoryElement = $null
foreach($e in $element.Directory)
{
#Write-Host("$($e.Name) == $($folder.Name)")
if($null -ne $e.Name -and $e.Name -eq $folder.Name){
$foundDirectoryElement = $e
break
}
}
if ($null -eq $foundDirectoryElement){
# Folder not found on manifest, add element
$guid = [guid]::NewGuid().ToString().ToUpper()
$guidNoSeparator = $guid.Replace('-', '')
$directoryRelativePath = $folder.FullName.Substring($msiSourceFiles.Length)
while ($directoryRelativePath.StartsWith('\'))
{
$directoryRelativePath = $directoryRelativePath.Remove(0, 1)
}
Write-Host("WIX: Directory '$directoryRelativePath' does not exist on manifest, adding with GUID: $guid")
$xmlDirectory = $element.OwnerDocument.CreateElement("Directory", $element.OwnerDocument.DocumentElement.NamespaceURI)
$xmlDirectory.SetAttribute("Id", "owd$guidNoSeparator")
$xmlDirectory.SetAttribute("Name", $folder.Name)
$element.AppendChild($xmlDirectory) | Out-Null
wixCleanUpElement $xmlDirectory $folder.FullName
}else{
# Folder found on manifest, recursive from this folder
wixCleanUpElement $foundDirectoryElement $folder.FullName
}
}
}
<#
$msiComponentsXml = [Xml] (Get-Content $msiComponentsFile)
foreach($element in $msiComponentsXml.Wix.Module.Directory.Directory)
{
if($element.Id -eq 'MergeRedirectFolder')
{
wixCleanUpElement $element $msiSourceFiles
#WriteXmlToScreen($msiComponentsXml);
#$msiComponentsXml.Save("$rootFolder\$publishFolder\test.xml")
return
break
}
}
#>
function WriteXmlToScreen([xml]$xml)
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
# Script working directory
Set-Location $PSScriptRoot\..
####################################
### Configuration ###
####################################
$enableMSI = $true
#$buildOnly = 'win-x64'
#$buildOnly = 'linux-x64'
#$buildOnly = 'osx-x64'
#$buildOnly = 'osx-arm64'
$zipPackages = $true
#$enableNugetPublish = $true
# Profilling
$stopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$deployStopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$stopWatch.Start()
# Variables
$software = "UVtools"
$project = "UVtools.WPF"
$buildWith = "Release"
$netVersion = "6.0"
$rootFolder = $(Get-Location)
$buildFolder = "$rootFolder\build"
$releaseFolder = "$project\bin\$buildWith\net$netVersion"
$objFolder = "$project\obj\$buildWith\net$netVersion"
$publishFolder = "publish"
$platformsFolder = "$buildFolder\platforms"
$changelogFile = "$rootFolder\CHANGELOG.md"
#$version = (Get-Command "$releaseFolder\UVtools.dll").FileVersionInfo.ProductVersion
$projectXml = [Xml] (Get-Content "$project\$project.csproj")
$version = "$($projectXml.Project.PropertyGroup.Version)".Trim();
if([string]::IsNullOrWhiteSpace($version)){
Write-Error "Can not detect the UVtools version, does $project\$project.csproj exists?"
exit
}
# MSI Variables
$installers = @("UVtools.InstallerMM", "UVtools.Installer")
$msiOutputFile = "$rootFolder\UVtools.Installer\bin\x64\Release\UVtools.msi"
$msiComponentsFile = "$rootFolder\UVtools.InstallerMM\UVtools.InstallerMM.wxs"
$msiProductFile = "$rootFolder\UVtools.Installer\Code\Product.wxs"
$msiSourceFiles = "$rootFolder\$publishFolder\${software}_win-x64_v$version"
$msbuild = "`"${env:ProgramFiles}\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe`" /t:Build /p:Configuration=$buildWith /p:MSIProductVersion=$version"
Write-Output "
####################################
### UVtools builder & deployer ###
####################################
Version: $version [$buildWith]
"
####################################
### Clean up previous publish ###
####################################
# Clean up previous publish
#Remove-Item $publishFolder -Recurse -ErrorAction Ignore # Clean
####################################
### Self-contained runtimes ###
####################################
$runtimes =
@{
"win-x64" = @{
"extraCmd" = ""
"exclude" = @("UVtools.sh")
"include" = @()
}
"linux-x64" = @{
"extraCmd" = ""
"exclude" = @()
"include" = @("libcvextern.so")
}
"arch-x64" = @{
"extraCmd" = ""
"exclude" = @()
"include" = @("libcvextern.so")
}
"rhel-x64" = @{
"extraCmd" = ""
"exclude" = @()
"include" = @("libcvextern.so")
}
#"linux-arm64" = @{
# "extraCmd" = ""
# "exclude" = @()
# "include" = @("libcvextern.so")
#}
#"unix-x64" = @{
# "extraCmd" = ""
# "exclude" = @()
#}
"osx-x64" = @{
"extraCmd" = ""
"exclude" = @()
"include" = @("libcvextern.dylib")
}
"osx-arm64" = @{
"extraCmd" = ""
"exclude" = @()
"include" = @("libcvextern.dylib")
}
}
# Set release notes on projects
$changelog = Get-Content -Path "$changelogFile"
$foundHashTag = $false
$sb = [System.Text.StringBuilder]::new()
foreach($line in $changelog) {
$line = $line.TrimEnd()
if([string]::IsNullOrWhiteSpace($line)) { continue }
if($line.StartsWith('##')) {
if(!$foundHashTag)
{
$foundHashTag = $true
continue
}
else { break }
}
elseif($foundHashTag){
[void]$sb.AppendLine($line)
}
}
Write-Host $sb.ToString()
Set-Content -Path "$rootFolder/RELEASE_NOTES.md" -Value $sb.ToString()
if($null -ne $enableNugetPublish -and $enableNugetPublish)
{
$nugetApiKeyFile = 'build/secret/nuget_api.key'
if (Test-Path -Path $nugetApiKeyFile -PathType Leaf)
{
Write-Output "Creating nuget package"
dotnet pack UVtools.Core --configuration 'Release'
$nupkg = "UVtools.Core/bin/Release/UVtools.Core.$version.nupkg"
if (Test-Path -Path $nupkg -PathType Leaf){
$nugetApiKeyFile = (Get-Content $nugetApiKeyFile)
dotnet nuget push $nupkg --api-key $nugetApiKeyFile --source https://api.nuget.org/v3/index.json
#Remove-Item $nupkg
}else {
Write-Error "Nuget package publish failed!"
Write-Error "File '$nupkg' was not found"
return
}
}
}
foreach ($obj in $runtimes.GetEnumerator()) {
if(![string]::IsNullOrWhiteSpace($buildOnly) -and !$buildOnly.Equals($obj.Name)) {continue}
# Configuration
$deployStopWatch.Restart()
$runtime = $obj.Name; # runtime name
$extraCmd = $obj.extraCmd; # extra cmd to run with dotnet
$publishName="${software}_${runtime}_v$version"
#dotnet build "UVtools.Cmd" -c $buildWith
#dotnet build $project -c $buildWith
if($runtime.StartsWith("win-"))
{
$targetZip = "$publishFolder/${software}_${runtime}_v$version.zip" # Target zip filename
Remove-Item "$publishFolder/$publishName" -Recurse -ErrorAction Ignore
Remove-Item "$targetZip" -ErrorAction Ignore
# Deploy
Write-Output "################################
Building: $runtime"
dotnet publish "UVtools.Cmd" -o "$publishFolder/$publishName" -c $buildWith -r $runtime -p:PublishReadyToRun=true --self-contained $extraCmd
dotnet publish $project -o "$publishFolder/$publishName" -c $buildWith -r $runtime -p:PublishReadyToRun=true --self-contained $extraCmd
New-Item "$publishFolder/$publishName/runtime_package.dat" -ItemType File -Value $runtime
# Cleanup
Remove-Item "UVtools.Cmd\bin\$buildWith\net$netVersion\$runtime" -Recurse -ErrorAction Ignore
Remove-Item "UVtools.Cmd\obj\$buildWith\net$netVersion\$runtime" -Recurse -ErrorAction Ignore
Remove-Item "$releaseFolder\$runtime" -Recurse -ErrorAction Ignore
Remove-Item "$objFolder\$runtime" -Recurse -ErrorAction Ignore
Write-Output "$releaseFolder\$runtime"
foreach ($excludeObj in $obj.Value.exclude) {
Write-Output "Excluding: $excludeObj"
Remove-Item "$publishFolder\$runtime\$excludeObj" -Recurse -ErrorAction Ignore
}
foreach ($includeObj in $obj.Value.include) {
Write-Output "Including: $includeObj"
Copy-Item "$platformsFolder\$runtime\$includeObj" -Destination "$publishFolder\$publishName" -Recurse -ErrorAction Ignore
}
if($null -ne $zipPackages -and $zipPackages)
{
Write-Output "Compressing $runtime to: $targetZip"
wsl cd "$publishFolder/$publishName" `&`& pwd `&`& zip -rq "../../$targetZip" .
}
}
else
{
$args = '-b' # Bundle
if($null -ne $zipPackages -and $zipPackages)
{
$args += ' -z' # Zip
}
bash -c "'build/createRelease.sh' $args $runtime"
#Start-Job { bash -c "'build/createRelease.sh' $using:args $using:runtime" }
}
$deployStopWatch.Stop()
Write-Output "Took: $($deployStopWatch.Elapsed)
################################
"
}
<#
$deployStopWatch.Restart()
#Wait for all jobs to finish.
While ($(Get-Job -State Running).count -gt 0){
Write-Host -NoNewline "."
Start-Sleep 1
}
Write-Output " Took: $($deployStopWatch.Elapsed)"
#Get information from each job.
foreach($job in Get-Job){
$job
#Receive-Job -Id ($job.Id)
}
#Remove all jobs created.
Get-Job | Remove-Job
#>
# Universal package
<#
$deployStopWatch.Restart()
$runtime = "universal-x86-x64"
$targetZip = "$publishFolder\${software}_${runtime}_v$version.zip"
Write-Output "################################
Building: $runtime"
dotnet build $project -c $buildWith
Write-Output "Compressing $runtime to: $targetZip"
[System.IO.Compression.ZipFile]::CreateFromDirectory($releaseFolder, $targetZip, [System.IO.Compression.CompressionLevel]::Optimal, $false, [FixedEncoder]::new())
Write-Output "Took: $($deployStopWatch.Elapsed)
################################
"
$stopWatch.Stop()
#>
# MSI Installer for Windows
if($null -ne $enableMSI -and $enableMSI)
{
$deployStopWatch.Restart()
$runtime = 'win-x64'
$publishName = "${software}_${runtime}_v$version";
if ((Test-Path -Path $msiSourceFiles) -and ((Get-ChildItem "$msiSourceFiles" | Measure-Object).Count) -gt 0) {
$msiTargetFile = "$publishFolder\$publishName.msi"
Write-Output "################################"
Write-Output "Clean and build MSI components manifest file"
Remove-Item "$msiTargetFile" -ErrorAction Ignore
(Get-Content "$msiComponentsFile") -replace 'SourceDir="\.\.\\publish\\.+"', "SourceDir=`"..\publish\$publishName`"" | Out-File "$msiComponentsFile"
$msiComponentsXml = [Xml] (Get-Content "$msiComponentsFile")
foreach($element in $msiComponentsXml.Wix.Module.Directory.Directory)
{
if($element.Id -eq 'MergeRedirectFolder')
{
wixCleanUpElement $element $msiSourceFiles
$msiComponentsXml.Save($msiComponentsFile)
break
}
}
if(Test-Path "$publishFolder\$publishName\UVtools.Core.dll" -PathType Leaf){
Add-Type -Path "$publishFolder\$publishName\UVtools.Core.dll"
} else {
Write-Error "Unable to find UVtools.Core.dll"
return
}
# Add edit with UVtools possible extensions
$extensions = [UVtools.Core.FileFormats.FileFormat]::AllFileExtensions;
$extensionList = New-Object Collections.Generic.List[String]
foreach($ext in $extensions)
{
if($ext.Extension.Contains('.')) { continue; } # Virtual extension
$extKey = "System.FileName:"*.$($ext.Extension.ToLowerInvariant())"";
if($extensionList.Contains($extKey)) { continue; } # Already here
$extensionList.Add($extKey);
}
if($extensionList.Count -gt 0)
{
$regValue = [String]::Join(' OR ', $extensionList)
(Get-Content "$msiProductFile") -replace '(?<A><RegistryValue Root="HKCR".+Name="AppliesTo".+Value=").+(?<B>" Type=.+)', "`${A}$regValue`${B}" | Out-File "$msiProductFile"
}
Write-Output "Building: $runtime MSI Installer"
foreach($installer in $installers)
{
# Clean and build MSI
Remove-Item "$installer\obj" -Recurse -ErrorAction Ignore
Remove-Item "$installer\bin" -Recurse -ErrorAction Ignore
Invoke-Expression "& $msbuild $installer\$installer.wixproj"
}
Write-Output "Copying $runtime MSI to: $msiTargetFile"
Copy-Item $msiOutputFile $msiTargetFile
Write-Output "Took: $($deployStopWatch.Elapsed)
################################
"
}
#else {
# Write-Error "MSI build is enabled but the runtime '$runtime' is not found."
#}
}
Write-Output "
####################################
### Completed ###
####################################
In: $($stopWatch.Elapsed)"
|