Skip to content

Commit

Permalink
Merge pull request #2341 from vexx32/2340/ensure-licensed-extension-l…
Browse files Browse the repository at this point in the history
…oads-during-install

(#2340) Ensure licensed extension is loaded during install when available
  • Loading branch information
gep13 authored Sep 2, 2021
2 parents 8e6b9f1 + e3967ef commit 7a4d1a0
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions src/chocolatey.resources/helpers/chocolateyInstaller.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

$helpersPath = (Split-Path -parent $MyInvocation.MyCommand.Definition);
$helpersPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

$global:DebugPreference = "SilentlyContinue"
if ($env:ChocolateyEnvironmentDebug -eq 'true') { $global:DebugPreference = "Continue"; }
if ($env:ChocolateyEnvironmentDebug -eq 'true') {
$global:DebugPreference = "Continue"
}
$global:VerbosePreference = "SilentlyContinue"
if ($env:ChocolateyEnvironmentVerbose -eq 'true') { $global:VerbosePreference = "Continue"; $verbosity = $true }
if ($env:ChocolateyEnvironmentVerbose -eq 'true') {
$global:VerbosePreference = "Continue"
$verbosity = $true
}

$installArguments = $env:chocolateyInstallArguments
$overrideArgs = $env:chocolateyInstallOverride -eq 'true'

$overrideArgs = $false
if ($env:chocolateyInstallOverride -eq 'true') { $overrideArgs = $true }
$forceX86 = $env:chocolateyForceX86 -eq 'true'

$forceX86 = $false
if ($env:chocolateyForceX86 -eq 'true') { $forceX86 = $true }
$installArguments = $env:chocolateyInstallArguments

$packageParameters = $env:chocolateyPackageParameters

# ensure module loading preference is on
$PSModuleAutoLoadingPreference = "All";
$PSModuleAutoLoadingPreference = "All"

Write-Debug "Host version is $($host.Version), PowerShell Version is '$($PSVersionTable.PSVersion)' and CLR Version is '$($PSVersionTable.CLRVersion)'."

# grab functions from files
Get-Item $helpersPath\functions\*.ps1 |
? { -not ($_.Name.Contains(".Tests.")) } |
% {
. $_.FullName;
#Export-ModuleMember -Function $_.BaseName
# Import functions from files
Get-Item -Path "$helpersPath\functions\*.ps1" |
Where-Object { -not $_.Name.Contains(".Tests.") } |
ForEach-Object {
. $_.FullName
}

# Export built-in functions prior to loading extensions so that
Expand All @@ -52,12 +54,36 @@ Export-ModuleMember -Function * -Alias * -Cmdlet *

$currentAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies()

# load extensions if they exist
$extensionsPath = Join-Path "$helpersPath" '..\extensions'
if (Test-Path($extensionsPath)) {
Write-Debug 'Loading community extensions'
#Resolve-Path $extensionsPath\**\*\*.psm1 | % { Write-Debug "Importing `'$_`'"; Import-Module $_.ProviderPath }
Get-ChildItem $extensionsPath -recurse -filter "*.psm1" | Select -ExpandProperty FullName | % { Write-Debug "Importing `'$_`'"; Import-Module $_; }
# Load community extensions if they exist
$extensionsPath = Join-Path $helpersPath -ChildPath '..\extensions'
if (Test-Path $extensionsPath) {
$licensedExtensionPath = Join-Path $extensionsPath -ChildPath 'chocolatey\chocolatey.licensed.dll'
if (Test-Path $licensedExtensionPath) {
Write-Debug "Importing '$licensedExtensionPath'"
Write-Debug "Loading 'chocolatey.licensed' extension"

# Attempt to import module via already-loaded assembly
$licensedAssembly = $currentAssemblies |
Where-Object { $_.GetName().Name -eq 'chocolatey.licensed' } |
Select-Object -First 1

if ($licensedAssembly) {
# It's already loaded, just import the existing assembly as a module for PowerShell to use
Import-Module $licensedAssembly
}
else {
# Fallback: load the extension DLL from the path directly.
Import-Module $licensedExtensionPath
}
}

Write-Debug 'Loading community extensions'
Get-ChildItem -Path $extensionsPath -Recurse -Filter '*.psm1' |
Select-Object -ExpandProperty FullName |
ForEach-Object {
Write-Debug "Importing '$_'"
Import-Module $_
}
}

# todo: explore removing this for a future version
Expand Down

0 comments on commit 7a4d1a0

Please sign in to comment.