Skip to content

Commit 8984065

Browse files
committed
Pog.psm1: Add new 'Get-PogDownloadCache' cmdlet for listing cache entries
1 parent 86d06c5 commit 8984065

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

app/Pog/Pog.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
'New-PogRepositoryPackage'
4848
'New-PogPackage'
4949

50+
'Get-PogDownloadCache'
51+
5052
'Edit-PogRoot'
5153
)
5254

app/Pog/Pog.psm1

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,56 @@ function Update-Pog {
197197
$SelectedPackages | pog -Force
198198
}
199199
}
200+
201+
class DownloadCacheEntry {
202+
[string]$PackageName
203+
# FIXME: this should be a Pog.PackageVersion (but Pog.dll is not loaded when this is parsed)
204+
[string]$Version
205+
[string]$Hash
206+
[string]$FileName
207+
208+
hidden [ulong]$Size
209+
# with a `Path` field, this type can be piped to `rm -Recurse`
210+
hidden [string]$Path
211+
}
212+
213+
function Get-PogDownloadCache {
214+
[CmdletBinding(PositionalBinding=$false)]
215+
[OutputType([DownloadCacheEntry])]
216+
param(
217+
[Parameter(Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
218+
[ArgumentCompleter([Pog.PSAttributes.DownloadCachePackageNameCompleter])]
219+
[string[]]
220+
$PackageName
221+
)
222+
223+
begin {
224+
$Entries = [array][Pog.InternalState]::DownloadCache.EnumerateEntries()
225+
}
226+
227+
process {
228+
foreach ($pn in $PackageName) {
229+
$Found = $false
230+
foreach ($Entry in $Entries) {
231+
foreach ($Source in $Entry.SourcePackages | ? PackageName -eq $pn) {
232+
$Found = $true
233+
[DownloadCacheEntry]@{
234+
PackageName = $Source.PackageName
235+
Version = $Source.ManifestVersion
236+
Hash = $Entry.EntryKey
237+
FileName = [System.IO.Path]::GetFileName($Entry.Path)
238+
239+
Size = $Entry.Size
240+
Path = [System.IO.Path]::GetDirectoryName($Entry.Path)
241+
}
242+
}
243+
}
244+
245+
if (-not $Found) {
246+
# hacky way to create an ErrorRecord
247+
$e = try {throw "No download cache entries found for package '$pn'."} catch {$_}
248+
$PSCmdlet.WriteError($e)
249+
}
250+
}
251+
}
252+
}

app/Pog/lib_compiled/Pog/src/PSAttributes/Completers.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public abstract class DirectoryListingArgumentCompleter : QuotingArgumentComplet
9494

9595
protected override IEnumerable<string> GetCompletions(string wordToComplete, IDictionary fakeBoundParameters) {
9696
if (0 <= wordToComplete.IndexOfAny(InvalidCharacters)) {
97-
return Enumerable.Empty<string>();
97+
return [];
9898
}
9999
return GetMatchingItems($"{wordToComplete}*", fakeBoundParameters);
100100
}
@@ -169,3 +169,13 @@ protected override IEnumerable<string> GetMatchingItems(string searchPattern, ID
169169
return package.EnumerateVersions(searchPattern).Select(v => v.ToString());
170170
}
171171
}
172+
173+
/// Completer for package names that have an entry in the download cache.
174+
[PublicAPI]
175+
public sealed class DownloadCachePackageNameCompleter : QuotingArgumentCompleter {
176+
protected override IEnumerable<string> GetCompletions(string wordToComplete, IDictionary fakeBoundParameters) {
177+
return InternalState.DownloadCache.EnumerateEntries()
178+
.SelectMany(e => e.SourcePackages.Select(s => s.PackageName))
179+
.Where(s => s.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase));
180+
}
181+
}

0 commit comments

Comments
 (0)