import 'dart:io'; class Shell { static Future cp(String source, String destination) { return _Cmd.run('cp', ['-Rf', source, destination]); } static Future mkdir(String destination) { return _Cmd.run('mkdir', ['-p', destination]); } } class _Cmd { static Future run( String cmd, List args, { bool throwOnError = true, String? processWorkingDir, }) async { final result = await Process.run(cmd, args, workingDirectory: processWorkingDir, runInShell: true); if (throwOnError) { _throwIfProcessFailed(result, cmd, args); } return result; } static void _throwIfProcessFailed( ProcessResult pr, String process, List args, ) { if (pr.exitCode != 0) { final values = { 'Standard out': pr.stdout.toString().trim(), 'Standard error': pr.stderr.toString().trim() }..removeWhere((k, v) => v.isEmpty); String message; if (values.isEmpty) { message = 'Unknown error'; } else if (values.length == 1) { message = values.values.single; } else { message = values.entries.map((e) => '${e.key}\n${e.value}').join('\n'); } throw ProcessException(process, args, message, pr.exitCode); } } }