Skip to content

Commit

Permalink
(chocolatey#3051) Rewrite Get-ChocolateyPath helper command
Browse files Browse the repository at this point in the history
Rewrite of the Get-ChocolateyPath into C# cmdlet, and associated
changes to helper methods.
  • Loading branch information
vexx32 committed Oct 15, 2024
1 parent f9be806 commit fcd0371
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 299 deletions.
2 changes: 2 additions & 0 deletions src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
<Compile Include="Shared\ChecksumVerificationFailedException.cs" />
Expand All @@ -81,6 +82,7 @@
</Compile>
<Compile Include="Shared\ChecksumType.cs" />
<Compile Include="Shared\ChocolateyCmdlet.cs" />
<Compile Include="Shared\ChocolateyPathType.cs" />
<Compile Include="Shared\EnvironmentVariables.cs" />
<Compile Include="Win32\NativeMethods.cs" />
</ItemGroup>
Expand Down
72 changes: 72 additions & 0 deletions src/Chocolatey.PowerShell/Commands/GetChocolateyPathCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Chocolatey.PowerShell;
using Chocolatey.PowerShell.Helpers;
using Chocolatey.PowerShell.Shared;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;

using static Chocolatey.PowerShell.Helpers.PSHelper;

namespace Chocolatey.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Get, "ChocolateyPath")]
public class GetChocolateyPathCommand : ChocolateyCmdlet
{
/*
.SYNOPSIS
Retrieve the paths available to be used by maintainers of packages.
.DESCRIPTION
This function will attempt to retrieve the path according to the specified Path Type
to a valid location that can be used by maintainers in certain scenarios.
.NOTES
Available in 1.2.0+.
.INPUTS
None
.OUTPUTS
This function outputs the full path stored accordingly with specified path type.
If no path could be found, there is no output.
.PARAMETER pathType
The type of path that should be looked up.
Available values are:
- `PackagePath` - The path to the the package that is being installed. Typically `C:\ProgramData\chocolatey\lib\<PackageName>`
- `InstallPath` - The path to where Chocolatey is installed. Typically `C:\ProgramData\chocolatey`
.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.
.EXAMPLE
>
$path = Get-ChocolateyPath -PathType 'PackagePath'
*/
[Parameter(Mandatory = true, Position = 0)]
[Alias("Type")]
public ChocolateyPathType PathType { get; set; }

protected override void End()
{
try
{
var path = Paths.GetChocolateyPathType(this, PathType);

if (ContainerExists(this, path))
{
WriteObject(path);
}
}
catch (NotImplementedException error)
{
ThrowTerminatingError(new ErrorRecord(error, $"{ErrorId}.NotImplemented", ErrorCategory.NotImplemented, PathType));
}
catch (Exception error)
{
ThrowTerminatingError(new ErrorRecord(error, $"{ErrorId}.Unknown", ErrorCategory.NotSpecified, PathType));
}
}
}
}
36 changes: 36 additions & 0 deletions src/Chocolatey.PowerShell/Helpers/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,41 @@ void updatePath()
}
}
}

/// <summary>
/// Gets the file path corresponding to the desired <paramref name="pathType"/>.
/// </summary>
/// <param name="pathType">The type of path to retrieve the value for.</param>
/// <returns>The requested path as a string.</returns>
/// <exception cref="NotImplementedException">If the provided path type is not implemented.</exception>
public static string GetChocolateyPathType(PSCmdlet cmdlet, ChocolateyPathType pathType)
{
switch (pathType)
{
case ChocolateyPathType.PackagePath:
var path = EnvironmentHelper.GetVariable(EnvironmentVariables.ChocolateyPackageFolder);
if (!string.IsNullOrEmpty(path))
{
return path;
}

path = EnvironmentHelper.GetVariable(EnvironmentVariables.PackageFolder);
if (!string.IsNullOrEmpty(path))
{
return path;
}
else
{
var installPath = GetChocolateyPathType(cmdlet, ChocolateyPathType.InstallPath);
var packageName = Environment.GetEnvironmentVariable(EnvironmentVariables.ChocolateyPackageName);

return PSHelper.CombinePaths(cmdlet, installPath, "lib", packageName);
}
case ChocolateyPathType.InstallPath:
return PSHelper.GetInstallLocation(cmdlet);
default:
throw new NotImplementedException($"The path value for type '{pathType}' is not known.");
};
}
}
}
11 changes: 11 additions & 0 deletions src/Chocolatey.PowerShell/Shared/ChocolateyCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ protected string ErrorId
}
}

/// <summary>
/// Gets the directory that Chocolatey is installed in.
/// </summary>
protected string ChocolateyInstallLocation
{
get
{
return PSHelper.GetInstallLocation(this);
}
}

/// <summary>
/// For compatibility reasons, we always add the -IgnoredArguments parameter, so that newly added parameters
/// won't break things too much if a package is run with an older version of Chocolatey.
Expand Down
12 changes: 12 additions & 0 deletions src/Chocolatey.PowerShell/Shared/ChocolateyPathType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Chocolatey.PowerShell.Shared
{
public enum ChocolateyPathType
{
PackagePath,
InstallPath,
}
}
11 changes: 11 additions & 0 deletions src/Chocolatey.PowerShell/Shared/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,16 @@ public static class EnvironmentVariables
[Browsable(false)]
public const string ChocolateyAllowEmptyChecksumsSecure = nameof(ChocolateyAllowEmptyChecksumsSecure);

[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public const string ChocolateyPackageName = nameof(ChocolateyPackageName);

[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public const string ChocolateyPackageFolder = nameof(ChocolateyPackageFolder);

[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public const string PackageFolder = nameof(PackageFolder);
}
}
1 change: 0 additions & 1 deletion src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<EmbeddedResource Include="helpers\chocolateyInstaller.psm1" />
<EmbeddedResource Include="helpers\functions\Format-FileSize.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyConfigValue.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyPath.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyUnzip.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyWebFile.ps1" />
<EmbeddedResource Include="helpers\functions\Get-FtpFile.ps1" />
Expand Down
Loading

0 comments on commit fcd0371

Please sign in to comment.