106 lines
5.5 KiB
PowerShell
106 lines
5.5 KiB
PowerShell
# Installer of the aio command (MozOut All in One AI) for Windows. Generated by the docs build.
|
|
# Usage, in PowerShell: irm https://docs.mozout.com/cli/install.ps1 | iex
|
|
# To remove: & ([scriptblock]::Create((irm https://docs.mozout.com/cli/install.ps1))) -Uninstall
|
|
# Never asks for administrator rights: installs in %LOCALAPPDATA%\aio and adds that folder to the user PATH.
|
|
# The published file has ASCII characters only: the build turns accented letters into \uXXXX, and the Msg function restores them.
|
|
|
|
param([switch]$Uninstall)
|
|
|
|
& {
|
|
param([bool]$Uninstall)
|
|
$ErrorActionPreference = 'Stop'
|
|
$ProgressPreference = 'SilentlyContinue'
|
|
|
|
$From = 'https://docs.mozout.com/cli'
|
|
$Version = '1.0.1'
|
|
$Dir = Join-Path $env:LOCALAPPDATA 'aio'
|
|
$UsePt = ((Get-Culture).Name -like 'pt*') -or ((Get-UICulture).Name -like 'pt*')
|
|
|
|
function Msg([string]$pt, [string]$en) {
|
|
if ($UsePt) { return [regex]::Unescape($pt) } else { return $en }
|
|
}
|
|
function Say([string]$pt, [string]$en, [object[]]$f = @()) {
|
|
Write-Host ((Msg $pt $en) -f $f)
|
|
}
|
|
function Fail([string]$pt, [string]$en, [object[]]$f = @()) {
|
|
Write-Host ((Msg 'Erro: ' 'Error: ') + ((Msg $pt $en) -f $f)) -ForegroundColor Red
|
|
throw 'aio-install-failed'
|
|
}
|
|
|
|
try {
|
|
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
|
|
if ($null -eq $userPath) { $userPath = '' }
|
|
|
|
if ($Uninstall) {
|
|
foreach ($n in @('aio.js', 'aio.cmd', 'aio.ps1')) {
|
|
$p = Join-Path $Dir $n
|
|
if (Test-Path $p) { Remove-Item -Force $p }
|
|
}
|
|
if ((Test-Path $Dir) -and -not (Get-ChildItem $Dir -Force)) { Remove-Item -Force $Dir }
|
|
$parts = $userPath.Split(';') | Where-Object { $_ -and ($_.TrimEnd('\') -ne $Dir.TrimEnd('\')) }
|
|
[Environment]::SetEnvironmentVariable('Path', ($parts -join ';'), 'User')
|
|
Say 'O aio foi removido de {0}.' 'aio was removed from {0}.' @($Dir)
|
|
Say 'A configuração com a chave fica em {0}. Pode apagar essa pasta à mão.' 'Your settings and key stay in {0}. You can delete that folder by hand.' @((Join-Path $env:APPDATA 'aio'))
|
|
return
|
|
}
|
|
|
|
Say 'A instalar o comando aio {0} ({1} All in One AI)...' 'Installing the aio command {0} ({1} All in One AI)...' @($Version, 'MozOut')
|
|
|
|
# 1. Node.js 18 or newer
|
|
$node = Get-Command node -ErrorAction SilentlyContinue
|
|
if (-not $node) {
|
|
Fail 'o aio precisa do Node.js 18 ou mais recente, e o Node.js não foi encontrado. Instale-o em https://nodejs.org e volte a correr este comando.' 'aio needs Node.js 18 or later, and Node.js was not found. Install it from https://nodejs.org and run this command again.'
|
|
}
|
|
$nodeVersion = (& node --version) -replace '^v', ''
|
|
$major = 0
|
|
[void][int]::TryParse(($nodeVersion -split '\.')[0], [ref]$major)
|
|
if ($major -lt 18) {
|
|
Fail 'o aio precisa do Node.js 18 ou mais recente, e este computador tem o {0}. Actualize-o em https://nodejs.org e volte a correr este comando.' 'aio needs Node.js 18 or later, and this computer has {0}. Update it from https://nodejs.org and run this command again.' @($nodeVersion)
|
|
}
|
|
|
|
# 2. Download over https (TLS 1.2 also in Windows PowerShell 5.1)
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
|
|
$tmp = Join-Path ([IO.Path]::GetTempPath()) ('aio-' + [Guid]::NewGuid().ToString('N'))
|
|
New-Item -ItemType Directory -Path $tmp | Out-Null
|
|
try {
|
|
$js = Join-Path $tmp 'aio.js'
|
|
$sums = Join-Path $tmp 'SHA256SUMS'
|
|
try {
|
|
Invoke-WebRequest -UseBasicParsing -Uri "$From/aio.js" -OutFile $js
|
|
Invoke-WebRequest -UseBasicParsing -Uri "$From/SHA256SUMS" -OutFile $sums
|
|
} catch {
|
|
Fail 'não foi possível descarregar o aio de {0}.' 'could not download aio from {0}.' @($From)
|
|
}
|
|
|
|
# 3. Check the SHA256
|
|
$expected = ''
|
|
foreach ($line in Get-Content $sums) {
|
|
$c = $line -split '\s+'
|
|
if ($c.Count -ge 2 -and ($c[1] -eq 'aio.js' -or $c[1] -eq '*aio.js')) { $expected = $c[0].ToLower() }
|
|
}
|
|
$actual = (Get-FileHash -Algorithm SHA256 -Path $js).Hash.ToLower()
|
|
if (-not $expected -or $expected -ne $actual) {
|
|
Fail 'o SHA256 do ficheiro descarregado não confere. Nada foi instalado. Tente de novo dentro de alguns minutos.' 'the SHA256 of the downloaded file does not match. Nothing was installed. Try again in a few minutes.'
|
|
}
|
|
|
|
# 4. Install in %LOCALAPPDATA%\aio
|
|
New-Item -ItemType Directory -Force -Path $Dir | Out-Null
|
|
Copy-Item -Force $js (Join-Path $Dir 'aio.js')
|
|
Set-Content -Path (Join-Path $Dir 'aio.cmd') -Encoding ASCII -Value "@echo off`r`nnode `"%~dp0aio.js`" %*"
|
|
} finally {
|
|
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
$inPath = $userPath.Split(';') | Where-Object { $_.TrimEnd('\') -eq $Dir.TrimEnd('\') }
|
|
if (-not $inPath) {
|
|
$novo = if ($userPath) { $userPath.TrimEnd(';') + ';' + $Dir } else { $Dir }
|
|
[Environment]::SetEnvironmentVariable('Path', $novo, 'User')
|
|
}
|
|
if (-not (($env:Path -split ';') -contains $Dir)) { $env:Path = $env:Path.TrimEnd(';') + ';' + $Dir }
|
|
|
|
Say 'Pronto: o aio ficou instalado em {0}.' 'Done: aio is installed in {0}.' @($Dir)
|
|
Say 'Para começar: aio auth login (nos terminais que já estavam abertos, feche-os e abra-os de novo).' 'To start: aio auth login (close and reopen any terminals that were already open).'
|
|
} catch {
|
|
if ("$_" -ne 'aio-install-failed') { Write-Host ("$_") -ForegroundColor Red }
|
|
}
|
|
} $Uninstall.IsPresent
|