diff --git a/lib/PowerShell/Microsoft.Management.Infrastructure.dll b/lib/PowerShell/Microsoft.Management.Infrastructure.dll new file mode 100644 index 0000000000..9c72405baf Binary files /dev/null and b/lib/PowerShell/Microsoft.Management.Infrastructure.dll differ diff --git a/lib/PowerShell/System.Management.Automation.dll b/lib/PowerShell/System.Management.Automation.dll index d4be277488..ee40d04542 100644 Binary files a/lib/PowerShell/System.Management.Automation.dll and b/lib/PowerShell/System.Management.Automation.dll differ diff --git a/src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj b/src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj index 232e14eb66..6753249855 100644 --- a/src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj +++ b/src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj @@ -63,6 +63,7 @@ + @@ -70,6 +71,7 @@ + diff --git a/src/Chocolatey.PowerShell/Commands/GetPackageScriptParametersCommand.cs b/src/Chocolatey.PowerShell/Commands/GetPackageScriptParametersCommand.cs new file mode 100644 index 0000000000..38e0973c29 --- /dev/null +++ b/src/Chocolatey.PowerShell/Commands/GetPackageScriptParametersCommand.cs @@ -0,0 +1,81 @@ +// Copyright © 2017 - 2025 Chocolatey Software, Inc +// Copyright © 2011 - 2017 RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Management.Automation; +using System.Management.Automation.Language; +using Chocolatey.PowerShell.Helpers; +using Chocolatey.PowerShell.Shared; + +namespace Chocolatey.PowerShell.Commands +{ + /// + /// Parses a script and returns a hash table of parameters that are present in the package params, along with their values. + /// + /// A path to a script to parse parameters from. + /// A string containing parameters to be parsed. + /// A hashtable of parameters present in and also in (or the envvar). + [Cmdlet(VerbsCommon.Get, "PackageScriptParameters")] + [OutputType(typeof(Hashtable))] + public class GetScriptParametersCommand : ChocolateyCmdlet + { + [Parameter(Mandatory = true, Position = 0)] + public string ScriptPath { get; set; } + + [Parameter(Mandatory = false, Position = 1)] + public string Parameters { get; set; } = string.Empty; + + protected override void End() + { + var packageParameters = PackageParameter.GetParameters(this, Parameters); + WriteObject(GetPackageParameterHashtable(ScriptPath, packageParameters)); + } + + private Hashtable GetPackageParameterHashtable(string scriptPath, Hashtable packageParameters) + { + + var splatHash = new Hashtable(StringComparer.OrdinalIgnoreCase); + + // Check what parameters the script has + var scriptParameters = GetScriptParameters(scriptPath); + + // For each of those in PackageParameters, add it to the splat + foreach (var parameter in scriptParameters) + { + if (packageParameters.ContainsKey(parameter)) + { + splatHash.Add(parameter, packageParameters[parameter]); + } + } + + return splatHash; + } + + private List GetScriptParameters(string scriptPath) + { + Token[] tokensRef = null; + ParseError[] errorsRef = null; + var parsedAst = Parser.ParseFile(scriptPath, out tokensRef, out errorsRef); + var scriptParameters = parsedAst.ParamBlock != null ? parsedAst.ParamBlock.Parameters.Select(p => p.Name.VariablePath.UserPath.ToString()).ToList() : new List(); + WriteVerbose($"Found {scriptParameters.Count()} parameter(s) in '{scriptPath}'"); + + return scriptParameters; + } + } +} \ No newline at end of file diff --git a/src/Chocolatey.PowerShell/Helpers/PackageParameter.cs b/src/Chocolatey.PowerShell/Helpers/PackageParameter.cs new file mode 100644 index 0000000000..dd722f0f47 --- /dev/null +++ b/src/Chocolatey.PowerShell/Helpers/PackageParameter.cs @@ -0,0 +1,94 @@ +// Copyright © 2017 - 2025 Chocolatey Software, Inc +// Copyright © 2011 - 2017 RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Management.Automation; +using System.Text.RegularExpressions; + +namespace Chocolatey.PowerShell.Helpers +{ + public static class PackageParameter + { + private const string PackageParameterPattern = @"(?:^|\s+)\/(?[^\:\=\s)]+)(?:(?:\:|=){1}(?:\'|\""){0,1}(?.*?)(?:\'|\""){0,1}(?:(?=\s+\/)|$))?"; + private static readonly Regex _packageParameterRegex = new Regex(PackageParameterPattern, RegexOptions.Compiled); + + public static Hashtable GetParameters(PSCmdlet cmdlet, string parameters) + { + var paramHash = new Hashtable(StringComparer.OrdinalIgnoreCase); + + if (!string.IsNullOrEmpty(parameters)) + { + paramHash = AddParameters(cmdlet, parameters, paramHash); + } + else + { + var packageParameters = EnvironmentHelper.GetVariable( + cmdlet, + "ChocolateyPackageParameters", + EnvironmentVariableTarget.Process); + if (!string.IsNullOrEmpty(packageParameters)) + { + paramHash = AddParameters(cmdlet, packageParameters, paramHash); + } + + var sensitivePackageParameters = EnvironmentHelper.GetVariable( + cmdlet, + "ChocolateyPackageParametersSensitive", + EnvironmentVariableTarget.Process); + if (!string.IsNullOrEmpty(sensitivePackageParameters)) + { + paramHash = AddParameters(cmdlet, sensitivePackageParameters, paramHash, logParams: false); + } + } + + return paramHash; + } + + private static Hashtable AddParameters(PSCmdlet cmdlet, string paramString, Hashtable paramHash, bool logParams = true) + {; + foreach (Match match in _packageParameterRegex.Matches(paramString)) + { + var name = match.Groups["ItemKey"].Value.Trim(); + var valueGroup = match.Groups["ItemValue"]; + + object value; + if (valueGroup.Success) + { + value = valueGroup.Value.Trim(); + } + else + { + value = (object)true; + } + + if (logParams) + { + cmdlet.WriteDebug($"Adding package param '{name}'='{value}'"); + } + else + { + cmdlet.WriteDebug($"Adding package param '{name}' (value not logged)"); + } + + paramHash[name] = value; + } + + return paramHash; + } + } +} \ No newline at end of file diff --git a/src/chocolatey.resources/chocolatey.resources.csproj b/src/chocolatey.resources/chocolatey.resources.csproj index 7fe3b535a8..a44abea16c 100644 --- a/src/chocolatey.resources/chocolatey.resources.csproj +++ b/src/chocolatey.resources/chocolatey.resources.csproj @@ -260,11 +260,11 @@ PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + +