Files
winutil/Compile.ps1
T

140 lines
4.5 KiB
PowerShell
Raw Normal View History

2024-06-10 15:24:12 -05:00
param (
[switch]$Debug,
[switch]$Run,
[string]$Arguments
2024-06-10 15:24:12 -05:00
)
if ((Get-Item ".\winutil.ps1" -ErrorAction SilentlyContinue).IsReadOnly) {
Remove-Item ".\winutil.ps1" -Force
}
+3
2023-03-07 12:28:00 -08:00
$OFS = "`r`n"
$scriptname = "winutil.ps1"
$workingdir = $PSScriptRoot
2024-06-10 15:24:12 -05:00
2024-02-21 16:06:27 -06:00
# Variable to sync between runspaces
$sync = [Hashtable]::Synchronized(@{})
$sync.configs = @{}
+3
2023-03-07 12:28:00 -08:00
function Update-Progress {
param (
[Parameter(Mandatory, position=0)]
[string]$StatusMessage,
[Parameter(Mandatory, position=1)]
[ValidateRange(0,100)]
[int]$Percent,
[Parameter(position=2)]
[string]$Activity = "Compiling"
)
Write-Progress -Activity $Activity -Status $StatusMessage -PercentComplete $Percent
}
Update-Progress "Pre-req: Running Preprocessor..." 0
# Dot source the 'Invoke-Preprocessing' Function from 'tools/Invoke-Preprocessing.ps1' Script
$preprocessingFilePath = ".\tools\Invoke-Preprocessing.ps1"
. $preprocessingFilePath
$excludedFiles = @()
# Add directories only if they exist
if (Test-Path '.\.git\') { $excludedFiles += '.\.git\' }
if (Test-Path '.\binary\') { $excludedFiles += '.\binary\' }
# Add files that should always be excluded
$excludedFiles += @(
'.\.gitignore',
'.\.gitattributes',
'.\.github\CODEOWNERS',
'.\LICENSE',
"$preprocessingFilePath",
'*.png',
'*.exe',
'.\.preprocessor_hashes.json'
)
$msg = "Pre-req: Code Formatting"
Invoke-Preprocessing -WorkingDir "$workingdir" -ExcludedFiles $excludedFiles -ProgressStatusMessage $msg
2024-06-10 15:24:12 -05:00
# Create the script in memory.
Update-Progress "Pre-req: Allocating Memory" 0
2024-06-10 15:24:12 -05:00
$script_content = [System.Collections.Generic.List[string]]::new()
+3
2023-03-07 12:28:00 -08:00
Update-Progress "Adding: Version" 10
2024-09-10 04:05:10 +03:00
$script_content.Add($(Get-Content "scripts\start.ps1").replace('#{replaceme}',"$(Get-Date -Format yy.MM.dd)"))
+3
2023-03-07 12:28:00 -08:00
Update-Progress "Adding: Functions" 20
2024-09-10 04:05:10 +03:00
Get-ChildItem "functions" -Recurse -File | ForEach-Object {
2024-06-10 15:24:12 -05:00
$script_content.Add($(Get-Content $psitem.FullName))
}
Update-Progress "Adding: Config *.json" 40
2024-09-10 04:05:10 +03:00
Get-ChildItem "config" | Where-Object {$psitem.extension -eq ".json"} | ForEach-Object {
$json = (Get-Content $psitem.FullName -Raw)
$jsonAsObject = $json | ConvertFrom-Json
# Add 'WPFInstall' as a prefix to every entry-name in 'applications.json' file
if ($psitem.Name -eq "applications.json") {
foreach ($appEntryName in $jsonAsObject.PSObject.Properties.Name) {
$appEntryContent = $jsonAsObject.$appEntryName
$jsonAsObject.PSObject.Properties.Remove($appEntryName)
$jsonAsObject | Add-Member -MemberType NoteProperty -Name "WPFInstall$appEntryName" -Value $appEntryContent
}
}
# Line 90 requires no whitespace inside the here-strings, to keep formatting of the JSON in the final script.
$json = @"
$($jsonAsObject | ConvertTo-Json -Depth 3)
"@
+1
2024-04-20 16:38:43 -05:00
$sync.configs.$($psitem.BaseName) = $json | ConvertFrom-Json
$script_content.Add($(Write-Output "`$sync.configs.$($psitem.BaseName) = @'`r`n$json`r`n'@ `| ConvertFrom-Json" ))
+3
2023-03-07 12:28:00 -08:00
}
# Read the entire XAML file as a single string, preserving line breaks
$xaml = Get-Content "$workingdir\xaml\inputXML.xaml" -Raw
2024-03-21 16:58:40 -07:00
Update-Progress "Adding: Xaml " 90
2024-03-21 16:58:40 -07:00
# Add the XAML content to $script_content using a here-string
$script_content.Add(@"
`$inputXML = @'
$xaml
'@
"@)
2024-06-10 15:24:12 -05:00
2024-09-10 04:05:10 +03:00
$script_content.Add($(Get-Content "scripts\main.ps1"))
2024-06-10 15:24:12 -05:00
if ($Debug) {
Update-Progress "Writing debug files" 95
2024-09-10 04:05:10 +03:00
$appXamlContent | Out-File -FilePath "xaml\inputApp.xaml" -Encoding ascii
$tweaksXamlContent | Out-File -FilePath "xaml\inputTweaks.xaml" -Encoding ascii
$featuresXamlContent | Out-File -FilePath "xaml\inputFeatures.xaml" -Encoding ascii
} else {
Update-Progress "Removing temporary files" 99
2024-09-10 04:05:10 +03:00
Remove-Item "xaml\inputApp.xaml" -ErrorAction SilentlyContinue
Remove-Item "xaml\inputTweaks.xaml" -ErrorAction SilentlyContinue
Remove-Item "xaml\inputFeatures.xaml" -ErrorAction SilentlyContinue
2024-06-10 15:24:12 -05:00
}
2024-03-21 16:58:40 -07:00
2024-09-10 04:05:10 +03:00
Set-Content -Path "$scriptname" -Value ($script_content -join "`r`n") -Encoding ascii
Write-Progress -Activity "Compiling" -Completed
2024-08-07 15:24:26 -05:00
Update-Progress -Activity "Validating" -StatusMessage "Checking winutil.ps1 Syntax" -Percent 0
try {
Get-Command -Syntax .\winutil.ps1 | Out-Null
2024-09-10 04:05:10 +03:00
} catch {
2024-08-07 15:24:26 -05:00
Write-Warning "Syntax Validation for 'winutil.ps1' has failed"
Write-Host "$($Error[0])" -ForegroundColor Red
exit 1
2024-08-07 15:24:26 -05:00
}
Write-Progress -Activity "Validating" -Completed
if ($run) {
2026-02-10 21:19:46 +02:00
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\Winutil.ps1 $Arguments
break
}