Skip to content

Commit

Permalink
docs: update docs for DaemonCubit class
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 31, 2025
1 parent 3d90633 commit 0e3c3f3
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lib/src/core/utils/daemon_manager/bloc/daemon_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,40 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gui/src/core/utils/daemon_manager/bloc/daemon_state.dart';
import 'package:path/path.dart' show dirname;

/// [DaemonCubit] Documentation:
/// `DaemonCubit` manages the execution of the Pactus daemon process.
/// It extends `Cubit<DaemonState>` and provides methods to run the daemon
/// while handling its execution state.
///
/// ## Features:
/// - Runs the Pactus daemon using a given command and arguments.
/// - Emits different states (`DaemonLoading`, `DaemonSuccess`, `DaemonError`)
/// based on the execution outcome.
/// - Handles standard output and errors from the process.
///
/// ## Usage:
/// ```dart
/// final daemonCubit = DaemonCubit();
/// daemonCubit.runPactusDaemon(command: 'pactusd', arguments: ['--start']);
/// ```
///
/// ## Notes:
/// - The working directory is set to a Linux-specific path.
/// - Needs adaptation for other operating systems.
/// - Handles exceptions and errors gracefully.
class DaemonCubit extends Cubit<DaemonState> {
DaemonCubit() : super(DaemonInitial());

/// [runPactusDaemon] Documentation:
/// Runs the Pactus daemon process.
///
/// - [command]: The command to execute (e.g., "pactusd").
/// - [arguments]: A list of arguments to pass to the command.
///
/// Emits:
/// - `DaemonLoading` before execution starts.
/// - `DaemonSuccess` if the process runs successfully.
/// - `DaemonError` if an error occurs.
Future<void> runPactusDaemon({
// required String workingDirectory,
required String command,
Expand All @@ -14,24 +45,18 @@ class DaemonCubit extends Cubit<DaemonState> {
emit(DaemonLoading());

try {
// Get the directory of the script
final scriptDir = dirname(Platform.script.toFilePath());

// TODO(Esmaeil): this part need handled for another os
final targetPath = '$scriptDir/lib/src/core/native_resources/linux/';

// print('scriptDir: $targetPath');

final result =
await Process.run(command, arguments, workingDirectory: targetPath);
if (result.exitCode == 0) {
// دستور با موفقیت اجرا شد
emit(DaemonSuccess('${result.stdout}'));
} else {
// دستور با خطا مواجه شد
emit(DaemonError('${result.stderr}'));
}
} on Exception catch (e) {
// برخورد با خطای استثنا
emit(DaemonError('Exception occurred: $e'));
}
}
Expand Down

0 comments on commit 0e3c3f3

Please sign in to comment.