This repository has been archived on 2025-09-07. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
dotnet-installer/src/dotnet/ToolPackage/ToolPackageStore.cs

67 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.ToolPackage
{
internal class ToolPackageStore : IToolPackageStore
{
public ToolPackageStore(DirectoryPath root)
{
Root = root;
}
public DirectoryPath Root { get; private set; }
public IEnumerable<IToolPackage> GetInstalledPackages(string packageId = null)
{
if (packageId != null)
{
return EnumerateVersions(packageId);
}
return EnumerateAllPackages().SelectMany(p => p);
}
private IEnumerable<IEnumerable<IToolPackage>> EnumerateAllPackages()
{
if (!Directory.Exists(Root.Value))
{
yield break;
}
foreach (var subdirectory in Directory.EnumerateDirectories(Root.Value))
{
var packageId = Path.GetFileName(subdirectory);
if (packageId == ToolPackageInstaller.StagingDirectory)
{
continue;
}
yield return EnumerateVersions(packageId);
}
}
private IEnumerable<IToolPackage> EnumerateVersions(string packageId)
{
var packageRootDirectory = Root.WithSubDirectories(packageId);
if (!Directory.Exists(packageRootDirectory.Value))
{
yield break;
}
foreach (var subdirectory in Directory.EnumerateDirectories(packageRootDirectory.Value))
{
var version = Path.GetFileName(subdirectory);
yield return new ToolPackageInstance(
this,
packageId,
version,
packageRootDirectory.WithSubDirectories(version));
}
}
}
}