Skip to content

Commit d899a89

Browse files
committed
tools enhanced qtbuild-qmysql-driver
- upgraded to MySQL 8.2 - added MySQLVersion parameter - added Test-MySQLServerInstalled test function - used our Write-Xyz functions from Common-Host - used Write-ExitError instead of throw [skip ci]
1 parent e680744 commit d899a89

File tree

1 file changed

+71
-43
lines changed

1 file changed

+71
-43
lines changed

tools/qtbuild-qmysql-driver.ps1

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ Param(
1010
'The argument "{0}" does not match the "{1}" pattern.')]
1111
[string] $QtVersion,
1212

13+
[Parameter(Position = 1,
14+
HelpMessage = 'Specifies the MySQL Server version against which to build ' +
15+
'the QMYSQL driver (version number of the MySQL Server installation folder).')]
16+
[ValidateNotNullOrEmpty()]
17+
[ValidatePattern('^[8-12]\.\d{1,2}\$',
18+
ErrorMessage = 'The argument "{0}" is not the correct Qt version number. ' +
19+
'The argument "{0}" does not match the "{1}" pattern.')]
20+
[string] $MySQLVersion = '8.2',
21+
1322
[Parameter(HelpMessage = 'Clean CMake build (delete the $QtVersion build folder).')]
1423
[switch] $CleanBuild
1524
)
@@ -23,7 +32,8 @@ Set-Variable STACK_NAME -Option Constant -Value $MyInvocation.MyCommand.Name
2332
$Script:QtMajorVersion = $null
2433
$Script:QtEnvVersion = $null
2534
$Script:VisualStudioVersion = '17.0'
26-
$Script:MySqlServerPath = 'C:/Program Files/MySQL/MySQL Server 8.1'
35+
$Script:MySqlServerPath = "C:/Program Files/MySQL/MySQL Server $MySQLVersion"
36+
$Script:BOL = ' '
2737

2838
# Functions section
2939
# ---
@@ -34,7 +44,7 @@ function Initialize-QtVersions
3444
{
3545
# Extract major and minor versions from the passed $QtVersion
3646
if (-not ($QtVersion -match '^(?<major>[5-8])\.(?<minor>\d{1,2})\.\d{1,2}$')) {
37-
throw 'Match for the ''$QtEnvVersion'' variable failed.'
47+
Write-ExitError 'Match for the ''$QtEnvVersion'' variable failed.' -NoNewlineBefore
3848
}
3949

4050
$Script:QtMajorVersion = $Matches['major']
@@ -44,38 +54,52 @@ function Initialize-QtVersions
4454
# Check if the Qt version is >5
4555
function Test-QtVersion
4656
{
47-
Write-Host 'Testing if the Qt version is >5' -ForegroundColor DarkYellow
57+
Write-Progress 'Testing if the Qt version is >5'
4858

4959
if ($Script:QtMajorVersion -gt 5) {
5060
return
5161
}
5262

53-
throw "The passed '$QtVersion' is not supported because it doesn't support the CMake build."
63+
Write-ExitError ("The passed Qt version '$QtVersion' is not supported because it doesn't " +
64+
'support the CMake build.')
5465
}
5566

5667
# Check whether the passed QtVersion is installed
5768
function Test-QtVersionInstalled
5869
{
59-
Write-Host "Testing whether Qt $QtVersion is installed" -ForegroundColor DarkYellow
70+
Write-Progress "Testing whether Qt $QtVersion is installed"
6071

6172
if (Test-Path "C:\Qt\$QtVersion") {
6273
return
6374
}
6475

65-
throw "The passed '$QtVersion' version is not installed in the 'C:\Qt\$QtVersion\' folder."
76+
Write-ExitError ("The passed Qt version '$QtVersion' is not installed " +
77+
"in the 'C:\Qt\$QtVersion\' folder.")
6678
}
6779

6880
# Check whether the source files to build the Qt MySQL plugin are installed
6981
function Test-QtSourcesInstalled {
70-
Write-Host "Testing whether Qt $QtVersion source files are installed" `
71-
-ForegroundColor DarkYellow
82+
Write-Progress "Testing whether Qt $QtVersion source files are installed"
7283

7384
if (Test-Path "C:\Qt\$QtVersion\Src\qtbase\src\plugins\sqldrivers") {
7485
return
7586
}
7687

77-
throw "Source files to build the Qt MySQL plugin for the passed '$QtVersion' version are not " +
78-
"installed in the 'C:\Qt\$QtVersion\Src\qtbase\src\plugins\sqldrivers' folder."
88+
Write-ExitError ("Source files to build the Qt MySQL plugin for the passed " +
89+
"Qt version '$QtVersion' are not installed " +
90+
"in the 'C:\Qt\$QtVersion\Src\qtbase\src\plugins\sqldrivers' folder.")
91+
}
92+
93+
# Check whether the MySQL Server is installed
94+
function Test-MySQLServerInstalled
95+
{
96+
Write-Progress "Testing whether MySQL Server is installed"
97+
98+
if (Test-Path $Script:MySqlServerPath) {
99+
return
100+
}
101+
102+
Write-ExitError "The MySQL Server is not installed in the '$Script:MySqlServerPath' folder."
79103
}
80104

81105
# Remove $QtVersion build folder if the $CleanBuild was passed
@@ -85,53 +109,52 @@ function Invoke-CleanBuild
85109
return
86110
}
87111

88-
Write-Host "Removing $QtVersion build folder (Clean build)" -ForegroundColor DarkYellow
112+
Write-Progress "Removing $QtVersion build folder (Clean build)"
89113

90-
Remove-Item -Force -Recurse "$QtVersion"
114+
Remove-Item -Force -Recurse "./$QtVersion" -ErrorAction SilentlyContinue
91115
}
92116

93117
# Create the build folders for debug and release builds
94118
function New-BuildFolders
95119
{
96-
Write-Host 'Creating build folders' -ForegroundColor DarkYellow
120+
Write-Progress 'Creating build folders'
97121

98-
$created = $false
99-
$relWithDebInfoPath = "$QtVersion/msvc2019_64/RelWithDebInfo"
122+
$relWithDebInfoPath = Join-Path -Path $PWD -ChildPath "./$QtVersion/msvc2019_64/RelWithDebInfo"
100123

101124
if (-not (Test-Path $relWithDebInfoPath)) {
102-
mkdir -p $relWithDebInfoPath
103-
$created = $true
125+
New-Item -Type Directory -Path $relWithDebInfoPath | Out-Null
126+
127+
Write-Progress "${Script:BOL}Created the Release folder at: $relWithDebInfoPath"
104128
}
105129
else {
106-
Write-Host " Release folder already exists ($relWithDebInfoPath)" -ForegroundColor DarkRed
130+
Write-Error "${Script:BOL}Release folder already exists ($relWithDebInfoPath)"
107131
}
108132

109-
$debugPath = "$QtVersion/msvc2019_64/Debug"
133+
$debugPath = Join-Path -Path $PWD -ChildPath "$QtVersion/msvc2019_64/Debug"
110134

111135
if (-not (Test-Path $debugPath)) {
112-
mkdir -p $debugPath
113-
$created = $true
136+
New-Item -Type Directory -Path $debugPath | Out-Null
137+
138+
Write-Progress "${Script:BOL}Created the Debug folder at: $debugPath"
114139
}
115140
else {
116-
Write-Host " Debug folder already exists ($debugPath)" -ForegroundColor DarkRed
117-
}
118-
119-
if ($created) {
120-
NewLine
141+
Write-Error "${Script:BOL}Debug folder already exists ($debugPath)"
121142
}
122143
}
123144

124145
# Test whether a Qt environment initialization was successful
125146
function Test-BuildEnvironment
126147
{
127-
Write-Host 'Testing whether the build environment is ready' -ForegroundColor DarkYellow
148+
Newline
149+
Write-Progress 'Testing whether the build environment is ready'
128150

129151
# Test MSVC build environment
130152
if (-not (Test-Path env:VisualStudioVersion) -or `
131153
$env:VisualStudioVersion -ne $Script:VisualStudioVersion
132154
) {
133155
$majorVersion = ($Script:VisualStudioVersion -split '\.', 2)[0]
134-
throw "The Visual Studio '$majorVersion' build environment is not on the system path."
156+
Write-ExitError ("The Visual Studio '$majorVersion' build environment is not " +
157+
'on the system path.')
135158
}
136159

137160
# Test Qt build environment
@@ -141,20 +164,21 @@ function Test-BuildEnvironment
141164
# [System.Version] $qtVersion = (Get-Command qmake -ErrorAction SilentlyContinue).Version
142165

143166
if (-not $qtVersionOnPath -or $qtVersionOnPath -ne $QtVersion) {
144-
throw "The requested Qt version '$QtVersion' is not on the system path."
167+
Write-ExitError ("The requested Qt version '$QtVersion' is not on the system path.")
145168
}
146169

147-
Write-Host 'Build environment is ready 🥳' -ForegroundColor DarkGreen
170+
Write-Info 'Build environment is ready 🥳'
171+
Newline
148172
}
149173

150174
# Initialize the Qt and MSVC2022 build environment if it's not already there
151175
function Initialize-QtEnvironment
152176
{
153-
Write-Host 'Initializing Qt and MSVC2022 build environment' -ForegroundColor DarkYellow
177+
Write-Progress 'Initializing Qt and MSVC2022 build environment'
154178

155179
if (Test-Path env:WindowsSDKLibVersion) {
156-
Write-Host ('The MSVC build environment already initialized. Exiting the Qt environment ' +
157-
'initialization!') -ForegroundColor DarkRed
180+
Write-Error ('The MSVC build environment already initialized. Exiting the Qt environment ' +
181+
'initialization!')
158182

159183
return
160184
}
@@ -176,7 +200,13 @@ Initialize-QtVersions
176200
Test-QtVersion
177201
Test-QtVersionInstalled
178202
Test-QtSourcesInstalled
203+
Test-MySQLServerInstalled
204+
205+
# Initialize the Qt and MSVC2022 build environment if it's not already there
206+
Initialize-QtEnvironment
207+
Test-BuildEnvironment
179208

209+
# Prepare the build folder
180210
Push-Location -StackName $STACK_NAME
181211

182212
Set-Location "E:\c_libs\qt${Script:QtMajorVersion}_sqldrivers"
@@ -186,18 +216,14 @@ Invoke-CleanBuild
186216
# Create the build folders for debug and release builds
187217
New-BuildFolders
188218

189-
# Initialize the Qt and MSVC2022 build environment if it's not already there
190-
Initialize-QtEnvironment
191-
Test-BuildEnvironment
192-
193219
# Debug build
194220
# ---
195221
Write-Header 'Debug Build'
196222

197223
Set-Location "$QtVersion/msvc2019_64/Debug"
198224

199225
# Configure
200-
Write-Host 'Configuring...' -ForegroundColor DarkYellow
226+
Write-Progress 'Configuring...'
201227

202228
qt-cmake `
203229
-S "C:/Qt/$QtVersion/Src/qtbase/src/plugins/sqldrivers" `
@@ -214,7 +240,7 @@ qt-cmake `
214240
NewLine
215241

216242
# Build and install
217-
Write-Host 'Building and installing...' -ForegroundColor DarkYellow
243+
Write-Progress 'Building and installing...'
218244

219245
cmake --build . --target install
220246

@@ -224,7 +250,7 @@ Write-Header 'Release Build'
224250

225251
Set-Location '../RelWithDebInfo'
226252

227-
Write-Host 'Configuring...' -ForegroundColor DarkYellow
253+
Write-Progress 'Configuring...'
228254

229255
qt-cmake `
230256
-S "C:/Qt/$QtVersion/Src/qtbase/src/plugins/sqldrivers" `
@@ -241,13 +267,15 @@ qt-cmake `
241267
NewLine
242268

243269
# Build and install
244-
Write-Host 'Building and installing...' -ForegroundColor DarkYellow
270+
Write-Progress 'Building and installing...'
245271

246272
cmake --build . --target install
247273

248274
# Done
249275
# ---
250276
Pop-Location -StackName $STACK_NAME
251277

252-
Write-Host "The QMYSQL driver for Qt $QtVersion was built and installed successfully. 🥳" `
253-
-ForegroundColor DarkGreen
278+
Write-Progress "Linked against the MySQL Server at: $Script:MySqlServerPath"
279+
280+
Newline
281+
Write-Info "The QMYSQL driver for Qt $QtVersion was built and installed successfully. 🥳"

0 commit comments

Comments
 (0)