1
1
# 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/Write-TimedMessage.ps1"
7
+ . " $scriptPath /scripts/utils/Invoke.ps1" # Function to invoke a script with a timeout
8
+
2
9
param (
3
10
[string ]$SourceDir = " src"
4
11
)
5
12
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 "
8
19
9
20
# Read name of the folders under the specified source directory into an array
10
21
$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
12
23
13
24
# Check if the folders array is empty
14
25
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
16
27
exit 1
17
28
}
18
29
@@ -34,6 +45,7 @@ function Get-OfficeApp {
34
45
}
35
46
36
47
# 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
37
49
foreach ($folder in $folders ) {
38
50
$FileExtension = $folder.Substring ($folder.LastIndexOf (' .' ) + 1 )
39
51
$app = Get-OfficeApp - FileExtension $FileExtension
@@ -43,50 +55,66 @@ foreach ($folder in $folders) {
43
55
$officeApps += $app
44
56
}
45
57
} else {
46
- Write-Host " Unknown file extension: $FileExtension . Skipping..."
58
+ Write-TimedMessage " Unknown file extension: $FileExtension . Skipping..."
47
59
continue
48
60
}
49
61
}
62
+ Write-TimedMessage " Required Office applications: $officeApps "
50
63
51
64
# 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
53
66
. " $PSScriptRoot /scripts/Open-Close-Office.ps1" $officeApps
67
+ Write-TimedMessage " Completed opening and closing Office applications"
54
68
Write-Host " ========================="
55
69
56
70
# Enable VBOM for each Office application
57
71
foreach ($app in $officeApps ) {
58
- Write-Host " Enabling VBOM for $app "
72
+ Write-TimedMessage " Enabling VBOM for $app " - StartNewStep
59
73
. " $PSScriptRoot /scripts/Enable-VBOM.ps1" $app
74
+ Write-TimedMessage " VBOM enabled for $app "
60
75
Write-Host " ========================="
61
76
}
62
77
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"
66
78
79
+ Write-TimedMessage " Minimizing Administrator window" - StartNewStep
67
80
Minimize- Window " Administrator: C:\actions"
81
+ Write-TimedMessage " Window minimized"
82
+
68
83
69
84
70
85
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
72
89
73
90
$ext = " "
74
91
if ($app -ne " Access" ) {
75
92
$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
77
94
. " $PSScriptRoot /scripts/Zip-It.ps1" " ${SourceDir} /${folder} "
95
+ Write-TimedMessage " Zip file created"
78
96
}
79
97
else {
80
98
$ext = " accdb"
81
- Write-Host " Copy folder and content to Skeleton folder"
99
+ Write-TimedMessage " Copying folder and content to Skeleton folder" - StartNewStep
82
100
Copy-Item - Path " ${SourceDir} /${folder} /DBSource" - Destination " ${SourceDir} /${folder} /Skeleton" - Recurse - Force
101
+ Write-TimedMessage " Folder copied"
83
102
}
84
103
85
- Write-Host " Copy and rename the file to the correct name"
104
+ Write-TimedMessage " Copying and renaming file to correct name" - StartNewStep
86
105
. " $PSScriptRoot /scripts/Rename-It.ps1" " ${SourceDir} /${folder} " " $ext "
106
+ Write-TimedMessage " File renamed"
87
107
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
+ }
90
117
118
+ Write-TimedMessage " Completed processing folder: $folder "
91
119
Write-Host " ========================="
92
120
}
0 commit comments