dscript 2.0.0

dscript #

Execute standalone Dart shell scripts

Note: Fork of mezoni's dartx. Special thanks to him!

Installation #

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

Usage #

Execute standalone Dart script #

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:

asciicast

Embedded pubspec #

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

Scripts with dependencies #

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

asciicast

Multi file scripts #

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

asciicast

Shebang #

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!

Changelog #

1.0.3 #

  • Moved dartx LICENSE to root folder

1.0.2 #

  • Attribution to mezoni

1.0.1 #

  • Shebang fix

1.0.0 #

  • Execute standalone Dart script

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate dscript

2. Use it

The package has the following executables:


$ dscript

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dscript: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dscript/dscript.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
39
Learn more about scoring.

We analyzed this package on Aug 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:dscript/dscript.dart with components: io.

Health suggestions

Fix lib/src/args.dart. (-1.99 points)

Analysis of lib/src/args.dart reported 4 hints:

line 34 col 20: Use = to separate a named parameter from its default value.

line 34 col 47: Use = to separate a named parameter from its default value.

line 49 col 13: DO use curly braces for all flow control structures.

line 54 col 13: DO use curly braces for all flow control structures.

Fix bin/dscript.dart. (-1 points)

Analysis of bin/dscript.dart reported 2 hints:

line 21 col 26: Use isEmpty instead of length

line 90 col 7: Use isEmpty instead of length

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://github.com/tejainece/dscript was unreachable.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and dscript.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
path ^1.6.2 1.6.4
Dev dependencies
test ^1.5.0