-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathImport-ModulePackage.ps1
More file actions
63 lines (53 loc) · 3.58 KB
/
Import-ModulePackage.ps1
File metadata and controls
63 lines (53 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function private:Import-ModulePackage {
<#
.SYNOPSIS
Installs compressed PowerShell modules from NuGet packages (*.nupkg)
.DESCRIPTION
Installs compressed PowerShell modules from NuGet packages (*.nupkg) into a subdirectory of /tmp.
This folder is later added to $env:PSModulePath, before user code runs, if module packages existed.
.NOTES
These packages should match the NuPkg format used by PSResourceGet or PowerShellGet.
Packages can be exported either by:
* Downloading the .nupkg files directly from an upstream source (e.g. PowerShell Gallery)
* Using the -AsNuPkg parameter on Save-PSResource in the Microsoft.PowerShell.PSResourceGet module.
Module packages are imported from two locations, from lowest to highest precedence:
* /opt/module-nupkgs/ (Combined Lambda layer directory)
* $Env:LAMBDA_TASK_ROOT/module-nupkgs/ (Lambda Function Package deployment directory)
#>
[CmdletBinding()]
param(
[ValidatePattern(".nupkg$")]
[ValidateNotNullOrEmpty()]
[Parameter(
Mandatory,
Position = 0
)]
[System.IO.FileInfo[]]$PackagePath
)
Begin {
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host '[RUNTIME-Import-ModulePackage]Creating unpack directory for individual module packages' }
$UnpackDirectory = [System.IO.Directory]::CreateDirectory($Script:ModulePaths.Unpacked.NuPkg)
}
Process {
$PackagePath | Group-Object -Property Directory | ForEach-Object {
# The group key should be the directory for the folder containing the nupkgs.
$PackageDirectory = $_.Name
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host "[RUNTIME-Import-ModulePackage]Importing module packages from $PackageDirectory" }
# We split-path that directory to strip off "module-nupkgs".
$RepositoryName = "Lambda-Local-$($_.Group | Split-Path -Parent)"
# Attach a PSResourceGet repository to the directory holding the packages.
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host "[RUNTIME-Import-ModulePackage]Registering local package repository $RepositoryName" }
Register-PSResourceRepository -Name $RepositoryName -Uri $PackageDirectory -Trusted -Priority 1
# Then, enumerate all the packages in that repository (again, just a directory) and "save" (install/unpack them) into /tmp.
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host "[RUNTIME-Import-ModulePackage]Enumerating packages in $PackageDirectory (PSResource repository $RepositoryName)" }
Find-PSResource -Name * -Repository $RepositoryName | ForEach-Object -Parallel {
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host "[RUNTIME-Import-ModulePackage]Saving package $($_.Name) version $($_.Version) (PSResource repository $($using:RepositoryName))" }
$_ | Save-PSResource -SkipDependencyCheck -Path $using:UnpackDirectory -Quiet -AcceptLicense -Confirm:$false
}
# Clean up the local repository config. This doesn't uninstall anything (just edits some XML files in PSResourceGet)
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host "[RUNTIME-Import-ModulePackage]Registering local package repository $RepositoryName" }
Unregister-PSResourceRepository -Name $RepositoryName -Confirm:$false
}
if ($env:POWERSHELL_RUNTIME_VERBOSE -eq 'TRUE') { Write-Host '[RUNTIME-Import-ModulePackage]Archive unpack complete' }
}
}