-
Notifications
You must be signed in to change notification settings - Fork 45
/
install.ps1
129 lines (101 loc) · 4.28 KB
/
install.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
# remove the space in the following to require running this script as administrator
# Requires -RunAsAdministrator
# terminate on uncaught exceptions
$ErrorActionPreference = "Stop"
# Configure supported HTTPS protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls
$libVersion = '0.21.0'
$libVariant = 'objectbox' # or 'objectbox-sync'
$downloadDir = 'download'
$extractedLibDir = "$downloadDir\objectbox-$libVersion"
function DownloadLibrary {
# Windows hashes and URL sourced from objectbox-c download.sh
$machine = if ([Environment]::Is64BitOperatingSystem) { 'x64' } else { 'x86' }
$url = "https://github.com/objectbox/objectbox-c/releases/download/v$libVersion/$libVariant-windows-$machine.zip"
$archiveFile = "$downloadDir\objectbox-$libVersion.zip"
if (!(Test-Path "$downloadDir" -PathType Container)) {
Write-Host "Creating download directory: '$downloadDir'"
New-Item "$downloadDir" -ItemType directory
}
Write-Host "Downloading C-API v$libVersion into $archiveFile"
Write-Host "Downloading from URL: $url"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $archiveFile)
if (!(Test-Path "$extractedLibDir" -PathType Container)) {
New-Item "$extractedLibDir" -ItemType directory
}
Write-Host "Extracting into $extractedLibDir"
Expand-Archive "$archiveFile" "$extractedLibDir" -Force
}
function ValidateInstallation {
param ($sourceFile, $targetFile)
if ((Get-Item $sourceFile).Length -eq (Get-Item $targetFile).Length) {
Write-Host "Successfully installed $targetFile" -ForegroundColor Green
} else {
throw "Installation to $targetFile failed - source and target contents don't match"
}
}
function InstallWithPrompt {
param ($libDir)
$reply = Read-Host -Prompt "Would you like to install ObjectBox library into $($libDir)? [y/N]"
if (!($reply -match "[yY]")) {
Write-Host "OK, skipping installation to $libDir"
return
}
$sourceFile = "$extractedLibDir\lib\objectbox.dll"
$targetFile = "$libDir\objectbox.dll"
Write-Host "Copying $sourceFile to $libDir"
try {
Copy-Item $sourceFile $libDir
ValidateInstallation $sourceFile $targetFile
return
} catch [System.UnauthorizedAccessException] {
Write-Host "Can't copy: $($_.Exception.Message)" -ForegroundColor Yellow
}
# reaches here only when copying fails because of UnauthorizedAccessException
$reply = Read-Host -Prompt "Would you like to retry as administrator? [y/N]"
if (!($reply -match "[yY]")) {
Write-Host "OK, skipping installation to $libDir"
return
}
$sourceFile = "$pwd\$sourceFile" # sub-shell requires an absolute path. -WorkingDirectory argument doesn't work either.
$expectedSize = (Get-Item $sourceFile).Length
$verifyCmd = "if ((Get-Item $targetFile).Length -ne $expectedSize) {Write-Host 'Installation failed.'; Read-Host -Prompt 'Press any key to exit this window'}"
$cmd = "Copy-Item $sourceFile $libDir ; $verifyCmd"
Start-Process powershell.exe -Verb runas -ArgumentList $cmd
ValidateInstallation $sourceFile $targetFile
}
function InstallIntoGCC {
Write-Host "Determining path to your local GCC installation - necessary for compiling your programs with ObjectBox"
$libDir = "."
try {
# try to find gcc
$gcc = Get-Command gcc
Write-Host "Found GCC: $($gcc.Path)"
$libDir = Split-Path $gcc.Path -Parent
$libDir = Split-Path $libDir -Parent
$libDir = Join-Path $libDir "lib"
}
catch {
Write-Host "GCC installation not found, skipping"
return
}
InstallWithPrompt $libDir
}
function InstallIntoSys32 {
Write-Host "Windows needs to find your library during runtime (incl. tests execution)."
Write-Host "The simplest way to achieve this is to install the library globally."
InstallWithPrompt "C:\Windows\System32"
}
try {
DownloadLibrary
Write-Host ""
InstallIntoGCC
Write-Host ""
InstallIntoSys32
Write-Host ""
Write-Host "Installation complete" -ForegroundColor Green
} catch {
Write-Error $error[0].Exception -ErrorAction Continue
}
Read-Host -Prompt 'Press any key to exit'