-
Notifications
You must be signed in to change notification settings - Fork 942
Expand file tree
/
Copy pathGetPackageScriptParametersCommand.cs
More file actions
81 lines (70 loc) · 3.26 KB
/
GetPackageScriptParametersCommand.cs
File metadata and controls
81 lines (70 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
{
/// <summary>
/// Parses a script and returns a hash table of parameters that are present in the package params, along with their values.
/// </summary>
/// <param name="ScriptPath">A path to a script to parse parameters from.</param>
/// <param name="Parameters">A string containing parameters to be parsed.</param>
/// <returns>A hashtable of parameters present in <paramref name="ScriptPath"> and also in <paramref name="Parameters"> (or the envvar).</returns>
[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<string> 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<string>();
WriteVerbose($"Found {scriptParameters.Count()} parameter(s) in '{scriptPath}'");
return scriptParameters;
}
}
}