Skip to content

Commit

Permalink
Add command not found (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto authored Aug 15, 2024
1 parent aef9679 commit 51a3a00
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/AnyPackage.PSResourceGet.psd1
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
@{
RootModule = 'AnyPackage.PSResourceGet.psm1'
ModuleVersion = '0.3.0'
ModuleVersion = '0.4.0'
CompatiblePSEditions = @('Desktop', 'Core')
GUID = '4ffeffd3-7f83-4655-ac94-19eb41ebc792'
Author = 'Thomas Nieto'
Copyright = '(c) 2023 Thomas Nieto. All rights reserved.'
Description = 'PSResourceGet provider for AnyPackage.'
PowerShellVersion = '5.1'
RequiredModules = @('AnyPackage', 'Microsoft.PowerShell.PSResourceGet')
RequiredModules = @(
@{ ModuleName = 'AnyPackage'; ModuleVersion = '0.6.0' },
'Microsoft.PowerShell.PSResourceGet')
FunctionsToExport = @()
CmdletsToExport = @()
AliasesToExport = @()
Expand Down
50 changes: 49 additions & 1 deletion src/AnyPackage.PSResourceGet.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@ using module AnyPackage
using module Microsoft.PowerShell.PSResourceGet

using namespace System.Collections.Generic
using namespace System.Threading
using namespace AnyPackage.Provider
using namespace AnyPackage.Feedback
using namespace Microsoft.PowerShell.PSResourceGet.UtilClasses

[PackageProvider('PSResourceGet')]
class PSResourceGetProvider : PackageProvider, IGetPackage, IFindPackage,
IInstallPackage, ISavePackage, IUninstallPackage,
IUpdatePackage, IPublishPackage, IGetSource, ISetSource {
IUpdatePackage, IPublishPackage, IGetSource, ISetSource, ICommandNotFound {
[PackageProviderInfo] Initialize([PackageProviderInfo] $providerInfo) {
return [PSResourceGetProviderInfo]::new($providerInfo)
}

[IEnumerable[CommandNotFoundFeedback]] FindPackage([CommandNotFoundContext] $context, [CancellationToken] $token) {
$dict = [Dictionary[string, CommandNotFoundFeedback]]::new([StringComparer]::OrdinalIgnoreCase)
$packages = $this.ProviderInfo.CommandCache[$context.Command]

foreach ($package in $packages) {
if (!$dict.ContainsKey($package.Name)) {
$feedback = [CommandNotFoundFeedback]::new($package.Name, $this.ProviderInfo)
$dict.Add($package.Name, $feedback)
}
}

return $dict.Values
}

#region GetPackage
[void] GetPackage([PackageRequest] $request) {
$params = @{
Expand Down Expand Up @@ -284,6 +304,34 @@ IUpdatePackage, IPublishPackage, IGetSource, ISetSource {
}
}

class PSResourceGetProviderInfo : PackageProviderInfo {
[Dictionary[string, List[PSResourceInfo]]] $CommandCache = [Dictionary[string, List[PSResourceInfo]]]::new([StringComparer]::OrdinalIgnoreCase)

PSResourceGetProviderInfo([PackageProviderInfo] $providerInfo) : base($providerInfo) {
if ([Runspace]::DefaultRunspace.Name -eq $this.FullName) {
$this.SetCommandCache()
}
}

[void] SetCommandCache() {
$packages = Find-PSResource -Name * -Type Module

foreach ($package in $packages) {
$commands = $package.Tags | Where-Object { $_ -like "PSCommand*" } | ForEach-Object { $_ -replace 'PSCommand_', '' }

foreach ($command in $commands) {
if ($this.CommandCache.ContainsKey($command)) {
$this.CommandCache[$command] += $package
} else {
$list = [List[PSResourceInfo]]::new()
$list += $package
$this.CommandCache.Add($command, $package)
}
}
}
}
}

class GetPackageDynamicParameters {
[Parameter()]
[string] $Path
Expand Down

0 comments on commit 51a3a00

Please sign in to comment.