Skip to content

Commit 6c4e71c

Browse files
committed
(#3477, #3635) Add Expand-ChocolateyArchive helper cmdlet
1 parent 693fc33 commit 6c4e71c

13 files changed

Lines changed: 1088 additions & 4 deletions

File tree

src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<ItemGroup>
4747
<Reference Include="System" />
4848
<Reference Include="System.Core" />
49+
<Reference Include="System.IO.Compression" />
4950
<Reference Include="System.Xml.Linq" />
5051
<Reference Include="System.Data.DataSetExtensions" />
5152
<Reference Include="Microsoft.CSharp" />
@@ -65,6 +66,9 @@
6566
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
6667
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
6768
<Compile Include="Helpers\ConfigHelper.cs" />
69+
<Compile Include="Commands\ExpandChocolateyArchiveCommand.cs" />
70+
<Compile Include="Shared\SevenZipException.cs" />
71+
<Compile Include="Helpers\ExtractArchiveHelper.cs" />
6872
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
6973
<Compile Include="Shared\ChecksumVerificationFailedException.cs" />
7074
<Compile Include="Shared\ChecksumMissingException.cs" />
@@ -75,6 +79,7 @@
7579
<Compile Include="Commands\TestProcessAdminRightsCommand.cs" />
7680
<Compile Include="Commands\UninstallChocolateyPathCommand.cs" />
7781
<Compile Include="Commands\UpdateSessionEnvironmentCommand.cs" />
82+
<Compile Include="Helpers\ArchitectureWidth.cs" />
7883
<Compile Include="Helpers\ChecksumValidator.cs" />
7984
<Compile Include="Helpers\Elevation.cs" />
8085
<Compile Include="Helpers\EnvironmentHelper.cs" />
@@ -90,6 +95,7 @@
9095
<Compile Include="Shared\ChocolateyPathType.cs" />
9196
<Compile Include="Shared\EnvironmentNames.cs" />
9297
<Compile Include="Shared\EnvironmentVariables.cs" />
98+
<Compile Include="Shared\ProcessHandler.cs" />
9399
<Compile Include="Win32\NativeMethods.cs" />
94100
</ItemGroup>
95101
<ItemGroup>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Chocolatey.PowerShell.Helpers;
2+
using Chocolatey.PowerShell.Shared;
3+
using System;
4+
using System.IO;
5+
using System.Management.Automation;
6+
using System.Text.RegularExpressions;
7+
using System.Threading.Tasks;
8+
using static chocolatey.StringResources.EnvironmentVariables;
9+
10+
namespace Chocolatey.PowerShell.Commands
11+
{
12+
[Cmdlet(VerbsData.Expand, "ChocolateyArchive", DefaultParameterSetName = "Path", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
13+
[OutputType(typeof(string))]
14+
public class ExpandChocolateyArchiveCommand : ChocolateyCmdlet
15+
{
16+
[Alias("File", "FileFullPath")]
17+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Path")]
18+
[Parameter(Mandatory = true, ParameterSetName = "BothPaths")]
19+
public string Path { get; set; } = string.Empty;
20+
21+
[Alias("UnzipLocation")]
22+
[Parameter(Mandatory = true, Position = 1)]
23+
public string Destination { get; set; } = string.Empty;
24+
25+
[Parameter(Position = 2)]
26+
[Alias("SpecificFolder")]
27+
public string FilesToExtract { get; set; }
28+
29+
[Parameter(Position = 3)]
30+
public string PackageName { get; set; }
31+
32+
[Alias("File64", "FileFullPath64")]
33+
[Parameter(Mandatory = true, ParameterSetName = "Path64")]
34+
[Parameter(Mandatory = true, ParameterSetName = "BothPaths")]
35+
public string Path64 { get; set; }
36+
37+
[Parameter]
38+
public SwitchParameter DisableLogging { get; set; }
39+
40+
[Parameter]
41+
public SwitchParameter UseBuiltinCompression { get; set; } = EnvironmentHelper.GetVariable(Package.ChocolateyUseBuiltinCompression) == "true";
42+
43+
protected override void End()
44+
{
45+
var helper = new ExtractArchiveHelper(this, PipelineStopToken);
46+
try
47+
{
48+
helper.ExtractFiles(Path, Path64, PackageName, Destination, FilesToExtract, UseBuiltinCompression, DisableLogging);
49+
50+
WriteObject(Destination);
51+
}
52+
catch (FileNotFoundException error)
53+
{
54+
ThrowTerminatingError(new ErrorRecord(
55+
error,
56+
$"{ErrorId}.FileNotFound",
57+
ErrorCategory.ObjectNotFound,
58+
string.IsNullOrEmpty(Path) ? Path64 : Path));
59+
}
60+
catch (InvalidOperationException error)
61+
{
62+
ThrowTerminatingError(new ErrorRecord(
63+
error,
64+
$"{ErrorId}.ApplicationMissing",
65+
ErrorCategory.InvalidOperation,
66+
targetObject: null));
67+
}
68+
catch (NotSupportedException error)
69+
{
70+
ThrowTerminatingError(new ErrorRecord(
71+
error,
72+
$"{ErrorId}.UnsupportedArchitecture",
73+
ErrorCategory.NotImplemented,
74+
string.IsNullOrEmpty(Path) ? Path64 : Path));
75+
}
76+
catch (SevenZipException error)
77+
{
78+
ThrowTerminatingError(new ErrorRecord(
79+
error,
80+
$"{ErrorId}.ExtractionFailed",
81+
ErrorCategory.InvalidResult,
82+
string.IsNullOrEmpty(Path) ? Path64 : Path));
83+
}
84+
catch (Exception error)
85+
{
86+
ThrowTerminatingError(new ErrorRecord(
87+
error,
88+
$"{ErrorId}.Unknown",
89+
ErrorCategory.NotSpecified,
90+
string.IsNullOrEmpty(Path) ? Path64 : Path));
91+
}
92+
}
93+
}
94+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Chocolatey.PowerShell.Helpers
6+
{
7+
/// <summary>
8+
/// Provides information on the current running architecture width of the process.
9+
/// </summary>
10+
internal static class ArchitectureWidth
11+
{
12+
/// <summary>
13+
/// Returns either 64 or 32, depending on whether the current process environment is 64-bit.
14+
/// </summary>
15+
/// <returns>The current architecture width as an integer.</returns>
16+
internal static int Get()
17+
{
18+
return Environment.Is64BitProcess ? 64 : 32;
19+
}
20+
21+
/// <summary>
22+
/// Compares the current architecture to the expected value.
23+
/// </summary>
24+
/// <param name="compareTo">The architecture width to compare to.</param>
25+
/// <returns>True if the provided value matches the current architecture width, otherwise false.</returns>
26+
internal static bool Matches(int compareTo)
27+
{
28+
return Get() == compareTo;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)