dart_spawner 1.1.0 copy "dart_spawner: ^1.1.0" to clipboard
dart_spawner: ^1.1.0 copied to clipboard

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package into the current Dart VM.

dart_spawner #

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package, using its dependencies, into the current Dart VM.

Usage #

You can run a Dart script (from a String) into a new Isolate:

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner();
  print(spawner);

  var script = r'''
    void main(List<String> args) {
      print('>> From Script! Args: $args');
    }
  ''';

  var spawned = await spawner.spawnDart(script, ['a', 'b', 'c']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}

Output:

DartSpawner{ id: 1, spawned: false, finished: false }
>> From Script! Args: [a, b, c]
Spawned: SpawnedIsolate{ id: 1, type: <script>, finished: false, projectDirectory: Directory: '/path/to/current/dart_project' }
Exit code: 0

Spawning a Dart file inside the current Dart project: #

To run a Dart File inside the current Dart project (used in the main Dart entry point).

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner();
  print(spawner);

  var file = await spawner.projectSubFile('test/hello-world.dart');
  print('Spawning file: $file');

  var spawned = await spawner.spawnDart(file, ['x', 'y']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}

Spawning a Dart file in ANOTHER Dart project: #

You can also run a Dart script/file that exists in another Dart project/package, passing the project directory to the DartSpawner constructor:

import 'dart:io';

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner(
    directory: Directory('/path/to/another/dart_project/'),
  );

  print(spawner);

  var file = await spawner.projectSubFile('example/project_example.dart');
  print('Spawning file: $file');

  var spawned = await spawner.spawnDart(file, ['some', 'args']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}
  • NOTE: It will use the dependencies of the target project and not the ones used in the current Isolate.

Dart VM #

Note that DartSpawner uses Isolate.spawnUri to run a Dart Entry Point (script/file/Uri). This is not supported outside the Dart VM or in a compiled dart file (dart compile exe foo.dart)!

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Author #

Graciliano M. Passos: gmpassos@GitHub.

License #

Dart free & open-source license.

12
likes
160
points
1.88k
downloads

Publisher

unverified uploader

Weekly Downloads

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package into the current Dart VM.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

async_extension, path, yaml

More

Packages that depend on dart_spawner