Skip to content

Commit

Permalink
(chocolatey#3315) Save software installation location
Browse files Browse the repository at this point in the history
Saves the software installation location into a ".softwareLocation"
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).
  • Loading branch information
TheCakeIsNaOH committed Jan 9, 2024
1 parent a7f72e0 commit 8f33c0c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 SoftwareInstallLocation { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 SoftwareInstallLocationFile = ".softwareLocation";

// We need to store the package identifiers we have warned about
// to prevent duplicated outputs.
Expand Down Expand Up @@ -163,6 +164,20 @@ has errored attempting to read it. This file will be renamed to
);
}

var locationFile = _fileSystem.CombinePaths(pkgStorePath, SoftwareInstallLocationFile);
if (_fileSystem.FileExists(locationFile))
{
FaultTolerance.TryCatchWithLoggingException(
() =>
{
packageInformation.SoftwareInstallLocation = _fileSystem.ReadFile(locationFile);
},
"Unable to read software location file",
throwError: false,
logWarningInsteadOfError: true
);
}

return packageInformation;
}

Expand Down Expand Up @@ -248,6 +263,17 @@ public void Save(ChocolateyPackageInformation packageInformation)
{
_fileSystem.DeleteFile(_fileSystem.CombinePaths(pkgStorePath, PinFile));
}

if (!string.IsNullOrWhiteSpace(packageInformation.SoftwareInstallLocation))
{
var locationFile = _fileSystem.CombinePaths(pkgStorePath, SoftwareInstallLocationFile);
if (_fileSystem.FileExists(locationFile)) _fileSystem.DeleteFile(locationFile);
_fileSystem.WriteFile(locationFile, packageInformation.SoftwareInstallLocation);
}
else
{
_fileSystem.DeleteFile(_fileSystem.CombinePaths(pkgStorePath, SoftwareInstallLocationFile));
}
}

public void Remove(IPackageMetadata package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ public virtual void HandlePackageResult(PackageResult packageResult, ChocolateyC
if (key != null) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallLocation, key.InstallLocation, EnvironmentVariableTarget.Process);
}

pkgInfo.SoftwareInstallLocation = Environment.GetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallLocation);

UpdatePackageInformation(pkgInfo);
EnsureBadPackagesPathIsClean(config, packageResult);
EventManager.Publish(new HandlePackageResultCompletedMessage(packageResult, config, commandName));
Expand Down Expand Up @@ -578,11 +580,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.SoftwareInstallLocation))
{
this.Log().Info(ChocolateyLoggers.Important, " Software installed to '{0}'".FormatWith(installLocation.EscapeCurlyBraces()));
this.Log().Info(ChocolateyLoggers.Important, " Software installed to '{0}'".FormatWith(pkgInfo.SoftwareInstallLocation.EscapeCurlyBraces()));
}
else if (!string.IsNullOrWhiteSpace(installerDetected))
{
Expand Down

0 comments on commit 8f33c0c

Please # to comment.