dscript 1.0.3

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 50

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

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

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages 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

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
50

Platforms

Detected platforms: Flutter, other

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

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dscript.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
path ^1.5.1 1.5.1
Dev dependencies
test ^0.12.0