From c8c2c65137847abf23fc1a08d92f0e099ff7a44c Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 6 Sep 2023 22:25:45 -0500 Subject: [PATCH] (#3315) Save package contents deployment location Saves the location of where the contents of the Chocolatey package (whether that is a native installer, or an extraction of a zip file, or whatever else is in the package) is deployed to into a ".deploymentLocation" file inside the package info directory. This allows Chocolatey CLI or GUI to later retrieve it and display it to the user when listing the details of locally installed package(s). --- .../domain/ChocolateyPackageInformation.cs | 1 + .../ChocolateyPackageInformationService.cs | 30 +++++++++++++++++++ .../services/ChocolateyPackageService.cs | 7 +++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/domain/ChocolateyPackageInformation.cs b/src/chocolatey/infrastructure.app/domain/ChocolateyPackageInformation.cs index f345412bc8..9fb89fd800 100644 --- a/src/chocolatey/infrastructure.app/domain/ChocolateyPackageInformation.cs +++ b/src/chocolatey/infrastructure.app/domain/ChocolateyPackageInformation.cs @@ -36,5 +36,6 @@ public ChocolateyPackageInformation(IPackageMetadata package) public bool HasSilentUninstall { get; set; } public bool IsPinned { get; set; } public string ExtraInformation { get; set; } + public string DeploymentLocation { get; set; } } } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs index 6eb0d7d715..4eaab3b165 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageInformationService.cs @@ -44,6 +44,7 @@ public class ChocolateyPackageInformationService : IChocolateyPackageInformation private const string ArgsFile = ".arguments"; private const string ExtraFile = ".extra"; private const string VersionOverrideFile = ".version"; + private const string DeploymentLocationFile = ".deploymentLocation"; // We need to store the package identifiers we have warned about // to prevent duplicated outputs. @@ -179,6 +180,20 @@ has errored attempting to read it. This file will be renamed to ); } + var locationFile = _fileSystem.CombinePaths(pkgStorePath, DeploymentLocationFile); + if (_fileSystem.FileExists(locationFile)) + { + FaultTolerance.TryCatchWithLoggingException( + () => + { + packageInformation.DeploymentLocation = _fileSystem.ReadFile(locationFile); + }, + "Unable to read deployment location file", + throwError: false, + logWarningInsteadOfError: true + ); + } + return packageInformation; } @@ -280,6 +295,21 @@ public void Save(ChocolateyPackageInformation packageInformation) { _fileSystem.DeleteFile(_fileSystem.CombinePaths(pkgStorePath, PinFile)); } + + if (!string.IsNullOrWhiteSpace(packageInformation.DeploymentLocation)) + { + var locationFile = _fileSystem.CombinePaths(pkgStorePath, DeploymentLocationFile); + if (_fileSystem.FileExists(locationFile)) + { + _fileSystem.DeleteFile(locationFile); + } + + _fileSystem.WriteFile(locationFile, packageInformation.DeploymentLocation); + } + else + { + _fileSystem.DeleteFile(_fileSystem.CombinePaths(pkgStorePath, DeploymentLocationFile)); + } } public void Remove(IPackageMetadata package) diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 93e1a1b338..fc9b292606 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -566,6 +566,8 @@ public virtual void HandlePackageResult(PackageResult packageResult, ChocolateyC } } + pkgInfo.DeploymentLocation = Environment.GetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallLocation); + UpdatePackageInformation(pkgInfo); EnsureBadPackagesPathIsClean(packageResult); EventManager.Publish(new HandlePackageResultCompletedMessage(packageResult, config, commandName)); @@ -601,11 +603,10 @@ public virtual void HandlePackageResult(PackageResult packageResult, ChocolateyC this.Log().Info(ChocolateyLoggers.Important, " The {0} of {1} was successful.".FormatWith(commandName.ToStringSafe(), packageResult.Name)); - var installLocation = Environment.GetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallLocation); var installerDetected = Environment.GetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallerType); - if (!string.IsNullOrWhiteSpace(installLocation)) + if (!string.IsNullOrWhiteSpace(pkgInfo.DeploymentLocation)) { - this.Log().Info(ChocolateyLoggers.Important, " Software installed to '{0}'".FormatWith(installLocation.EscapeCurlyBraces())); + this.Log().Info(ChocolateyLoggers.Important, " Deployed to '{0}'".FormatWith(pkgInfo.DeploymentLocation.EscapeCurlyBraces())); } else if (!string.IsNullOrWhiteSpace(installerDetected)) {