Skip to content

Commit

Permalink
Merge pull request #2671 from marticliment/get-package-install-location
Browse files Browse the repository at this point in the history
Open package install location
  • Loading branch information
marticliment authored Aug 29, 2024
2 parents 2dc1fb8 + bec0ad0 commit 443c89f
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ public interface IPackageDetailsProvider
/// <param name="package">The package from which to load the screenshots</param>
/// <returns>An array with valid URIs to the screenshots</returns>
public abstract Task<Uri[]> GetPackageScreenshotsUrl(IPackage package);

/// <summary>
/// Returns the location where the package is installed, or null if the location cannot be loaded.
/// </summary>
/// <param name="package">The package for which to get the location</param>
/// <returns>A valid path in the form of a string or a null object</returns>
public abstract string? GetPackageInstallLocation(IPackage package);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,27 @@ protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage packa

return versions.ToArray();
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
string portable_path = Manager.Status.ExecutablePath.Replace("choco.exe", $"bin\\{package.Id}.exe");
if (File.Exists(portable_path))
return Path.GetDirectoryName(portable_path);

foreach (var base_path in new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs"),
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
})
{
var path_with_id = Path.Join(base_path, package.Id);
if (Directory.Exists(path_with_id)) return path_with_id;
}

return Path.Join(Path.GetDirectoryName(Manager.Status.ExecutablePath), "lib", package.Id);

}
}
}
2 changes: 2 additions & 0 deletions src/UniGetUI.PackageEngine.Managers.Dotnet/DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.Managers.Chocolatey;
using UniGetUI.PackageEngine.Managers.PowerShellManager;
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUI.PackageEngine.Structs;
Expand Down Expand Up @@ -58,6 +59,7 @@ public DotNet()
KnownSources = [new ManagerSource(this, "nuget.org", new Uri("https://www.nuget.org/api/v2"))],
};

PackageDetailsProvider = new DotNetDetailsProvider(this);
OperationProvider = new DotNetOperationProvider(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Diagnostics;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.Managers.PowerShellManager;

namespace UniGetUI.PackageEngine.Managers.Chocolatey
{
public class DotNetDetailsProvider : BaseNuGetDetailsProvider
{
public DotNetDetailsProvider(BaseNuGet manager) : base(manager)
{ }

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dotnet", "tools");
}
}
}
11 changes: 1 addition & 10 deletions src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ namespace UniGetUI.PackageEngine.Managers.PowerShellManager
{
public abstract class BaseNuGet : PackageManager
{
public static new string[] FALSE_PACKAGE_NAMES = [""];
public static new string[] FALSE_PACKAGE_IDS = [""];
public static new string[] FALSE_PACKAGE_VERSIONS = [""];

public BaseNuGet()
{
PackageDetailsProvider = new BaseNuGetDetailsProvider(this);
}

public sealed override async Task InitializeAsync()
{
if (PackageDetailsProvider is not BaseNuGetDetailsProvider)
Expand Down Expand Up @@ -62,7 +53,7 @@ protected sealed override async Task<Package[]> FindPackages_UnSafe(string query
{
sources = [ Properties.DefaultSource ];
}

foreach(IManagerSource source in sources)
{
Uri SearchUrl = new($"{source.Url}/Search()?searchTerm=%27{HttpUtility.UrlEncode(query)}%27&targetFramework=%27%27&includePrerelease=false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace UniGetUI.PackageEngine.Managers.PowerShellManager
{
public class BaseNuGetDetailsProvider : BasePackageDetailsProvider<PackageManager>
public abstract class BaseNuGetDetailsProvider : BasePackageDetailsProvider<PackageManager>
{
public BaseNuGetDetailsProvider(BaseNuGet manager) : base(manager) { }

Expand Down Expand Up @@ -145,6 +145,7 @@ protected override Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage package)
throw new NotImplementedException();
}


protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage package)
{
Uri SearchUrl = new($"{package.Source.Url}/FindPackagesById()?id='{package.Id}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UniGetUI.Core.Logging;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
Expand Down Expand Up @@ -110,6 +111,15 @@ protected override Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage package)
throw new NotImplementedException();
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
if (package.OverridenOptions.Scope is PackageScope.Local)
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "node_modules", package.Id);
else
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Roaming", "npm",
"node_modules", package.Id);
}

protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage package)
{
Process p = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UniGetUI.Core.IconEngine;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
Expand Down Expand Up @@ -136,7 +137,6 @@ protected override async Task GetPackageDetails_Unsafe(IPackageDetails details)
catch (Exception ex) { logger.Error("Can't load installer data: " + ex); }

logger.Close(0);
return;
}

protected override Task<CacheableIcon?> GetPackageIcon_Unsafe(IPackage package)
Expand All @@ -149,6 +149,12 @@ protected override Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage package)
throw new NotImplementedException();
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
var full_path = Path.Join(Path.GetDirectoryName(Manager.Status.ExecutablePath), "Lib", "site-packages", package.Id);
return Directory.Exists(full_path) ? full_path : Path.GetDirectoryName(full_path);
}

protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage package)
{
Process p = new()
Expand Down
2 changes: 2 additions & 0 deletions src/UniGetUI.PackageEngine.Managers.PowerShell/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.Managers.Chocolatey;
using UniGetUI.PackageEngine.PackageClasses;

namespace UniGetUI.PackageEngine.Managers.PowerShellManager
Expand Down Expand Up @@ -49,6 +50,7 @@ public PowerShell()
DefaultSource = new ManagerSource(this, "PSGallery", new Uri("https://www.powershellgallery.com/api/v2")),
};

PackageDetailsProvider = new PowerShellDetailsProvider(this);
SourceProvider = new PowerShellSourceProvider(this);
OperationProvider = new PowerShellOperationProvider(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;
using Windows.ApplicationModel.Core;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.Managers.PowerShellManager;

namespace UniGetUI.PackageEngine.Managers.Chocolatey
{
public class PowerShellDetailsProvider : BaseNuGetDetailsProvider
{
public PowerShellDetailsProvider(BaseNuGet manager) : base(manager)
{ }

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
var user_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"WindowsPowerShell", "Modules", package.Id);
if (Directory.Exists(user_path)) return user_path;

var system_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"WindowsPowerShell", "Modules", package.Id);
if (Directory.Exists(system_path)) return system_path;

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.Managers.Chocolatey;
using UniGetUI.PackageEngine.Managers.PowerShellManager;
using UniGetUI.PackageEngine.PackageClasses;

Expand Down Expand Up @@ -50,6 +51,7 @@ public PowerShell7()
DefaultSource = new ManagerSource(this, "PSGallery", new Uri("https://www.powershellgallery.com/api/v2")),
};

PackageDetailsProvider = new PowerShell7DetailsProvider(this);
SourceProvider = new PowerShell7SourceProvider(this);
OperationProvider = new PowerShell7OperationProvider(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.Managers.PowerShellManager;

namespace UniGetUI.PackageEngine.Managers.Chocolatey
{
public class PowerShell7DetailsProvider : BaseNuGetDetailsProvider
{
public PowerShell7DetailsProvider(BaseNuGet manager) : base(manager)
{
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
var user_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"PowerShell", "Modules", package.Id);
if (Directory.Exists(user_path)) return user_path;

var system_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"PowerShell", "Modules", package.Id);
if (Directory.Exists(system_path)) return system_path;

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Text.Json.Nodes;
using Windows.System;
using UniGetUI.Core.IconEngine;
using UniGetUI.Core.Logging;
using UniGetUI.Core.Tools;
Expand Down Expand Up @@ -198,6 +199,12 @@ protected override Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage package)
throw new NotImplementedException();
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "scoop", "apps",
package.Id, "current");
}

#pragma warning disable
protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage package)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
using ABI.Windows.UI.Composition;
using UniGetUI.Core.IconEngine;
using UniGetUI.Core.Logging;
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUIManagers = UniGetUI.PackageEngine.ManagerClasses.Manager;

namespace UniGetUI.PackageEngine.Managers.WingetManager
Expand Down Expand Up @@ -91,6 +93,32 @@ protected override async Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage packa
return FoundIcons.ToArray();
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
foreach (var base_path in new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs"),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "WinGet", "Packages"),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "WinGet", "Packages"),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "WinGet", "Packages"),
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
})
{
var path_with_name = Path.Join(base_path, package.Name);
if (Directory.Exists(path_with_name)) return path_with_name;

var path_with_id = Path.Join(base_path, package.Id);
if (Directory.Exists(path_with_id)) return path_with_id;

var path_with_source = Path.Join(base_path, $"{package.Id}_{package.Source.Name}");
if (Directory.Exists(path_with_source)) return path_with_source;
}

return null;
}

private static async Task<string?> GetMicrosoftStorePackageManifest(IPackage package)
{
if (__msstore_package_manifests.TryGetValue(package.Id, out var manifest))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,29 @@ public async Task<Uri[]> GetPackageScreenshotsUrl(IPackage package)
protected abstract Task<string[]> GetPackageVersions_Unsafe(IPackage package);
protected abstract Task<CacheableIcon?> GetPackageIcon_Unsafe(IPackage package);
protected abstract Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage package);
protected abstract string? GetPackageInstallLocation_Unsafe(IPackage package);

public string? GetPackageInstallLocation(IPackage package)
{
try
{
string? path = GetPackageInstallLocation_Unsafe(package);
if (path is not null && !Directory.Exists(path))
{
Logger.Warn($"Path returned by the package manager \"{path}\" did not exist while loading package install location for package Id={package.Id} with Manager={package.Manager.Name}");
return null;
}

return path;
}
catch (Exception ex)
{
Logger.Error($"An error occurred while loading package install location for package Id={package.Id} with Manager={package.Manager.Name}");
Logger.Error(ex);
return null;
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public Task<Uri[]> GetPackageScreenshotsUrl(IPackage package)
throw new NotImplementedException();
}

public string? GetPackageInstallLocation(IPackage package)
{
throw new NotImplementedException();
}

public Task<string[]> GetPackageVersions(IPackage package)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ public async Task<Uri[]> GetPackageScreenshotsUrl(IPackage package)
return [];
}
}

public string? GetPackageInstallLocation(IPackage package)
{
return PackageDetailsProvider.GetPackageInstallLocation(package);
}
// END PACKAGEDETAILS-RELATED METHODS


Expand Down Expand Up @@ -503,6 +508,11 @@ protected override async Task<Uri[]> GetPackageScreenshots_Unsafe(IPackage packa
return [];
}

protected override string? GetPackageInstallLocation_Unsafe(IPackage package)
{
return null;
}

protected override async Task<string[]> GetPackageVersions_Unsafe(IPackage package)
{
return [];
Expand Down
Loading

0 comments on commit 443c89f

Please sign in to comment.