Execute standalone Dart shell scripts
Note: Fork of mezoni's dartx. Special thanks to him!
dscript
can be using pub:
pub global activate dscript
Make sure, it is installed in your path:
dscript version
Lookup dscript
command documentation:
dscript help
Create a Dart standalone script:
list.dart
import 'dart:io';
main() async {
final dir = Directory.current;
await for(FileSystemEntity entity in dir.list()) {
final FileStat stat = await entity.stat();
if(stat.type == FileSystemEntityType.FILE) {
print(entity.uri.pathSegments.last);
} else if (stat.type == FileSystemEntityType.DIRECTORY) {
print(entity.uri.pathSegments.reversed.elementAt(1));
}
}
}
Execute it:
dscript list.dart
The following screen cast shows how to execute a Dart standalone shell script using dscript
:
dscript
allows embedding pubspec in the scripts itself.
/*
@pubspec.yaml
name: list
*/
import 'dart:io';
main() async {
final dir = Directory.current;
await for(FileSystemEntity entity in dir.list()) {
final FileStat stat = await entity.stat();
if(stat.type == FileSystemEntityType.FILE) {
print(entity.uri.pathSegments.last);
} else if (stat.type == FileSystemEntityType.DIRECTORY) {
print(entity.uri.pathSegments.reversed.elementAt(1));
}
}
}
Execute it:
dscript list.dart
We can leverage embedded pubspec to use external packages from pub.dartlang.org or github.
ok.dart
/*
@pubspec.yaml
name: ok
dependencies:
zenity:
*/
import 'package:zenity/zenity.dart';
main() async {
final bool isOk = await Zenity.showQuestionMessage(
title: 'Hello!', text: 'Are you feeling ok?');
if(isOk) print(':)');
else print(':(');
}
Execute it:
dscript ok.dart
math.dart
/*
@pubspec.yaml
name: calc
*/
import 'dart:io';
import 'package:calc/calc.dart';
void printUsage() {
print('calc arg1 operator arg2');
print('Supported operators:');
print(' + : Addition');
print(' - : Subtraction');
print(' * : Multiplication');
print(' / : Division');
}
main(List<String> arguments) {
if(arguments.length != 3) {
printUsage();
exit(1);
}
final int a = int.parse(arguments[0]);
final int b = int.parse(arguments[0]);
Function op;
switch (arguments[1]) {
case '+':
op = add;
break;
case '-':
op = sub;
break;
case '*':
op = mul;
break;
case '/':
op = div;
break;
default:
print('Invalid operator!\n');
printUsage();
exit(1);
}
int res = op(a, b);
print('=> $res');
}
lib/calc.dart
int add(int a, int b) => a + b;
int sub(int a, int b) => a - b;
int mul(int a, int b) => a * b;
int div(int a, int b) => a ~/ b;
Execute it:
dscript math.dart 20 + 5
Shebangs can be used to execute a Dart script directly.
say_hello.dart
#! /usr/bin/env dscript
main() {
print('Hello!');
}
Make it executable:
chmod ug+x say_hello.dart
Execute it:
./say_hello.dart
Put it in system PATH
and use it like any other shell script!