Skip to content

Commit

Permalink
added code property
Browse files Browse the repository at this point in the history
hidden, code property contains the inner code of a node
i think it will be easier to work with this for writeback() and setdescription() methods... !

general cleanup
  • Loading branch information
LxLeChat committed Oct 15, 2019
1 parent 3476d40 commit e286dc1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 173 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ Idea: Fetching all ifs,loops etc ... to graph a diagram of an entire script (not
example of i what i want to achieve
![plopy](example.png)

## Just Done
ALl nodes now have a hidden code property, containing their inner "code".. exept for switchnode. (ok for switchcasenode).

# ToDO
-Fix SetDescription() method for elseifnode (maybe others ??). To test: run test.ps1, on a ``elseifnode`` the ``setdescription`` should fail..
Fix SetDescription() method for elseifnode (maybe others ??). To test: run test.ps1, on a ``elseifnode`` the ``setdescription`` should fail..
use the code property to do the setdescription.. !
```powershell
$path = "C:\users\lx\gitperso\PSScriptDiagram\sample.ps1"
$ParsedFile = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$null, [ref]$Null)
Expand Down Expand Up @@ -39,11 +43,8 @@ At line:104 char:9
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
```

-Create ``writeback`` method, to update script file... in ``nodeutility`` class ? this should be recursif...

-Make sure the ``statement`` property alwas reference the parent. Or is it really necessary ?

-If you dont want to put a description, maybe graph nodes text should contain ``statement``

-Start Graph based on description
finish FindDescrption
Create ``writeback`` method, to update script file... in ``nodeutility`` class ? this should be recursif...
Make sure the ``statement`` property alwas reference the parent. Or is it really necessary ?
If you dont want to put a description, maybe graph nodes text should contain ``statement``
Start Graph based on description
119 changes: 36 additions & 83 deletions classes.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


class nodeutility {

[node] static SetNode ([object]$e) {
Expand Down Expand Up @@ -49,12 +48,11 @@ class nodeutility {
class node {
[string]$Type
[string]$Statement
[int]$OffSetStart
[int]$OffSetEnd
[String]$Description
$Children = [System.Collections.Generic.List[node]]::new()
[node]$parent
$file
hidden $code
hidden $NewContent
hidden $raw

Expand All @@ -65,26 +63,12 @@ class node {
## override with parent, for sublevels
[void] FindChildren ([System.Management.Automation.Language.Ast[]]$e,[node]$f) {
foreach ( $d in $e ) {
#write-host "ok..."
If ( $d.GetType() -in [nodeutility]::GetASTitems() ) {
#Write-Host "plop"
$this.Children.add([nodeutility]::SetNode($d,$f))
}
}
}

# normalement on en a plus besoin
<#inutile
[void] FindChildren ([System.Management.Automation.Language.Ast[]]$e) {
foreach ( $d in $e ) {
#write-host "ok..."
If ( $d.GetType() -in [nodeutility]::GetASTitems() ) {
#Write-Host "plop"
$this.Children.add([nodeutility]::SetNode($d,$this))
}
}
}
#>

[void] FindDescription () {
$tokens=@()
Expand All @@ -99,6 +83,7 @@ class node {
}
}

## a revoir, avec comme base $code !
[void] SetDescription ([string]$e) {
$this.Description = $e
$f = (($this.raw.Extent.Text -split '\r?\n')[0]).Length
Expand All @@ -118,8 +103,7 @@ Class IfNode : node {
for( $i=0; $i -lt $e.Clauses.Count ; $i++ ) {
if ( $i -eq 0 ) {
$this.Statement = "If ( {0} )" -f $e.Clauses[$i].Item1.Extent.Text
$this.OffsetStart = $e.Clauses[$i].Item2.extent.StartLineNumber
$this.OffsetEnd = $e.Clauses[$i].Item2.extent.EndLineNumber
$this.Code = $e.Clauses[$i].Item2.Extent.Text
} else {
$this.Children.Add([ElseIfNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2,$this))
}
Expand All @@ -132,8 +116,9 @@ Class IfNode : node {

$this.raw = $e
$this.file = $e.extent.file
$this.FindDescription()

$this.FindChildren($this.raw.Clauses[0].Item2.Statements,$this)
#$this.FindDescription()

}

Expand All @@ -143,8 +128,7 @@ Class IfNode : node {
for( $i=0; $i -lt $e.Clauses.Count ; $i++ ) {
if ( $i -eq 0 ) {
$this.Statement = "If ( {0} )" -f $e.Clauses[$i].Item1.Extent.Text
$this.OffsetStart = $e.Clauses[$i].Item2.extent.StartOffset
$this.OffsetEnd = $e.Clauses[$i].Item2.extent.EndOffset
$this.Code = $e.Clauses[$i].Item2.Extent.Text
} else {
$this.Children.Add([ElseIfNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2,$this))
}
Expand All @@ -158,9 +142,9 @@ Class IfNode : node {
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file
$this.FindChildren($this.raw.Clauses[0].Item2.Statements,$this)

$this.FindDescription()
$this.FindChildren($this.raw.Clauses[0].Item2.Statements,$this)
#$this.FindDescription()

}

Expand All @@ -171,42 +155,28 @@ Class ElseNode : node {

ElseNode ([System.Management.Automation.Language.Ast]$e,[string]$d,[node]$f) {
$this.Statement = "Else From {0}" -f $d
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.file = $e.extent.Text
$this.parent = $f
$this.file = $e.extent.file
$this.FindChildren($this.raw.statements,$this)
$this.code = $e.extent.Text
}
}

Class ElseIfNode : node {
[String]$Type = "ElseIf"
#$f represente l element2 du tuple donc si on veut chercher ce qu il y a en dessous il faut utiliser ça
#inutile
ElseIfNode ([System.Management.Automation.Language.Ast]$e,[string]$d,[System.Management.Automation.Language.Ast]$f) {
$this.Statement = "ElseIf ( {0} ) From {1}" -f $e.Extent.Text,$d
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.file = $e.extent.file

#$ast = $this.raw.Parent.Clauses.where({$_.item1.extent.text -eq $this.raw.extent.text}).item2.Statements

$this.FindChildren($this.raw.Parent.Clauses.where({$_.item1.extent.text -eq $this.raw.extent.text}).item2.Statements,$this)
}

ElseIfNode ([System.Management.Automation.Language.Ast]$e,[string]$d,[System.Management.Automation.Language.Ast]$f,[node]$j) {
$this.Statement = "ElseIf ( {0} ) From {1}" -f $e.Extent.Text,$d
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.parent = $j
$this.file = $e.extent.file

#$ast = $this.raw.Parent.Clauses.where({$_.item1.extent.text -eq $this.raw.extent.text}).item2.Statements
$item1ToSearch = $this.raw.extent.text
$this.Code = ($this.raw.Parent.Clauses.where({$_.Item1.extent.text -eq $item1ToSearch})).Item2.Extent.Text

#$this.FindChildren($this.raw.Parent.Clauses.where({$_.item1.extent.text -eq $this.raw.extent.text}).item2.Statements)
$this.FindChildren($this.raw.Parent.Clauses.where({$_.item1.extent.text -eq $this.raw.extent.text}).item2.Statements,$this)
}

Expand All @@ -217,65 +187,56 @@ Class SwitchNode : node {

SwitchNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "Switch ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.file = $e.extent.file

for( $i=0; $i -lt $e.Clauses.Count ; $i++ ) {
#$this.Children.Add([SwitchCaseNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2))
$this.Children.Add([SwitchCaseNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2,$this))
}

}

SwitchNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "Switch ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file

for( $i=0; $i -lt $e.Clauses.Count ; $i++ ) {
#$this.Children.Add([SwitchCaseNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2))
$this.Children.Add([SwitchCaseNode]::new($e.clauses[$i].Item1,$this.Statement,$e.clauses[$i].Item2,$this))
}

}

## pas réussi a chopper le "code" du switch .. du coup la description ne sra pas settable dans le script
## la description ne sera utilisable que pour le graph
[void]SetDescription([string]$e) {
$this.Description = $e
}
}

Class SwitchCaseNode : node {
[String]$Type = "SwitchCase"

#inutile
SwitchCaseNode ([System.Management.Automation.Language.Ast]$e,[string]$d,[System.Management.Automation.Language.Ast]$f) {
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.file = $e.extent.file
$this.FindChildren($f.statements,$this)
$this.Statement = "Case: {1} for Switch {0}" -f $d,$this.raw.Extent.Text
}

SwitchCaseNode ([System.Management.Automation.Language.Ast]$e,[string]$d,[System.Management.Automation.Language.Ast]$f,[node]$j) {
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.FindChildren($f.statements,$this)
$this.parent = $j
$this.file = $e.extent.file
$this.Statement = "Case: {1} for Switch {0}" -f $d,$this.raw.Extent.Text

$item1ToSearch = $this.raw.Value
$this.Code = ($this.raw.Parent.Clauses.where({$_.Item1.Value -eq $item1ToSearch})).Item2.Extent.Text
}

}

Class ForeachNode : node {
[String]$Type = "Foreach"

ForeachNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "Foreach ( "+ $e.Variable.extent.Text +" in " + $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.Extent.Text
$this.raw = $e
$this.file = $e.extent.file

Expand All @@ -284,9 +245,8 @@ Class ForeachNode : node {

ForeachNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "Foreach ( "+ $e.Variable.extent.Text +" in " + $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.raw = $e
$this.code = $e.body.extent.Text
$this.parent = $f
$this.file = $e.extent.file

Expand All @@ -299,8 +259,7 @@ Class WhileNode : node {

WhileNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "While ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.file = $e.extent.file

Expand All @@ -310,8 +269,7 @@ Class WhileNode : node {

WhileNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "While ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file
Expand All @@ -326,8 +284,7 @@ Class ForNode : node {

ForNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "For ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.file = $e.extent.file

Expand All @@ -336,8 +293,7 @@ Class ForNode : node {

ForNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "For ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file
Expand All @@ -351,23 +307,21 @@ Class DoUntilNode : node {

DoUntilNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "Do Until ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.file = $e.extent.file

$this.FindChildren($this.raw.Body.Statements,$this)
$this.FindChildren($this.raw.Body.Statements,$this)
}

DoUntilNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "Do Until ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file

$this.FindChildren($this.raw.Body.Statements,$this)
$this.FindChildren($this.raw.Body.Statements,$this)
}
}

Expand All @@ -376,8 +330,7 @@ Class DoWhileNode : node {

DoWhileNode ([System.Management.Automation.Language.Ast]$e) {
$this.Statement = "Do While ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.file = $e.extent.file

Expand All @@ -386,13 +339,13 @@ Class DoWhileNode : node {

DoWhileNode ([System.Management.Automation.Language.Ast]$e,[node]$f) {
$this.Statement = "Do While ( "+ $e.Condition.extent.Text + " )"
$this.OffsetStart = $e.extent.StartOffset
$this.OffsetEnd = $e.extent.EndOffset
$this.code = $e.body.extent.Text
$this.raw = $e
$this.parent = $f
$this.file = $e.extent.file


$this.FindChildren($this.raw.Body.Statements,$this)
}
}
}

Loading

0 comments on commit e286dc1

Please sign in to comment.