Files
winutil/Compile.ps1
T

145 lines
5.5 KiB
PowerShell
Raw Normal View History

2024-06-10 15:24:12 -05:00
param (
[switch]$Debug,
[switch]$Run,
[switch]$SkipPreprocessing,
[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-09-10 04:05:10 +03:00
Push-Location
Set-Location $workingdir
2024-02-21 16:06:27 -06:00
# Variable to sync between runspaces
$sync = [Hashtable]::Synchronized(@{})
$sync.PSScriptRoot = $workingdir
2024-02-21 16:06:27 -06:00
$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
}
2024-06-10 15:24:12 -05:00
$header = @"
+3
2023-03-07 12:28:00 -08: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
"@
+3
2023-03-07 12:28:00 -08:00
if (-NOT $SkipPreprocessing) {
Update-Progress "Pre-req: Running Preprocessor..." 0
# Dot source the 'Invoke-Preprocessing' Function from 'tools/Invoke-Preprocessing.ps1' Script
$preprocessingFilePath = ".\tools\Invoke-Preprocessing.ps1"
2024-09-10 04:05:10 +03:00
. $preprocessingFilePath
2024-08-07 15:30:23 -05:00
$excludedFiles = @('.\.git\', '.\.gitignore', '.\.gitattributes', '.\.github\CODEOWNERS', '.\LICENSE', "$preprocessingFilePath", '*.png', '*.exe')
$msg = "Pre-req: Code Formatting"
2024-09-10 04:05:10 +03:00
Invoke-Preprocessing -WorkingDir "$workingdir" -ExcludedFiles $excludedFiles -ProgressStatusMessage $msg -ThrowExceptionOnEmptyFilesList
}
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: Header" 5
2024-06-10 15:24:12 -05:00
$script_content.Add($header)
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 {
+3
2023-03-07 12:28:00 -08:00
$json = (Get-Content $psitem.FullName).replace("'","''")
$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
}
}
# The replace at the end is required, as without it the output of 'converto-json' will be somewhat weird for Multiline Strings
# Most Notably is the scripts in some json files, making it harder for users who want to review these scripts, which're found in the compiled script
$json = ($jsonAsObject | convertto-json -Depth 3).replace('\r\n',"`r`n")
+1
2024-04-20 16:38:43 -05:00
2024-02-21 16:06:27 -06:00
$sync.configs.$($psitem.BaseName) = $json | convertfrom-json
2024-06-10 15:24:12 -05:00
$script_content.Add($(Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| 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 {
$null = Get-Command -Syntax .\winutil.ps1
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
}
Write-Progress -Activity "Validating" -Completed
if ($run) {
$script = "& '$workingdir\$scriptname' $Arguments"
$powershellcmd = if (Get-Command pwsh -ErrorAction SilentlyContinue) { "pwsh" } else { "powershell" }
$processCmd = if (Get-Command wt.exe -ErrorAction SilentlyContinue) { "wt.exe" } else { $powershellcmd }
Start-Process $processCmd -ArgumentList "$powershellcmd -NoProfile -Command $script"
break
}
2024-09-10 04:05:10 +03:00
Pop-Location