xref: /Universal-ctags/Units/parser-powershell.r/simple-powershell.d/input.ps1 (revision b689382a439ee0dac0de08ade43b93c0a24a1ce9)
1 # pseudo #!/PowerShell :)
2 #
3 # test file for the CTags/Geany PowerShell tag parser
4 # based on real world code but simplified for automated tests
5 
6 <#
7 multiline comment including a function and variable, both should be ignored:
8 
9 $IgnoreThisVaribale = "Stop"
10 
IgnoreThisFunction($arg1)11 function IgnoreThisFunction($arg1)  {
12     Write-Host "dummy"
13 }
14 #>
15 
16 # immediately stop the script if an errors occurs
17 $ErrorActionPreference = "Stop"
18 
19 # a global scoped variable
20 $Global:Settings = $null
21 
22 # a local scoped variable
23 $Local:ALocalVar = $null
24 
25 # a usual variable
26 $BasePath = split-path -parent $Global:MyInvocation.InvocationName
27 
28 
29 FUNCTION Read-Configuration-File() {
30     $Hostname = [System.Environment]::MachineName
31     $ConfigurationFileName = $BasePath + "\script-${Hostname}.conf"
32     LogMessage "Read configuration '${ConfigurationFileName}'"
33 
34     $ConfigFileContent = Get-Content -raw $ConfigurationFileName
35     $Global:Settings = Convertfrom-Stringdata $ConfigFileContent
36 }
37 
LogMessageOK()38 Function LogMessageOK()
39 {
40     $x = [Console]::WindowWidth - 6
41     $y = [Console]::CursorTop
42     Try {
43         [Console]::setcursorposition($x, $y)
44     } Catch [system.exception] {
45         # intentionally left empty for redirect of outputs to file
46     }
47 
48     Write-Host -foregroundcolor "green" "[ok]"
49 }
50 
LogMessage()51 function LogMessage() {
52     param(
53         [Parameter(Mandatory=$false)][switch] $NoNewLine,
54         [Parameter(Mandatory=$true)][string] $Message
55     )
56     $Date = Get-Date -UFormat "%Y-%m-%d %T: "
57     Write-Host -foregroundcolor "yellow" -NoNewLine $Date
58 
59     if ($NoNewLine) {
60         Write-Host -foregroundcolor "green" -NoNewLine $Message
61     } else {
62         Write-Host -foregroundcolor "green" $Message
63     }
64 }
65 
global:A-Global-Scope-Function()66 function global:A-Global-Scope-Function() {
67     Write-Host "dummy"
68 }
69 
Script:MyFilter()70 filter Script:MyFilter {
71     filter-something
72 }
73 
Private:MyPrivateFilter()74 Filter Private:MyPrivateFilter {
75     filter-something
76 }
77 
LoadTemplate($template)78 function LoadTemplate($template) {
79     # woah, this is real magic,
80     # see http://stackoverflow.com/questions/10754582/string-interpolation-of-hashtable-values-in-powershell
81 
82     # Set all unbound variables (@args) in the local context
83     while ($args)
84     {
85         ($key, $val, $args) = $args
86         Set-Variable -Name $key.SubString(1, $key.Length-2) -Value $val
87     }
88     $ExecutionContext.InvokeCommand.ExpandString($template)
89 }
90 
TopLevelFunction()91 function TopLevelFunction() {
92     function SecondLevelNestedFunction() {
93         function ThirdLevelNestedFunction() {
94             doSomething()
95         }
96 
97         ThirdLevelNestedFunction
98     }
99 
100     SecondLevelNestedFunction
101 }
102 
Main()103 function Main() {
104     Read-Configuration-File
105     LogMessage $("Working on Environment '{0}'" -f $Settings["EnvironmentName"])
106 
107     LogMessage "do something ..."
108     Stripped-Down-Code
109     LogMessageOK
110 }
111 
112 Main
113