Skip to content

Commit

Permalink
throw errors, catch them
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garriss committed Jul 9, 2024
1 parent aa938b8 commit a2ce7d8
Showing 1 changed file with 86 additions and 64 deletions.
150 changes: 86 additions & 64 deletions utils/workflow/Publish-ScubaGear.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -127,61 +127,68 @@ function Publish-ScubaGearModule {
$NuGetApiKey
)

Write-Output "Copying the module to a temp location..."
$ModuleDestinationPath = Copy-ModuleToTempLocation `
-ModuleSourcePath $ModuleSourcePath `
-ModuleTempPath $env:TEMP

Write-Output "Editing the manifest file..."
Edit-ManifestFile `
-ModuleDestinationPath $ModuleDestinationPath `
-OverrideModuleVersion $OverrideModuleVersion `
-PrereleaseTag $PrereleaseTag

Write-Output "Creating an array of the files to sign..."
$ArrayOfFilePaths = New-ArrayOfFilePaths `
-ModuleDestinationPath $ModuleDestinationPath

Write-Output "Creating a file with a list of the files to sign..."
$FileListFileName = New-FileList `
-ArrayOfFilePaths $ArrayOfFilePaths

Write-Output "Calling AzureSignTool function to sign scripts, manifest, and modules..."
Use-AzureSignTool `
-AzureKeyVaultUrl $AzureKeyVaultUrl `
-CertificateName $CertificateName `
-FileList $FileListFileName

Write-Output "Creating the catalog list file..."
$ReturnObject = New-ScubaCatalogFile `
try {
# Most of the functions called below can throw an error if something goes wrong,
# hence the try-catch block.

Write-Output "Copying the module to a temp location..."
$ModuleDestinationPath = Copy-ModuleToTempLocation `
-ModuleSourcePath $ModuleSourcePath `
-ModuleTempPath $env:TEMP

Write-Output "Editing the manifest file..."
Edit-ManifestFile `
-ModuleDestinationPath $ModuleDestinationPath `
-OverrideModuleVersion $OverrideModuleVersion `
-PrereleaseTag $PrereleaseTag

Write-Output "Creating an array of the files to sign..."
$ArrayOfFilePaths = New-ArrayOfFilePaths `
-ModuleDestinationPath $ModuleDestinationPath
Write-Output $ReturnObject
$CatalogFilePath = $($ReturnObject.CatalogFilePath)
$CatalogList = $($ReturnObject.TempCatalogList)

Write-Output "Calling AzureSignTool function to sign catalog list..."
Use-AzureSignTool `
-AzureKeyVaultUrl $AzureKeyVaultUrl `
-CertificateName $CertificateName `
-FileList $CatalogList

Write-Output "Testing the catalog file..."
Test-ScubaCatalogFile `
-CatalogFilePath $CatalogFilePath

$Parameters = @{
Path = $ModuleDestinationPath
Repository = $GalleryName
}
if ($GalleryName -eq 'PSGallery') {
$Parameters.Add('NuGetApiKey', $NuGetApiKey)
}

Write-Output "The ScubaGear module will be published..."
# The -Force parameter is only required if the new version is less than or equal to
# the current version, which is typically only true when testing.
# Publish-Module @Parameters -Force
Write-Output "Creating a file with a list of the files to sign..."
$FileListFileName = New-FileList `
-ArrayOfFilePaths $ArrayOfFilePaths

Write-Output "Calling AzureSignTool function to sign scripts, manifest, and modules..."
Use-AzureSignTool `
-AzureKeyVaultUrl $AzureKeyVaultUrl `
-CertificateName $CertificateName `
-FileList $FileListFileName

Write-Output "Creating the catalog list file..."
$ReturnObject = New-ScubaCatalogFile `
-ModuleDestinationPath $ModuleDestinationPath
$CatalogFilePath = $($ReturnObject.CatalogFilePath)
$CatalogList = $($ReturnObject.TempCatalogList)

Write-Output "Calling AzureSignTool function to sign catalog list..."
Use-AzureSignTool `
-AzureKeyVaultUrl $AzureKeyVaultUrl `
-CertificateName $CertificateName `
-FileList $CatalogList

Write-Output "Testing the catalog file..."
Test-ScubaCatalogFile `
-CatalogFilePath $CatalogFilePath

$Parameters = @{
Path = $ModuleDestinationPath
Repository = $GalleryName
}
if ($GalleryName -eq 'PSGallery') {
$Parameters.Add('NuGetApiKey', $NuGetApiKey)
}

Write-Output "The ScubaGear module will be published..."
# The -Force parameter is only required if the new version is less than or equal to
# the current version, which is typically only true when testing.
# Publish-Module @Parameters -Force
}
catch {
Write-Error "An error occurred which publishing ScubaGear. Exiting..."
exit 1
}
}

function Copy-ModuleToTempLocation {
Expand Down Expand Up @@ -221,7 +228,9 @@ function Copy-ModuleToTempLocation {
Write-Warning "The module desintination path exists."
}
else {
Write-Error "Failed to find the module desintination path."
$ErrorMessage = "Failed to find the module desintination path."
Write-Error = $ErrorMessage
throw $ErrorMessage
}

return $ModuleDestinationPath
Expand Down Expand Up @@ -255,7 +264,9 @@ function Edit-ManifestFile {
Write-Warning "The manifest file exists."
}
else {
Write-Error "Failed to find the manifest file."
$ErrorMessage = "Failed to find the manifest file."
Write-Error = $ErrorMessage
throw $ErrorMessage
}

# The module needs some version
Expand Down Expand Up @@ -299,7 +310,9 @@ function Edit-ManifestFile {
catch {
Write-Warning "Error: Cannot edit the module because:"
Write-Warning $_.Exception
Write-Error "Failed to edit the module manifest."
$ErrorMessage = "Failed to edit the module manifest."
Write-Error = $ErrorMessage
throw $ErrorMessage
}
try {
$CurrentErrorActionPreference = $ErrorActionPreference
Expand All @@ -310,7 +323,9 @@ function Edit-ManifestFile {
catch {
Write-Warning "Error: Cannot test the manifest file because:"
Write-Warning $_.Exception
Write-Error "Failed to test the manifest file."
$ErrorMessage = "Failed to test the manifest file."
Write-Error = $ErrorMessage
throw $ErrorMessage
}
}

Expand Down Expand Up @@ -340,7 +355,9 @@ function New-ArrayOfFilePaths {
Write-Warning "Found $($ArrayOfFilePaths.Count) files to sign"
}
else {
Write-Error "Failed to find any .ps1, .psm1, or .psd files."
$ErrorMessage = "Failed to find any .ps1, .psm1, or .psd1 files."
Write-Error = $ErrorMessage
throw $ErrorMessage
}

return $ArrayOfFilePaths
Expand All @@ -367,7 +384,9 @@ function New-FileList {
Write-Warning "The list file exists."
}
else {
Write-Error "Failed to find the list file."
$ErrorMessage = "Failed to find the list file."
Write-Error = $ErrorMessage
throw $ErrorMessage
}

return $FileListFileName
Expand Down Expand Up @@ -415,7 +434,9 @@ function Use-AzureSignTool {
# Get-Command returns a System.Management.Automation.ApplicationInfo object
$NumberOfCommands = (Get-Command AzureSignTool) # Should return 1
if ($NumberOfCommands -eq 0) {
Write-Error "Failed to find the AzureSignTool on this system."
$ErrorMessage = "Failed to find the AzureSignTool on this system."
Write-Error = $ErrorMessage
throw $ErrorMessage
}
$ToolPath = (Get-Command AzureSignTool).Path
Write-Warning "The path to AzureSignTool is $ToolPath"
Expand All @@ -432,9 +453,9 @@ function Use-AzureSignTool {
Write-Warning "Signed the filelist without errors."
}
else {
# Write-Warning "The sign results:"
# Write-Warning $Results
Write-Error "Failed to sign the filelist without errors."
$ErrorMessage = "Failed to sign the filelist without errors."
Write-Error = $ErrorMessage
throw $ErrorMessage
}
}

Expand Down Expand Up @@ -491,7 +512,8 @@ function Test-ScubaCatalogFile {
Write-Warning "Signing the module was successful."
}
else {
Write-Error "Signing the module was NOT successful."
return 1
$ErrorMessage = "Signing the module was NOT successful."
Write-Error = $ErrorMessage
throw $ErrorMessage
}
}

0 comments on commit a2ce7d8

Please sign in to comment.