Skip to content

Commit

Permalink
Add Get-Package (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto authored Sep 21, 2024
1 parent 64b07ca commit 8eba0df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/AnyPackage.Pkgx.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Copyright (c) Thomas Nieto - All Rights Reserved
# Copyright (c) Thomas Nieto - All Rights Reserved
# You may use, distribute and modify this code under the
# terms of the MIT license.

using module AnyPackage
using namespace AnyPackage.Provider

[PackageProvider('pkgx')]
class PkgxProvider : PackageProvider, IFindPackage, IInstallPackage, IUninstallPackage {
class PkgxProvider : PackageProvider, IFindPackage, IGetPackage, IInstallPackage, IUninstallPackage {
[void] FindPackage([PackageRequest] $request) {
if ($request.Name -eq '*') {
$request.WriteVerbose('pkgx does not support wildcards.')
Expand All @@ -22,6 +22,40 @@ class PkgxProvider : PackageProvider, IFindPackage, IInstallPackage, IUninstallP
}
}

[void] GetPackage([PackageRequest] $request) {
$pattern = 'exec pkgx \+(?<name>[\w\./]+)((?:[@^])(?<version>[\d\.]+))?'
$packages = Select-String -Path ./.local/bin/* -Pattern $pattern |
Select-Object -ExpandProperty Matches -Unique

$basePath = '~/.pkgx'

foreach ($package in $packages) {
$name = $package.Groups['name']

if ($package.Groups['version'].Success) {
$versionBin = $package.Groups['version'].Value
$versionPath = Join-Path -Path $basePath -ChildPath "$name/v$versionBin"
} else {
$versionPath = Join-Path -Path $basePath -ChildPath "$name/v*"
}

$versionDirectory = Get-Item -LiteralPath $versionPath

if ($versionDirectory.Target) {
$resolvedVersion = $versionDirectory.Target
} else {
$resolvedVersion = $versionDirectory.Name
}

$version = $resolvedVersion -replace 'v', ''

if ($request.IsMatch($name, $version)) {
$packageInfo = [PackageInfo]::new($name, $version, $request.ProviderInfo)
$request.WritePackage($packageInfo)
}
}
}

[void] InstallPackage([PackageRequest] $request) {
if ($request.Version -and $request.Version.MinVersion -ne $request.Version.MaxVersion) {
throw 'pkgx does not support version ranges, use only exact versions.'
Expand Down
18 changes: 18 additions & 0 deletions tests/Get-Package.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Requires -Modules AnyPackage.Pkgx

Describe Find-Package {
BeforeAll {
Install-Package -Name bun
}

AfterAll {
Uninstall-Package -Name bun
}

Context 'with -Name parameter' {
It 'should get' {
{ Get-Package -Name bun } |
Should -Not -BeNullOrEmpty
}
}
}

0 comments on commit 8eba0df

Please sign in to comment.