From 807800e3c1ce0fa985253a36e6ecf8a3cb8abff7 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Wed, 26 Apr 2023 15:02:43 -0400 Subject: [PATCH] feat(shorebird_cli): initial support for windows command line (#369) --- bin/shorebird.bat | 21 +++++++++++ bin/shorebird.ps1 | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 bin/shorebird.bat create mode 100644 bin/shorebird.ps1 diff --git a/bin/shorebird.bat b/bin/shorebird.bat new file mode 100644 index 00000000..10f8ff0e --- /dev/null +++ b/bin/shorebird.bat @@ -0,0 +1,21 @@ +@ECHO OFF + +REM Detect which PowerShell executable is available on the Host +REM PowerShell version <= 5: PowerShell.exe +REM PowerShell version >= 6: pwsh.exe +WHERE /Q pwsh.exe && ( + SET powershell_executable=pwsh.exe +) || WHERE /Q PowerShell.exe && ( + SET powershell_executable=PowerShell.exe +) || ( + ECHO Error: PowerShell executable not found. 1>&2 + ECHO Either pwsh.exe or PowerShell.exe must be in your PATH. 1>&2 + EXIT 1 +) + +SET CurrentDirectory=%~dp0 +SET PowerShellScriptPath=%CurrentDirectory%shorebird.ps1 + +REM Pass up to four arguments to the PowerShell script +REM This number is arbitrary and can be increased if needed +& %powershell_executable% -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' %1% %2% %3% %4%"; diff --git a/bin/shorebird.ps1 b/bin/shorebird.ps1 new file mode 100644 index 00000000..928a51de --- /dev/null +++ b/bin/shorebird.ps1 @@ -0,0 +1,96 @@ +# This is the windows equivalent of the `third_party/flutter/bin/internal/shared.sh` script +# compiles `shorebird_cli/bin/shorebird_cli.dart` to `bin/cache/shorebird.shapshot` + +# We are running from $shorebirdRootDir\bin +$shorebirdBinDir = (Get-Item $PSScriptRoot).FullName +$shorebirdRootDir = (Get-Item $shorebirdBinDir\..\).FullName +$shorebirdCacheDir = [IO.Path]::Combine($shorebirdRootDir, "bin", "cache") +$shorebirdCliDir = [IO.Path]::Combine($shorebirdRootDir, "packages", "shorebird_cli") +$snapshotPath = [IO.Path]::Combine($shorebirdCacheDir, "shorebird.snapshot") +$stampPath = [IO.Path]::Combine($shorebirdCacheDir, "shorebird.stamp") +$flutterPath = [IO.Path]::Combine($shorebirdCacheDir, "flutter") +$flutter = [IO.Path]::Combine($shorebirdCacheDir, "flutter", "bin", "flutter.bat") +$shorebirdScript = [IO.Path]::Combine($shorebirdCliDir, "bin", "shorebird.dart") +$dart = [IO.Path]::Combine($flutterPath, "bin", "cache", "dart-sdk", "bin", "dart.exe") + +function Update-Flutter { + if (!(Test-Path $flutterPath)) { + Write-Output "Cloning flutter, this may take a bit..." + git clone --filter=tree:0 https://github.com/shorebirdtech/flutter.git --no-checkout "$flutterPath" *> $null + } + else { + git -C "$flutterPath" fetch *> $null + } + + $flutterVersion = Get-Content "$shorebirdBinDir\internal\flutter.version" + + # -c to avoid printing a warning about being in a detached head state. + git -C "$flutterPath" -c advice.detachedHead=false checkout "$flutterVersion" *> $null + + # Set FLUTTER_STORAGE_BASE_URL=https://download.shorebird.dev and execute + # a `flutter` command to trigger a download of Dart, etc. + $env:FLUTTER_STORAGE_BASE_URL = 'https://download.shorebird.dev'; + & $flutter --version + Remove-Item Env:\FLUTTER_STORAGE_BASE_URL +} + +function Update-Shorebird { + # Invalidate cache if: + # * snapshotFile is not a file, or + # * stampFile is not a file, or + # * stampFile is an empty file, or + # * Contents of stampFile contains a different git hash than HEAD, or + # * pubspec.yaml last modified after pubspec.lock + $snapshotFile = [System.IO.FileInfo] $snapshotPath + $stampFile = [System.IO.FileInfo] $stampPath + $pubspecFile = [System.IO.FileInfo] "$shorebirdCliDir\pubspec.yaml" + $pubspecLockFile = [System.IO.FileInfo] "$shorebirdCliDir\pubspec.lock" + + Push-Location $shorebirdRootDir + $compileKey = & { git rev-parse HEAD } -split + Pop-Location + + Write-Debug "Checking whether shorebird needs to be rebuilt" + + Write-Debug "compileKey is $compileKey" + Write-Debug "Snapshot file exists: $($snapshotFile.Exists)" + Write-Debug "Stamp file: $($stampFile)" + Write-Debug "Stamp file exists: $($stampFile.Exists)" + Write-Debug "pubspec.yaml file exists: $($pubspecFile.Exists)" + Write-Debug "pubspec.lock file exists: $($pubspecLockFile.Exists)" + if ($stampFile.Exists) { + Write-Debug "contents of stamp file: $(Get-Content $stampFile)" + } + + $invalidateCache = !$snapshotFile.Exists -or ` + !$stampFile.Exists -or ` + $stampFile.Length -eq 0 -or ` + (Get-Content $stampFile) -ne $compilekey -or ` + $pubspecFile.LastWriteTime -gt $pubspecLockFile.LastWriteTime + + Write-Debug "Invalidate cache: $invalidateCache" + + if ($invalidateCache) { + Update-Flutter + + Push-Location $shorebirdCliDir + & $dart pub upgrade + Pop-Location + + Write-Output "Compiling shorebird..." + + & $dart --verbosity=error --disable-dart-dev --snapshot="$snapshotPath" ` + --snapshot-kind="app-jit" --packages="$shorebirdCliDir/.dart_tool/package_config.json" ` + --no-enable-mirrors "$shorebirdScript" > $null + + Write-Debug "writing $compileKey to $stampPath" + Set-Content -Path $stampPath -Value $compileKey + } + else { + Write-Debug "Shorebird is up-to-date" + } +} + +Update-Shorebird + +& $dart --disable-dart-dev --packages="$shorebirdCliDir\.dart_tool\package_config.json" "$snapshotPath" $args