Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
576 changes: 306 additions & 270 deletions .devcontainer/Dockerfile

Large diffs are not rendered by default.

211 changes: 118 additions & 93 deletions .devcontainer/Start.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<#
.SYNOPSIS
GhostClass Sandbox dev-env launcher for Docker Desktop.
Setup ADB, supports Emulator, Physical target, or Skip.
GhostClass Dev-Environment & ADB Bridge Launcher for Windows.
Connects Android Emulator / Physical Device from Windows Host to DevContainer.

.PARAMETER Mode
"Emulator", "Physical", or "Skip".
Expand All @@ -11,30 +11,45 @@ param(
[string]$Mode
)

$ContainerName = "GhostClass_Sandbox"
$User = "vscode"
$VmServicePort = 8181
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

$User = "vscode"

# Ensure WSL SSH agent socket is initialized before Docker mounts it
Write-Host "🔑 Initializing WSL SSH Relay..." -ForegroundColor Cyan
wsl --exec bash -c 'source ~/.bashrc'
Write-Host "[WSL] Initializing WSL SSH Relay..." -ForegroundColor Cyan
wsl --exec bash -c 'source ~/.bashrc' 2>$null

Write-Host "`n[GhostClass] Initializing Windows Development Bridge..." -ForegroundColor Cyan

# 1. Verify Docker Desktop
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
Write-Host "[ERROR] Docker CLI not found on host." -ForegroundColor Red
exit 1
}

Write-Host "🔧 Verifying Docker Desktop..." -ForegroundColor Cyan
if (!(docker info 2>$null)) {
Write-Host "❌ Docker Desktop is not running." -ForegroundColor Red
Write-Host "[ERROR] Docker daemon is not accessible. Ensure Docker Desktop is running." -ForegroundColor Red
exit 1
}

# Find DevContainer name dynamically or fall back to default
$ContainerName = $(docker ps --filter "label=devcontainer.local_folder" --format "{{.Names}}" 2>$null | Select-Object -First 1)
if (-not $ContainerName) {
$ContainerName = "GhostClass_Sandbox"
}

$Status = (docker inspect -f '{{.State.Status}}' $ContainerName 2>$null).Trim()
if ($Status -ne "running") {
Write-Host "🚀 Starting container..." -ForegroundColor Cyan
wsl --exec bash -ic "docker start $ContainerName" > $null
Write-Host "[Docker] Starting container $ContainerName..." -ForegroundColor Cyan
docker start $ContainerName > $null
Start-Sleep -Seconds 2
}

# 2. Select Debug Target
if (-not $Mode) {
Write-Host "`n🤖 Select Debug Target Environment:" -ForegroundColor Magenta
Write-Host " [1] Emulator" -ForegroundColor Cyan
Write-Host "`n[Target] Select Debug Target Environment:" -ForegroundColor Magenta
Write-Host " [1] Android Emulator (Host GPU Accelerated)" -ForegroundColor Cyan
Write-Host " [2] Physical Device (Wi-Fi ADB)" -ForegroundColor Cyan
Write-Host " [3] Skip ADB (Container Only)" -ForegroundColor Cyan
$Choice = Read-Host "Enter selection (1, 2, or 3)"
Expand All @@ -44,87 +59,130 @@ if (-not $Mode) {
else { $Mode = "Emulator" }
}

$EmulatorProc = $null

if ($Mode -ne "Skip") {
# Reset ADB on container to start fresh
docker exec -u $User $ContainerName adb kill-server 2>$null
Write-Host "`n[ADB] Ensuring Host ADB Server is listening on all interfaces (-a)..." -ForegroundColor Cyan
# Stop any localhost-bound ADB server to re-bind with -a
adb kill-server 2>$null
Start-Process adb -ArgumentList "-a nodaemon server" -WindowStyle Hidden
Start-Sleep -Seconds 2

if ($Mode -eq "Physical") {
Write-Host "📱 Preparing Physical Device via USB/Wi-Fi..." -ForegroundColor Cyan
Write-Host "[Device] Preparing Physical Device via USB/Wi-Fi..." -ForegroundColor Cyan
adb start-server | Out-Null

# 1. Fetch Wi-Fi IP while USB is attached
# 1. Fetch device Wi-Fi IP while USB is attached
$RouteOutput = adb shell ip route 2>$null
$DeviceIp = ($RouteOutput | Select-String -Pattern 'src\s+(\d+\.\d+\.\d+\.\d+)').Matches.Groups[1].Value

if (-not $DeviceIp) {
Write-Host "❌ Could not determine device's Wi-Fi IP. Is USB plugged in and authorized?" -ForegroundColor Red
exit 1
Write-Host "[WARN] Could not auto-detect device Wi-Fi IP. Ensure USB debugging is authorized." -ForegroundColor Yellow
$DeviceIp = Read-Host "Enter device Wi-Fi IP manually (e.g. 192.168.1.50)"
}

# 2. Switch phone to TCP mode on host
Write-Host "📡 Switching device ADB to TCP mode (Port 5555)..." -ForegroundColor Cyan
adb tcpip 5555 | Out-Null
Start-Sleep -Seconds 3

# 3. Connect Host ADB over Wi-Fi first to authenticate key pair
Write-Host "🤝 Connecting Windows Host ADB to ${DeviceIp}:5555..." -ForegroundColor Cyan
adb connect "${DeviceIp}:5555" | Out-Null
Start-Sleep -Seconds 2
if ($DeviceIp) {
# 2. Switch phone to TCP mode on host
Write-Host "[ADB] Switching device to TCP mode (Port 5555)..." -ForegroundColor Cyan
adb tcpip 5555 | Out-Null
Start-Sleep -Seconds 2

Write-Host "📱 Connecting Container ADB to Device IP: ${DeviceIp}:5555" -ForegroundColor Green
docker exec -u $User $ContainerName adb connect "${DeviceIp}:5555" | Out-Null
# 3. Connect Host ADB over Wi-Fi first to authenticate key pair
Write-Host "[ADB] Connecting Windows Host ADB to ${DeviceIp}:5555..." -ForegroundColor Cyan
adb connect "${DeviceIp}:5555" | Out-Null
Start-Sleep -Seconds 1

if (docker ps -q -f name="$ContainerName" 2>$null) {
Write-Host "[Container] Connecting Container ADB to Device IP: ${DeviceIp}:5555..." -ForegroundColor Green
docker exec -u $User $ContainerName adb connect "${DeviceIp}:5555" | Out-Null
}
}
} else {
Write-Host "📱 Launching Local Emulator..." -ForegroundColor Cyan
$EmulatorProc = Start-Process emulator -ArgumentList "-avd Medium_Phone_API_36.1 -netdelay none -netspeed full" -PassThru -WindowStyle Hidden

# Wait for emulator to fully register on Windows ADB
Write-Host "⏳ Waiting for Emulator ADB boot..." -ForegroundColor Cyan
$emuBooted = $false
for ($i = 0; $i -lt 20; $i++) {
$emuState = (adb -e get-state 2>$null)
if ($emuState -eq "device") { $emuBooted = $true; break }
Start-Sleep -Seconds 2
Write-Host "[AVD] Checking Local Android Emulator..." -ForegroundColor Cyan
$emuState = (adb -e get-state 2>$null)
if ($emuState -ne "device") {
Write-Host " [AVD] Inspecting available AVD emulators..." -ForegroundColor Cyan
[string[]]$avds = @(emulator -list-avds 2>$null | Where-Object { $_ -and $_.Trim() -ne "" })

$targetAvd = $null
if ($avds.Length -gt 1) {
Write-Host "`n[AVD] Available Android Virtual Devices (AVDs):" -ForegroundColor Magenta
for ($idx = 0; $idx -lt $avds.Length; $idx++) {
Write-Host " [$($idx + 1)] $($avds[$idx])" -ForegroundColor Cyan
}
$selection = Read-Host "Select AVD to launch (1-$($avds.Length)) [Default: 1]"
if ($selection -match '^\d+$' -and [int]$selection -ge 1 -and [int]$selection -le $avds.Length) {
$targetAvd = $avds[[int]$selection - 1]
} else {
$targetAvd = $avds[0]
}
} elseif ($avds.Length -eq 1) {
$targetAvd = $avds[0]
Write-Host " [AVD] Selected AVD: $targetAvd" -ForegroundColor Green
} else {
$manualAvd = Read-Host "No AVD found with 'emulator -list-avds'. Enter AVD name [Default: Medium_Phone_API_36.1]"
$targetAvd = if ($manualAvd) { $manualAvd } else { "Medium_Phone_API_36.1" }
}

Write-Host " [AVD] Launching AVD emulator: $targetAvd..." -ForegroundColor Cyan
$EmulatorProc = Start-Process emulator -ArgumentList "-avd $targetAvd -netdelay none -netspeed full" -PassThru -WindowStyle Hidden

Write-Host " [AVD] Waiting for Emulator to complete boot..." -ForegroundColor Cyan
for ($i = 0; $i -lt 35; $i++) {
$emuState = (adb -e get-state 2>$null)
if ($emuState -eq "device") { break }
Start-Sleep -Seconds 2
}
}

if (-not $emuBooted) {
Write-Host "❌ Emulator failed to boot or register ADB on host." -ForegroundColor Red
exit 1

if ($emuState -eq "device") {
Write-Host " [OK] Android Emulator is online and registered on host." -ForegroundColor Green
if (docker ps -q -f name="$ContainerName" 2>$null) {
docker exec -u $User $ContainerName adb connect host.docker.internal:5555 2>$null | Out-Null
}
} else {
Write-Host "[WARN] Emulator took too long to boot. Check Android Studio Device Manager." -ForegroundColor Yellow
}

# Use host.docker.internal so the container routes to Windows Host
Write-Host "🔌 Connecting Container ADB to host.docker.internal:5555..." -ForegroundColor Cyan
docker exec -u $User $ContainerName adb connect host.docker.internal:5555 | Out-Null
}

$retryCount = 0
# Match target to host.docker.internal for Emulator mode
$Target = if ($Mode -eq "Physical") { "${DeviceIp}:5555" } else { "host.docker.internal:5555" }
$authorizedDevice = $null

while ($retryCount -lt 15) {
$containerState = (docker exec -u $User $ContainerName adb -s $Target get-state 2>$null)
if ($containerState -eq "device") {
Write-Host "✅ Container ADB Connected & Authorized to $Target!" -ForegroundColor Green
# Check all devices reported by the container ADB client
$deviceLines = (docker exec -u $User $ContainerName adb devices 2>$null) | Where-Object { $_ -match '\s+device$' }
if ($deviceLines) {
$authorizedDevice = ($deviceLines[0] -split '\s+')[0]
Write-Host "[OK] Container ADB Connected & Authorized to $authorizedDevice!" -ForegroundColor Green
break
}
Start-Sleep -Seconds 2
Start-Sleep -Seconds 1
$retryCount++
}

if ($retryCount -ge 15) {
Write-Host "⚠️ Warning: ADB connection timed out inside container. You may need to accept an RSA auth prompt on phone screen." -ForegroundColor Yellow
if (-not $authorizedDevice) {
Write-Host "[WARN] ADB connection timed out inside container. You may need to accept an RSA auth prompt on phone screen." -ForegroundColor Yellow
}

# Verify container status if running
if (docker ps -q -f name="$ContainerName" 2>$null) {
Write-Host "`n[ADB] Verifying Container ADB devices list:" -ForegroundColor Cyan
docker exec -u $User $ContainerName adb devices
}
} else {
Write-Host "⏭️ Skipping ADB configuration." -ForegroundColor Cyan
Write-Host "[ADB] Skipping ADB configuration." -ForegroundColor Cyan
}

Write-Host "`n🎯 All systems go!" -ForegroundColor Magenta
Read-Host "🛑 PRESS ENTER TO TEARDOWN ENVIRONMENT 🛑"
Write-Host "`n[Ready] All systems go!" -ForegroundColor Magenta
Write-Host "[Ready] Environment ready! You can now run Flutter apps directly inside your container or IDE." -ForegroundColor Green
Read-Host "[STOP] PRESS ENTER TO TEARDOWN ENVIRONMENT [STOP]"

Write-Host "🗑️ Tearing down..." -ForegroundColor Cyan
Write-Host "[Cleanup] Tearing down..." -ForegroundColor Cyan

if ($Mode -eq "Emulator") {
Write-Host "🛑 Terminating emulator..." -ForegroundColor Cyan
Write-Host "[Cleanup] Terminating emulator..." -ForegroundColor Cyan
adb -e emu kill 2>$null
Start-Sleep -Seconds 3
if ($EmulatorProc) { Stop-Process -Id $EmulatorProc.Id -Force -ErrorAction SilentlyContinue }
Expand All @@ -136,37 +194,4 @@ if ($Mode -ne "Skip") {
docker exec -u $User $ContainerName adb kill-server 2>$null
}

Write-Host "✅ Environment sanitized. gg." -ForegroundColor Green

# SIG # Begin signature block
# MIIFfQYJKoZIhvcNAQcCoIIFbjCCBWoCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCDExE0VmBcJhrmd
# u6wbjbci3pk4NvreeYH+8iNVZ/RTUKCCAvowggL2MIIB3qADAgECAhAmKGzz2Y/i
# k00s+ReTOHmTMA0GCSqGSIb3DQEBCwUAMBMxETAPBgNVBAMMCGRldmFrZXN1MB4X
# DTI2MDYwNzEzNTA0MloXDTI3MDYwNzE0MTA0MlowEzERMA8GA1UEAwwIZGV2YWtl
# c3UwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDEoLmTS8czHXtaQFpw
# 6w6gmTU9ciThCmc2H78V47zO+J+3NrdSHRx5jqT/liWIQ8konlM+DiozDx4TXz8U
# LBbI1UJTR7lbTUJayzy7d59NzVD9YKLBvfw/KAleMFaAIPbM1xMfZnHreppsnMMj
# rh/N1XmO7/0sLa2F9vV4xGhM17b24U/bnozmP3Gtm+kxO5j4XCr1vX3H9JDcBAPl
# Cuu3YQASdN/iOtLZ4Qu25R8onqzYF4vv4pFta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n9UzGOf8mRD/RDcZgvHFV
# aPeACodp1CLzMgghFx8t0V6TP+kWlbDlg5NRRtxwj1zEsBMZ3tsrijTE7PtttMIg
# UAc0f9JAZGJEpixxPoUHPUN+ejYJQ1xzpPvrIUcAEr7Aa+6lxV73dWn+bNgdNOqo
# BQcMwKj1RPLAVl8m6mvCd2g=
# SIG # End signature block
Write-Host "[OK] Environment sanitized." -ForegroundColor Green
9 changes: 9 additions & 0 deletions .devcontainer/devcontainer-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
"version": "1.10.2",
"resolved": "ghcr.io/devcontainers/features/docker-outside-of-docker@sha256:661b0fd4c57a7e0bd14d99701ab9bc824719678a9319118728f8d9c59441b7e4",
"integrity": "sha256:661b0fd4c57a7e0bd14d99701ab9bc824719678a9319118728f8d9c59441b7e4"
}
}
}
Loading
Loading