mirror of
https://github.com/chenasraf/dart_script_runner.git
synced 2026-05-17 17:48:03 +00:00
chore: clean up files
test: add basic tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import 'package:script_runner/src/script_runner_base.dart';
|
||||
import 'package:script_runner/src/base.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
final scriptCmd = args.first;
|
||||
final scriptArgs = args.sublist(1);
|
||||
await runScript(scriptCmd, scriptArgs);
|
||||
return runScript(scriptCmd, scriptArgs);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'package:script_runner/script_runner.dart';
|
||||
|
||||
void main() {
|
||||
// var awesome = Awesome();
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
/// Support for doing something awesome.
|
||||
///
|
||||
/// More dartdocs go here.
|
||||
library script_runner;
|
||||
|
||||
import 'package:script_runner/src/script_runner_base.dart' as _base;
|
||||
|
||||
export 'src/script_runner_base.dart' show runScript, ScriptRunnerConfig, RunnableScript;
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
final scriptCmd = args.first;
|
||||
final scriptArgs = args.sublist(1);
|
||||
await _base.runScript(scriptCmd, scriptArgs);
|
||||
}
|
||||
export 'src/base.dart' show runScript;
|
||||
export 'src/config.dart';
|
||||
export 'src/runnable_script.dart';
|
||||
|
||||
14
lib/src/base.dart
Normal file
14
lib/src/base.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:script_runner/src/config.dart';
|
||||
|
||||
Future<void> runScript(String entryName, List<String> args) async {
|
||||
final config = await ScriptRunnerConfig.get();
|
||||
final entry = config.scriptsMap[entryName];
|
||||
if (entry == null) {
|
||||
throw Exception(
|
||||
'No script named "$entryName" found.\n'
|
||||
'Available scripts: ${config.scriptsMap.keys.join(', ')}',
|
||||
);
|
||||
}
|
||||
|
||||
return entry.run(args);
|
||||
}
|
||||
67
lib/src/config.dart
Normal file
67
lib/src/config.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
library script_runner;
|
||||
|
||||
import 'dart:io' as io;
|
||||
import 'package:script_runner/src/runnable_script.dart';
|
||||
import 'package:yaml/yaml.dart' as yaml;
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/local.dart';
|
||||
|
||||
class ScriptRunnerConfig {
|
||||
final List<RunnableScript> scripts;
|
||||
final String? shell;
|
||||
final String? workingDir;
|
||||
final Map<String, String>? env;
|
||||
|
||||
ScriptRunnerConfig({
|
||||
required this.scripts,
|
||||
this.shell,
|
||||
this.workingDir,
|
||||
this.env,
|
||||
});
|
||||
|
||||
Map<String, RunnableScript> get scriptsMap => Map.fromIterable(
|
||||
scripts,
|
||||
key: (element) => (element as RunnableScript).name,
|
||||
);
|
||||
|
||||
static Future<ScriptRunnerConfig> get([FileSystem? fileSystem]) async {
|
||||
final fs = fileSystem ?? LocalFileSystem();
|
||||
final source = (await _getPubspecScripts(fs)) ?? (await _getConfigScripts(fs));
|
||||
|
||||
if (source == null) {
|
||||
throw StateError('Must provide scripts in either pubspec.yaml or script_runner.yaml');
|
||||
}
|
||||
|
||||
final env = <String, String>{}..addAll(
|
||||
(source['env'] as yaml.YamlMap?)?.cast<String, String>() ?? {},
|
||||
);
|
||||
|
||||
return ScriptRunnerConfig(
|
||||
shell: source['shell'],
|
||||
scripts: _parseScriptsList(source['scripts']),
|
||||
env: env,
|
||||
workingDir: source['cwd'],
|
||||
);
|
||||
}
|
||||
|
||||
static Future<yaml.YamlMap?> _getPubspecScripts(FileSystem fileSystem) async {
|
||||
final filePath = path.join(fileSystem.currentDirectory.path, 'pubspec.yaml');
|
||||
final pubspec = await fileSystem.file(filePath).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 {
|
||||
final filePath = path.join(fileSystem.currentDirectory.path, 'script_runner.yaml');
|
||||
final pubspec = await fileSystem.file(filePath).readAsString();
|
||||
final yaml.YamlMap? conf = yaml.loadYaml(pubspec);
|
||||
return conf;
|
||||
}
|
||||
|
||||
static List<RunnableScript> _parseScriptsList(yaml.YamlList scriptsRaw) {
|
||||
final scripts = scriptsRaw.map((script) => RunnableScript.fromYamlMap(script)).toList();
|
||||
return scripts.map((s) => s..dependencies = scripts).toList();
|
||||
}
|
||||
}
|
||||
@@ -1,69 +1,9 @@
|
||||
// ignore: no_leading_underscores_for_library_prefixes
|
||||
import 'dart:io';
|
||||
|
||||
// ignore: no_leading_underscores_for_library_prefixes
|
||||
import 'package:script_runner/src/utils.dart' as _utils;
|
||||
import 'package:script_runner/src/config.dart';
|
||||
import 'package:script_runner/src/utils.dart' as utils;
|
||||
import 'package:yaml/yaml.dart' as yaml;
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
Future<void> runScript(String entryName, List<String> args) async {
|
||||
final config = await getConfig();
|
||||
final entry = config.scriptsMap[entryName];
|
||||
if (entry == null) {
|
||||
throw Exception(
|
||||
'No script named "$entryName" found. Available scripts: ${config.scriptsMap.keys.join(', ')}');
|
||||
}
|
||||
|
||||
return entry.run(args);
|
||||
}
|
||||
|
||||
Future<ScriptRunnerConfig> getConfig() async {
|
||||
final source = (await getPubspecScripts()) ?? (await getConfigScripts());
|
||||
|
||||
if (source == null) {
|
||||
throw StateError('Must provide scripts in either pubspec.yaml or script_runner.yaml');
|
||||
}
|
||||
|
||||
return ScriptRunnerConfig(
|
||||
shell: source['shell'],
|
||||
scripts: _parseScriptsList(source['scripts']),
|
||||
);
|
||||
}
|
||||
|
||||
List<RunnableScript> _parseScriptsList(yaml.YamlList scriptsRaw) {
|
||||
final scripts = scriptsRaw.map((script) => RunnableScript.fromYamlMap(script)).toList();
|
||||
return scripts.map((s) => s..dependencies = scripts).toList();
|
||||
}
|
||||
|
||||
Future<yaml.YamlMap?> getPubspecScripts() async {
|
||||
final pubspec = await File(path.join(Directory.current.path, 'pubspec.yaml')).readAsString();
|
||||
final yaml.YamlMap contents = yaml.loadYaml(pubspec);
|
||||
final yaml.YamlMap? conf = contents['script_runner'];
|
||||
return conf;
|
||||
}
|
||||
|
||||
Future<yaml.YamlMap?>? getConfigScripts() async {
|
||||
final pubspec =
|
||||
await File(path.join(Directory.current.path, 'script_runner.yaml')).readAsString();
|
||||
final yaml.YamlMap? conf = yaml.loadYaml(pubspec);
|
||||
return conf;
|
||||
}
|
||||
|
||||
class ScriptRunnerConfig {
|
||||
final List<RunnableScript> scripts;
|
||||
final String? shell;
|
||||
|
||||
ScriptRunnerConfig({
|
||||
this.shell,
|
||||
required this.scripts,
|
||||
});
|
||||
|
||||
Map<String, RunnableScript> get scriptsMap {
|
||||
return Map.fromIterable(
|
||||
scripts,
|
||||
key: (element) => (element as RunnableScript).name,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RunnableScript {
|
||||
final String name;
|
||||
@@ -92,7 +32,7 @@ class RunnableScript {
|
||||
final rawCmd = map['cmd'] as String;
|
||||
final cmd = rawCmd.split(' ').first;
|
||||
final rawArgs = (map['args'] as List<String>?) ?? [];
|
||||
final cmdArgs = _utils.splitArgs(rawCmd.substring(cmd.length));
|
||||
final cmdArgs = utils.splitArgs(rawCmd.substring(cmd.length));
|
||||
// print('cmdArgs: $cmdArgs');
|
||||
|
||||
return RunnableScript(
|
||||
@@ -105,7 +45,8 @@ class RunnableScript {
|
||||
Future<dynamic> run(List<String> extraArgs) async {
|
||||
final effectiveArgs = args + extraArgs;
|
||||
// final argsStr = effectiveArgs.map(_wrap).join(' ');
|
||||
final shell = (await getConfig()).shell ?? '/bin/sh';
|
||||
var config = await ScriptRunnerConfig.get();
|
||||
final shell = config.shell ?? '/bin/sh';
|
||||
|
||||
final preRun = dependencies.map((d) => 'alias ${d.name}=\'dartsc ${d.name}\'').join(';');
|
||||
final origCmd = [cmd, ...effectiveArgs.map(_wrap)].join(' ');
|
||||
@@ -115,10 +56,15 @@ class RunnableScript {
|
||||
// print("Before parse $cmd $args");
|
||||
|
||||
try {
|
||||
final result = await Process.start(shell, [
|
||||
'-c',
|
||||
passCmd,
|
||||
]);
|
||||
final result = await Process.start(
|
||||
shell,
|
||||
[
|
||||
'-c',
|
||||
passCmd,
|
||||
],
|
||||
environment: config.env,
|
||||
workingDirectory: config.workingDir,
|
||||
);
|
||||
result.stdout.listen((event) {
|
||||
stdout.write(String.fromCharCodes(event));
|
||||
});
|
||||
@@ -132,7 +78,7 @@ class RunnableScript {
|
||||
ProcessException(cmd, args, 'Process exited with error code: $exitCode', exitCode);
|
||||
throw e;
|
||||
}
|
||||
} catch (e, stack) {
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ environment:
|
||||
sdk: ">=2.18.0-271.4.beta <3.0.0"
|
||||
|
||||
dependencies:
|
||||
file: ^6.1.2
|
||||
path: ^1.8.2
|
||||
yaml: ^3.1.1
|
||||
|
||||
@@ -29,6 +30,7 @@ script_runner:
|
||||
- name: combined
|
||||
cmd: echo 'test' && echo1 && echo2
|
||||
- short: echo 'this is a short script'
|
||||
- auto-fix: dart fix --apply
|
||||
|
||||
executables:
|
||||
dartsc: script_runner
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
import 'package:script_runner/script_runner.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
void main() {
|
||||
group('A group of tests', () {
|
||||
// final awesome = Awesome();
|
||||
late FileSystem fs;
|
||||
group('ScriptRunnerConfig', () {
|
||||
group('pubspec.yaml loader', () {
|
||||
setUp(() async {
|
||||
fs = MemoryFileSystem();
|
||||
final pubFile = fs.file(path.join(fs.currentDirectory.path, 'pubspec.yaml'));
|
||||
pubFile.create(recursive: true);
|
||||
await pubFile.writeAsString(
|
||||
[
|
||||
'script_runner:',
|
||||
' shell: /bin/sh',
|
||||
' scripts:',
|
||||
' - name: test',
|
||||
' cwd: .',
|
||||
' cmd: echo "hello"',
|
||||
].join('\n'),
|
||||
);
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
// Additional setup goes here.
|
||||
});
|
||||
|
||||
test('First Test', () {
|
||||
// expect(awesome.isAwesome, isTrue);
|
||||
test('Works', () async {
|
||||
final conf = await ScriptRunnerConfig.get(fs);
|
||||
expect(conf.scriptsMap['test']!.name, 'test');
|
||||
expect(conf.scriptsMap['test']!.cmd, 'echo');
|
||||
expect(conf.scriptsMap['test']!.args, ['hello']);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user