Skip to content

Commit 49c941a

Browse files
authored
Merge pull request #3646 from vexx32/get-chocolateypath
Rewrite Get-ChocolateyPath cmdlet
2 parents 870d600 + 6d79bd3 commit 49c941a

File tree

10 files changed

+312
-316
lines changed

10 files changed

+312
-316
lines changed

src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
</ItemGroup>
6060
<ItemGroup>
6161
<Compile Include="Commands\GetChocolateyConfigValueCommand.cs" />
62+
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
6263
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
6364
<Compile Include="Helpers\ConfigHelper.cs" />
6465
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
@@ -83,6 +84,7 @@
8384
</Compile>
8485
<Compile Include="Shared\ChecksumType.cs" />
8586
<Compile Include="Shared\ChocolateyCmdlet.cs" />
87+
<Compile Include="Shared\ChocolateyPathType.cs" />
8688
<Compile Include="Shared\EnvironmentVariables.cs" />
8789
<Compile Include="Win32\NativeMethods.cs" />
8890
</ItemGroup>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright © 2017 - 2025 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using Chocolatey.PowerShell;
18+
using Chocolatey.PowerShell.Helpers;
19+
using Chocolatey.PowerShell.Shared;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Management.Automation;
23+
using System.Text;
24+
25+
using static Chocolatey.PowerShell.Helpers.PSHelper;
26+
27+
namespace Chocolatey.PowerShell.Commands
28+
{
29+
[Cmdlet(VerbsCommon.Get, "ChocolateyPath")]
30+
[OutputType(typeof(string))]
31+
public class GetChocolateyPathCommand : ChocolateyCmdlet
32+
{
33+
[Parameter(Mandatory = true, Position = 0)]
34+
[Alias("Type")]
35+
public ChocolateyPathType PathType { get; set; }
36+
37+
protected override void End()
38+
{
39+
try
40+
{
41+
var path = Paths.GetChocolateyPathType(this, PathType);
42+
43+
if (ContainerExists(this, path))
44+
{
45+
WriteObject(path);
46+
}
47+
}
48+
catch (NotImplementedException error)
49+
{
50+
ThrowTerminatingError(new ErrorRecord(error, $"{ErrorId}.NotImplemented", ErrorCategory.NotImplemented, PathType));
51+
}
52+
catch (Exception error)
53+
{
54+
ThrowTerminatingError(new ErrorRecord(error, $"{ErrorId}.Unknown", ErrorCategory.NotSpecified, PathType));
55+
}
56+
}
57+
}
58+
}

src/Chocolatey.PowerShell/Helpers/Paths.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,41 @@ void updatePath()
162162
}
163163
}
164164
}
165+
166+
/// <summary>
167+
/// Gets the file path corresponding to the desired <paramref name="pathType"/>.
168+
/// </summary>
169+
/// <param name="pathType">The type of path to retrieve the value for.</param>
170+
/// <returns>The requested path as a string.</returns>
171+
/// <exception cref="NotImplementedException">If the provided path type is not implemented.</exception>
172+
public static string GetChocolateyPathType(PSCmdlet cmdlet, ChocolateyPathType pathType)
173+
{
174+
switch (pathType)
175+
{
176+
case ChocolateyPathType.PackagePath:
177+
var path = EnvironmentHelper.GetVariable(EnvironmentVariables.ChocolateyPackageFolder);
178+
if (!string.IsNullOrEmpty(path))
179+
{
180+
return path;
181+
}
182+
183+
path = EnvironmentHelper.GetVariable(EnvironmentVariables.PackageFolder);
184+
if (!string.IsNullOrEmpty(path))
185+
{
186+
return path;
187+
}
188+
else
189+
{
190+
var installPath = GetChocolateyPathType(cmdlet, ChocolateyPathType.InstallPath);
191+
var packageName = Environment.GetEnvironmentVariable(EnvironmentVariables.ChocolateyPackageName);
192+
193+
return PSHelper.CombinePaths(cmdlet, installPath, "lib", packageName);
194+
}
195+
case ChocolateyPathType.InstallPath:
196+
return PSHelper.GetInstallLocation(cmdlet);
197+
default:
198+
throw new NotImplementedException($"The path value for type '{pathType}' is not known.");
199+
};
200+
}
165201
}
166202
}

src/Chocolatey.PowerShell/Shared/ChocolateyCmdlet.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ protected string ErrorId
5151
}
5252
}
5353

54+
/// <summary>
55+
/// Gets the directory that Chocolatey is installed in.
56+
/// </summary>
57+
protected string ChocolateyInstallLocation
58+
{
59+
get
60+
{
61+
return PSHelper.GetInstallLocation(this);
62+
}
63+
}
64+
5465
/// <summary>
5566
/// For compatibility reasons, we always add the -IgnoredArguments parameter, so that newly added parameters
5667
/// won't break things too much if a package is run with an older version of Chocolatey.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright © 2017 - 2025 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Text;
20+
21+
namespace Chocolatey.PowerShell.Shared
22+
{
23+
/// <summary>
24+
/// The type of path to be retrieved by the <see cref="GetChocolateyPathCmdlet"/>
25+
/// </summary>
26+
public enum ChocolateyPathType
27+
{
28+
/// <summary>
29+
/// Retrieves the path to the package folder; <c>$env:ChocolateyInstall\lib\$env:ChocolateyPackageName</c>
30+
/// </summary>
31+
PackagePath,
32+
/// <summary>
33+
/// Retrieves the path to the Chocolatey install folder; <c>$env:ChocolateyInstall</c> or an appropriate fallback.
34+
/// </summary>
35+
InstallPath,
36+
}
37+
}

src/Chocolatey.PowerShell/Shared/EnvironmentVariables.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,16 @@ public static class EnvironmentVariables
6464
[Browsable(false)]
6565
public const string ChocolateyAllowEmptyChecksumsSecure = nameof(ChocolateyAllowEmptyChecksumsSecure);
6666

67+
[EditorBrowsable(EditorBrowsableState.Never)]
68+
[Browsable(false)]
69+
public const string ChocolateyPackageName = nameof(ChocolateyPackageName);
70+
71+
[EditorBrowsable(EditorBrowsableState.Never)]
72+
[Browsable(false)]
73+
public const string ChocolateyPackageFolder = nameof(ChocolateyPackageFolder);
74+
75+
[EditorBrowsable(EditorBrowsableState.Never)]
76+
[Browsable(false)]
77+
public const string PackageFolder = nameof(PackageFolder);
6778
}
6879
}

src/chocolatey.resources/chocolatey.resources.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@
7070
<EmbeddedResource Include="helpers\functions\Format-FileSize.ps1">
7171
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7272
</EmbeddedResource>
73-
<EmbeddedResource Include="helpers\functions\Get-ChocolateyPath.ps1">
74-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
75-
</EmbeddedResource>
7673
<EmbeddedResource Include="helpers\functions\Get-ChocolateyUnzip.ps1">
7774
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7875
</EmbeddedResource>

0 commit comments

Comments
 (0)