InstallCommand.cs (4623B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="InstallCommand.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.Common 8 { 9 using System.Management.Automation; 10 using Microsoft.Management.Deployment; 11 using Microsoft.WinGet.Client.Engine.Helpers; 12 13 /// <summary> 14 /// This is the base class for all commands that parse a <see cref="FindPackagesOptions" /> result 15 /// from the provided parameters i.e., the "install" and "upgrade" commands. 16 /// </summary> 17 public abstract class InstallCommand : PackageCommand 18 { 19 /// <summary> 20 /// Initializes a new instance of the <see cref="InstallCommand"/> class. 21 /// </summary> 22 /// <param name="psCmdlet">PSCmdlet.</param> 23 internal InstallCommand(PSCmdlet psCmdlet) 24 : base(psCmdlet) 25 { 26 } 27 28 /// <summary> 29 /// Gets or sets a value indicating whether to skip the installer hash validation check. 30 /// </summary> 31 protected bool AllowHashMismatch { get; set; } 32 33 /// <summary> 34 /// Gets or sets a value indicating whether to skip dependencies. 35 /// </summary> 36 protected bool SkipDependencies { get; set; } 37 38 /// <summary> 39 /// Gets or sets the override arguments to be passed on to the installer. 40 /// </summary> 41 protected string? Override { get; set; } 42 43 /// <summary> 44 /// Gets or sets the arguments to be passed on to the installer in addition to the defaults. 45 /// </summary> 46 protected string? Custom { get; set; } 47 48 /// <summary> 49 /// Gets or sets the installation location. 50 /// </summary> 51 protected string? Location { get; set; } 52 53 /// <summary> 54 /// Gets or sets the path to the logging file. 55 /// </summary> 56 protected string? Log { get; set; } 57 58 /// <summary> 59 /// Gets or sets a value indicating whether to continue upon non security related failures. 60 /// </summary> 61 protected bool Force { get; set; } 62 63 /// <summary> 64 /// Gets or sets the optional HTTP Header to pass on to the REST Source. 65 /// </summary> 66 protected string? Header { get; set; } 67 68 /// <summary> 69 /// Gets the install options from the configured parameters. 70 /// DO NOT pass PackageInstallMode WinRT enum type in this method. 71 /// That will cause the type to attempt to be loaded in the construction 72 /// of this method and throw a different exception for Windows PowerShell. 73 /// </summary> 74 /// <param name="version">The <see cref="PackageVersionId" /> to install.</param> 75 /// <param name="mode">Package install mode as string.</param> 76 /// <returns>An <see cref="InstallOptions" /> instance.</returns> 77 internal virtual InstallOptions GetInstallOptions(PackageVersionId? version, string mode) 78 { 79 InstallOptions options = ManagementDeploymentFactory.Instance.CreateInstallOptions(); 80 options.AllowHashMismatch = this.AllowHashMismatch; 81 options.SkipDependencies = this.SkipDependencies; 82 options.Force = this.Force; 83 options.PackageInstallMode = PSEnumHelpers.ToPackageInstallMode(mode); 84 if (version != null) 85 { 86 options.PackageVersionId = version; 87 } 88 89 if (this.Log != null) 90 { 91 options.LogOutputPath = this.Log; 92 } 93 94 if (this.Override != null) 95 { 96 options.ReplacementInstallerArguments = this.Override; 97 } 98 99 // Since these arguments are appended to the installer at runtime, it doesn't make sense to append them if they are whitespace 100 if (!string.IsNullOrWhiteSpace(this.Custom)) 101 { 102 options.AdditionalInstallerArguments = this.Custom; 103 } 104 105 if (this.Location != null) 106 { 107 options.PreferredInstallLocation = this.Location; 108 } 109 110 if (this.Header != null) 111 { 112 options.AdditionalPackageCatalogArguments = this.Header; 113 } 114 115 return options; 116 } 117 } 118 }