aboutsummaryrefslogtreecommitdiff
path: root/bootstrap-win/git.ps1
blob: 6accba40e8adcc148716b79816512e56ae511045 (plain)
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
#Requires -Version 5.1
<#
  Installiert Git und delta (Pager fuer git/config) per winget.

  Aufruf (keine Admin-Rechte noetig):
    .\bootstrap-win\git.ps1
#>

$ErrorActionPreference = "Stop"
$script:Failures = @()

function Write-Ok($msg) {
  Write-Host "OK   $msg" -ForegroundColor Green
}

function Write-Step($msg) {
  Write-Host "==> $msg" -ForegroundColor Cyan
}

function Write-Skip($msg) {
  Write-Host "-    $msg bereits vorhanden" -ForegroundColor DarkGray
}

function Write-Warn($msg) {
  Write-Host "!    $msg" -ForegroundColor Yellow
  $script:Failures += $msg
}

function Test-CommandExists([string]$Name) {
  return [bool](Get-Command $Name -ErrorAction SilentlyContinue)
}

function Install-WingetPackage([string]$Binary, [string]$Id) {
  if (Test-CommandExists $Binary) {
    Write-Skip $Binary
    return
  }

  Write-Step "winget install $Id"
  $output = winget install --id $Id -e --source winget --accept-package-agreements --accept-source-agreements --silent 2>&1

  if ($LASTEXITCODE -ne 0) {
    $lastLine = ($output | Select-Object -Last 3) -join " / "
    Write-Warn "winget install $Id fehlgeschlagen (Binary '$Binary' nicht gefunden): $lastLine -- Pruefen mit: winget search $Id"
    return
  }

  Write-Ok $Id
}

function Test-Prerequisites {
  if (-not (Test-CommandExists "winget")) {
    Write-Host "Fehler: winget wurde nicht gefunden. 'App Installer' aus dem Microsoft Store installieren." -ForegroundColor Red
    exit 1
  }
}

function Main {
  Test-Prerequisites

  Install-WingetPackage -Binary "git" -Id "Git.Git"
  Install-WingetPackage -Binary "delta" -Id "dandavison.delta"

  Write-Host ""
  if ($script:Failures.Count -eq 0) {
    Write-Host "Git-Bootstrap abgeschlossen." -ForegroundColor Green
  } else {
    Write-Host "Git-Bootstrap abgeschlossen, mit $($script:Failures.Count) Warnung(en):" -ForegroundColor Yellow
    foreach ($f in $script:Failures) {
      Write-Host "  - $f" -ForegroundColor Yellow
    }
  }
}

Main