dart_spawner

Creator: coderz1093

Last updated:

Add to Cart

Description:

dart spawner

dart_spawner #










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');
}
copied to clipboard
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
copied to clipboard
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');
}

copied to clipboard
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');
}
copied to clipboard

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.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files:

Customer Reviews

There are no reviews.