-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
222 lines (197 loc) · 7.18 KB
/
install.ps1
File metadata and controls
222 lines (197 loc) · 7.18 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
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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Install lightweight launchers for notes.ps1 and notes-gui into a user bin directory.
#>
[CmdletBinding()]
param(
[string]$BinDir
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Get-PythonCommand {
foreach ($candidate in @('python', 'python3')) {
if (Get-Command $candidate -ErrorAction SilentlyContinue) {
return $candidate
}
}
return $null
}
function Get-VenvPythonPath {
param([string]$VenvPath)
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
return Join-Path $VenvPath 'Scripts\python.exe'
}
return Join-Path $VenvPath 'bin/python'
}
function Get-PlatformSuffix {
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
return 'windows'
}
if ($IsMacOS) {
return 'macos'
}
return 'linux'
}
function Test-PythonImport {
param(
[string]$PythonCommand,
[string]$ModuleName
)
& $PythonCommand -c "import $ModuleName" 2>$null
return $LASTEXITCODE -eq 0
}
$repoRoot = Split-Path -Parent $PSCommandPath
$notesScript = Join-Path $repoRoot 'notes.ps1'
$guiEntryPoint = Join-Path $repoRoot 'gui\main.py'
$guiRequirementsPath = Join-Path $repoRoot 'requirements-gui.txt'
if (-not (Test-Path -LiteralPath $notesScript)) {
throw "Could not find notes.ps1 next to install.ps1."
}
if (-not $BinDir) {
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
$BinDir = Join-Path $HOME 'bin'
} else {
$BinDir = Join-Path $HOME '.local/bin'
}
}
New-Item -ItemType Directory -Path $BinDir -Force | Out-Null
$binDir = (Get-Item -LiteralPath $BinDir).FullName
$notesScriptPath = (Get-Item -LiteralPath $notesScript).FullName
$psLauncherPath = Join-Path $binDir 'notes.ps1'
$escapedPowerShellPath = $notesScriptPath.Replace("'", "''")
$psLauncherContent = @"
#!/usr/bin/env pwsh
& '$escapedPowerShellPath' @args
`$script:notesExitCode = `$LASTEXITCODE
if (`$null -eq `$script:notesExitCode) {
`$script:notesExitCode = 0
}
`$global:LASTEXITCODE = `$script:notesExitCode
if ((Get-PSCallStack).Count -le 2) {
`$host.SetShouldExit(`$script:notesExitCode)
}
"@
Set-Content -Path $psLauncherPath -Value $psLauncherContent -Encoding utf8
$createdLaunchers = @($psLauncherPath)
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
$cmdLauncherPath = Join-Path $binDir 'notes.cmd'
$cmdLauncherContent = @"
@echo off
pwsh -NoProfile -File "$notesScriptPath" %*
exit /b %errorlevel%
"@
Set-Content -Path $cmdLauncherPath -Value $cmdLauncherContent -Encoding ascii
$createdLaunchers += $cmdLauncherPath
} else {
$shellLauncherPath = Join-Path $binDir 'notes'
$escapedShellPath = $notesScriptPath.Replace("'", "'""'""'")
$shellLauncherContent = @"
#!/bin/sh
exec pwsh -NoProfile -File '$escapedShellPath' "`$@"
"@
[System.IO.File]::WriteAllText(
$shellLauncherPath,
$shellLauncherContent.Replace("`r`n", "`n"),
[System.Text.UTF8Encoding]::new($false)
)
chmod +x $shellLauncherPath
$createdLaunchers += $shellLauncherPath
}
if (Test-Path -LiteralPath $guiEntryPoint) {
$python = Get-PythonCommand
if ($null -eq $python) {
Write-Warning "Python 3 was not found. Skipping notes-gui launcher installation."
} else {
$platformSuffix = Get-PlatformSuffix
$guiVenvPath = Join-Path $repoRoot ".venv-gui-$platformSuffix"
$guiPython = Get-VenvPythonPath -VenvPath $guiVenvPath
$guiPackagePath = $null
$guiPipExtraArgs = @()
$useVenv = (Test-Path -LiteralPath $guiPython) -and (Test-PythonImport -PythonCommand $guiPython -ModuleName 'pip')
if (-not $useVenv) {
Write-Host "Creating notes-gui virtual environment..." -ForegroundColor Cyan
& $python -m venv $guiVenvPath
$useVenv = $LASTEXITCODE -eq 0 -and (Test-Path -LiteralPath $guiPython) -and (Test-PythonImport -PythonCommand $guiPython -ModuleName 'pip')
if (-not $useVenv) {
Write-Warning "Could not create a virtual environment. Falling back to installing GUI dependencies for the detected Python."
$guiPython = $python
if (-not ($IsWindows -or $env:OS -eq 'Windows_NT')) {
$guiPipExtraArgs = @('--user')
if ($platformSuffix -eq 'linux') {
$guiPipExtraArgs += '--break-system-packages'
}
}
}
}
Write-Host "Installing notes-gui dependencies..." -ForegroundColor Cyan
& $guiPython -m pip install --disable-pip-version-check @guiPipExtraArgs -r $guiRequirementsPath
if ($LASTEXITCODE -ne 0) {
throw "Failed to install notes-gui dependencies."
}
$guiPsLauncherPath = Join-Path $binDir 'notes-gui.ps1'
$escapedRepoRoot = $repoRoot.Replace("'", "''")
$escapedGuiPython = $guiPython.Replace("'", "''")
$guiPsLauncherContent = @"
#!/usr/bin/env pwsh
Push-Location '$escapedRepoRoot'
try {
& '$escapedGuiPython' -m gui.main @args
`$script:notesGuiExitCode = `$LASTEXITCODE
if (`$null -eq `$script:notesGuiExitCode) {
`$script:notesGuiExitCode = 0
}
`$global:LASTEXITCODE = `$script:notesGuiExitCode
if ((Get-PSCallStack).Count -le 2) {
`$host.SetShouldExit(`$script:notesGuiExitCode)
}
} finally {
Pop-Location
}
"@
Set-Content -Path $guiPsLauncherPath -Value $guiPsLauncherContent -Encoding utf8
$createdLaunchers += $guiPsLauncherPath
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
$guiCmdLauncherPath = Join-Path $binDir 'notes-gui.cmd'
$guiCmdLauncherContent = @"
@echo off
setlocal
pushd "$repoRoot"
"$guiPython" -m gui.main %*
set EXITCODE=%errorlevel%
popd
exit /b %EXITCODE%
"@
Set-Content -Path $guiCmdLauncherPath -Value $guiCmdLauncherContent -Encoding ascii
$createdLaunchers += $guiCmdLauncherPath
} else {
$guiShellLauncherPath = Join-Path $binDir 'notes-gui'
$escapedShellRepoRoot = $repoRoot.Replace("'", "'""'""'")
$escapedShellGuiPython = $guiPython.Replace("'", "'""'""'")
$guiShellLauncherContent = @"
#!/bin/sh
cd '$escapedShellRepoRoot' || exit 1
exec '$escapedShellGuiPython' -m gui.main "`$@"
"@
[System.IO.File]::WriteAllText(
$guiShellLauncherPath,
$guiShellLauncherContent.Replace("`r`n", "`n"),
[System.Text.UTF8Encoding]::new($false)
)
chmod +x $guiShellLauncherPath
$createdLaunchers += $guiShellLauncherPath
}
}
}
Write-Host "Created launchers:" -ForegroundColor Green
$createdLaunchers | ForEach-Object { Write-Host " $_" }
if ((Test-Path -LiteralPath $guiEntryPoint) -and ($createdLaunchers -contains (Join-Path $binDir 'notes-gui.ps1'))) {
Write-Host ""
Write-Host "notes-gui is configured for this repository and ready to launch." -ForegroundColor Green
}
$pathSeparator = [System.IO.Path]::PathSeparator
$pathEntries = @($env:PATH -split [regex]::Escape([string]$pathSeparator))
if ($pathEntries -notcontains $binDir) {
Write-Host ""
Write-Host "Add '$binDir' to PATH to use the launcher from any shell." -ForegroundColor Yellow
}