Skip to content

Commit

Permalink
Merge pull request #14 from NikCharlebois/master
Browse files Browse the repository at this point in the history
1.3.0.0
  • Loading branch information
NikCharlebois authored Oct 22, 2020
2 parents ff73856 + 8b9f4c6 commit 383f56c
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 19 deletions.
6 changes: 3 additions & 3 deletions DSCParser.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft Corporation
#
# Generated on: 08/05/2020
# Generated on: 22/10/2020
#

@{
Expand All @@ -12,7 +12,7 @@
# RootModule = ''

# Version number of this module.
ModuleVersion = '1.1.0.3'
ModuleVersion = '1.3.0.0'

# ID used to uniquely identify this module
GUID = 'e168239a-233d-468d-9025-d6dfc0e4e2b6'
Expand Down Expand Up @@ -69,7 +69,7 @@ NestedModules = @("modules\DSCParser.psm1")
#FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = @("ConvertTo-DSCObject")
CmdletsToExport = @("ConvertTo-DSCObject", "Get-VariableListFromDSC")

# Variables to export from this module
#VariablesToExport = '*'
Expand Down
179 changes: 163 additions & 16 deletions Modules/DSCParser.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@

[Parameter()]
[System.String]
$Content
$Content,

[Parameter()]
[System.Boolean]
$IncludeComments = $false
)

#region Variables
$ParsedResults = @()
#endregion

# Define components we wish to filter out
$noisyTypes = @("NewLine", "StatementSeparator", "Command", "CommandArgument", "CommandParameter", "Comment")
$noisyParts = @("in", "if", "node", "localconfigurationmanager", "for", "foreach", "when", "configuration", "Where", "_")
$noisyTypesDesktop = @("NewLine", "StatementSeparator", "Command", "CommandArgument", "CommandParameter")
$noisyTypesCore = @("NewLine", "StatementSeparator", "CommandArgument", "CommandParameter")
if (-not $IncludeComments)
{
$noisyTypesDesktop += "Comment"
$noisyTypesCore += "Comment"
}
$noisyOperators = (".",",", "")

# Tokenize the file's content to break it down into its various components;
Expand Down Expand Up @@ -184,26 +193,60 @@ function Get-HashtableFromGroup
{$_ -in @("Member")} {
$result.$currentProperty += "." + $component.Content
}
{$_ -in @("Comment")} {
$result.$("_metadata_" + $currentProperty) += $component.Content
}
{$_ -in @("GroupStart")} {
$result.$currentProperty += "@("

do
# Property is an Array
$result.$currentProperty = @()
$currentPropertyIndex++
while ($group[$currentPropertyIndex].Type -eq 'NewLine')
{
if ($group[$currentPropertyIndex].Content -notin @("@(", ")"))
{
$result.$currentProperty += "`"" + $group[$currentPropertyIndex].Content + "`",`""
}
$currentPropertyIndex++
}
while ($group[$currentPropertyIndex].Type -ne 'GroupEnd')

if (($result.$currentProperty).EndsWith(","))
{
$result.$currentProperty = ($result.$CurrentProperty).Substring(0, ($result.$CurrentProperty).Length -1) + ")"
}
else
switch ($group[$currentPropertyIndex].Type)
{
$result.$currentProperty += ')'
# Property is an array of string or integer
{$_ -in @("String", "Number")} {
do
{
if ($group[$currentPropertyIndex].Content -notin $noisyOperators)
{
$result.$currentProperty += $group[$currentPropertyIndex].Content
}
$currentPropertyIndex++
}
while ($group[$currentPropertyIndex].Type -ne 'GroupEnd')
}

# Property is an array of CIMInstance
"Keyword"{
$CimInstanceComponents = @()
$GroupsToClose = 0
$FoundOneGroup = $false
do
{
if ($group[$currentPropertyIndex].Type -eq 'GroupStart')
{
$FoundOneGroup = $true
$GroupsToClose ++
}
elseif ($group[$currentPropertyIndex].Type -eq 'GroupEnd')
{
$GroupsToClose --
}
if ($group[$currentPropertyIndex].Content -notin $noisyOperators)
{
$CimInstanceComponents += $group[$currentPropertyIndex]
}
$currentPropertyIndex++
}
while ($group[$currentPropertyIndex-1].Type -ne 'GroupEnd' -or $GroupsToClose -ne 0 -or -not $FoundOneGroup)
$CimInstanceObject = Convert-CIMInstanceToPSObject -CIMInstance $CimInstanceComponents
$result.$CurrentProperty = $CimInstanceObject
}
}
}
}
Expand Down Expand Up @@ -234,3 +277,107 @@ function Get-HashtableFromGroup
}
return $ParsedResults
}

function Convert-CIMInstanceToPSObject
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Param(
[Parameter(Mandatory = $true)]
[System.Object[]]
$CIMInstance
)

$result = @{}
$index = 0
$CurrentMemberName = $null
while($index -lt $CimInstance.Count)
{
$token = $CIMInstance[$index]
switch ($token.Type)
{
# The main token for the CIMInstance
"Keyword" {
$result.Add("CIMInstance", $token.Content)
}
"Member" {
$result.Add($token.Content, "")
$CurrentMemberName = $token.Content
}
{ $_ -in "String", "Number", "Variable"} {
# Get the name of the Member associated with this value;
$content = $token.Content
if ($content.ToLower() -eq 'true')
{
$content = $true
}
elseif ($content.ToLower() -eq 'false')
{
$content = $false
}
$result.$CurrentMemberName = $content
}
{$_ -eq "GroupStart" -and $token.Content -eq '@('} {
$result.$CurrentMemberName = @()
$index++
while ($CimInstance[$index].Type -eq 'NewLine')
{
$index++
}
switch ($CIMInstance[$index].Type)
{
{ $_ -in "String", "Number", "Variable"} {
$arrayContent = @()
do {
$content = $CIMInstance[$index].Content
if ($content.ToLower() -eq 'true')
{
$content = $true
}
elseif ($content.ToLower() -eq 'false')
{
$content = $false
}
$arrayContent += $content
$index++
} while ($CIMInstance[$index].Type -ne 'GroupEnd')
$result.$CurrentMemberName = $arrayContent
}

# The Content of the Array is yet again CIMInstances. Recursively call into the current method;
"Keyword" {
while ($CIMInstance[$index].Content -ne ')' -and $CImInstance[$index].Type -ne 'GroupEnd')
{
$CIMInstanceComponents = @()
$GroupsToClose = 0
$FoundOneGroup = $false
do
{
if ($CIMInstance[$index].Type -eq 'GroupStart')
{
$FoundOneGroup = $true
$GroupsToClose ++
}
elseif ($CIMInstance[$index].Type -eq 'GroupEnd')
{
$GroupsToClose --
}
if ($CIMInstance[$index].Content -notin $noisyOperators)
{
$CimInstanceComponents += $CIMInstance[$index]
}
$index++
}
while ($CIMInstance[$index-1].Type -ne 'GroupEnd' -or $GroupsToClose -ne 0 -or -not $FoundOneGroup)
$CimInstanceObject = Convert-CIMInstanceToPSObject -CIMInstance $CimInstanceComponents
$result.$CurrentMemberName += $CimInstanceObject
$index++
}
}
}
}
}
$index++
}
return $result
}

0 comments on commit 383f56c

Please sign in to comment.