Skip to content

Commit

Permalink
Add support for Hashtable syntax of Import-DscRessource for -SaveDscD…
Browse files Browse the repository at this point in the history
…ependency switch (#1138)
  • Loading branch information
bergmeister authored Mar 5, 2019
1 parent dff9522 commit 0574565
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
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

0 comments on commit 0574565

Please sign in to comment.