-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-SQLiteSource.ps1
More file actions
143 lines (130 loc) · 5.61 KB
/
Get-SQLiteSource.ps1
File metadata and controls
143 lines (130 loc) · 5.61 KB
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
#!/usr/bin/env pwsh
# Copyright (c) 2025 Nicholas Bissell (TheFreeman193) MIT License: https://spdx.org/licenses/MIT.html
using namespace System.IO
using namespace System.Management.Automation
[CmdletBinding(DefaultParameterSetName = 'Latest')]
param(
[Parameter(ParameterSetName = 'Latest')]
[switch]$Latest,
[Parameter(Mandatory, ParameterSetName = 'Specific')]
[version]$Version,
[string]$SourceDir = $(Join-Path $PSScriptRoot 'src'),
[string]$ModuleProp = $(Join-Path $PSScriptRoot 'module_template/module.prop')
)
begin {
function Get-SQLiteVersionCode {
param([version]$Ver)
$Ver.Major * 1e6 + $Ver.Minor * 1e4 + [math]::Max(0, $Ver.Build) * 100 + [math]::Max(0, $Ver.Revision) -as [string]
}
$SrcTemplate = 'https://sqlite.org/{0}/sqlite-amalgamation-{1}.zip'
$ChangelogPage = 'https://sqlite.org/chronology.html'
$RLogMatch = 'releaselog/(\d+)_(\d+)_(\d+)(?:_(\d+))?'
$WebParams = @{UserAgent = 'curl/8.13.0' }
if ($PSVersionTable.PSVersion -lt '6.0') { $WebParams['UseBasicParsing'] = $true }
# Defaults
[version]$DefaultVersion = '3.51.2'
$DefaultYear = 2026
$DefaultDownload = $SrcTemplate -f $DefaultYear, (Get-SQLiteVersionCode $DefaultVersion)
}
process {
$SqlPage = Invoke-WebRequest $ChangelogPage @WebParams
if (-not $?) { return }
$LinkNo = -1
$TargetYear = -1
for ($i = 0; $i -lt $SqlPage.Links.Count; $i++) {
$RelLink = $SqlPage.Links[$i]
if ($RelLink.href -match $RLogMatch -and ($PSCmdlet.ParameterSetName -eq 'Latest' -or $RelLink.outerHTML -like "*$Version<*")) {
if ($Matches.Count -gt 4) {
$TargetVersion = [version]::new($Matches[1], $Matches[2], $Matches[3], $Matches[4])
} else {
$TargetVersion = [version]::new($Matches[1], $Matches[2], $Matches[3])
}
$LinkNo = $i
break
}
}
$DownloadTarget = New-TemporaryFile
if ($LinkNo -lt 0) {
$PSCmdlet.WriteWarning("Unable to determine latest SQLite version. Using default: $DefaultVersion.")
$DownloadUrl = $DefaultDownload
Invoke-WebRequest $DownloadUrl -OutFile $DownloadTarget @WebParams
if (-not $?) { return }
} else {
if ($SqlPage.Links[$LinkNo - 1].outerHTML -match '(20\d\d)-\d{2}-\d{2}') {
$TargetYear = $Matches[1]
} else {
$PSCmdlet.WriteWarning("Unable to determine release year of SQLite version $TargetVersion.")
$TargetYear = [datetime]::Now.Year .. 2004
}
$FileVersion = Get-SQLiteVersionCode $TargetVersion
$Success = $false
foreach ($Year in $TargetYear) {
$DownloadUrl = $SrcTemplate -f $Year, $FileVersion
try {
Invoke-WebRequest $DownloadUrl -OutFile $DownloadTarget @WebParams
if ($?) { $Success = $true; break }
} catch {
$PSCmdlet.WriteWarning("Download failed for '$DownloadUrl'. Error: $($_.Exception.Message)")
$Success = $false
}
}
if (-not $Success) {
$Err = [ErrorRecord]::new([FileNotFoundException]::new("Unable to find download page for SQLite version '$TargetVersion'."), 'VersionNotFound', 'ObjectNotFound', $TargetVersion)
$PSCmdlet.WriteError($Err)
return
}
}
if (Test-Path $SourceDir) {
if (Test-Path "$SourceDir.old") {
Remove-Item "$SourceDir.old" -Recurse -Force
}
Rename-Item $SourceDir "$SourceDir.old" -Force
}
if (-not $?) { return }
$Extracted = Expand-Archive $DownloadTarget -DestinationPath $SourceDir -Force -PassThru
if (-not $? -or $Extracted.Count -eq 0) { return }
if (Test-Path $Extracted[0] -PathType Container) {
Move-Item (Join-Path $Extracted[0].FullName '*') $SourceDir -Force
Remove-Item $Extracted[0] -Force -Recurse
if (-not $?) { return }
}
$PropText = (@'
id=sqlite3-android
name=SQLite3 for Android
version={0}
versionCode={1}
author=TheFreeman193
description=Static up-to-date sqlite3 binaries for Android, built directly from sqlite3 source
updateJson=https://raw.githubusercontent.com/TheFreeman193/sqlite3-android-module/main/module-update.json
'@ -f $TargetVersion, $FileVersion).Replace("`r`n", "`n")
$ModuleDir = Split-Path $ModuleProp
if (-not (Test-Path $ModuleDir)) { $null = New-Item -ItemType Directory $ModuleDir }
Set-Content $ModuleProp -Value $PropText -NoNewline
Write-Host -fo Green 'Complete. ' -NoNewline
Write-Host -fo White "Amalgamated source for version '$TargetVersion' downloaded to '$SourceDir'."
}
end {
if (-not [string]::IsNullOrWhiteSpace($DownloadTarget) -and (Test-Path $DownloadTarget)) {
Remove-Item $DownloadTarget -Force
}
}
<#
.SYNOPSIS
Downloads SQLite3 source code
.DESCRIPTION
Downloads the latest (or a specific) version of the SQLite3 amalgamated source code.
.PARAMETER Latest
Gets the latest SQLite version published in the changelog.
.PARAMETER Version
Gets a specific SQLite version as listed in the changelog (https://sqlite.org/chronology.html).
.PARAMETER SourceDir
Specifies where to save the source code. Defaults to ./src relative to the script.
.LINK
https://github.com/TheFreeman193/sqlite3-android-module/blob/main/README.md
.EXAMPLE
./Get-SQLiteSource.ps1 -Latest
Downloads the latest SQLite source code to the ./src subdirectory relative to the script.
.EXAMPLE
./Get-SQLiteSource.ps1 -Version 3.47.0 -SourceDir "$PWD/src"
Downloads the SQLite source code for version 3.47.0 to the ./src subdirectory relative to the current directory.
#>