simplex-dsl.psm1

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

$script:currentParentNode = new-object System.Collections.Stack;

function root {
    param(
        [parameter(position=0, mandatory=$true)]
        [scriptblock]
        # a scriptblock that outputs child folders and script nodes
        $script
    )

    $root = new-object codeowls.ScriptProvider.nodes.rootfolder -arg 'root';
    $script:currentParentNode.Push($root);
    $items = [CodeOwls.ScriptProvider.nodes.iitem[]]@(& $script | ? {$_ -is [codeowls.ScriptProvider.nodes.iitem] });

    $root.children.addrange($items);
    $script:currentParentNode.Pop() | out-null;

    return $root;
<#
   .SYNOPSIS
    Defines the root node of the Simplex-based provider.
   .DESCRIPTION
   Defines the root node of the Simplex-based provider.
 
   The root node can contain folder nodes and script nodes.
 
   .EXAMPLE
    root {
      folder "myFolder" {
        script "myScript" {
          get-eventlog -LogName Application -Newest 20 -EntryType error;
        }
      }
    }
#>

}

function script {

    param(
        [parameter(position=0, mandatory=$true)]
        [string]
        # the name of the container
        $name,

        [parameter()]
        [string]
        # the name of the object property to use as the item name for child objects
        $idField,

        [parameter(mandatory=$true, position = 2)]
        [scriptblock]
        # a scriptblock that outputs child objects
        $script
    )

    $menu = new-object 'system.collections.generic.dictionary[string,ScriptBlock]'
    for( $c = 0; $c -lt $contextMenuItems.length; $c += 2 )
    {
        $key = $contextMenuItems[$c].TrimStart('-');
        $sb = $contextMenuItems[1+$c];

        if( $sb -isnot [scriptblock] )
        {
            write-error "context menu item '$key' specifies an invalid value '$sb' of type '$($sb.getType().fullName)'; only scriptblocks may be specified for context menu items";
            return;
        }

        $menu[$key] = [scriptblock]$sb;
    }

    if( $icon -and -not $icon.EndsWith('.ico') -and -not $icon.StartsWith('.') -and -not $icon.Contains(',') ) { $icon = ".$icon" }
    if( $itemicon -and -not $itemicon.StartsWith('.') ) { $itemicon = ".$itemicon" }
    $s = new-object codeowls.ScriptProvider.nodes.scriptfolder -arg $name,$script,$idField;

    return $s;
<#
   .SYNOPSIS
   Defines a script-based container node in the simplex provider.
   .DESCRIPTION
   Defines a script-based container node in the simplex provider. Script nodes
   are evaluated on-demand as necessary by the Simplex provider.
 
   The objects output by the script become child objects of this node. Script
   nodes can contain child Folder and Script nodes. This means you can
   generate dynamic child node hierarchies.
 
   If an object returned by the script is another provider item, the relevant
   item properties from the original provider are proxied to the simplex
   provider.
 
   .EXAMPLE
    root {
      folder "myFolder" {
        script "Errors" {
          get-eventlog -LogName Application -Newest 20 -EntryType error;
        }
        script "MyDocuments" {
          dir $home\documents;
        }
      }
    }
#>

}

function folder {

    param(
        [parameter(position=0, mandatory=$true)]
        [string]
        # the name of the container
        $name,

        [parameter(position=1, mandatory=$true)]
        [scriptblock]
        # a scriptblock that outputs child folders and script nodes
        $script
    )

    $folder = new-object codeowls.ScriptProvider.nodes.folder -arg $name;
    $script:currentParentNode.Push($folder);
    $items=[codeowls.ScriptProvider.nodes.iitem[]]@(& $script);
    $folder.Children.AddRange( $items );
    $script:currentParentNode.Pop() | out-null;

    return $folder;
<#
   .SYNOPSIS
   Defines a named container node in the simplex provider.
   .DESCRIPTION
   Defines a named container node in the simplex provider. Folder nodes
   are evaluated once in the context of the Simplex DSL script load.
 
   The folder node can contain other folder nodes and script nodes.
 
   .EXAMPLE
    root {
      folder "myFolder" {
        script "myScript" {
          get-eventlog -LogName Application -Newest 20 -EntryType error;
        }
      }
    }
#>

}