Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Hashtable syntax of Import-DscRessource for -SaveDscDependency switch #1138

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions Engine/Generic/ModuleDependencyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,38 @@ public static IEnumerable<string> GetModuleNameFromErrorExtent(ParseError error,
var paramValAst = dynamicKywdAst.CommandElements[positionOfModuleNameParamter];

// import-dscresource -ModuleName module1
var paramValStrConstExprAst = paramValAst as StringConstantExpressionAst;
if (paramValStrConstExprAst != null)
if (paramValAst is StringConstantExpressionAst paramValStrConstExprAst)
{
modules.Add(paramValStrConstExprAst.Value);

// import-dscresource -ModuleName module1 -ModuleVersion major.minor.patch.build
var versionParameterAst = dynamicKywdAst.CommandElements[positionOfModuleVersionParameter] as StringConstantExpressionAst;
if (versionParameterAst != null)
{
Version.TryParse(versionParameterAst.Value, out Version version); // ignore return value since a module version of null means no version
moduleVersion = version;
Version.TryParse(versionParameterAst.Value, out moduleVersion); // ignore return value since a module version of null means no version
}
return modules;
}


// Import-DscResource –ModuleName @{ModuleName="module1";ModuleVersion="1.2.3.4"}
//var paramValAstHashtableAst = paramValAst.Find(oneAst => oneAst is HashtableAst, true) as HashtableAst;
if (paramValAst.Find(oneAst => oneAst is HashtableAst, true) is HashtableAst paramValAstHashtableAst)
{
var moduleNameTuple = paramValAstHashtableAst.KeyValuePairs.SingleOrDefault(x => x.Item1.Extent.Text.Equals("ModuleName"));
var moduleName = moduleNameTuple.Item2.Find(astt => astt is StringConstantExpressionAst, true) as StringConstantExpressionAst;
if (moduleName == null)
{
return null;
}
modules.Add(moduleName.Value);
var moduleVersionTuple = paramValAstHashtableAst.KeyValuePairs.SingleOrDefault(x => x.Item1.Extent.Text.Equals("ModuleVersion"));
var moduleVersionAst = moduleVersionTuple.Item2.Find(astt => astt is StringConstantExpressionAst, true) as StringConstantExpressionAst;
Version.TryParse(moduleVersionAst.Value, out moduleVersion);
return modules;
}

// import-dscresource -ModuleName module1,module2
var paramValArrLtrlAst = paramValAst as ArrayLiteralAst;
if (paramValArrLtrlAst != null)
if (paramValAst is ArrayLiteralAst paramValArrLtrlAst)
{
foreach (var elem in paramValArrLtrlAst.Elements)
{
Expand Down
16 changes: 16 additions & 0 deletions Tests/Engine/ModuleDependencyHandler.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ Describe "Resolve DSC Resource Dependency" {
$moduleVersion | Should -Be ([version]'1.2.3.4')
}

It "Extracts 1 module name with version using HashTable syntax" -skip:$skipTest {
$sb = @"
{Configuration SomeConfiguration
{
Import-DscResource -ModuleName (@{ModuleName="SomeDscModule1";ModuleVersion="1.2.3.4"})
}}
"@
$tokens = $null
$parseError = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($sb, [ref]$tokens, [ref]$parseError)
$moduleVersion = $null
$resultModuleNames = $moduleHandlerType::GetModuleNameFromErrorExtent($parseError[0], $ast, [ref]$moduleVersion).ToArray()
$resultModuleNames[0] | Should -Be 'SomeDscModule1'
$moduleVersion | Should -Be ([version]'1.2.3.4')
}

It "Extracts more than 1 module names" -skip:$skipTest {
$sb = @"
{Configuration SomeConfiguration
Expand Down