feat: version 0.1.4

see CHANGELOG.md for full details
This commit is contained in:
Chen Asraf
2022-08-08 14:35:06 +03:00
parent 942827e367
commit a1eb23e447
5 changed files with 80 additions and 33 deletions

View File

@@ -1,3 +1,10 @@
## 0.1.4
- Rename bin from `dartsc` to `scr`
- Update help usage print format
- Fix bug when pubspec.yaml is missing
- Add line_length config
## 0.1.3
- Update code formatting

View File

@@ -1,17 +1,4 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
# script_runner
# Dart Script Runner
A general script runner for dart projects - run all your project-related scripts in a portable,
simple config.
@@ -24,7 +11,7 @@ simple config.
flexibility of creating configurable script execution orders &amp; dependencies.
- **Self-documenting:** Removes the need to document where and how to load different types of
scripts on your project. Unify all your runners into 1 config that lets you freely call everything
on-demand, and also supply an auto-generated documentation using `dartsc -h`.
on-demand, and also supply an auto-generated documentation using `scr -h`.
## Getting started
@@ -34,8 +21,12 @@ You can install this package globally for the most easy usage.
pub global activate script_runner
```
Once activated, you can use the supplied `dartsc` executable to directly call scripts from any
project you are currently in.
Once activated, you can use the supplied `scr` executable to directly call scripts from any project
you are currently in.
```shell
scr my-script ...args
```
## Usage
@@ -47,7 +38,7 @@ This is the structure of a config:
```yaml
# only use this key if you are inside pubspec.yaml. Otherwise, it's not needed
script_runner:
# The shell to run all of the scripts with. (optional)
# The shell to run all of the scripts with. (optional - defaults to OS shell)
shell: /bin/sh
# The current working directory to run the scripts in. (optional)
cwd: .
@@ -55,6 +46,9 @@ script_runner:
env:
MY_ENV: my-value
# ...
# The amount of characters to allow before considering the line over when
# printing help usage (scr -h)
line_length: 80
# Scripts support either a short-format config, or a more verbose one with
# more possible argument to pass to each script.
# Scripts can reference other scripts, e.g. `script1` can reference
@@ -69,7 +63,7 @@ script_runner:
# more verbose config, for extra configuration
- name: my-script
# Optional - will be used in docs when using `dartsc -h`.
# Optional - will be used in docs when using `scr -h`.
description: Run my script
# Optional - overrides the root-level config
cwd: .
@@ -85,3 +79,9 @@ script_runner:
- arg2
- arg3
```
For this config, running `scr my-script` will run the appropriate script, filling the env and
changing the working directory as needed.
More arguments can be passed during the call to the script, which will then be piped to the original
`cmd`.

View File

@@ -1,5 +1,6 @@
library script_runner;
import 'dart:math' as math;
import 'package:script_runner/src/runnable_script.dart';
import 'package:yaml/yaml.dart' as yaml;
import 'package:path/path.dart' as path;
@@ -24,6 +25,9 @@ class ScriptRunnerConfig {
/// If left `null`, defaults to no overrides.
final Map<String, String>? env;
/// The length of the lines in the help description, which causes text overflowing to the next line when necessary.
final int lineLength;
final FileSystem? _fileSystem;
/// The filesystem used for loading scripts.
@@ -40,6 +44,7 @@ class ScriptRunnerConfig {
this.workingDir,
this.env,
FileSystem? fileSystem,
this.lineLength = 80,
}) : _fileSystem = fileSystem ?? LocalFileSystem();
/// A map of the registered scripts, keyed by name.
@@ -60,7 +65,7 @@ class ScriptRunnerConfig {
static Future<ScriptRunnerConfig> get([FileSystem? fileSystem]) async {
final fs = fileSystem ?? LocalFileSystem();
final source =
(await _getPubspecScripts(fs)) ?? (await _getConfigScripts(fs));
(await _getPubspecConfig(fs)) ?? (await _getCustomConfig(fs));
if (source == null) {
throw StateError(
@@ -77,22 +82,31 @@ class ScriptRunnerConfig {
env: env,
workingDir: source['cwd'],
fileSystem: fileSystem,
lineLength: source['line_length'] ?? 80,
);
}
static Future<yaml.YamlMap?> _getPubspecScripts(FileSystem fileSystem) async {
static Future<yaml.YamlMap?> _getPubspecConfig(FileSystem fileSystem) async {
final filePath =
path.join(fileSystem.currentDirectory.path, 'pubspec.yaml');
final pubspec = await fileSystem.file(filePath).readAsString();
final file = fileSystem.file(filePath);
if (!file.existsSync()) {
return null;
}
final pubspec = await file.readAsString();
final yaml.YamlMap contents = yaml.loadYaml(pubspec);
final yaml.YamlMap? conf = contents['script_runner'];
return conf;
}
static Future<yaml.YamlMap?>? _getConfigScripts(FileSystem fileSystem) async {
static Future<yaml.YamlMap?>? _getCustomConfig(FileSystem fileSystem) async {
final filePath =
path.join(fileSystem.currentDirectory.path, 'script_runner.yaml');
final pubspec = await fileSystem.file(filePath).readAsString();
final file = fileSystem.file(filePath);
if (!file.existsSync()) {
return null;
}
final pubspec = await file.readAsString();
final yaml.YamlMap? conf = yaml.loadYaml(pubspec);
return conf;
}
@@ -109,14 +123,39 @@ class ScriptRunnerConfig {
/// Prints usage help text for this config
void printUsage() {
print('Dart Script Runner');
print(' Usage: dartsc script_name ...args');
print(' Usage: scr script_name ...args');
print('');
print(' ${'-h, --help'.padRight(16, ' ')} Print this help message');
var maxLen = 0;
for (final scr in scripts) {
print(' ${scr.name.padRight(16, ' ')} ${scr.description ?? [
scr.cmd,
...scr.args
].join(' ')}');
maxLen = math.max(maxLen, scr.name.length);
}
final padLen = maxLen + 6;
print(' ${'-h, --help'.padRight(padLen, ' ')} Print this help message\n');
for (final scr in scripts) {
final lines = _chunks(
scr.description ?? [scr.cmd, ...scr.args].join(' '),
80 - padLen,
);
print(' ${scr.name.padRight(padLen, ' ')} ${lines.first}');
for (final line in lines.sublist(1)) {
print(' ${''.padRight(padLen, ' ')} ${line}');
}
print('');
}
}
}
List<String> _chunks(String str, int maxLen) {
final words = str.split(' ');
final chunks = <String>[];
var chunk = '';
for (final word in words) {
if (chunk.length + word.length > maxLen) {
chunks.add(chunk);
chunk = '';
}
chunk += '$word ';
}
chunks.add(chunk);
return chunks;
}

View File

@@ -96,7 +96,7 @@ class RunnableScript {
final shell = config.shell ?? _getPlatformShell();
final preRun = preloadScripts
.map((d) => 'alias ${d.name}=\'dartsc ${d.name}\'')
.map((d) => 'alias ${d.name}=\'scr ${d.name}\'')
.join(';');
final origCmd = [cmd, ...effectiveArgs.map(_wrap)].join(' ');
final passCmd = '$preRun; eval \'$origCmd\'';

View File

@@ -18,10 +18,11 @@ dev_dependencies:
script_runner:
shell: /bin/zsh
line_length: 80
scripts:
- name: echo1
cmd: echo "Hello World"
description: test script foobar
description: Interdum a scelerisque arcu felis taciti ligula pellentesque curabitur, suspendisse adipiscing quisque sed luctus elementum in imperdiet id, praesent enim sem justo sapien diam nec. Quisque erat risus sagittis penatibus per, vehicula sociosqu cubilia convallis, sollicitudin scelerisque cras aptent. Natoque ornare dictumst netus litora mollis suspendisse cubilia proin morbi primis consequat eu massa, cursus non urna ridiculus dolor duis tempus ut nam velit lacus viverra. A interdum senectus eu mus leo aptent facilisi augue tristique ante purus condimentum pulvinar porta viverra morbi, et tellus gravida porttitor non euismod suscipit neque egestas praesent arcu luctus pharetra fusce. Luctus mauris a venenatis tempus cras ante efficitur massa ultricies mollis lacus, volutpat nisi lacinia himenaeos facilisi in aliquet sodales purus integer vitae quisque, libero torquent enim mattis placerat tortor mi dignissim viverra sem.
- name: echo2
cmd: echo
args:
@@ -35,4 +36,4 @@ script_runner:
- auto-fix: dart fix --apply
executables:
dartsc: script_runner
scr: script_runner