Skip to content

Commit

Permalink
Code Formatting of Repo - Add Preprocessing to Compilation Process - …
Browse files Browse the repository at this point in the history
…Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383)

* Replace Tabs with Spaces to follow the conventions

* Add Preprocessing in Compiler

* Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from

* Code Formatting Changes

* Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters

* Make Preprocessing more advanced

* Move Preprocessing to a separate script file

* Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency

* Revert commit b5dffd6

* Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet

* Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool

* Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments

* Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error

* Replace Tabs with Spaces to follow the conventions - 'applications.json' File

* Code Formatting Changes - 'Copy-Files' Private Function

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool

* Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed

* Result of 'Invoke-Preprocessing' Script

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
  • Loading branch information
og-mrk authored Aug 6, 2024
1 parent 3b2af3f commit 3903eaa
Show file tree
Hide file tree
Showing 94 changed files with 4,601 additions and 4,701 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/close-discussion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
exit 1
fi
done
shell: bash
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/issue-slash-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
ISSUE_NUMBER: ${{ github.event.issue.number }}
run: |
echo Closing the issue...
gh issue close $ISSUE_NUMBER --repo ${{ github.repository }}
gh issue close $ISSUE_NUMBER --repo ${{ github.repository }}
2 changes: 1 addition & 1 deletion .github/workflows/sponsors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: main
folder: '.'
folder: '.'
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
shell: pwsh
env:
TEMP: ${{ runner.temp }}
TEMP: ${{ runner.temp }}
71 changes: 41 additions & 30 deletions Compile.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
param (
[switch]$Debug,
[switch]$Run
[switch]$Run,
[switch]$SkipPreprocessing
)
$OFS = "`r`n"
$scriptname = "winutil.ps1"
$workingdir = $PSScriptRoot

# Variable to sync between runspaces
$sync = [Hashtable]::Synchronized(@{})
$sync.PSScriptRoot = $PSScriptRoot
$sync.PSScriptRoot = $workingdir
$sync.configs = @{}

function Update-Progress {
param (
[Parameter(Mandatory, position=0)]
[string]$StatusMessage,

[Parameter(Mandatory, position=1)]
[ValidateRange(0,100)]
[Parameter(Mandatory, position=1)]
[ValidateRange(0,100)]
[int]$Percent,

[Parameter(position=2)]
[string]$Activity = "Compiling"
[Parameter(position=2)]
[string]$Activity = "Compiling"
)

Write-Progress -Activity $Activity -Status $StatusMessage -PercentComplete $Percent
Expand All @@ -34,6 +36,17 @@ $header = @"
################################################################################################################
"@

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"
. "$(($workingdir -replace ('\\$', '')) + '\' + ($preprocessingFilePath -replace ('\.\\', '')))"

$excludedFiles = @('.\.git\', '.\.gitignore', '.\.gitattributes', '.\.github\CODEOWNERS', '.\LICENSE', '.\winutil.ps1', "$preprocessingFilePath", '.\docs\changelog.md', '*.png', '*.exe')
$msg = "Pre-req: Code Formatting"
Invoke-Preprocessing -WorkingDir "$workingdir" -ExcludedFiles $excludedFiles -ProgressStatusMessage $msg
}

# Create the script in memory.
Update-Progress "Pre-req: Allocating Memory" 0
Expand All @@ -43,14 +56,14 @@ Update-Progress "Adding: Header" 5
$script_content.Add($header)

Update-Progress "Adding: Version" 10
$script_content.Add($(Get-Content .\scripts\start.ps1).replace('#{replaceme}',"$(Get-Date -Format yy.MM.dd)"))
$script_content.Add($(Get-Content "$workingdir\scripts\start.ps1").replace('#{replaceme}',"$(Get-Date -Format yy.MM.dd)"))

Update-Progress "Adding: Functions" 20
Get-ChildItem .\functions -Recurse -File | ForEach-Object {
Get-ChildItem "$workingdir\functions" -Recurse -File | ForEach-Object {
$script_content.Add($(Get-Content $psitem.FullName))
}
Update-Progress "Adding: Config *.json" 40
Get-ChildItem .\config | Where-Object {$psitem.extension -eq ".json"} | ForEach-Object {
Get-ChildItem "$workingdir\config" | Where-Object {$psitem.extension -eq ".json"} | ForEach-Object {

$json = (Get-Content $psitem.FullName).replace("'","''")

Expand All @@ -65,15 +78,15 @@ Get-ChildItem .\config | Where-Object {$psitem.extension -eq ".json"} | ForEach-
# Use **HTML decimal/hex codes instead**, as using HTML Entity Codes will result in XML parse Error when running the compiled script.
for ($i = 0; $i -lt $firstLevelJsonList.Count; $i += 1) {
$firstLevelName = $firstLevelJsonList[$i]
if ($jsonAsObject.$firstLevelName.content -ne $null) {
if ($jsonAsObject.$firstLevelName.content -ne $null) {
$jsonAsObject.$firstLevelName.content = $jsonAsObject.$firstLevelName.content.replace('&','&#38;').replace('','&#8220;').replace('','&#8221;').replace("'",'&#39;').replace('<','&#60;').replace('>','&#62;').replace('','&#8212;')
$jsonAsObject.$firstLevelName.content = $jsonAsObject.$firstLevelName.content.replace('&#39;&#39;',"&#39;") # resolves the Double Apostrophe caused by the first replace function in the main loop
}
if ($jsonAsObject.$firstLevelName.description -ne $null) {
$jsonAsObject.$firstLevelName.description = $jsonAsObject.$firstLevelName.description.replace('&','&#38;').replace('','&#8220;').replace('','&#8221;').replace("'",'&#39;').replace('<','&#60;').replace('>','&#62;').replace('','&#8212;')
$jsonAsObject.$firstLevelName.description = $jsonAsObject.$firstLevelName.description.replace('&#39;&#39;',"&#39;") # resolves the Double Apostrophe caused by the first replace function in the main loop
}
}
}
}

# Add 'WPFInstall' as a prefix to every entry-name in 'applications.json' file
if ($psitem.Name -eq "applications.json") {
Expand All @@ -95,10 +108,10 @@ Get-ChildItem .\config | Where-Object {$psitem.extension -eq ".json"} | ForEach-
$script_content.Add($(Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| convertfrom-json" ))
}

$xaml = (Get-Content .\xaml\inputXML.xaml).replace("'","''")
$xaml = (Get-Content "$workingdir\xaml\inputXML.xaml").replace("'","''")

# Dot-source the Get-TabXaml function
. .\functions\private\Get-TabXaml.ps1
. "$workingdir\functions\private\Get-TabXaml.ps1"

Update-Progress "Building: Xaml " 75
$appXamlContent = Get-TabXaml "applications" 5
Expand All @@ -114,30 +127,28 @@ $xaml = $xaml -replace "{{InstallPanel_features}}", $featuresXamlContent

$script_content.Add($(Write-output "`$inputXML = '$xaml'"))

$script_content.Add($(Get-Content .\scripts\main.ps1))
$script_content.Add($(Get-Content "$workingdir\scripts\main.ps1"))

if ($Debug){
if ($Debug) {
Update-Progress "Writing debug files" 95
$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 {
$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 {
Update-Progress "Removing temporary files" 99
Remove-Item ".\xaml\inputApp.xaml" -ErrorAction SilentlyContinue
Remove-Item ".\xaml\inputTweaks.xaml" -ErrorAction SilentlyContinue
Remove-Item ".\xaml\inputFeatures.xaml" -ErrorAction SilentlyContinue
Remove-Item "$workingdir\xaml\inputApp.xaml" -ErrorAction SilentlyContinue
Remove-Item "$workingdir\xaml\inputTweaks.xaml" -ErrorAction SilentlyContinue
Remove-Item "$workingdir\xaml\inputFeatures.xaml" -ErrorAction SilentlyContinue
}

Set-Content -Path $scriptname -Value ($script_content -join "`r`n") -Encoding ascii
Set-Content -Path "$workingdir\$scriptname" -Value ($script_content -join "`r`n") -Encoding ascii
Write-Progress -Activity "Compiling" -Completed

if ($run){
if ($run) {
try {
Start-Process -FilePath "pwsh" -ArgumentList ".\$scriptname"
}
catch {
Start-Process -FilePath "powershell" -ArgumentList ".\$scriptname"
Start-Process -FilePath "pwsh" -ArgumentList "$workingdir\$scriptname"
} catch {
Start-Process -FilePath "powershell" -ArgumentList "$workingdir\$scriptname"
}

}
Loading

0 comments on commit 3903eaa

Please sign in to comment.