// ignore_for_file: avoid_init_to_null import 'dart:async'; import 'dart:io'; import 'package:dart_framework/charset/euc_kr.dart'; 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'; class CommandExe extends CommandBase { late IsolateHandler _handler; bool _complete = 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; } if (item.startsWith('-t')) { _handler = IsolateManager.create(IsolateExe(BuildType.test)); await isComplete(); } 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; } } return simpleFuture; } Future isComplete() async { _handler.addListener('complete', (data) => _complete = true); while (!_complete) { await Future.delayed(const Duration(milliseconds: 200)); } } 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 { final BuildType _buildType; String? _yamleContent = null; IsolateExe(this._buildType, {String? yamlContent}) { _yamleContent = yamlContent; } @override void onReady(Object? initData) async { await Application.instance.build(_buildType, yamlContent: _yamleContent); send(IsoMessege('complete')); } }