Skip to content

Instantly share code, notes, and snippets.

@Luckey-Elijah
Created February 19, 2024 20:11
Show Gist options
  • Save Luckey-Elijah/db5d85104fa1a9b2ad9b5b9f431d2920 to your computer and use it in GitHub Desktop.
Save Luckey-Elijah/db5d85104fa1a9b2ad9b5b9f431d2920 to your computer and use it in GitHub Desktop.

dpad.dart

dpad is your local dart scripting environment. This is a simple guide to start using dart scripts across your machine.

Requirements

  • dart installed (I recommend using puro to manage dart/flutter versions)
  • bash/zsh-like environment (this is likely possible in other terminals but idk how to set that up and am too lazy to tell you)

Setup

These steps should only be run once for the setup.

  1. Create dpad: dart create dpad
  2. cd dpad
  3. Try dart run (optional):
    Building package executable... 
    Built dpad:dpad.
    Hello world: 42!
    
  4. dart pub global activate -s path .: makes dpad an available executable on your system
    Resolving dependencies...
    Got dependencies!
    Activated dpad 1.0.0 at path "/path/to/dpad".
    
  5. Try dart pub global run dpad (optional):
    Building package executable...
    Built dpad:dpad.
    Hello world: 42!
    
    Same entry point as the dart run (which targets dpad/bin/dpad.dart for now)
  6. Add the dpad function to you .zshrc
    dpad() {
      dart pub global run dpad:$1 ${@:2}
    }
    
  7. Restart your terminal

Using

  • Write new scripts/entry points under dpad/bin/*.dart: dpad/bin/my_new_script.dart
  • Execute them with the dpad shell function: dpad my_new_script
  • Arguments are forwarded to the void main(List<String> args) { }
  • Write shared code under dpad/lib
  • Manage your own dependencies and code analysis
@Luckey-Elijah
Copy link
Author

I find it helpful to add an environment variable in ~/.zshrc

export DPAD="path/to/dpad"

in case you might need it in dart or other scripts:

// dpad/bin/ls.dart
import 'dart:io';

import 'package:path/path.dart';
import 'package:stream_transform/stream_transform.dart';

void main() async {
  await for (final file in Directory(join(Platform.environment['DPAD']!, 'bin'))
      .list()
      .whereType<File>()
      .map((file) => file.path)
      .where((path) => extension(path) == '.dart')
      .map(basenameWithoutExtension)) {
    print(file);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment