Skip to content

Commit 48ba4e2

Browse files
committed
Add version checks and error handling.
1 parent 7e83d2c commit 48ba4e2

1 file changed

Lines changed: 51 additions & 30 deletions

File tree

build/Install-Dependencies.ps1

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,70 @@
11
# ------------------------------------------------------------------------------
22
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3-
# ------------------------------------------------------------------------------
3+
#
44

5-
[cmdletbinding()]
5+
[CmdletBinding()]
66
param(
7-
[ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })]
8-
[string]
9-
$ModuleName = 'Entra',
7+
# The name of the module to install dependencies for. The module name should match the folder name in the module folder. If not provided, the script will default to 'Entra'.
8+
[ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })]
9+
[string]
10+
$ModuleName = 'Entra',
1011

11-
[ValidateScript({ Test-Path $_ })]
12-
[string]
13-
$ModuleSettingsPath,
12+
# Path to the ModuleSettings.json file. If not provided, the script will look for the file in the module folder.
13+
[ValidateScript({ Test-Path $_ })]
14+
[string]
15+
$ModuleSettingsPath,
1416

15-
# Force installation of required modules, even if they are already installed.
16-
[switch]
17-
$Force
17+
# Force installation of the required modules, even if they are already installed.
18+
[switch]
19+
$Force
1820
)
1921

20-
. "$psscriptroot/common-functions.ps1"
22+
try {
23+
# Review: are the common functions required for this script? There does not seem to be any dependencies on them [yet?].
24+
. "$PSScriptRoot/common-functions.ps1"
25+
} catch {
26+
Write-Error -Message "Failed to load common-functions.ps1. Error: $_" -RecommendedAction "This script should be run from the 'build' folder. Ensure 'common-functions.ps1' exists and is accessible."
27+
}
2128

22-
$settingPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
23-
if ($ModuleSettingsPath) { $settingPath = $ModuleSettingsPath }
24-
$content = Get-Content -Path $settingPath | ConvertFrom-Json
29+
if ($ModuleSettingsPath) {
30+
$SettingsPath = $ModuleSettingsPath
31+
} else {
32+
$SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
33+
}
34+
$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json
35+
$RequiredVersion = $ModuleSettings.destinationModuleVersion
2536

2637
if ($Force) {
27-
Write-Verbose 'Skipping the check for installed prerequisites.'
38+
Write-Verbose 'Skipping the check for installed prerequisites. Forcing the installation of all required modules.'
2839
} else {
29-
Write-Verbose 'Checking installed modules for required dependencies.'
30-
$InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property Name
40+
Write-Verbose 'Checking installed modules for required dependencies.'
41+
$InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property Name
3142
}
3243

33-
$SourceModule = $content.sourceModule
44+
# Install the AzureAD module.
45+
$SourceModule = $ModuleSettings.sourceModule
3446
if (($InstalledModules.Name -contains $SourceModule) -and -not $Force) {
35-
Write-Verbose "The $SourceModule module is already installed."
47+
Write-Verbose "The $SourceModule module is already installed."
3648
} else {
37-
Write-Verbose("Installing Module: $sourceModule")
38-
Install-Module $sourceModule -Scope CurrentUser -Force -AllowClobber
49+
Write-Verbose("Installing Module: $sourceModule")
50+
try {
51+
Install-Module $sourceModule -Scope CurrentUser -Force -AllowClobber
52+
} catch {
53+
Write-Error "Failed to install the $sourceModule module. Error: $_"
54+
}
3955
}
4056

41-
foreach ($moduleName in $content.destinationModuleName) {
42-
$InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName }).Group
43-
if (($InstalledModuleReference.Version -ge $content.destinationModuleVersion) -and -not $Force) {
44-
Write-Verbose "The $moduleName module is already installed."
45-
} else {
46-
Write-Verbose "Installing Module: $moduleName"
47-
Install-Module $moduleName -Scope CurrentUser -RequiredVersion $content.destinationModuleVersion -Force -AllowClobber
48-
}
57+
# Install the required module dependencies if missing the required version or if -Force.
58+
foreach ($moduleName in $ModuleSettings.destinationModuleName) {
59+
$InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName })
60+
if (-not $InstalledModuleReference -or $Force) {
61+
Write-Verbose "Installing version $RequiredVersion of $moduleName"
62+
try {
63+
Install-Module $moduleName -Scope CurrentUser -RequiredVersion $RequiredVersion -Force -AllowClobber
64+
} catch {
65+
Write-Error "Failed to install module $moduleName ${RequiredVersion}. Error: $_"
66+
}
67+
} elseif ($InstalledModuleReference.Group.Version -contains $RequiredVersion) {
68+
Write-Verbose "Found version $RequiredVersion of $moduleName"
69+
}
4970
}

0 commit comments

Comments
 (0)