DownloadCommand.cs (7706B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="DownloadCommand.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Commands 8 { 9 using System.Management.Automation; 10 using System.Threading.Tasks; 11 using Microsoft.Management.Deployment; 12 using Microsoft.WinGet.Client.Engine.Commands.Common; 13 using Microsoft.WinGet.Client.Engine.Helpers; 14 using Microsoft.WinGet.Client.Engine.PSObjects; 15 using Microsoft.WinGet.Common.Command; 16 using Microsoft.WinGet.Resources; 17 18 /// <summary> 19 /// Downloads a package installer. 20 /// </summary> 21 public sealed class DownloadCommand : PackageCommand 22 { 23 /// <summary> 24 /// Initializes a new instance of the <see cref="DownloadCommand"/> class. 25 /// </summary> 26 /// <param name="psCmdlet">Caller cmdlet.</param> 27 /// <param name="psCatalogPackage">PSCatalogPackage.</param> 28 /// <param name="version">Version to install.</param> 29 /// <param name="id">Package identifier.</param> 30 /// <param name="name">Name of package.</param> 31 /// <param name="moniker">Moniker of package.</param> 32 /// <param name="source">Source to search. If null, all are searched.</param> 33 /// <param name="query">Match against any field of a package.</param> 34 /// <param name="allowHashMismatch">To skip the installer hash validation check.</param> 35 /// <param name="skipDependencies">To skip package dependencies.</param> 36 /// <param name="locale">Locale of the package.</param> 37 public DownloadCommand( 38 PSCmdlet psCmdlet, 39 PSCatalogPackage psCatalogPackage, 40 string version, 41 string id, 42 string name, 43 string moniker, 44 string source, 45 string[] query, 46 bool allowHashMismatch, 47 bool skipDependencies, 48 string locale) 49 : base(psCmdlet) 50 { 51 // PackageCommand 52 if (psCatalogPackage != null) 53 { 54 this.CatalogPackage = psCatalogPackage; 55 } 56 57 this.Version = version; 58 59 // FinderCommand 60 this.Id = id; 61 this.Name = name; 62 this.Moniker = moniker; 63 this.Source = source; 64 this.Query = query; 65 66 // DownloadCommand 67 this.AllowHashMismatch = allowHashMismatch; 68 this.SkipDependencies = skipDependencies; 69 this.Locale = locale; 70 } 71 72 /// <summary> 73 /// Gets or sets a value indicating whether to skip the installer hash validation check. 74 /// </summary> 75 private bool AllowHashMismatch { get; set; } 76 77 /// <summary> 78 /// Gets or sets a value indicating whether to skip dependencies. 79 /// </summary> 80 private bool SkipDependencies { get; set; } 81 82 /// <summary> 83 /// Gets or sets the locale to install. 84 /// </summary> 85 private string? Locale { get; set; } 86 87 /// <summary> 88 /// Process download package. 89 /// </summary> 90 /// <param name="downloadDirectory">The target directory where the installer will be downloaded to.</param> 91 /// <param name="psPackageFieldMatchOption">PSPackageFieldMatchOption.</param> 92 /// <param name="psPackageInstallScope">PSPackageInstallScope.</param> 93 /// <param name="psProcessorArchitecture">PSProcessorArchitecture.</param> 94 /// <param name="psPackageInstallerType">PSPackageInstallerType.</param> 95 /// <param name="skipMicrosoftStoreLicense">If true, skips downloading a Store license.</param> 96 /// <param name="psWindowsPlatform">The platform to download the package for.</param> 97 /// <param name="targetOSVersion">The target OS version to download for.</param> 98 public void Download( 99 string downloadDirectory, 100 string psPackageFieldMatchOption, 101 string psPackageInstallScope, 102 string psProcessorArchitecture, 103 string psPackageInstallerType, 104 bool skipMicrosoftStoreLicense, 105 string psWindowsPlatform, 106 string targetOSVersion) 107 { 108 var result = this.Execute( 109 async () => await this.GetPackageAndExecuteAsync( 110 CompositeSearchBehavior.RemotePackagesFromRemoteCatalogs, 111 PSEnumHelpers.ToPackageFieldMatchOption(psPackageFieldMatchOption), 112 async (package, version) => 113 { 114 DownloadOptions options = this.GetDownloadOptions(version); 115 116 if (!string.IsNullOrEmpty(downloadDirectory)) 117 { 118 options.DownloadDirectory = downloadDirectory; 119 } 120 121 if (!PSEnumHelpers.IsDefaultEnum(psProcessorArchitecture)) 122 { 123 options.Architecture = PSEnumHelpers.ToProcessorArchitecture(psProcessorArchitecture); 124 } 125 126 if (!PSEnumHelpers.IsDefaultEnum(psPackageInstallerType)) 127 { 128 options.InstallerType = PSEnumHelpers.ToPackageInstallerType(psPackageInstallerType); 129 } 130 131 options.Scope = PSEnumHelpers.ToPackageInstallScope(psPackageInstallScope); 132 options.SkipMicrosoftStoreLicense = skipMicrosoftStoreLicense; 133 134 if (!PSEnumHelpers.IsDefaultEnum(psWindowsPlatform)) 135 { 136 options.Platform = PSEnumHelpers.ToWindowsPlatform(psWindowsPlatform); 137 } 138 139 if (!string.IsNullOrEmpty(targetOSVersion)) 140 { 141 options.TargetOSVersion = targetOSVersion; 142 } 143 144 return await this.DownloadPackageAsync(package, options); 145 })); 146 147 if (result != null) 148 { 149 this.Write(StreamType.Object, new PSDownloadResult(result.Item1, result.Item2)); 150 } 151 } 152 153 private DownloadOptions GetDownloadOptions(PackageVersionId? version) 154 { 155 var options = ManagementDeploymentFactory.Instance.CreateDownloadOptions(); 156 if (version != null) 157 { 158 options.PackageVersionId = version; 159 } 160 161 if (this.Locale != null) 162 { 163 options.Locale = this.Locale; 164 } 165 166 options.AllowHashMismatch = this.AllowHashMismatch; 167 options.SkipDependencies = this.SkipDependencies; 168 169 return options; 170 } 171 172 private async Task<DownloadResult> DownloadPackageAsync( 173 CatalogPackage package, 174 DownloadOptions options) 175 { 176 var activity = string.Format(Resources.ProgressRecordActivityExporting, package.Name); 177 var progressOperation = new DownloadOperationWithProgress(this, activity); 178 return await progressOperation.ExecuteAsync( 179 () => PackageManagerWrapper.Instance.DownloadPackageAsync(package, options)); 180 } 181 } 182 }