-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_flutter.template.ps1
More file actions
128 lines (106 loc) · 4.53 KB
/
setup_flutter.template.ps1
File metadata and controls
128 lines (106 loc) · 4.53 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
# Requires -Version 7.0
$ErrorActionPreference = "Stop"
# This makes native commands (git) throw exceptions on failure, acting like 'set -e'
$PSNativeCommandUseErrorActionPreference = $true
# --- Configuration ---
# Check if OriginUrl is provided (e.g. via environment variable or parameter context)
# If not, prompt the user interactively.
if ([string]::IsNullOrWhiteSpace($OriginUrl)) {
if (-not [string]::IsNullOrWhiteSpace($env:ORIGIN_URL)) {
$OriginUrl = $env:ORIGIN_URL.Trim()
}
else {
Write-Host "Don't have a fork yet? https://github.com/flutter/flutter/fork" -ForegroundColor Cyan
Write-Host "Please enter your Flutter fork URL (e.g. git@github.com:username/flutter.git)" -ForegroundColor Cyan
$OriginUrl = Read-Host "Origin URL"
}
}
if ([string]::IsNullOrWhiteSpace($OriginUrl)) {
Write-Error "❌ Error: Origin URL is required. Aborting."
exit 1
}
$UpstreamUrl = "https://github.com/flutter/flutter.git"
# Specific refs
$RefStable = "stable"
Write-Host "🚀 Starting Flutter Worktree Setup (PS 7.5)..." -ForegroundColor Cyan
# 1. Create root directory
if (Test-Path -Path flutter -PathType Container) {
Write-Error "❌ Error: Directory 'flutter' already exists. Aborting."
exit 1
}
New-Item -ItemType Directory -Path flutter
Set-Location flutter
$RootPath = Get-Location
# 2. Clone Bare Repo
Write-Host "📦 Cloning origin as bare repository..." -ForegroundColor Yellow
Write-Host " Origin: '$OriginUrl'" -ForegroundColor DarkGray
git clone --bare "$OriginUrl" .bare
# Create .git pointer (Ascii is safest for git, though PS7 UTF8NoBOM works too)
Set-Content -Path .git -Value "gitdir: ./.bare" -Encoding Ascii
# 3. Configure Remotes
Write-Host "⚙️ Configuring remotes..." -ForegroundColor Yellow
# Fix Origin and Add Upstream
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git remote add upstream "$UpstreamUrl"
git config remote.upstream.fetch "+refs/heads/*:refs/remotes/upstream/*"
# 4. Fetch All
Write-Host "⬇️ Fetching everything (--all --tags --prune)..." -ForegroundColor Yellow
git fetch --all --tags --prune
# --- Setup MASTER ---
Write-Host "🌲 Creating 'master' worktree..." -ForegroundColor Green
# Use a try-catch block for more idiomatic error handling.
try {
git branch -D master 2>$null
}
catch {
Write-Host " (No existing master branch to delete, continuing...)" -ForegroundColor DarkGray
}
# Create new master worktree tracking upstream
git worktree add -B master master --track upstream/master
# --- Setup STABLE ---
if ([string]::IsNullOrWhiteSpace($setupStable)) {
if (-not [string]::IsNullOrWhiteSpace($env:SETUP_STABLE)) {
$setupStable = $env:SETUP_STABLE
}
else {
$setupStable = Read-Host "Do you want to setup the 'stable' worktree? (y/N)"
}
}
if ($setupStable -match "^[yY]" -or $setupStable -eq "true" -or $setupStable -eq $true) {
# Normalize for later use
$setupStable = "y"
Write-Host "🌲 Creating 'stable' worktree (based on upstream/$RefStable)..." -ForegroundColor Green
git worktree add -B stable stable --track upstream/"$RefStable"
}
# 5. Pre-load Artifacts
# Determine correct flutter command based on OS for cross-platform compatibility
$flutterCmd = if ($IsWindows) { ".\bin\flutter.bat" } else { "./bin/flutter" }
if ($setupStable -match "^[yY]") {
Write-Host "🛠️ Hydrating 'stable' artifacts..." -ForegroundColor Magenta
Set-Location stable
& $flutterCmd --version | Out-Null
Set-Location ..
}
Write-Host "🛠️ Hydrating 'master' artifacts..." -ForegroundColor Magenta
Set-Location master
& $flutterCmd --version | Out-Null
Set-Location ..
# 6. Generate The Switcher Script
Write-Host "🔗 Generating context switcher..." -ForegroundColor Cyan
$SwitchFile = Join-Path $RootPath "fswitch.ps1"
$PAYLOAD = "REPLACE_ME"
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($PAYLOAD)) | Set-Content -Path $SwitchFile -Encoding UTF8
Write-Host ""
Write-Host "✅ Setup Complete!" -ForegroundColor Green
Write-Host "------------------------------------------------------"
Write-Host "📂 Root: $RootPath"
Write-Host "👉 To enable the switcher, add this to your PowerShell Profile:"
Write-Host " . '$SwitchFile'"
Write-Host ""
Write-Host "Usage:"
Write-Host " PS> fswitch master -> Activates master branch"
Write-Host " PS> fswitch stable -> Activates stable branch"
Write-Host ""
Write-Host "Want to create a new worktree?"
Write-Host " PS> git worktree add my_feature"
Write-Host "------------------------------------------------------"