Skip to content

Commit f2cd6f9

Browse files
committed
Add version checks and error handling.
1 parent 0ff9488 commit f2cd6f9

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

build/Install-Dependencies.ps1

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

55
[CmdletBinding()]
66
param(
@@ -19,6 +19,7 @@ param(
1919
$Force
2020
)
2121

22+
<<<<<<< HEAD
2223
# Get module settings from the relevant ModuleSettings.json file or a file that is manually specified.
2324
if ($ModuleSettingsPath) {
2425
$SettingsPath = $ModuleSettingsPath
@@ -73,5 +74,54 @@ foreach ($DestinationModuleName in $ModuleSettings.destinationModuleName) {
7374
} catch {
7475
Write-Warning "Failed to update module '$DestinationModuleName' to version ${RequiredVersion}. Error: $_"
7576
}
77+
=======
78+
try {
79+
# Review: are the common functions required for this script? There does not seem to be any dependencies on them [yet?].
80+
. "$PSScriptRoot/common-functions.ps1"
81+
} catch {
82+
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."
83+
}
84+
85+
if ($ModuleSettingsPath) {
86+
$SettingsPath = $ModuleSettingsPath
87+
} else {
88+
$SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
89+
}
90+
$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json
91+
$RequiredVersion = $ModuleSettings.destinationModuleVersion
92+
93+
if ($Force) {
94+
Write-Verbose 'Skipping the check for installed prerequisites. Forcing the installation of all required modules.'
95+
} else {
96+
Write-Verbose 'Checking installed modules for required dependencies.'
97+
$InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property Name
98+
}
99+
100+
# Install the AzureAD module.
101+
$SourceModule = $ModuleSettings.sourceModule
102+
if (($InstalledModules.Name -contains $SourceModule) -and -not $Force) {
103+
Write-Verbose "The $SourceModule module is already installed."
104+
} else {
105+
Write-Verbose("Installing Module: $sourceModule")
106+
try {
107+
Install-Module $sourceModule -Scope CurrentUser -Force -AllowClobber
108+
} catch {
109+
Write-Error "Failed to install the $sourceModule module. Error: $_"
110+
}
111+
}
112+
113+
# Install the required module dependencies if missing the required version or if -Force.
114+
foreach ($moduleName in $ModuleSettings.destinationModuleName) {
115+
$InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName })
116+
if (-not $InstalledModuleReference -or $Force) {
117+
Write-Verbose "Installing version $RequiredVersion of $moduleName"
118+
try {
119+
Install-Module $moduleName -Scope CurrentUser -RequiredVersion $RequiredVersion -Force -AllowClobber
120+
} catch {
121+
Write-Error "Failed to install module $moduleName ${RequiredVersion}. Error: $_"
122+
}
123+
} elseif ($InstalledModuleReference.Group.Version -contains $RequiredVersion) {
124+
Write-Verbose "Found version $RequiredVersion of $moduleName"
125+
>>>>>>> 48ba4e296 (Add version checks and error handling.)
76126
}
77127
}

0 commit comments

Comments
 (0)