2024-06-10 15:24:12 -05:00
|
|
|
param (
|
2024-06-25 14:10:16 -05:00
|
|
|
[switch]$Debug,
|
2024-08-06 15:35:17 -05:00
|
|
|
[switch]$Run,
|
|
|
|
[switch]$SkipPreprocessing
|
2024-06-10 15:24:12 -05:00
|
|
|
)
|
2024-08-19 18:26:05 -05:00
|
|
|
|
2023-03-07 14:28:00 -06:00
|
|
|
$OFS = "`r`n"
|
|
|
|
$scriptname = "winutil.ps1"
|
2024-08-06 15:35:17 -05:00
|
|
|
$workingdir = $PSScriptRoot
|
2024-08-19 18:26:05 -05:00
|
|
|
$logFilePath = Join-Path $workingdir "compile.log"
|
2024-06-10 15:24:12 -05:00
|
|
|
|
2024-02-21 16:06:27 -06:00
|
|
|
# Variable to sync between runspaces
|
|
|
|
$sync = [Hashtable]::Synchronized(@{})
|
2024-08-06 15:35:17 -05:00
|
|
|
$sync.PSScriptRoot = $workingdir
|
2024-02-21 16:06:27 -06:00
|
|
|
$sync.configs = @{}
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
# Dot-source external functions
|
|
|
|
. "$PSScriptRoot\tools\Update-Progress.ps1"
|
2024-06-28 16:59:38 -05:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
# Function to log messages
|
|
|
|
function Log-Message {
|
|
|
|
param([string]$Message)
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
|
|
Add-Content -Path $logFilePath -Value "$timestamp - $Message"
|
|
|
|
}
|
2024-06-28 16:59:38 -05:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
# Function to encode special characters in JSON
|
|
|
|
function Encode-JsonSpecialChars {
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[PSObject]$jsonObject
|
2024-06-28 16:59:38 -05:00
|
|
|
)
|
2024-08-19 18:26:05 -05:00
|
|
|
foreach ($prop in $jsonObject.PSObject.Properties) {
|
|
|
|
if ($prop.Value -is [string]) {
|
|
|
|
$prop.Value = $prop.Value.Replace('&','&').Replace('“','“').Replace('”','”').Replace("'",''').Replace('<','<').Replace('>','>').Replace('—','—')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $jsonObject
|
2024-06-28 16:59:38 -05:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:24:12 -05:00
|
|
|
$header = @"
|
2023-03-07 14:28:00 -06:00
|
|
|
################################################################################################################
|
|
|
|
### ###
|
|
|
|
### WARNING: This file is automatically generated DO NOT modify this file directly as it will be overwritten ###
|
|
|
|
### ###
|
|
|
|
################################################################################################################
|
2024-06-10 15:24:12 -05:00
|
|
|
"@
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
if ($Debug) {
|
|
|
|
Write-Debug "Debug mode enabled"
|
|
|
|
}
|
2024-08-06 15:35:17 -05:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
if (-NOT $SkipPreprocessing) {
|
|
|
|
try {
|
|
|
|
Update-Progress "Pre-req: Running Preprocessor..." 0
|
|
|
|
$preprocessingFilePath = Join-Path $PSScriptRoot "tools\Invoke-Preprocessing.ps1"
|
|
|
|
. "$preprocessingFilePath"
|
2024-08-06 15:35:17 -05:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
$excludedFiles = @('.\.git\', '.\.gitignore', '.\.gitattributes', '.\.github\CODEOWNERS', '.\LICENSE', "$preprocessingFilePath", '*.png', '*.exe')
|
|
|
|
$msg = "Pre-req: Code Formatting"
|
|
|
|
Invoke-Preprocessing -WorkingDir "$workingdir" -ExcludedFiles $excludedFiles -ProgressStatusMessage $msg
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Error "Preprocessing failed: $($_.Exception.Message)"
|
|
|
|
Log-Message "Preprocessing failed: $($_.Exception.Message)"
|
|
|
|
exit 1
|
|
|
|
}
|
2024-08-06 15:35:17 -05:00
|
|
|
}
|
2024-06-28 16:59:38 -05:00
|
|
|
|
2024-06-10 15:24:12 -05:00
|
|
|
# Create the script in memory.
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Pre-req: Allocating Memory" 0
|
2024-06-10 15:24:12 -05:00
|
|
|
$script_content = [System.Collections.Generic.List[string]]::new()
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Adding: Header" 5
|
2024-06-10 15:24:12 -05:00
|
|
|
$script_content.Add($header)
|
|
|
|
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Adding: Version" 10
|
2024-08-06 15:35:17 -05:00
|
|
|
$script_content.Add($(Get-Content "$workingdir\scripts\start.ps1").replace('#{replaceme}',"$(Get-Date -Format yy.MM.dd)"))
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Adding: Functions" 20
|
2024-08-06 15:35:17 -05:00
|
|
|
Get-ChildItem "$workingdir\functions" -Recurse -File | ForEach-Object {
|
2024-06-10 15:24:12 -05:00
|
|
|
$script_content.Add($(Get-Content $psitem.FullName))
|
2024-08-19 18:26:05 -05:00
|
|
|
}
|
|
|
|
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Adding: Config *.json" 40
|
2024-08-19 18:26:05 -05:00
|
|
|
Get-ChildItem "$workingdir\config" | Where-Object {$_.extension -eq ".json"} | ForEach-Object {
|
|
|
|
$json = Get-Content -Path $psitem.FullName -Raw
|
|
|
|
$jsonAsObject = $json | ConvertFrom-Json
|
|
|
|
$jsonAsObject = Encode-JsonSpecialChars -jsonObject $jsonAsObject
|
2024-06-28 16:59:38 -05:00
|
|
|
|
|
|
|
if ($psitem.Name -eq "applications.json") {
|
2024-08-19 18:26:05 -05:00
|
|
|
foreach ($appEntryName in $jsonAsObject.PSObject.Properties.Name) {
|
2024-06-28 16:59:38 -05:00
|
|
|
$appEntryContent = $jsonAsObject.$appEntryName
|
|
|
|
$jsonAsObject.PSObject.Properties.Remove($appEntryName)
|
|
|
|
$jsonAsObject | Add-Member -MemberType NoteProperty -Name "WPFInstall$appEntryName" -Value $appEntryContent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
$json = ($jsonAsObject | ConvertTo-Json -Depth 3).replace('\r\n', "`r`n")
|
|
|
|
$sync.configs.$($psitem.BaseName) = $json | ConvertFrom-Json
|
|
|
|
$script_content.Add("`$sync.configs.$($psitem.BaseName) = '$json' | ConvertFrom-Json")
|
2023-03-07 14:28:00 -06:00
|
|
|
}
|
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
$xaml = (Get-Content "$workingdir\xaml\inputXML.xaml" -Raw).Replace("'","''")
|
2024-03-21 18:58:40 -05:00
|
|
|
|
2024-02-21 16:06:27 -06:00
|
|
|
# Dot-source the Get-TabXaml function
|
2024-08-06 15:35:17 -05:00
|
|
|
. "$workingdir\functions\private\Get-TabXaml.ps1"
|
2024-02-21 16:06:27 -06:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
Update-Progress "Building: Xaml" 75
|
2024-06-10 15:24:12 -05:00
|
|
|
$appXamlContent = Get-TabXaml "applications" 5
|
|
|
|
$tweaksXamlContent = Get-TabXaml "tweaks"
|
|
|
|
$featuresXamlContent = Get-TabXaml "feature"
|
2024-02-21 16:06:27 -06:00
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
Update-Progress "Adding: Xaml" 90
|
2024-03-21 18:58:40 -05:00
|
|
|
$xaml = $xaml -replace "{{InstallPanel_applications}}", $appXamlContent
|
|
|
|
$xaml = $xaml -replace "{{InstallPanel_tweaks}}", $tweaksXamlContent
|
|
|
|
$xaml = $xaml -replace "{{InstallPanel_features}}", $featuresXamlContent
|
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
$script_content.Add("`$inputXML = '$xaml'")
|
2024-06-10 15:24:12 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
$script_content.Add($(Get-Content "$workingdir\scripts\main.ps1"))
|
2024-06-10 15:24:12 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
if ($Debug) {
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Writing debug files" 95
|
2024-08-06 15:35:17 -05:00
|
|
|
$appXamlContent | Out-File -FilePath "$workingdir\xaml\inputApp.xaml" -Encoding ascii
|
|
|
|
$tweaksXamlContent | Out-File -FilePath "$workingdir\xaml\inputTweaks.xaml" -Encoding ascii
|
|
|
|
$featuresXamlContent | Out-File -FilePath "$workingdir\xaml\inputFeatures.xaml" -Encoding ascii
|
|
|
|
} else {
|
2024-06-28 16:59:38 -05:00
|
|
|
Update-Progress "Removing temporary files" 99
|
2024-08-06 15:35:17 -05:00
|
|
|
Remove-Item "$workingdir\xaml\inputApp.xaml" -ErrorAction SilentlyContinue
|
|
|
|
Remove-Item "$workingdir\xaml\inputTweaks.xaml" -ErrorAction SilentlyContinue
|
|
|
|
Remove-Item "$workingdir\xaml\inputFeatures.xaml" -ErrorAction SilentlyContinue
|
2024-06-10 15:24:12 -05:00
|
|
|
}
|
2024-03-21 18:58:40 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
Set-Content -Path "$workingdir\$scriptname" -Value ($script_content -join "`r`n") -Encoding ascii
|
2024-06-25 14:10:16 -05:00
|
|
|
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 {
|
2024-08-19 18:26:05 -05:00
|
|
|
$null = Get-Command -Syntax ".\winutil.ps1"
|
2024-08-07 15:24:26 -05:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Warning "Syntax Validation for 'winutil.ps1' has failed"
|
|
|
|
Write-Host "$($Error[0])" -ForegroundColor Red
|
2024-08-19 18:26:05 -05:00
|
|
|
Log-Message "Syntax Validation failed: $($Error[0])"
|
2024-08-07 15:24:26 -05:00
|
|
|
}
|
|
|
|
Write-Progress -Activity "Validating" -Completed
|
|
|
|
|
2024-08-19 18:26:05 -05:00
|
|
|
if ($Run) {
|
2024-06-25 15:05:19 -05:00
|
|
|
try {
|
2024-08-06 15:35:17 -05:00
|
|
|
Start-Process -FilePath "pwsh" -ArgumentList "$workingdir\$scriptname"
|
2024-08-19 18:26:05 -05:00
|
|
|
}
|
|
|
|
catch {
|
2024-08-06 15:35:17 -05:00
|
|
|
Start-Process -FilePath "powershell" -ArgumentList "$workingdir\$scriptname"
|
2024-06-25 15:05:19 -05:00
|
|
|
}
|
2024-06-28 16:23:21 -05:00
|
|
|
}
|
2024-08-19 18:26:05 -05:00
|
|
|
|
|
|
|
Log-Message "Compilation completed"
|