Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
241 lines (199 loc) · 8.11 KB
/
Copy pathinstall.ps1
File metadata and controls
241 lines (199 loc) · 8.11 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<#
.SYNOPSIS
Instalador do Presenter para Windows.
.DESCRIPTION
Detecta a arquitetura, baixa o instalador oficial mais recente do GitHub
Releases e executa a instalacao silenciosa.
O script exige PowerShell 5.1 ou superior.
.EXAMPLE
Invoke-WebRequest https://raw.githubusercontent.com/silv4b/presenter/develop/install.ps1 -OutFile install.ps1
./install.ps1
.EXAMPLE
./install.ps1 -Version v1.3.0
.EXAMPLE
./install.ps1 -DryRun
#>
[CmdletBinding()]
param(
# Versão especifica (ex.: v1.3.0). Padrão: a mais recente.
[string]$Version,
# Mostra o que seria feito, sem baixar nem instalar.
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$Repo = "silv4b/presenter"
$Product = "presenter"
$GitHubApi = "https://api.github.com/repos/$Repo"
# ---------------------------------------------------------------------------
# Saida
# ---------------------------------------------------------------------------
function Write-Info { param($m) Write-Host ":: $m" -ForegroundColor Cyan }
function Write-Success { param($m) Write-Host "ok $m" -ForegroundColor Green }
function Write-Warn { param($m) Write-Host "!! $m" -ForegroundColor Yellow }
function Write-Err { param($m) Write-Host "erro: $m" -ForegroundColor Red }
# ---------------------------------------------------------------------------
# Pre-requisitos
# ---------------------------------------------------------------------------
if ($PSVersionTable.PSVersion.Major -lt 5) {
Write-Err "PowerShell 5.1 ou superior e necessário (encontrado $($PSVersionTable.PSVersion))"
exit 1
}
# ---------------------------------------------------------------------------
# Detecção de arquitetura
# ---------------------------------------------------------------------------
$arch = $env:PROCESSOR_ARCHITECTURE
if (-not $arch) { $arch = "AMD64" }
if ($env:PROCESSOR_ARCHITEW6432) { $arch = $env:PROCESSOR_ARCHITEW6432 }
switch ($arch) {
"AMD64" { $TauriArch = "x64" }
"ARM64" { $TauriArch = "arm64" }
default {
Write-Err "arquitetura nao suportada: $arch (suportado: AMD64, ARM64)"
exit 1
}
}
# ---------------------------------------------------------------------------
# Consulta da release
# ---------------------------------------------------------------------------
function Get-Headers {
$h = @{ "Accept" = "application/vnd.github+json"; "User-Agent" = "presenter-install" }
if ($env:GITHUB_TOKEN) { $h["Authorization"] = "Bearer $env:GITHUB_TOKEN" }
return $h
}
if (-not $Version) {
Write-Info "Consultando a versão mais recente..."
try {
$release = Invoke-RestMethod -Uri "$GitHubApi/releases/latest" -Headers (Get-Headers)
$Version = $release.tag_name
} catch {
Write-Err "nao foi possível consultar a API do GitHub: $($_.Exception.Message)"
exit 1
}
} else {
if ($Version -notmatch '^v\d') { $Version = "v$Version" }
try {
$release = Invoke-RestMethod -Uri "$GitHubApi/releases/tags/$Version" -Headers (Get-Headers)
} catch {
Write-Err "release $Version nao encontrada: $($_.Exception.Message)"
exit 1
}
}
if (-not $Version) {
Write-Err "resposta da API nao contem tag_name"
exit 1
}
# ---------------------------------------------------------------------------
# Seleção do artefato
# ---------------------------------------------------------------------------
# O Tauri nomeia o NSIS como _x64-setup.exe. O padrão é ancorado no final
# para não casar com outros assets que contenham o nome no meio.
$patterns = @("_$TauriArch-setup\.exe$")
$asset = $null
foreach ($p in $patterns) {
$asset = $release.assets |
Where-Object { $_.name -match $p } |
Select-Object -First 1
if ($asset) { break }
}
if (-not $asset) {
Write-Err "nenhum instalador compatível encontrado em $Version para $arch"
Write-Host ""
Write-Host "Instaladores disponíveis nesta release:"
$release.assets |
Where-Object { $_.name -match '\.exe$' } |
ForEach-Object { Write-Host " - $($_.name)" }
Write-Host ""
exit 1
}
# ---------------------------------------------------------------------------
# Resumo
# ---------------------------------------------------------------------------
$os = [System.Environment]::OSVersion.VersionString
Write-Host ""
Write-Host "Presenter $Version" -ForegroundColor Cyan
Write-Host " sistema: $os"
Write-Host " arquitetura: $arch"
Write-Host " instalador: $($asset.name) (NSIS)"
Write-Host " tamanho: $([math]::Round($asset.size / 1MB, 2)) MB"
Write-Host ""
# ---------------------------------------------------------------------------
# Download
# ---------------------------------------------------------------------------
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("presenter-install-" + [System.Guid]::NewGuid().ToString("N").Substring(0, 8))
$target = Join-Path $tempDir $asset.name
try {
if ($DryRun) {
Write-Host "[dry-run] Invoke-WebRequest $($asset.browser_download_url) -OutFile $target" -ForegroundColor Yellow
} else {
Write-Info "Baixando $($asset.name)..."
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
$oldProgress = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
try {
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $target -Headers (Get-Headers)
} finally {
$ProgressPreference = $oldProgress
}
if (-not (Test-Path $target)) { Write-Err "download falhou"; exit 1 }
$size = (Get-Item $target).Length
if ($size -ne $asset.size) {
Write-Err "download incompleto: esperado $($asset.size) bytes, obtido $size"
exit 1
}
Write-Success "Download concluído ($([math]::Round($size / 1MB, 2)) MB)"
}
# ------------------------------------------------------------------------
# Instalação
# ------------------------------------------------------------------------
if ($DryRun) {
Write-Host "[dry-run] Start-Process '$target' -ArgumentList '/S' -Wait" -ForegroundColor Yellow
Write-Host ""
Write-Info "dry-run concluído. Nada foi instalado."
exit 0
}
# O NSIS e instalado no modo currentUser (default do Tauri), que nao exige
# privilegio de administrador. Nao ha elevação aqui de proposito: o script
# também roda via `irm | iex`, onde nao existe arquivo para reinvocar.
Write-Info "Instalando (pode levar alguns segundos)..."
# /S e o modo silencioso do instalador NSIS gerado pelo Tauri.
$proc = Start-Process -FilePath $target -ArgumentList "/S" -Wait -PassThru
$code = $proc.ExitCode
if ($code -ne 0) {
Write-Err "instalador retornou código de saída $code"
Write-Host ""
Write-Host "Se a instalação foi recusada por falta de permissão, rode o"
Write-Host "comando em um PowerShell aberto como Administrador."
exit $code
}
# ------------------------------------------------------------------------
# Conclusão
# ------------------------------------------------------------------------
# currentUser instala em %LOCALAPPDATA%\presenter; perMachine, em
# C:\Program Files\presenter. As duas formas sao cobertas.
$roots = @(
"C:\Program Files\presenter",
"$env:LOCALAPPDATA\presenter",
"$env:LOCALAPPDATA\Programs\presenter"
) | Where-Object { $_ -and (Test-Path -LiteralPath $_) }
$exe = $null
if ($roots) {
$exe = Get-ChildItem -Path $roots `
-Filter "presenter.exe" -Recurse -ErrorAction SilentlyContinue |
Select-Object -First 1
}
Write-Host ""
if ($exe) {
Write-Success "Presenter $Version instalado em $($exe.DirectoryName)"
} else {
Write-Success "Presenter $Version instalado."
}
Write-Host ""
Write-Host " Atalhos: F5 inicia a apresentação, Esc encerra"
Write-Host " Desinstalar: Configurações > Aplicativos > Presenter"
Write-Host ""
} finally {
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}