dscript 1.0.3

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 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: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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';
  
Version Uploaded Documentation Archive
1.0.3 Dec 17, 2017 Go to the documentation of dscript 1.0.3 Download dscript 1.0.3 archive
1.0.2 Dec 17, 2017 Go to the documentation of dscript 1.0.2 Download dscript 1.0.2 archive
1.0.1 Dec 17, 2017 Go to the documentation of dscript 1.0.1 Download dscript 1.0.1 archive
1.0.0 Dec 17, 2017 Go to the documentation of dscript 1.0.0 Download dscript 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0