Skip to content

Rewrite Get-ChocolateyPath cmdlet #3646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\GetChocolateyConfigValueCommand.cs" />
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
<Compile Include="Helpers\ConfigHelper.cs" />
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
Expand All @@ -83,6 +84,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
58 changes: 58 additions & 0 deletions src/Chocolatey.PowerShell/Commands/GetChocolateyPathCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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")]
[OutputType(typeof(string))]
public class GetChocolateyPathCommand : ChocolateyCmdlet
{
[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
37 changes: 37 additions & 0 deletions src/Chocolatey.PowerShell/Shared/ChocolateyPathType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Text;

namespace Chocolatey.PowerShell.Shared
{
/// <summary>
/// The type of path to be retrieved by the <see cref="GetChocolateyPathCmdlet"/>
/// </summary>
public enum ChocolateyPathType
{
/// <summary>
/// Retrieves the path to the package folder; <c>$env:ChocolateyInstall\lib\$env:ChocolateyPackageName</c>
/// </summary>
PackagePath,
/// <summary>
/// Retrieves the path to the Chocolatey install folder; <c>$env:ChocolateyInstall</c> or an appropriate fallback.
/// </summary>
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);
}
}
3 changes: 0 additions & 3 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@
<EmbeddedResource Include="helpers\functions\Format-FileSize.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="helpers\functions\Get-ChocolateyPath.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="helpers\functions\Get-ChocolateyUnzip.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
Loading