Skip to content

Commit 8ea8211

Browse files
committed
Made much faster by only installing missing dependencies unless -Force is specified.
Add version checks and error handling. Update/fix comments. Remove unused Common-Functions import. Update text case to be consistent. Refactor Install-Dependencies to only install or update as needed unless Force is used. Add version checks and error handling. Update/fix comments. Made much faster by only installing missing dependencies unless -Force is specified.
1 parent 0029d93 commit 8ea8211

File tree

1 file changed

+67
-17
lines changed

1 file changed

+67
-17
lines changed

build/Install-Dependencies.ps1

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,76 @@
22
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
# ------------------------------------------------------------------------------
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,
16+
17+
# Force installation of the required modules, even if they are already installed.
18+
[switch]
19+
$Force
1420
)
1521

16-
. "$psscriptroot/common-functions.ps1"
22+
# Get module settings from the relevant ModuleSettings.json file or a file that is manually specified.
23+
if ($ModuleSettingsPath) {
24+
$SettingsPath = $ModuleSettingsPath
25+
} else {
26+
$SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
27+
}
28+
29+
Write-Host "Getting module settings from: '$SettingsPath'" -ForegroundColor Cyan
30+
$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json
31+
$SourceModule = $ModuleSettings.sourceModule
32+
$DependencyRequiredVersion = $ModuleSettings.destinationModuleVersion
33+
34+
# Get the names of the source and destination modules from the ModuleSettings.json file.
35+
[string[]]$ModuleNames = (($ModuleSettings.destinationModuleName) + $SourceModule).Split()
36+
37+
Write-Host 'Checking for installed source and dependency modules...' -ForegroundColor Cyan
38+
$InstalledModules = foreach ( $Module in $ModuleNames ) {
39+
Get-Module -Name $Module -ListAvailable -Verbose:$false | Sort-Object Name
40+
}
1741

18-
$settingPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
19-
if ($ModuleSettingsPath) { $settingPath = $ModuleSettingsPath }
20-
$content = Get-Content -Path $settingPath | ConvertFrom-Json
21-
Write-Verbose("Installing Module $($content.sourceModule)")
22-
Install-Module $content.sourceModule -scope currentuser -Force -AllowClobber
42+
# Check if the source module is installed and then install it or update it.
43+
$IsSourceInstalled = $InstalledModules.Name -contains $SourceModule
44+
if (
45+
(-not $IsSourceInstalled) -or
46+
($IsSourceInstalled -and $Force.IsPresent)
47+
) {
48+
Write-Host "Installing Module: '$SourceModule'" -ForegroundColor Green
49+
try {
50+
Install-Module $SourceModule -Scope CurrentUser -Force -AllowClobber
51+
} catch {
52+
Write-Error "Failed to install the '$SourceModule' module. Error: $_"
53+
}
54+
}
2355

24-
foreach ($moduleName in $content.destinationModuleName){
25-
Write-Verbose("Installing Module $($moduleName)")
26-
Install-Module $moduleName -scope currentuser -RequiredVersion $content.destinationModuleVersion -Force -AllowClobber
27-
}
56+
# Install or update the destination modules specified in the ModuleSettings.json file.
57+
foreach ($DestinationModuleName in $ModuleSettings.destinationModuleName) {
58+
$IsDependencyInstalled = $InstalledModules.Name -contains $DestinationModuleName
59+
if (
60+
# Force installation if the module is not installed or if the Force switch is present.
61+
(-not $IsDependencyInstalled) -or ($IsDependencyInstalled -and $Force.IsPresent)
62+
) {
63+
Write-Host "Installing Module: '$DestinationModuleName'" -ForegroundColor Green
64+
try {
65+
Install-Module $DestinationModuleName -RequiredVersion $DependencyRequiredVersion -Scope CurrentUser -Force -AllowClobber
66+
} catch {
67+
Write-Error "Failed to install the '$DestinationModuleName' module. Error: $_"
68+
}
69+
} else {
70+
Write-Host "Updating Module: '$DestinationModuleName'" -ForegroundColor Green
71+
try {
72+
Update-Module -Name $DestinationModuleName -RequiredVersion $DependencyRequiredVersion -ErrorAction SilentlyContinue
73+
} catch {
74+
Write-Warning "Failed to update module '$DestinationModuleName' to version ${RequiredVersion}. Error: $_"
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)