|
| 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 | +} |
0 commit comments