Skip to content

Commit f4114fd

Browse files
committed
Fixup paths
1 parent 1e6c0a0 commit f4114fd

File tree

5 files changed

+108
-44
lines changed

5 files changed

+108
-44
lines changed

Main.ps1

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@ param(
44
[string]$SourceDir = "src"
55
)
66

7+
# Start the main timer
8+
$mainTimer = [System.Diagnostics.Stopwatch]::StartNew()
9+
$stepTimer = [System.Diagnostics.Stopwatch]::StartNew()
10+
711
# Import utility functions
812
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
913
. "$scriptPath/scripts/utils/Minimize.ps1" # To get better screenshots we need to minimize the "Administrator" CMD window
1014
. "$scriptPath/scripts/utils/TimedMessage.ps1"
1115
. "$scriptPath/scripts/utils/Invoke.ps1" # Function to invoke a script with a timeout
1216
. "$scriptPath/scripts/utils/Screenshot.ps1"
17+
. "$scriptPath/scripts/utils/Path.ps1"
1318

14-
# Start the main timer
15-
$mainTimer = [System.Diagnostics.Stopwatch]::StartNew()
16-
$stepTimer = [System.Diagnostics.Stopwatch]::StartNew()
1719

1820
Write-TimedMessage "Current directory: $(pwd)" -StartNewStep
19-
Write-Host "Using source directory: $SourceDir"
20-
$SourceDir = Normalize-Path $SourceDir
21-
Write-TimedMessage "Normalized source directory: $SourceDir"
21+
$srcDir = NormalizeDirPath $SourceDir
22+
Write-Host "Using source directory: $srcDir"
23+
$srcDir = GetAbsPath -path $srcDir -basePath (GetDirName $PSScriptRoot)
24+
Write-TimedMessage "Normalized source abs directory: $srcDir"
2225

2326
# Read name of the folders under the specified source directory into an array
24-
$folders = Get-ChildItem -Path "$PSScriptRoot/$SourceDir" -Directory | Select-Object -ExpandProperty Name
25-
Write-TimedMessage "Folders in ${SourceDir}: $folders" -StartNewStep
27+
$folders = Get-ChildItem -Path "$srcDir" -Directory | Select-Object -ExpandProperty Name
28+
Write-TimedMessage "Folders in ${srcDir}: $folders" -StartNewStep
2629

2730
# Check if the folders array is empty
2831
if ($folders.Count -eq 0) {
29-
Write-TimedMessage "No folders found in ${SourceDir}. Exiting script." -StartNewStep
32+
Write-TimedMessage "No folders found in ${srcDir}. Exiting script." -StartNewStep
3033
exit 1
3134
}
3235

@@ -50,15 +53,15 @@ function Get-OfficeApp {
5053
# Create a list of Office applications that are needed based on the file extensions of the folders
5154
Write-TimedMessage "Identifying required Office applications" -StartNewStep
5255
foreach ($folder in $folders) {
53-
$FileExtension = $folder.Substring($folder.LastIndexOf('.') + 1)
54-
$app = Get-OfficeApp -FileExtension $FileExtension
56+
$fileExtension = GetFileExtension $folder
57+
$app = Get-OfficeApp -FileExtension $fileExtension
5558

5659
if ($app) {
5760
if ($officeApps -notcontains $app) {
5861
$officeApps += $app
5962
}
6063
} else {
61-
Write-TimedMessage "Unknown file extension: $FileExtension. Skipping..."
64+
Write-TimedMessage "Unknown file extension: $fileExtension. Skipping..."
6265
continue
6366
}
6467
}
@@ -87,36 +90,36 @@ Write-TimedMessage "Window minimized"
8790

8891
foreach ($folder in $folders) {
8992
Write-TimedMessage "Processing folder: $folder" -StartNewStep
90-
$FileExtension = $folder.Substring($folder.LastIndexOf('.') + 1)
91-
$app = Get-OfficeApp -FileExtension $FileExtension
93+
$fileExtension = $folder.Substring($folder.LastIndexOf('.') + 1)
94+
$app = Get-OfficeApp -FileExtension $fileExtension
9295

9396
$ext = ""
9497
if ($app -ne "Access") {
9598
$ext = "zip"
9699
Write-TimedMessage "Creating Zip file and renaming to Office document target" -StartNewStep
97-
. "$PSScriptRoot/scripts/Zip-It.ps1" "${SourceDir}${folder}"
100+
. "$PSScriptRoot/scripts/Zip-It.ps1" "${srcDir}${folder}"
98101
Write-TimedMessage "Zip file created"
99102
}
100103
else {
101104
$ext = "accdb"
102105
Write-TimedMessage "Copying folder and content to Skeleton folder" -StartNewStep
103-
Copy-Item -Path "${SourceDir}${folder}/DBSource" -Destination "${SourceDir}${folder}/Skeleton" -Recurse -Force
106+
Copy-Item -Path "${srcDir}${folder}/DBSource" -Destination "${srcDir}${folder}/Skeleton" -Recurse -Force
104107
Write-TimedMessage "Folder copied"
105108
}
106109

107110
Write-TimedMessage "Copying and renaming file to correct name" -StartNewStep
108-
. "$PSScriptRoot/scripts/Rename-It.ps1" "${SourceDir}${folder}" "$ext"
111+
. "$PSScriptRoot/scripts/Rename-It.ps1" "${srcDir}${folder}" "$ext"
109112
Write-TimedMessage "File renamed"
110113

111114
Write-TimedMessage "Importing VBA code into Office document" -StartNewStep
112115
# Replace the direct Build-VBA call with the timeout version
113116
$buildVbaScriptPath = "$PSScriptRoot/scripts/Build-VBA.ps1"
114-
$success = Invoke-ScriptWithTimeout -ScriptPath $buildVbaScriptPath -Arguments @("${SourceDir}${folder}", "$app") -TimeoutSeconds 30
117+
$success = Invoke-ScriptWithTimeout -ScriptPath $buildVbaScriptPath -Arguments @("${srcDir}${folder}", "$app") -TimeoutSeconds 30
115118

116119
if (-not $success) {
117120
Write-TimedMessage "🔴 Build-VBA.ps1 execution timed out or failed for ${folder}. Continuing with next file..."
118121

119-
$screenshotDir = ${SourceDir} + "screenshots/"
122+
$screenshotDir = ${srcDir} + "screenshots/"
120123
if (-not (Test-Path $screenshotDir)) {
121124
New-Item -ItemType Directory -Path $screenshotDir -Force | Out-Null
122125
Write-Host "Created screenshot directory: $screenshotDir"

scripts/Build-VBA.ps1

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,69 @@ if ($officeAppName -eq "Excel") {
108108
$doc = $officeApp.Presentations.Open($outputFilePath)
109109
} elseif ($officeAppName -eq "Access") {
110110
try {
111-
if ($officeApp.CurrentDb -ne $null) {
112-
$officeApp.CloseCurrentDatabase()
111+
# Check if the file exists and is accessible
112+
if (-not (Test-Path $outputFilePath)) {
113+
Write-Host "🔴 Error: Access database file not found: $outputFilePath"
114+
exit 1
113115
}
114116

115-
# Set default database properties
116-
$officeApp.DefaultOpenExclusive = $false # Initially try shared mode
117+
# Close any existing database
118+
try {
119+
if ($null -ne $officeApp.CurrentDb) {
120+
$officeApp.CloseCurrentDatabase()
121+
Start-Sleep -Seconds 1
122+
}
123+
} catch {
124+
# Ignore errors when checking CurrentDb - it may throw if no database is open
125+
}
117126

118-
# First attempt
127+
# First attempt - normal open
128+
Write-Host "Attempting to open Access database in shared mode..."
119129
$doc = $officeApp.OpenCurrentDatabase($outputFilePath)
120130

121-
# If we got $null or can't modify VBA project, try exclusive mode
122-
if ($null -eq $doc -or $null -eq $doc.VBProject) {
123-
$officeApp.CloseCurrentDatabase()
124-
Start-Sleep -Seconds 1
125-
$officeApp.DefaultOpenExclusive = $true # Try exclusive mode
126-
$doc = $officeApp.OpenCurrentDatabase($outputFilePath)
131+
# Check if VBA project is accessible
132+
$vbaAccessible = $false
133+
try {
134+
if ($null -ne $doc -and $null -ne $doc.VBProject) {
135+
$vbaAccessible = $true
136+
Write-Host "VBA Project accessible in shared mode"
137+
}
138+
} catch {
139+
Write-Host "Cannot access VBA Project in shared mode: $($_.Exception.Message)"
140+
}
141+
142+
# If VBA project isn't accessible, try reopening with different flags
143+
if (-not $vbaAccessible) {
144+
Write-Host "Attempting to reopen database with exclusive access..."
145+
try {
146+
$officeApp.CloseCurrentDatabase()
147+
Start-Sleep -Seconds 1
148+
149+
# Try a different approach for exclusive access
150+
# Some versions of Access use different methods
151+
152+
# For newer Access versions
153+
$officeApp.OpenCurrentDatabase($outputFilePath, $true) # $true = exclusive mode
154+
$doc = $officeApp.CurrentDb
155+
} catch {
156+
Write-Host "Failed with exclusive flag: $($_.Exception.Message)"
157+
158+
# For older Access versions - one last attempt
159+
try {
160+
$officeApp.Quit()
161+
Start-Sleep -Seconds 2
162+
$officeApp = New-Object -ComObject "Access.Application"
163+
$officeApp.Visible = $true
164+
165+
# Open in exclusive mode
166+
# Use a slightly different technique as a last resort
167+
$officeApp.OpenCurrentDatabase($outputFilePath, $true)
168+
$doc = $officeApp.CurrentDb
169+
} catch {
170+
Write-Host "🔴 Error: Failed to open the database after multiple attempts: $($_.Exception.Message)"
171+
exit 1
172+
}
173+
}
127174
}
128175
}
129176
catch {

scripts/Rename-It.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ if (-not $ext) {
1414
exit 1
1515
}
1616

17-
$sourceDir = $folderName.Substring(0, $folderName.LastIndexOf('/'))
17+
# Import utility functions
18+
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
19+
. "$scriptPath/utils/Path.ps1"
1820

19-
$filNameWithExtension = $folderName.Substring($folderName.LastIndexOf('/') + 1)
20-
$fileName = $filNameWithExtension.Substring(0, $filNameWithExtension.LastIndexOf('.'))
21-
$fileExtension = $filNameWithExtension.Substring($filNameWithExtension.LastIndexOf('.') + 1)
21+
$rootSrcDir = DirUp (NormalizeDirPath $folderName)
22+
23+
$currentDir = (Get-Location).Path + "/"
24+
$srcDir = GetAbsPath -path $folderName -basePath $currentDir
25+
26+
$fileName = GetDirName $srcDir
27+
$fileNameNoExt = $fileName.Substring(0, $fileName.LastIndexOf('.'))
28+
$fileExtension = $fileName.Substring($fileName.LastIndexOf('.') + 1)
2229

2330
# Create a copy of the zip/document file in the $folderName/Skeleton folder at the top level
24-
$copySource = "$folderName/Skeleton/$fileName.$ext"
25-
$renameDestinationFolder = "$sourceDir/out"
26-
$renameDestinationFilePath = "$renameDestinationFolder/$fileName.$fileExtension"
31+
$copySource = "$folderName/Skeleton/$fileNameNoExt.$ext"
32+
$renameDestinationFolder = "$rootSrcDir/out"
33+
$renameDestinationFilePath = "$renameDestinationFolder/$fileNameNoExt.$fileExtension"
2734

2835
# Create rename destination folder if it doesn't exist
2936
if (-not (Test-Path $renameDestinationFolder)) {

scripts/Zip-It.ps1

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ if (-not $folderName) {
77
exit 1
88
}
99

10-
$sourceDir = $folderName.Substring(0, $folderName.LastIndexOf('/'))
11-
12-
$filNameWithExtension = $folderName.Substring($folderName.LastIndexOf('/') + 1)
13-
$fileName = $filNameWithExtension.Substring(0, $filNameWithExtension.LastIndexOf('.'))
14-
$fileExtension = $filNameWithExtension.Substring($filNameWithExtension.LastIndexOf('.') + 1)
10+
$fileName = GetDirName $folderName
11+
$fileNameNoExt = $fileName.Substring(0, $fileName.LastIndexOf('.'))
12+
$fileExtension = $fileName.Substring($fileName.LastIndexOf('.') + 1)
1513

1614
Write-Host "Staring the compression process..."
1715

@@ -20,7 +18,7 @@ Write-Host "Current directory: $currentDir"
2018

2119
# Define the source folder and the output zip file
2220
$sourceFolder = Join-Path -Path $currentDir -ChildPath "$folderName/XMLsource/"
23-
$outputZipFile = Join-Path -Path $currentDir -ChildPath "$folderName/Skeleton/$fileName.zip"
21+
$outputZipFile = Join-Path -Path $currentDir -ChildPath "$folderName/Skeleton/$fileNameNoExt.zip"
2422

2523
# Path to the 7-Zip executable
2624
$sevenZipPath = "7z" # Assumes 7-Zip is in the system PATH. Adjust if necessary.
@@ -73,7 +71,7 @@ Write-Host "Current directory after change: $(Get-Location)"
7371

7472
# Compress the files and folders using 7-Zip
7573
Write-Host "Compressing files in $sourceFolder to $absoluteDestinationFolder..."
76-
& $sevenZipPath a -tzip "$absoluteDestinationFolder/$fileName.zip" "*" | Out-Null
74+
& $sevenZipPath a -tzip "$absoluteDestinationFolder/$fileNameNoExt.zip" "*" | Out-Null
7775

7876
# Check if the compression was successful using $LASTEXITCODE
7977
if ($LASTEXITCODE -ne 0) {

scripts/utils/Path.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ function GetFileNameWithoutExtension {
5656
return $temp.Substring(0, $temp.LastIndexOf('.'))
5757
}
5858

59+
function GetFileExtension {
60+
param (
61+
[string]$path
62+
)
63+
$path = NormalizeFilePath($path)
64+
$temp = GetFileNameFromPath($path)
65+
return $temp.Substring($temp.LastIndexOf('.') + 1)
66+
}
67+
5968
function GetAbsPath {
6069
param (
6170
[string]$path,

0 commit comments

Comments
 (0)