Skip to content

Made Install-Dependencies much faster by only installing missing requirements #894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
79 changes: 62 additions & 17 deletions build/Install-Dependencies.ps1
Original file line number Diff line number Diff line change
@@ -2,26 +2,71 @@
# 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"
try {
# Review: are the common functions required for this script? There does not seem to be any dependencies on them [yet?].
. "$PSScriptRoot/common-functions.ps1"
} catch {
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."
}

# Get module settings from the relevant ModuleSettings.json file.
if ($ModuleSettingsPath) {
$SettingsPath = $ModuleSettingsPath
} else {
$SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json"
}
$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json
$RequiredVersion = $ModuleSettings.destinationModuleVersion

# Do not check for installed modules if -Force is specified.
if ($Force) {
Write-Verbose 'Skipping the check for installed prerequisites. Forcing the installation of all required modules.'
} else {
Write-Verbose 'Checking installed modules for required dependencies.'
$InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property 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
# Install the AzureAD module.
$SourceModule = $ModuleSettings.sourceModule
if (($InstalledModules.Name -contains $SourceModule) -and -not $Force) {
Write-Verbose "The $SourceModule module is already installed."
} else {
Write-Verbose("Installing Module: $sourceModule")
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 the required module dependencies if missing the required version or if -Force.
foreach ($moduleName in $ModuleSettings.destinationModuleName) {
$InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName })
if (-not $InstalledModuleReference -or $Force) {
Write-Verbose "Installing version $RequiredVersion of $moduleName"
try {
Install-Module $moduleName -Scope CurrentUser -RequiredVersion $RequiredVersion -Force -AllowClobber
} catch {
Write-Error "Failed to install module $moduleName ${RequiredVersion}. Error: $_"
}
} elseif ($InstalledModuleReference.Group.Version -contains $RequiredVersion) {
Write-Verbose "Found version $RequiredVersion of $moduleName"
}
}