Debian Codegraph @

builds/install/arch-specific/win32/test_installer/fbit-functions.psm1

raw
1 #region Helper functions #############
2
3 function spacer() {
4 param ( [string]$message )
5 if ( $message ) {
6 Write-Output ''
7 Write-Output "=================== $message ==================="
8 Write-Output ''
9 } else {
10 Write-Output '------------------------------------------------------------------------------------------'
11 }
12 }
13
14
15 function Pause( [string] $_msg = "Press any key to continue..." ) {
16 Set-PSDebug -Trace 2
17
18 $null = Read-Host $_msg
19
20 }
21
22
23 function RunTimeStamp() {
24 Get-Date -UFormat '%Y-%m-%d_%H-%M-%S'
25 }
26
27
28 <#
29 .SYNOPSIS
30 Shorten the prompt
31
32 .DESCRIPTION
33 The path to the test installer script can get too long
34
35 .PARAMETER reset
36 Restore the full path
37
38 #>
39 function prompt( [switch]$reset ) {
40
41
42 $local:identity = [Security.Principal.WindowsIdentity]::GetCurrent()
43 $local:principal = [Security.Principal.WindowsPrincipal] $local:identity
44 $local:adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
45
46 $( if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
47 elseif ($principal.IsInRole($adminRole)) { '[ADMIN]: ' }
48 else { '' }
49 ) + $( if ( $reset -eq $true ) {
50 'PS ' + $(Get-Location) + $( if ( $NestedPromptLevel -ge 1 ) { '>>' } ) + '> '
51 } else {
52 'FBIT ' + '> '
53 } )
54
55 }
56
57
58 function check_file_exists( [string]$_apath, [switch]$_isdir ) {
59 if ( $_isdir ) {
60 return ( (Test-Path -Path $_apath ) -ne "" )
61 } else {
62 return ( ( Test-Path -Path $_apath -PathType Leaf ) -ne "" )
63 }
64 }
65
66
67 function Invoke-Command ($_commandPath, $_commandArguments, $_commandTitle, $_outfile ) {
68 Try {
69 $local:pinfo = New-Object System.Diagnostics.ProcessStartInfo
70 $local:pinfo.FileName = "$_commandPath"
71 $local:pinfo.RedirectStandardError = $true
72 $local:pinfo.RedirectStandardOutput = $true
73 $local:pinfo.UseShellExecute = $false
74 $local:pinfo.Arguments = "$_commandArguments"
75 $global:p = New-Object System.Diagnostics.Process
76 $global:p.StartInfo = $pinfo
77 $global:p.Start() | Out-Null
78 $local:result_object = [pscustomobject]@{
79 commandTitle = $_commandTitle
80 stdout = $p.StandardOutput.ReadToEnd()
81 stderr = $p.StandardError.ReadToEnd()
82 ExitCode = $p.ExitCode
83 }
84 $global:p.WaitForExit()
85 if ( "$_outfile" -ne '' ) {
86 $local:result_object.stdout > $_outfile
87 } else {
88 Write-Verbose $local:result_object.stdout
89 }
90 return $local:result_object.ExitCode
91 } catch {
92 Write-Output $local:result_object.stderr
93 return $local:result_object.ExitCode
94
95 }
96
97 # Write-Verbose "stdout: $stdout"
98 # Write-Verbose "stderr: $stderr"
99 # Write-Verbose "exit code: $p.ExitCode"
100
101 }
102
103
104 #endregion end of helper functions #############
105
106
107 <#
108 .SYNOPSIS
109 Indicate if the (non-)existence of a file is a good or a bad thing.
110 .DESCRIPTION
111 When installing Firebird we expect certain files to exist.
112 When uninstalling we do not expect files to exist.
113
114 .PARAMETER afile
115 The file to check for
116
117 .PARAMETER str_if_true
118 The string to output if file exists. Defaults to 'good'
119
120 .PARAMETER str_if_false
121 The string to output if the file does not exist
122
123 .PARAMETER status_true_is_fail
124 When installing set status_true_is_fail to false.
125 When uninstalling set status_true_is_fail to true.
126
127 .PARAMETER isdir
128 Set isdir if testing for a directory.
129
130 .EXAMPLE
131 An example
132
133 .NOTES
134 General notes
135 #>
136 function check_file_status( $afile, [boolean]$status_true_is_fail, [boolean]$isdir
137 , [string]$str_if_true = 'good', [string]$str_if_false = 'bad'
138 ) {
139 Write-Debug "Entering function $($MyInvocation.MyCommand.Name)"
140 $local:retval = check_file_exists $afile $isdir
141 if ( $local:retval -eq $true ) {
142 Write-Output "$TAB $afile exists - $str_if_true"
143 } else {
144 Write-Output "$TAB $afile not found - $str_if_false"
145 }
146
147 if ( $status_true_is_fail -eq $true -and $local:retval -eq $true ) {
148 Write-Verbose "$TAB $status_true_is_fail -eq $true -and $local:retval -eq $true "
149 $ErrorCount += 1
150 }
151
152 if ( $status_true_is_fail -eq $false -and $local:retval -eq $false ) {
153 Write-Verbose "$TAB $status_true_is_fail -eq $true -and $local:retval -eq $true "
154 $ErrorCount += 1
155 }
156 Write-Debug "Leaving function $($MyInvocation.MyCommand.Name)"
157 }
158
159
160 function check_server_arch_configured() {
161 Write-Debug "Entering function $($MyInvocation.MyCommand.Name)"
162
163 if ( $global:classicserver ) { $local:str_to_test = 'servermode = classic' }
164 if ( $global:superclassic ) { $local:str_to_test = 'servermode = superclassic' }
165 if ( $global:superserver ) { $local:str_to_test = 'servermode = super' }
166
167 # FIXME What if the fb.conf does not exist?
168 $local:found = (Select-String -Path "$FIREBIRD/firebird.conf" -Pattern ^$local:str_to_test)
169
170 if ( ! $local:found.Length -gt 0 ) {
171 $ErrorCount += 1
172 Write-Verbose $TAB $local:str_to_test not set in $FIREBIRD/firebird.conf
173
174 }
175
176 Write-Debug "Leaving function $($MyInvocation.MyCommand.Name)"
177 }
178
179
180
181 <#
182 .SYNOPSIS
183 Compare two strings
184
185 .DESCRIPTION
186 Long description
187
188 .PARAMETER expected
189 The result expected
190
191 .PARAMETER actual
192 The actual result
193
194 .PARAMETER equals_is_true
195 Set True if the actual result should equal the expected result
196 Or Set True if actualt result should not equal the expected result
197
198 .NOTES
199 General notes
200 #>
201 function check_result ( [string]$expected, [string]$actual, [boolean]$equals_is_true) {
202
203 # Write-Verbose "if ( ($expected -eq $actual ) -and $equals_is_true ){ return $true }"
204 if ( ( "$expected" -eq "$actual" ) -and $equals_is_true ) { return $true }
205
206 # Write-Verbose "if ( ($expected -eq $actual ) -and !$equals_is_true ){ return $false }"
207 if ( ( "$expected" -eq "$actual" ) -and ! $equals_is_true ) { return $false }
208
209 # Write-Verbose "if ( ($expected -ne $actual ) -and $equals_is_true ){ return $false }"
210 if ( ( "$expected" -ne "$actual" ) -and $equals_is_true ) { return $false }
211
212 # Write-Verbose "if ( ($expected -ne $actual ) -and !$equals_is_true ){ return $true }"
213 if ( ( "$expected" -ne "$actual" ) -and ! $equals_is_true ) { return $true }
214 }
215
216 function check_file_output( $afile, $apattern ) {
217 $local:aretval = Select-String -Path $afile -Pattern $apattern -SimpleMatch -Quiet
218 return $local:aretval
219
220 }
221
222 function print_output( $astring, [boolean]$found, [boolean]$found_is_fail
223 , [string]$str_if_true = "GOOD", [string]$str_if_false = "BAD" ) {
224
225 # If we find the string (ie result is not empty)
226 if ( $found ) {
227 if ( $found_is_fail ) {
228 Write-Host -ForegroundColor Red "${TAB}$astring - $str_if_false"
229 $global:ErrorCount += 1
230 } else {
231 Write-Host -ForegroundColor Green "${TAB}$astring - $str_if_true"
232 }
233 # We did not find the string
234 } else {
235 if ( $found_is_fail ) {
236 Write-Host -ForegroundColor Green "${TAB}$astring - $str_if_true"
237 } else {
238 Write-Host -ForegroundColor Red "${TAB}$astring - $str_if_false"
239 $global:ErrorCount += 1
240 }
241 }
242
243 }
244
245
246
247 <#
248 .SYNOPSIS
249 Execute SQL via isql.exe
250
251 .NOTES
252 This function assumes that the script to execute exists in $env:Temp\infile.txt
253 and the output will be stored in $env:Temp\outfile.txt
254 #>
255 function Exec_SQL( [string]$db = "localhost:employee",
256 [string]$username = "sysdba", [string]$pw = "masterkey" ) {
257
258 # Always reset outfile otherwise output is appended.
259 Write-Output "" > $env:Temp\outfile.txt
260
261 $local:retval = Invoke-Command "$global:FIREBIRD\isql.exe" " -user $username -password $pw -z `
262 -i $env:Temp/infile.txt -o $env:Temp/outfile.txt -m -m2 $db"
263
264 }