Snippets
Somewhere to put various code snippets I use at least semi-regularly
Dart/Flutter
Update all dependencies
flutter pub upgrade --major-versions --tighten
Clear Web Cache
Replace in index.html
:
<script src="flutter_bootstrap.js" async></script>
with
<script id="flutterBootstrap" async></script>
<script>
document.getElementById('flutterBootstrap').src = "flutter_bootstrap.js?v="+{{flutter_service_worker_version}};
</script>
Terminal
Laravel Flakey Tests
Sometimes a test is set up incorrectly in a way where it is not apparent until it fails down the line, such as having a factory which populates a field with a random number between 1...100
, and the test checks object.myNumber > 10
, which will fail 1% of the time. Ideally the test shouldn’t be set up this way in the first place, but it happens, and this helps when debugging as I can just throw in a conditional breakpoint/log and have check all the fields when the test fails.
Do { $output = php artisan test --filter=test_my_test; Write-Output $output; } Until ($output -Match "failed")
Git delete branches
Used when I want to prune all branches except some specific ones. Remove -r
for local.
git branch -r | grep -v -e 'development' -e 'staging' -e 'main' | xargs git branch -r -d
Database
Drop all test databases
Simple SQL script to drop testing databases from my local environment
SELECT CONCAT('DROP DATABASE `',schema_name,'` ;') AS `-- stmt`
FROM information_schema.schemata
WHERE schema_name LIKE 'testing\_%' ESCAPE '\\'
ORDER BY SCHEMA_NAME
CI/CD
Github Flutter PR
Run Flutter analyze and tests when PR opened.
name: Pull Request Tests
on:
pull_request:
branches:
- main
types: [synchronize, opened, reopened, ready_for_review]
jobs:
Run-Flutter-Tests:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
channel: "stable"
cache: true
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:"
cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:"
architecture: x64
- run: flutter analyze
- run: flutter test
Github Flutter Package deploy to pub.dev on tag
Run Flutter tests, then build and publish to pub.dev, on tag which follows semantic versioning conventions. Replaces the pubspec.yaml::version
with the version in the tag before build.
name: Publish to pub.dev
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
jobs:
main:
permissions:
id-token: write
runs-on: ubuntu-latest
environment: pub.dev
steps:
- uses: actions/checkout@v3
- name: Replace version number in flutter/pubspec.yaml
run: |
tag=${{ github.ref_name }}
version=$(echo $tag | sed 's/^v//')
sed -i -r "s/version: [0-9]+\.[0-9]+\.[0-9]+*/version: $version/g" pubspec.yaml
# This action adds a token needed for pub.dev
- name: Install Dart
uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: "stable"
cache: true
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:"
cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:"
architecture: x64
- name: Install dependencies
run: flutter pub get
- name: Run tests
run: |
flutter analyze
flutter test
- name: Publish to pub.dev
run: flutter pub publish
Build Flutter Web
Simple (unoptimised) script to pull a version/branch of Flutter (in this case v3.22.3
) and build it for web in release mode.
echo "Cloning latest stable Flutter..."
git clone https://github.com/flutter/flutter.git -b 3.22.3 --depth 1
echo "Adding Flutter to path"
export PATH="$PATH:`pwd`/flutter/bin"
echo "Running Flutter precache..."
flutter precache --web
echo "Installing dependencies..."
flutter pub get
echo "Generating l10n..."
flutter pub run intl_utils:generate
echo "Starting Build..."
flutter build web --release