Skip to content

Commit 7f30e0a

Browse files
Merge pull request #3 from CodingWonders/updgui_vnext
UpdGUI 1.1
2 parents 0feb07d + 0b05cf0 commit 7f30e0a

File tree

6 files changed

+672
-147
lines changed

6 files changed

+672
-147
lines changed

Compile.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is a heavily modified compilation script for the Preinstallation Environment Helper, originally from the Windows Utility (WinUtil)
2+
#
3+
# Original license (affects both this script and the pre-processor):
4+
#
5+
# MIT License
6+
#
7+
# Copyright (c) 2022 CT Tech Group LLC
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
27+
$workingdir = $PSScriptRoot
28+
29+
Push-Location
30+
Set-Location $workingdir
31+
32+
# Variable to sync between runspaces
33+
$sync = [Hashtable]::Synchronized(@{})
34+
$sync.PSScriptRoot = $workingdir
35+
$sync.configs = @{}
36+
37+
function Update-Progress {
38+
param (
39+
[Parameter(Mandatory, position=0)]
40+
[string]$StatusMessage,
41+
42+
[Parameter(Mandatory, position=1)]
43+
[ValidateRange(0,100)]
44+
[int]$Percent,
45+
46+
[Parameter(position=2)]
47+
[string]$Activity = "Compiling"
48+
)
49+
50+
Write-Progress -Activity $Activity -Status $StatusMessage -PercentComplete $Percent
51+
}
52+
53+
Update-Progress "Pre-req: Running Preprocessor..." 0
54+
55+
# Dot source the 'Invoke-Preprocessing' Function from 'tools/Invoke-Preprocessing.ps1' Script
56+
$preprocessingFilePath = ".\tools\Invoke-Preprocessing.ps1"
57+
. $preprocessingFilePath
58+
59+
$excludedFiles = @('*.png', '*.exe', 'Compile.ps1')
60+
$msg = "Pre-req: Code Formatting"
61+
Invoke-Preprocessing -WorkingDir "$workingdir" -ExcludedFiles $excludedFiles -ProgressStatusMessage $msg -ThrowExceptionOnEmptyFilesList

aboutform.resources.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'Button1.Name' = 'Button1'
55
'RichTextBox1.Text' = 'UpdGUI is a simple front-end written in PowerShell for the PSWindowsUpdate module.
66
7-
Version: 1.0
7+
Version: 1.1
88
99
- Programming: CodingWonders (https://www.github.com/CodingWonders)
1010
- Idea: og-mrk (https://www.github.com/og-mrk)

tools/Invoke-Preprocessing.ps1

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
function Invoke-Preprocessing {
2+
<#
3+
.SYNOPSIS
4+
A function that does Code Formatting using RegEx, useful when trying to force specific coding standard(s) to a project.
5+
6+
.PARAMETER ThrowExceptionOnEmptyFilesList
7+
A switch which'll throw an exception upon not finding any files inside the provided 'WorkingDir'.
8+
9+
.PARAMETER SkipExcludedFilesValidation
10+
A switch to stop file path validation on 'ExcludedFiles' list.
11+
12+
.PARAMETER ExcludedFiles
13+
A list of file paths which're *relative to* 'WorkingDir' Folder, every item in the list can be pointing to File (doesn't end with '\') or Directory (ends with '\') or None-Existing File/Directory.
14+
By default, it checks if everyitem exists, and throws an exception if one or more are not found (None-Existing), if you want to skip this validation, please consider providing the '-SkipExcludedFilesValidation' switch to skip this check.
15+
16+
.PARAMETER WorkingDir
17+
The folder to search inside recursively for files which're going to be Preprocessed (Code Formatted), unless they're found in 'ExcludedFiles' List.
18+
Note: The path should be absolute, NOT relative.
19+
20+
.PARAMETER ProgressStatusMessage
21+
The status message used when displaying the progress bar, which's done through PowerShell 'Write-Progress' Cmdlet.
22+
This's a Required Parameter, as the information displayed to terminal is useful when running this function,
23+
which might take less than 1 sec to minutes depending on project's scale & hardware performance.
24+
25+
.PARAMETER ProgressActivity
26+
The activity message used when displaying the progress bar, which's done through PowerShell 'Write-Progress' Cmdlet,
27+
This's an Optional Parameter, default value is 'Preprocessing', used in combination with 'ProgressStatusMessage' Parameter Value.
28+
29+
.EXAMPLE
30+
Invoke-Preprocessing -WorkingDir "DRIVE:\Path\To\Folder\" -ExcludedFiles @('file.txt', '.\.git\', '*.png') -ProgressStatusMessage "Doing Preprocessing"
31+
32+
Calls 'Invoke-Preprocessing' function using Named Paramters, with 'WorkingDir' (Mandatory Parameter) which's used as the base folder when searching for files recursively (using 'Get-ChildItem'), other two paramters are, in order from right to left, the Optional 'ExcludeFiles', which can be a path to a file, folder, or pattern-matched (like '*.png'), and the 'ProgressStatusMessage', which's used in Progress Bar.
33+
34+
.EXAMPLE
35+
Invoke-Preprocessing -WorkingDir "DRIVE:\Path\To\Folder\" -ExcludedFiles @('file.txt', '.\.git\', '*.png') -ProgressStatusMessage "Doing Preprocessing" -ProgressActivity "Re-Formatting Code"
36+
37+
Same as Example No. 1, but uses 'ProgressActivity' which's used in Progress Bar.
38+
39+
.EXAMPLE
40+
Invoke-Preprocessing -ThrowExceptionOnEmptyFilesList -WorkingDir "DRIVE:\Path\To\Folder\" -ExcludedFiles @('file.txt', '.\.git\', '*.png') -ProgressStatusMessage "Doing Preprocessing"
41+
42+
Same as Example No. 1, but uses '-ThrowExceptionOnEmptyFilesList', which's an optional parameter that'll make 'Invoke-Preprocessing' throw an exception when no files are found in 'WorkingDir' (not including the ExcludedFiles, of course), useful when you want to double check your parameters & you're sure there's files to process in the 'WorkingDir'.
43+
44+
.EXAMPLE
45+
Invoke-Preprocessing -Skip -WorkingDir "DRIVE:\Path\To\Folder\" -ExcludedFiles @('file.txt', '.\.git\', '*.png') -ProgressStatusMessage "Doing Preprocessing"
46+
47+
Same as Example No. 1, but uses '-SkipExcludedFilesValidation', which'll skip the validation step for 'ExcludedFiles' list. This can be useful when 'ExcludedFiles' list is generated from another function, or from unreliable source (you can't guarantee every item in list is a valid path), but you want to silently continue through the function.
48+
#>
49+
50+
param (
51+
[Parameter(position=0)]
52+
[switch]$SkipExcludedFilesValidation,
53+
54+
[Parameter(position=1)]
55+
[switch]$ThrowExceptionOnEmptyFilesList,
56+
57+
[Parameter(Mandatory, position=2)]
58+
[ValidateScript({[System.IO.Path]::IsPathRooted($_)})]
59+
[string]$WorkingDir,
60+
61+
[Parameter(position=3)]
62+
[string[]]$ExcludedFiles,
63+
64+
[Parameter(Mandatory, position=4)]
65+
[string]$ProgressStatusMessage,
66+
67+
[Parameter(position=5)]
68+
[string]$ProgressActivity = "Preprocessing"
69+
)
70+
71+
if (-NOT (Test-Path -PathType Container -Path "$WorkingDir")) {
72+
throw "[Invoke-Preprocessing] Invalid Paramter Value for 'WorkingDir', passed value: '$WorkingDir'. Either the path is a File or Non-Existing/Invlid, please double check your code."
73+
}
74+
75+
$count = $ExcludedFiles.Count
76+
77+
# Make sure there's a * at the end of folders in ExcludedFiles list
78+
for ($i = 0; $i -lt $count; $i++) {
79+
$excludedFile = $ExcludedFiles[$i]
80+
$isFolder = ($excludedFile) -match '\\$'
81+
if ($isFolder) { $ExcludedFiles[$i] = $excludedFile + '*' }
82+
}
83+
84+
# Get Files List
85+
[System.Collections.ArrayList]$files = Get-ChildItem $WorkingDir -Recurse -Exclude $ExcludedFiles -File -Force
86+
$numOfFiles = $files.Count
87+
88+
# Only keep the 'FullName' Property for every entry in the list
89+
for ($i = 0; $i -lt $numOfFiles; $i++) {
90+
$file = $files[$i]
91+
$files[$i] = $file.FullName
92+
}
93+
94+
# If a file(s) are found in Exclude List,
95+
# Remove the file from files list.
96+
for ($j = 0; $j -lt $excludedFiles.Count; $j++) {
97+
# Prepare some variables
98+
$excluded = $excludedFiles[$j]
99+
$pathToFind = ($excluded) -replace ('^\.\\', '')
100+
$pathToFind = $WorkingDir + '\' + $pathToFind
101+
$index = -1 # reset index on every iteration
102+
103+
# Handle paths with wildcards in a different implementation
104+
$matches = ($pathToFind) -match '^.*?\*'
105+
106+
if ($matches) {
107+
$filesToCheck = Get-ChildItem -Recurse -Path "$pathToFind" -File -Force
108+
if ($filesToCheck) {
109+
for ($k = 0; $k -lt $filesToCheck.Count; $k++) {
110+
$fileToCheck = $filesToCheck[$k]
111+
$index = $files.IndexOf("$fileToCheck")
112+
if ($index -ge 0) { $files.RemoveAt($index) }
113+
}
114+
}
115+
} else {
116+
$index = $files.IndexOf("$pathToFind")
117+
if ($index -ge 0) { $files.RemoveAt($index) }
118+
}
119+
}
120+
121+
# Make sure 'numOfFiles' is synced with the actual Number of Files found in '$files'
122+
# This's done because previous may or may not edit the files list, so we should update it
123+
$numOfFiles = $files.Count
124+
125+
if ($numOfFiles -eq 0) {
126+
if ($ThrowExceptionOnEmptyFilesList) {
127+
throw "[Invoke-Preprocessing] Found 0 Files to Preprocess inside 'WorkingDir' Directory and '-ThrowExceptionOnEmptyFilesList' Switch is provided, value of 'WorkingDir': '$WorkingDir'."
128+
} else {
129+
return # Do an early return, there's nothing else to do
130+
}
131+
}
132+
133+
for ($i = 0; $i -lt 1; $i++) {
134+
$files[0] = "$((Get-Location).Path)\updgui.ps1"
135+
$files[1] = "$((Get-Location).Path)\updgui.designer.ps1"
136+
$files[2] = "$((Get-Location).Path)\updgui.resources.ps1"
137+
$files[3] = "$((Get-Location).Path)\aboutform.ps1"
138+
$files[4] = "$((Get-Location).Path)\aboutform.designer.ps1"
139+
$files[5] = "$((Get-Location).Path)\aboutform.resources.ps1"
140+
$fullFileName = $files[$i]
141+
(Get-Content "$fullFileName").TrimEnd() `
142+
-replace ('\t', ' ') `
143+
-replace ('\)\s*\{', ') {') `
144+
-replace ('(?<keyword>if|for|foreach)\s*(?<condition>\([.*?]\))\s*\{', '${keyword} ${condition} {') `
145+
-replace ('\}\s*elseif\s*(?<condition>\([.*?]\))\s*\{', '} elseif ${condition} {') `
146+
-replace ('\}\s*else\s*\{', '} else {') `
147+
-replace ('Try\s*\{', 'try {') `
148+
-replace ('Catch\s*\{', 'catch {') `
149+
-replace ('\}\s*Catch', '} catch') `
150+
-replace ('\}\s*Catch\s*(?<exceptions>(\[.*?\]\s*(\,)?\s*)+)\s*\{', '} catch ${exceptions} {') `
151+
-replace ('\}\s*Catch\s*(?<exceptions>\[.*?\])\s*\{', '} catch ${exceptions} {') `
152+
-replace ('(?<parameter_type>\[[^$0-9]+\])\s*(?<str_after_type>\$.*?)', '${parameter_type}${str_after_type}') `
153+
| Set-Content "$fullFileName"
154+
155+
Write-Progress -Activity $ProgressActivity -Status "$ProgressStatusMessage - Finished $i out of $numOfFiles" -PercentComplete (($i/$numOfFiles)*100)
156+
}
157+
158+
Write-Progress -Activity $ProgressActivity -Status "$ProgressStatusMessage - Finished Task Successfully" -Completed
159+
}

0 commit comments

Comments
 (0)