Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 67 additions & 17 deletions build/Install-Dependencies.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,76 @@
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

[cmdletbinding()]
[CmdletBinding()]
param(
[ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })]
[string]
$ModuleName = 'Entra',
# 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'.
[ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })]
[string]
$ModuleName = 'Entra',

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

# Force installation of the required modules, even if they are already installed.
[switch]
$Force
)

. "$psscriptroot/common-functions.ps1"
# Get module settings from the relevant ModuleSettings.json file or a file that is manually specified.
if ($ModuleSettingsPath) {
$SettingsPath = $ModuleSettingsPath
} else {
$SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
}

Write-Host "Getting module settings from: '$SettingsPath'" -ForegroundColor Cyan
$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json
$SourceModule = $ModuleSettings.sourceModule
$DependencyRequiredVersion = $ModuleSettings.destinationModuleVersion

# Get the names of the source and destination modules from the ModuleSettings.json file.
[string[]]$ModuleNames = (($ModuleSettings.destinationModuleName) + $SourceModule).Split()

Write-Host 'Checking for installed source and dependency modules...' -ForegroundColor Cyan
$InstalledModules = foreach ( $Module in $ModuleNames ) {
Get-Module -Name $Module -ListAvailable -Verbose:$false | Sort-Object Name
}

$settingPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
if ($ModuleSettingsPath) { $settingPath = $ModuleSettingsPath }
$content = Get-Content -Path $settingPath | ConvertFrom-Json
Write-Verbose("Installing Module $($content.sourceModule)")
Install-Module $content.sourceModule -scope currentuser -Force -AllowClobber
# Check if the source module is installed and then install it or update it.
$IsSourceInstalled = $InstalledModules.Name -contains $SourceModule
if (
(-not $IsSourceInstalled) -or
($IsSourceInstalled -and $Force.IsPresent)
) {
Write-Host "Installing Module: '$SourceModule'" -ForegroundColor Green
try {
Install-Module $SourceModule -Scope CurrentUser -Force -AllowClobber
} catch {
Write-Error "Failed to install the '$SourceModule' module. Error: $_"
}
}

foreach ($moduleName in $content.destinationModuleName){
Write-Verbose("Installing Module $($moduleName)")
Install-Module $moduleName -scope currentuser -RequiredVersion $content.destinationModuleVersion -Force -AllowClobber
}
# Install or update the destination modules specified in the ModuleSettings.json file.
foreach ($DestinationModuleName in $ModuleSettings.destinationModuleName) {
$IsDependencyInstalled = $InstalledModules.Name -contains $DestinationModuleName
if (
# Force installation if the module is not installed or if the Force switch is present.
(-not $IsDependencyInstalled) -or ($IsDependencyInstalled -and $Force.IsPresent)
) {
Write-Host "Installing Module: '$DestinationModuleName'" -ForegroundColor Green
try {
Install-Module $DestinationModuleName -RequiredVersion $DependencyRequiredVersion -Scope CurrentUser -Force -AllowClobber
} catch {
Write-Error "Failed to install the '$DestinationModuleName' module. Error: $_"
}
} else {
Write-Host "Updating Module: '$DestinationModuleName'" -ForegroundColor Green
try {
Update-Module -Name $DestinationModuleName -RequiredVersion $DependencyRequiredVersion -ErrorAction SilentlyContinue
} catch {
Write-Warning "Failed to update module '$DestinationModuleName' to version ${RequiredVersion}. Error: $_"
}
}
}