Skip to content

Commit 7d479fe

Browse files
committed
Better time monitoring
1 parent 0104b26 commit 7d479fe

File tree

5 files changed

+104
-19
lines changed

5 files changed

+104
-19
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
if-no-files-found: warn
3030
# TODO: Check if better method to visualize the screenshots: https://github.com/actions/upload-artifact/issues/14
3131
- name: "Take exiting screenshot"
32+
if: always()
3233
run: |
3334
. "./scripts/utils/Screenshot.ps1"
3435
Take-Screenshot -OutputPath ${{ github.workspace }}/tests/screenshots/ExitScreenshot.png

Main.ps1

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
# Get the source directory from command line argument or use default "src"
2+
3+
# Import utility functions
4+
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
. "$scriptPath/scripts/utils/Minimize.ps1" # To get better screenshots we need to minimize the "Administrator" CMD window
6+
. "$scriptPath/scripts/utils/TimedMessage.ps1"
7+
. "$scriptPath/scripts/utils/Invoke.ps1" # Function to invoke a script with a timeout
8+
29
param(
310
[string]$SourceDir = "src"
411
)
512

6-
Write-Host "Current directory: $(pwd)"
7-
Write-Host "Using source directory: $SourceDir"
13+
# Start the main timer
14+
$mainTimer = [System.Diagnostics.Stopwatch]::StartNew()
15+
$stepTimer = [System.Diagnostics.Stopwatch]::StartNew()
16+
17+
Write-TimedMessage "Current directory: $(pwd)" -StartNewStep
18+
Write-TimedMessage "Using source directory: $SourceDir"
819

920
# Read name of the folders under the specified source directory into an array
1021
$folders = Get-ChildItem -Path "$PSScriptRoot/$SourceDir" -Directory | Select-Object -ExpandProperty Name
11-
Write-Host "Folders in ${SourceDir}: $folders"
22+
Write-TimedMessage "Folders in ${SourceDir}: $folders" -StartNewStep
1223

1324
# Check if the folders array is empty
1425
if ($folders.Count -eq 0) {
15-
Write-Host "No folders found in ${SourceDir}. Exiting script."
26+
Write-TimedMessage "No folders found in ${SourceDir}. Exiting script." -StartNewStep
1627
exit 1
1728
}
1829

@@ -34,6 +45,7 @@ function Get-OfficeApp {
3445
}
3546

3647
# Create a list of Office applications that are needed based on the file extensions of the folders
48+
Write-TimedMessage "Identifying required Office applications" -StartNewStep
3749
foreach ($folder in $folders) {
3850
$FileExtension = $folder.Substring($folder.LastIndexOf('.') + 1)
3951
$app = Get-OfficeApp -FileExtension $FileExtension
@@ -43,50 +55,66 @@ foreach ($folder in $folders) {
4355
$officeApps += $app
4456
}
4557
} else {
46-
Write-Host "Unknown file extension: $FileExtension. Skipping..."
58+
Write-TimedMessage "Unknown file extension: $FileExtension. Skipping..."
4759
continue
4860
}
4961
}
62+
Write-TimedMessage "Required Office applications: $officeApps"
5063

5164
# We need to open and close the Office applications before we can enable VBOM
52-
Write-Host "Open and close Office applications"
65+
Write-TimedMessage "Open and close Office applications" -StartNewStep
5366
. "$PSScriptRoot/scripts/Open-Close-Office.ps1" $officeApps
67+
Write-TimedMessage "Completed opening and closing Office applications"
5468
Write-Host "========================="
5569

5670
# Enable VBOM for each Office application
5771
foreach ($app in $officeApps) {
58-
Write-Host "Enabling VBOM for $app"
72+
Write-TimedMessage "Enabling VBOM for $app" -StartNewStep
5973
. "$PSScriptRoot/scripts/Enable-VBOM.ps1" $app
74+
Write-TimedMessage "VBOM enabled for $app"
6075
Write-Host "========================="
6176
}
6277

63-
# To get better screenshots we need to minimize the "Administrator" CMD window
64-
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
65-
. "$scriptPath/scripts/utils/Minimize.ps1"
6678

79+
Write-TimedMessage "Minimizing Administrator window" -StartNewStep
6780
Minimize-Window "Administrator: C:\actions"
81+
Write-TimedMessage "Window minimized"
82+
6883

6984

7085
foreach ($folder in $folders) {
71-
$app = Get-OfficeApp -FileExtension $folder.Substring($folder.LastIndexOf('.') + 1)
86+
Write-TimedMessage "Processing folder: $folder" -StartNewStep
87+
$FileExtension = $folder.Substring($folder.LastIndexOf('.') + 1)
88+
$app = Get-OfficeApp -FileExtension $FileExtension
7289

7390
$ext = ""
7491
if ($app -ne "Access") {
7592
$ext = "zip"
76-
Write-Host "Create Zip file and rename it to Office document target"
93+
Write-TimedMessage "Creating Zip file and renaming to Office document target" -StartNewStep
7794
. "$PSScriptRoot/scripts/Zip-It.ps1" "${SourceDir}/${folder}"
95+
Write-TimedMessage "Zip file created"
7896
}
7997
else {
8098
$ext = "accdb"
81-
Write-Host "Copy folder and content to Skeleton folder"
99+
Write-TimedMessage "Copying folder and content to Skeleton folder" -StartNewStep
82100
Copy-Item -Path "${SourceDir}/${folder}/DBSource" -Destination "${SourceDir}/${folder}/Skeleton" -Recurse -Force
101+
Write-TimedMessage "Folder copied"
83102
}
84103

85-
Write-Host "Copy and rename the file to the correct name"
104+
Write-TimedMessage "Copying and renaming file to correct name" -StartNewStep
86105
. "$PSScriptRoot/scripts/Rename-It.ps1" "${SourceDir}/${folder}" "$ext"
106+
Write-TimedMessage "File renamed"
87107

88-
Write-Host "Importing VBA code into Office document"
89-
. "$PSScriptRoot/scripts/Build-VBA.ps1" "${SourceDir}/${folder}" "$app"
108+
Write-TimedMessage "Importing VBA code into Office document" -StartNewStep
109+
# Replace the direct Build-VBA call with the timeout version
110+
$buildVbaScriptPath = "$PSScriptRoot/scripts/Build-VBA.ps1"
111+
$success = Invoke-ScriptWithTimeout -ScriptPath $buildVbaScriptPath -Arguments @("${SourceDir}/${folder}", "$app") -TimeoutSeconds 300
112+
113+
if (-not $success) {
114+
Write-TimedMessage "🔴 Build-VBA.ps1 execution timed out or failed for ${folder}. Continuing with next file..." -ForegroundColor Yellow
115+
# Optionally add cleanup code here
116+
}
90117

118+
Write-TimedMessage "Completed processing folder: $folder"
91119
Write-Host "========================="
92120
}

scripts/Build-VBA.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ try {
192192
$tempPath = [System.IO.Path]::GetTempFileName() -replace '\.tmp$', '.pptm'
193193
Write-Host "Attempting to save to temporary location: $tempPath"
194194
$doc.SaveAs($tempPath)
195+
Write-Host "Temporary file saved successfully at: $tempPath"
195196

196197
# Close the document and application
197198
$doc.Close()
@@ -205,8 +206,8 @@ try {
205206
Start-Sleep -Seconds 2
206207

207208
# Copy the temp file to the intended destination
208-
Copy-Item -Path $tempPath -Destination $outputFilePath -Force
209-
Remove-Item -Path $tempPath -Force
209+
Copy-Item -Path $tempPath -Destination $outputFilePath -Force
210+
Remove-Item -Path $tempPath -Force
210211

211212
Write-Host "Document saved using alternative method"
212213

@@ -222,9 +223,21 @@ try {
222223

223224
# Call the WriteToFile macro to check if the module was imported correctly
224225
try {
226+
227+
$vbaModule = $doc.VBProject.VBComponents.Item(1)
228+
if ($null -eq $vbaModule) {
229+
Write-Host "Error: No VBA module found in the document."
230+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
231+
exit 1
232+
}
233+
Write-Host "VBA module found: $($vbaModule.Name)"
234+
225235
$macroName = "WriteToFile"
236+
Write-Host "Macro to execute: $macroName"
237+
Write-Host "Application state before macro execution: Type=$($officeApp.GetType().FullName)"
226238
$officeApp.Run($macroName)
227-
239+
Write-Host "Macro finished"
240+
228241
# Check if the file was created successfully with the correct content
229242
$outputFile = "$outputDir/$fileNameNoExt.txt"
230243
if (Test-Path $outputFile) {

scripts/utils/Invoke.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
function Invoke-ScriptWithTimeout {
3+
param (
4+
[string]$ScriptPath,
5+
[array]$Arguments,
6+
[int]$TimeoutSeconds = 300 # 5 minutes default timeout
7+
)
8+
9+
$job = Start-Job -ScriptBlock {
10+
param($scriptPath, $args)
11+
& $scriptPath $args
12+
} -ArgumentList $ScriptPath, $Arguments
13+
14+
$completed = Wait-Job -Job $job -Timeout $TimeoutSeconds
15+
16+
if ($completed -eq $null) {
17+
Write-Host "Script execution timed out after $TimeoutSeconds seconds" -ForegroundColor Red
18+
Stop-Job -Job $job
19+
Remove-Job -Job $job -Force
20+
return $false
21+
} else {
22+
$result = Receive-Job -Job $job
23+
Remove-Job -Job $job
24+
Write-Host $result
25+
return $true
26+
}
27+
}

scripts/utils/TimedMessage.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function Write-TimedMessage {
2+
param (
3+
[Parameter(Mandatory=$true)]
4+
[string]$Message,
5+
[switch]$StartNewStep
6+
)
7+
8+
$stepTime = $stepTimer.Elapsed.ToString("hh\:mm\:ss\.fff")
9+
$totalTime = $mainTimer.Elapsed.ToString("hh\:mm\:ss\.fff")
10+
11+
Write-Host "[$totalTime total | $stepTime step] $Message"
12+
13+
if ($StartNewStep) {
14+
$script:stepTimer.Restart()
15+
}
16+
}

0 commit comments

Comments
 (0)