winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

OpenConfigurationParameters.cs (8098B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="OpenConfigurationParameters.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Configuration.Engine.Helpers
      8 {
      9     using System;
     10     using System.IO;
     11     using System.Management.Automation;
     12     using Microsoft.Management.Configuration.Processor;
     13     using Microsoft.PowerShell;
     14     using Microsoft.WinGet.Common.Command;
     15     using Microsoft.WinGet.Resources;
     16 
     17     /// <summary>
     18     /// The parameters used to open a configuration.
     19     /// </summary>
     20     internal class OpenConfigurationParameters
     21     {
     22         private const string Default = "default";
     23         private const string AllUsers = "allusers";
     24         private const string CurrentUser = "currentuser";
     25 
     26         /// <summary>
     27         /// Initializes a new instance of the <see cref="OpenConfigurationParameters"/> class.
     28         /// </summary>
     29         /// <param name="pwshCmdlet">PowerShellCmdlet.</param>
     30         /// <param name="file">The configuration file.</param>
     31         /// <param name="modulePath">The module path to use.</param>
     32         /// <param name="executionPolicy">Execution policy.</param>
     33         /// <param name="processorPath">The processor path to use.</param>
     34         /// <param name="canUseTelemetry">If telemetry can be used.</param>
     35         /// <param name="fromHistory">If the configuration is from history; changes the meaning of `ConfigFile` to the instance identifier.</param>
     36         public OpenConfigurationParameters(
     37             PowerShellCmdlet pwshCmdlet,
     38             string file,
     39             string modulePath,
     40             ExecutionPolicy executionPolicy,
     41             string processorPath,
     42             bool canUseTelemetry,
     43             bool fromHistory = false)
     44         {
     45             if (!fromHistory)
     46             {
     47                 this.ConfigFile = this.VerifyFile(file, pwshCmdlet);
     48             }
     49             else
     50             {
     51                 this.ConfigFile = file;
     52             }
     53 
     54             this.InitializeModulePath(modulePath);
     55             this.ExecutionPolicy = executionPolicy;
     56             this.Policy = this.GetConfigurationProcessorPolicy(executionPolicy);
     57             this.ProcessorPath = processorPath;
     58             this.CanUseTelemetry = canUseTelemetry;
     59             this.FromHistory = fromHistory;
     60         }
     61 
     62         /// <summary>
     63         /// Initializes a new instance of the <see cref="OpenConfigurationParameters"/> class.
     64         /// </summary>
     65         /// <param name="pwshCmdlet">PowerShellCmdlet.</param>
     66         /// <param name="modulePath">The module path to use.</param>
     67         /// <param name="executionPolicy">Execution policy.</param>
     68         /// <param name="processorPath">The processor path to use.</param>
     69         /// <param name="canUseTelemetry">If telemetry can be used.</param>
     70         public OpenConfigurationParameters(
     71             PowerShellCmdlet pwshCmdlet,
     72             string modulePath,
     73             ExecutionPolicy executionPolicy,
     74             string processorPath,
     75             bool canUseTelemetry)
     76         {
     77             this.ConfigFile = string.Empty;
     78             this.InitializeModulePath(modulePath);
     79             this.ExecutionPolicy = executionPolicy;
     80             this.Policy = this.GetConfigurationProcessorPolicy(executionPolicy);
     81             this.ProcessorPath = processorPath;
     82             this.CanUseTelemetry = canUseTelemetry;
     83         }
     84 
     85         /// <summary>
     86         /// Gets the configuration file.
     87         /// </summary>
     88         public string ConfigFile { get; }
     89 
     90         /// <summary>
     91         /// Gets the location to install the modules.
     92         /// </summary>
     93         public PowerShellConfigurationProcessorLocation Location { get; private set; }
     94 
     95         /// <summary>
     96         /// Gets the custom location to install modules.
     97         /// </summary>
     98         public string? CustomLocation { get; private set; }
     99 
    100         /// <summary>
    101         /// Gets the original ExecutionPolicy value.
    102         /// </summary>
    103         public ExecutionPolicy ExecutionPolicy { get; private set; } = ExecutionPolicy.Undefined;
    104 
    105         /// <summary>
    106         /// Gets execution policy of the processor.
    107         /// </summary>
    108         public PowerShellConfigurationProcessorPolicy Policy { get; }
    109 
    110         /// <summary>
    111         /// Gets DSCv3 processor path.
    112         /// </summary>
    113         public string ProcessorPath { get; }
    114 
    115         /// <summary>
    116         /// Gets a value indicating whether to use telemetry or not.
    117         /// </summary>
    118         public bool CanUseTelemetry { get; }
    119 
    120         /// <summary>
    121         /// Gets a value indicating whether the configuration is from history.
    122         /// </summary>
    123         public bool FromHistory { get; }
    124 
    125         private string VerifyFile(string filePath, PowerShellCmdlet pwshCmdlet)
    126         {
    127             if (!Path.IsPathRooted(filePath))
    128             {
    129                 filePath = Path.GetFullPath(
    130                     Path.Combine(
    131                         pwshCmdlet.GetCurrentFileSystemLocation(),
    132                         filePath));
    133             }
    134             else
    135             {
    136                 filePath = Path.GetFullPath(filePath);
    137             }
    138 
    139             if (!File.Exists(filePath))
    140             {
    141                 throw new FileNotFoundException(filePath);
    142             }
    143 
    144             return filePath;
    145         }
    146 
    147         private PowerShellConfigurationProcessorPolicy GetConfigurationProcessorPolicy(ExecutionPolicy policy)
    148         {
    149             return policy switch
    150             {
    151                 ExecutionPolicy.Unrestricted => PowerShellConfigurationProcessorPolicy.Unrestricted,
    152                 ExecutionPolicy.RemoteSigned => PowerShellConfigurationProcessorPolicy.RemoteSigned,
    153                 ExecutionPolicy.AllSigned => PowerShellConfigurationProcessorPolicy.AllSigned,
    154                 ExecutionPolicy.Restricted => PowerShellConfigurationProcessorPolicy.Restricted,
    155                 ExecutionPolicy.Bypass => PowerShellConfigurationProcessorPolicy.Bypass,
    156                 _ => throw new InvalidOperationException(),
    157             };
    158         }
    159 
    160         private void InitializeModulePath(string modulePath)
    161         {
    162             // TODO: Create cmdlet that specify the global custom location for a PowerShell session.
    163             if (!string.IsNullOrEmpty(modulePath))
    164             {
    165                 if (string.Compare(modulePath, Default, true) == 0)
    166                 {
    167                     this.Location = PowerShellConfigurationProcessorLocation.Default;
    168                 }
    169                 else if (string.Compare(modulePath, CurrentUser, true) == 0)
    170                 {
    171                     this.Location = PowerShellConfigurationProcessorLocation.CurrentUser;
    172                 }
    173                 else if (string.Compare(modulePath, AllUsers, true) == 0)
    174                 {
    175                     if (!Utilities.ExecutingAsAdministrator)
    176                     {
    177                         throw new ArgumentException(Resources.ConfigurationAllUsersElevated);
    178                     }
    179 
    180                     this.Location = PowerShellConfigurationProcessorLocation.AllUsers;
    181                 }
    182                 else
    183                 {
    184                     string customLocation = modulePath;
    185                     if (!Path.IsPathRooted(customLocation))
    186                     {
    187                         throw new ArgumentException(Resources.ConfigurationModulePathArgError);
    188                     }
    189 
    190                     this.CustomLocation = Path.GetFullPath(customLocation);
    191                     this.Location = PowerShellConfigurationProcessorLocation.Custom;
    192                 }
    193             }
    194             else
    195             {
    196                 this.Location = PowerShellConfigurationProcessorLocation.WinGetModulePath;
    197             }
    198         }
    199     }
    200 }