forked from dmitry-merzlyakov/nledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-nledger-up.ps1
239 lines (190 loc) · 12.2 KB
/
get-nledger-up.ps1
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<#
.SYNOPSIS
Build, verify and optionally install NLedger from source code.
.DESCRIPTION
This script helps to get workable NLedger from source code
on their computer. It performs four basic steps: a) builds binaries;
b) runs unit tests; c) runs integration tests; d) optionally, installs created binaries
(adds to PATH and creates 'ledger' hard link). In case of any errors on any steps,
the script provides troubleshooting information to help people solve environmental
issues. Switches can regulate this process. The script works on any platform (Windows, Linux, OSX).
.PARAMETER coreOnly
Orders to create .Net Core binaries only. By default, the script creates both Framework
and Core binaries. This switch is selected automatically on non-Windows platforms.
.PARAMETER debugMode
Orders to create binaries in Debug mode (Release by default)
.PARAMETER noUnitTests
Omits xUnit tests
.PARAMETER noNLTests
Omits NLTest integration tests
.PARAMETER noPython
Disables Python unit and integration tests; Python module build is skipped
.PARAMETER install
If this switch is set, the script installs NLedger when binaries are built and verified.
.EXAMPLE
PS> ./get-nledger-up.ps1
Build, verify and install NLedger from source code.
.EXAMPLE
PS> ./get-nledger-up.ps1 -Verbose
Show detail diagnostic information to troubleshoot build issues.
.EXAMPLE
PS> ./get-nledger-up.ps1 -coreOnly
Create .Net Core binaries only.
Note: this switch is set automatically on non-windows platforms.
.NOTES
Author: Dmitry Merzlyakov
Date: October 19, 2021
===
Run the script on Windows: >powershell -ExecutionPolicy RemoteSigned -File ./get-nledger-up.ps1
Run the script on other OS: >powershell -File ./get-nledger-up.ps1
#>
[CmdletBinding()]
Param(
[Switch][bool]$coreOnly = $False,
[Switch][bool]$debugMode = $False,
[Switch][bool]$noUnitTests = $False,
[Switch][bool]$noNLTests = $False,
[Switch][bool]$noPython = $False,
[Switch][bool]$install = $False
)
trap
{
write-error $_
exit 1
}
[string]$Script:ScriptPath = Split-Path $MyInvocation.MyCommand.Path
[Version]$minDotnetVersion = "3.1"
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Initialization"
# Check environmental information
[bool]$isWindowsPlatform = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
[bool]$isOsxPlatform = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)
Write-Verbose "Detected: is windows platform=$isWindowsPlatform; is OSX platform: $isOsxPlatform"
[bool]$isDotNetInstalled = -not(-not($(get-command dotnet -ErrorAction SilentlyContinue)))
if (!$isDotNetInstalled) { throw "DotNet Core is not installed (command 'dotnet' is not available)" }
[Version]$dotnetVersion = $(dotnet --version)
Write-Verbose "Detected: dotnet version=$dotnetVersion"
if ($dotnetVersion -lt $minDotnetVersion) { throw "Detected dotnet version is $dotnetVersion but minimal required is $minDotnetVersion" }
# Validate switchers
if (!$isWindowsPlatform -and !$coreOnly) {
$coreOnly = $true
Write-Verbose "Since it is not windows platform, switch 'coreOnly' is changed to 'True'."
}
# Check codebase structure
[string]$solutionPath = [System.IO.Path]::GetFullPath("$Script:ScriptPath/Source/NLedger.sln")
Write-Verbose "Expected solution path: $solutionPath"
if (!(Test-Path -LiteralPath $solutionPath -PathType Leaf)) { "File '$solutionPath' does not exist. Check that source code base is in valid state." }
[string]$nlTestPath = [System.IO.Path]::GetFullPath("$Script:ScriptPath/Contrib/NLTestToolkit/NLTest.ps1")
Write-Verbose "Expected NLTest path: $nlTestPath"
if (!(Test-Path -LiteralPath $nlTestPath -PathType Leaf)) { "File '$nlTestPath' does not exist. Check that source code base is in valid state." }
# Check Python connection availability
[string]$powershell = $(if($Script:isWindowsPlatform){"powershell"}else{"pwsh"})
if (!$noPython) {
Write-Verbose "Checking NLedger Python extension settings..."
[string]$testConnectionOutput = & $powershell -File $("$Script:ScriptPath/Contrib/Python/GetPythonEnvironment.ps1") -command "test-connection"
Write-Verbose "GetPythonEnvironment's test-connection returned: $testConnectionOutput"
$pythonConnectionStatus = $(if($testConnectionOutput){[System.Management.Automation.PSSerializer]::Deserialize($testConnectionOutput)}else{$null})
if (!($pythonConnectionStatus.IsConnectionValid)){
Write-Warning "NLedger Python Extension settings are not configured or not valid. This does not prevent the build from completing, but unit and integration tests that require Python will be skipped. Building Ledger module will be skipped. Use './get-nledger-tools.ps1 -pythonTools' command to configure settings or disable this warning by adding a switch './get-nledger-up.ps1 -noPython'."
$noPython = $True
}
}
if ($noPython){$env:NLedgerPythonConnectionStatus = "Disabled"}
# First step: build sources
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Building source code [$(if($coreOnly){".Net Core"}else{".Net Framework,.Net Core"})] [$(if($debugMode){"Debug"}else{"Release"})]"
[string]$buildCommandLine = "dotnet build '$solutionPath'"
if ($coreOnly) { $buildCommandLine += " /p:CoreOnly=True"}
if ($debugMode) { $buildCommandLine += " --configuration Debug"} else { $buildCommandLine += " --configuration Release" }
if ($isOsxPlatform) { $buildCommandLine += " -r osx-x64"}
Write-Verbose "Build sources command line: $buildCommandLine"
$null = (Invoke-Expression $buildCommandLine | Write-Verbose)
if ($LASTEXITCODE -ne 0) { throw "Build failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
# Second step: run unit tests
if(!$noUnitTests) {
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Running unit tests [$(if($coreOnly){".Net Core"}else{".Net Framework,.Net Core"})] [$(if($debugMode){"Debug"}else{"Release"})]"
[string]$unittestCommandLine = "dotnet test '$solutionPath'"
if ($coreOnly) { $unittestCommandLine += " /p:CoreOnly=True"}
if ($debugMode) { $unittestCommandLine += " --configuration Debug"} else { $buildCommandLine += " --configuration Release" }
if ($isOsxPlatform) { $buildCommandLine += " -r osx-x64"}
Write-Verbose "Run unit tests command line: $unittestCommandLine"
$null = (Invoke-Expression $unittestCommandLine | Write-Verbose)
if ($LASTEXITCODE -ne 0) { throw "Unit tests failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
}
# Third step: run integration tests
function composeNLedgerExePath {
Param([Parameter(Mandatory=$True)][bool]$core)
[string]$private:config = $(if($debugMode){"Debug"}else{"Release"})
[string]$private:framework = $(if($core){"netcoreapp3.1"}else{"net472"})
[string]$private:osxBin = $(if($isOsxPlatform){"/osx-x64"})
[string]$private:extension = $(if($isWindowsPlatform){".exe"}else{""})
return [System.IO.Path]::GetFullPath("$Script:ScriptPath/Source/NLedger.CLI/bin/$private:config/$private:framework$($private:osxBin)/NLedger-cli$private:extension")
}
if (!$noNLTests)
{
[string]$ignoreCategories = $(if($noPython){"python"}else{""})
if (!$coreOnly){
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Running integration tests [.Net Framework] [$(if($debugMode){"Debug"}else{"Release"})]"
[string]$nledgerFrameworkExeFile = composeNLedgerExePath -core $false
if (!(Test-Path -LiteralPath $nledgerFrameworkExeFile -PathType Leaf)) { throw "Cannot find $nledgerFrameworkExeFile" }
Import-Module "$Script:ScriptPath/Contrib/Install/SysNGen.psm1"
if (Test-CanCallNGen) {$null = Add-NGenImage $nledgerFrameworkExeFile}
$null = (& $nlTestPath -nledgerExePath $nledgerFrameworkExeFile -noconsole -showProgress -ignoreCategories $ignoreCategories | Write-Verbose)
if ($LASTEXITCODE -ne 0) { throw "Integration tests failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
}
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Running integration tests [.Net Core] [$(if($debugMode){"Debug"}else{"Release"})]"
[string]$nledgerCoreExeFile = composeNLedgerExePath -core $true
if (!(Test-Path -LiteralPath $nledgerCoreExeFile -PathType Leaf)) { throw "Cannot find $nledgerCoreExeFile" }
$null = (& $nlTestPath -nledgerExePath $nledgerCoreExeFile -noconsole -showProgress -ignoreCategories $ignoreCategories | Write-Verbose)
if ($LASTEXITCODE -ne 0) { throw "Integration tests failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
}
# Fourth step: build Python module
if (!$noPython){
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Building Python Ledger module"
if(!($pythonConnectionStatus.IsWheelInstalled)){Write-Warning "Wheel is not installed on the connected Python environment. Ledger module will not be built. You can install wheel module by means of './Contrib/Python/GetPythonEnvironment.ps1 -command uninstall-wheel'."}
else {
if(!($pythonConnectionStatus.IsPythonNetInstalled)){Write-Warning "PythonNet is not installed on the connected Python environment. Ledger module will be built but not tested. It is not a critical problem since module code is tested by Ledger unit tests."}
[string]$pyBuildPath = [System.IO.Path]::GetFullPath("$Script:ScriptPath/Source/NLedger.Extensibility.Python.Module/build.ps1")
Write-Verbose "Expected Python Ledger module build script path: $pyBuildPath"
if (!(Test-Path -LiteralPath $nlTestPath -PathType Leaf)) { "File '$pyBuildPath' does not exist. Check that source code base is in valid state." }
$( $(& "$pyBuildPath" -build -test:$pythonConnectionStatus.IsPythonNetInstalled) 2>&1 | Out-String ) | Write-Verbose
if ($LASTEXITCODE -ne 0) { throw "Python module build failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
$pythonModuleBuilt = $True
}
}
# Fifth step: install built binaries
if ($install) {
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Installing NLeger binaries"
$null = (& $powershell -File $("$Script:ScriptPath/Contrib/Install/NLedger.Install.ps1") -install | Write-Verbose)
if ($LASTEXITCODE -ne 0) { throw "Installation failed for some reason. Run this script again with '-Verbose' to get more information about the cause." }
}
Write-Progress -Activity "Building, testing and installing NLedger" -Status "Completed" -Completed
# Print summary
Write-Host "*** NLedger Build succeeded ***"
Write-Host
Write-Host "Build source code: OK [$(if($coreOnly){".Net Core"}else{".Net Framework,.Net Core"})] [$(if($debugMode){"Debug"}else{"Release"})]"
if (!($coreOnly)) {Write-Host " .Net Framework binary: $(composeNLedgerExePath -core $false)"}
Write-Host " .Net Core binary: $(composeNLedgerExePath -core $true)"
Write-Host
if (!($noUnitTests)) {
Write-Host "Unit tests: OK [$(if($coreOnly){".Net Core"}else{".Net Framework,.Net Core"})] [$(if($debugMode){"Debug"}else{"Release"})]"
} else {
Write-Host "Unit tests: IGNORED"
}
Write-Host
if (!($noNLTests)) {
Write-Host "NLedger tests: OK [$(if($coreOnly){".Net Core"}else{".Net Framework,.Net Core"})] [$(if($debugMode){"Debug"}else{"Release"})]"
} else {
Write-Host "NLedger tests: IGNORED"
}
Write-Host
if ($pythonModuleBuilt) {
Write-Host "Python module: BUILT (see /Contrib/Python)"
} else {
Write-Host "Python module: IGNORED"
}
Write-Host
if ($install) {
Write-Host "NLedger install: OK (Note: run a new console session to get effect of changes in environment variables)"
} else {
Write-Host "NLedger is ready to be installed (use ./get-nledger-tools -install)"
}
Write-Host