|
1 | | -using Chocolatey.PowerShell.Shared; |
2 | | -using System; |
3 | | -using System.Collections; |
4 | | -using System.Collections.Generic; |
5 | | -using System.Management.Automation; |
6 | | -using System.Text; |
7 | | -using System.Text.RegularExpressions; |
8 | | - |
| 1 | +// Copyright © 2017 - 2025 Chocolatey Software, Inc |
| 2 | +// Copyright © 2011 - 2017 RealDimensions Software, LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +using Chocolatey.PowerShell.Shared; |
| 18 | +using System.Collections; |
| 19 | +using System.Management.Automation; |
| 20 | +using Chocolatey.PowerShell.Helpers; |
| 21 | + |
9 | 22 | namespace Chocolatey.PowerShell.Commands |
10 | | -{ |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Parses a string and returns a hash table array of those values for use in package scripts. |
| 26 | + /// </summary> |
| 27 | + /// <param name="Parameters">A string containing parameters to be parsed.</param> |
| 28 | + /// <returns>A hashtable of parameters that have been passed to <paramref name="Parameters"> and parsed.</returns> |
11 | 29 | [Cmdlet(VerbsCommon.Get, "PackageParameter")] |
| 30 | + [Alias("Get-PackageParameters")] |
12 | 31 | [OutputType(typeof(Hashtable))] |
13 | 32 | public class GetPackageParameterCommand : ChocolateyCmdlet |
14 | 33 | { |
15 | | - /* |
16 | | -.SYNOPSIS |
17 | | -Parses a string and returns a hash table array of those values for use |
18 | | -in package scripts. |
19 | | -
|
20 | | -.DESCRIPTION |
21 | | -This looks at a string value and parses it into a hash table array for |
22 | | -use in package scripts. By default this will look at |
23 | | -`$env:ChocolateyPackageParameters` (`--params="'/ITEM:value'"`) and |
24 | | -`$env:ChocolateyPackageParametersSensitive` |
25 | | -(`--package-parameters-sensitive="'/PASSWORD:value'"` in commercial |
26 | | -editions). |
27 | | -
|
28 | | -Learn more about using this at https://docs.chocolatey.org/en-us/guides/create/parse-packageparameters-argument |
29 | | -
|
30 | | -.NOTES |
31 | | -If you need compatibility with older versions of Chocolatey, |
32 | | -take a dependency on the `chocolatey-core.extension` package which |
33 | | -also provides this functionality. If you are pushing to the community |
34 | | -package repository (https://community.chocolatey.org/packages), you are required |
35 | | -to take a dependency on the core extension until January 2018. How to |
36 | | -do this is explained at https://docs.chocolatey.org/en-us/guides/create/parse-packageparameters-argument#step-3---use-core-community-extension. |
37 | | -
|
38 | | -The differences between this and the `chocolatey-core.extension` package |
39 | | -functionality is that the extension function can only do one string at a |
40 | | -time and it only looks at `$env:ChocolateyPackageParameters` by default. |
41 | | -It also only supports splitting by `:`, with this function you can |
42 | | -either split by `:` or `=`. For compatibility with the core extension, |
43 | | -build all docs with `/Item:Value`. |
44 | | -
|
45 | | -.INPUTS |
46 | | -None |
47 | | -
|
48 | | -.OUTPUTS |
49 | | -[HashTable] |
50 | | -
|
51 | | -.PARAMETER Parameters |
52 | | -OPTIONAL - Specify a string to parse. If not set, will use |
53 | | -`$env:ChocolateyPackageParameters` and |
54 | | -`$env:ChocolateyPackageParametersSensitive` to parse values from. |
55 | | -
|
56 | | -Parameters should be passed as "/NAME:value" or "/NAME=value". For |
57 | | -compatibility with `chocolatey-core.extension`, use `:`. |
58 | | -
|
59 | | -For example `-Parameters "/ITEM1:value /ITEM2:value with spaces" |
60 | | -
|
61 | | -To maintain compatibility with the prior art of the chocolatey-core.extension |
62 | | -function by the same name, quotes and apostrophes surrounding |
63 | | -parameter values will be removed. When the param is used, those items |
64 | | -can be added back if desired, but it's most important to ensure that |
65 | | -existing packages are compatible on upgrade. |
66 | | -
|
67 | | -.PARAMETER IgnoredArguments |
68 | | -Allows splatting with arguments that do not apply and future expansion. |
69 | | -Do not use directly. |
70 | | -
|
71 | | -.EXAMPLE |
72 | | -> |
73 | | -# The default way of calling, uses `$env:ChocolateyPackageParameters` |
74 | | -# and `$env:ChocolateyPackageParametersSensitive` - this is typically |
75 | | -# how things are passed in from choco.exe |
76 | | -$pp = Get-PackageParameters |
77 | | -
|
78 | | -.EXAMPLE |
79 | | -> |
80 | | -# see https://docs.chocolatey.org/en-us/guides/create/parse-packageparameters-argument |
81 | | -# command line call: `choco install <pkg_id> --params "'/LICENSE:value'"` |
82 | | -$pp = Get-PackageParameters |
83 | | -# Read-Host, PromptForChoice, etc are not blocking calls with Chocolatey. |
84 | | -# Chocolatey has a custom PowerShell host that will time these calls |
85 | | -# after 30 seconds, allowing headless operation to continue but offer |
86 | | -# prompts to users to ask questions during installation. |
87 | | -if (!$pp['LICENSE']) { $pp['LICENSE'] = Read-Host 'License key?' } |
88 | | -# set a default if not passed |
89 | | -if (!$pp['LICENSE']) { $pp['LICENSE'] = '1234' } |
90 | | -
|
91 | | -.EXAMPLE |
92 | | -> |
93 | | -$pp = Get-PackageParameters |
94 | | -if (!$pp['UserName']) { $pp['UserName'] = "$env:UserName" } |
95 | | -if (!$pp['Password']) { $pp['Password'] = Read-Host "Enter password for $($pp['UserName']):" -AsSecureString} |
96 | | -# fail the install/upgrade if not value is not determined |
97 | | -if (!$pp['Password']) { throw "Package needs Password to install, that must be provided in params or in prompt." } |
98 | | -
|
99 | | -.EXAMPLE |
100 | | -> |
101 | | -# Pass in your own values |
102 | | -Get-PackageParameters -Parameters "/Shortcut /InstallDir:'c:\program files\xyz' /NoStartup" | set r |
103 | | -if ($r.Shortcut) {... } |
104 | | -Write-Host $r.InstallDir |
105 | | -
|
106 | | -.LINK |
107 | | -Install-ChocolateyPackage |
108 | | -
|
109 | | -.LINK |
110 | | -Install-ChocolateyInstallPackage |
111 | | -
|
112 | | -.LINK |
113 | | -Install-ChocolateyZipPackage |
114 | | - */ |
115 | | - |
116 | | - private const string PackageParameterPattern = @"(?:^|\s+)\/(?<ItemKey>[^\:\=\s)]+)(?:(?:\:|=){1}(?:\''|\""){0,1}(?<ItemValue>.*?)(?:\''|\""){0,1}(?:(?=\s+\/)|$))?"; |
117 | | - private static readonly Regex _packageParameterRegex = new Regex(PackageParameterPattern, RegexOptions.Compiled); |
118 | | - |
119 | 34 | [Parameter(Position = 0)] |
120 | 35 | [Alias("Params")] |
121 | 36 | public string Parameters { get; set; } = string.Empty; |
122 | 37 |
|
123 | 38 | protected override void End() |
124 | 39 | { |
125 | | - var paramStrings = new List<string>(); |
126 | | - var logParams = true; |
127 | | - |
128 | | - if (!string.IsNullOrEmpty(Parameters)) |
129 | | - { |
130 | | - paramStrings.Add(Parameters); |
131 | | - } |
132 | | - else |
133 | | - { |
134 | | - WriteDebug("Parsing $env:ChocolateyPackageParameters and $env:ChocolateyPackageParametersSensitive for parameters"); |
135 | | - |
136 | | - var packageParams = EnvironmentVariable(EnvironmentVariables.ChocolateyPackageParameters); |
137 | | - if (!string.IsNullOrEmpty(packageParams)) |
138 | | - { |
139 | | - paramStrings.Add(packageParams); |
140 | | - } |
141 | | - |
142 | | - var sensitiveParams = EnvironmentVariable(EnvironmentVariables.ChocolateyPackageParametersSensitive); |
143 | | - if (!string.IsNullOrEmpty(sensitiveParams)) |
144 | | - { |
145 | | - logParams = false; |
146 | | - WriteDebug("Sensitive parameters detected, no logging of parameters."); |
147 | | - paramStrings.Add(sensitiveParams); |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - var paramHash = new Hashtable(StringComparer.OrdinalIgnoreCase); |
152 | | - |
153 | | - foreach (var param in paramStrings) |
154 | | - { |
155 | | - foreach (Match match in _packageParameterRegex.Matches(param)) |
156 | | - { |
157 | | - var name = match.Groups["ItemKey"].Value.Trim(); |
158 | | - var valueGroup = match.Groups["ItemValue"]; |
159 | | - |
160 | | - object value; |
161 | | - if (valueGroup.Success) |
162 | | - { |
163 | | - value = valueGroup.Value.Trim(); |
164 | | - } |
165 | | - else |
166 | | - { |
167 | | - value = (object)true; |
168 | | - } |
169 | | - |
170 | | - if (logParams) |
171 | | - { |
172 | | - WriteDebug($"Adding package param '{name}'='{value}'"); |
173 | | - } |
174 | | - |
175 | | - paramHash[name] = value; |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - WriteObject(paramHash); |
| 40 | + WriteObject(PackageParameter.GetParameters(this, Parameters)); |
180 | 41 | } |
181 | 42 | } |
182 | 43 | } |
0 commit comments