// ignore_for_file: avoid_init_to_null import 'dart:async'; import 'dart:io'; import 'package:dart_framework/data/isolate_data.dart'; import 'package:dart_framework/platform/isolate_manager.dart'; import 'package:oto_cli/cli/cli.dart'; import 'package:oto_cli/cli/commands/command_base.dart'; import 'package:dart_framework/utils/system_util.dart'; import 'package:oto_cli/oto/application.dart'; import 'package:oto_cli/oto/data/command_data.dart'; class CommandExe extends CommandBase { late IsolateHandler _handler; bool _complete = false; bool _ready = false; @override Map> get arguments => { '-j': ["Used when it's a Jenkins build."], '-t': ["This is the setup for testing."], '-f {file path}': ["Run the yaml file to process."] }; @override String get name => 'exe'; @override Future execute(List parameters) async { for (var i = 0; i < parameters.length; ++i) { var item = parameters[i]; if (item.startsWith('-j')) { _handler = IsolateManager.create(IsolateExe(BuildType.jenkins)); await isComplete(); break; } else if (item.startsWith('-t')) { _handler = IsolateManager.create(IsolateExe(BuildType.test)); await isComplete(); break; } else if (item.startsWith('-f')) { var file = ''; if (parameters.length > i + 1) { file = parameters[i + 1]; } else { file = item.replaceFirst('-f', '').trimLeft(); } await startYaml(file); break; } else { printHelp(); } } return simpleFuture; } Future executeScheduler( DataBuild build, bool logEnable, String logPath) async { _handler = IsolateManager.create(IsolateExe.scheduler(build, logEnable, logPath)); await isComplete(); _handler.exit(); return simpleFuture; } Future isComplete() async { _ready = false; _complete = false; _handler.addEventListener(IsolateEvent.ready, (data) => _ready = true); _handler.addListener('complete', (data) => _complete = true); while (!(_complete && _ready)) { await Future.delayed(const Duration(milliseconds: 200)); } return simpleFuture; } Future startYaml(String target) async { var file = File(target); if (file.existsSync()) { _handler = IsolateManager.create(IsolateExe(BuildType.file, yamlContent: file.readAsStringSync( /*encoding: Platform.isWindows ? eucKr : systemEncoding*/))); await isComplete(); } else { await CLI.print([ CLI.style('There are no files in path "$target"', color: Color.red) ]); } return simpleFuture; } @override String getDescription() { return ':::: Read documents written in yaml to do process task.'; } } class IsolateExe extends IsolateBase { late BuildType _buildType; bool _logEnable = true; DataBuild? _build = null; String? _yamleContent = null; String? _logPath = null; IsolateExe(this._buildType, {String? yamlContent, DataBuild? build, bool logEnable = true, String? logPath}) { _yamleContent = yamlContent; _build = build; _logEnable = logEnable; _logPath = logPath; } factory IsolateExe.scheduler( DataBuild build, bool logEnable, String logPath) { return IsolateExe(BuildType.scheduler, build: build, logEnable: logEnable, logPath: logPath); } @override void onReady(Object? initData) async { await Application.instance.build(_buildType, yamlContent: _yamleContent, buildData: _build, logEnable: _logEnable, logPath: _logPath); send(IsoMessege('complete')); } }