Skip to content

Commit

Permalink
fix: improved logging for the cli with cli_spin
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Mar 8, 2024
1 parent 701316d commit 49274f1
Show file tree
Hide file tree
Showing 22 changed files with 539 additions and 314 deletions.
2 changes: 1 addition & 1 deletion bin/impaktfull_cli.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import 'package:impaktfull_cli/src/impaktfull_cli.dart';

Future<void> main(List<String> arguments) => ImpaktfullCli().runCli(arguments);
Future<void> main(List<String> arguments) => ImpaktfullCli(arguments: arguments).runCli();
11 changes: 11 additions & 0 deletions lib/src/core/cli_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dart:io';

import 'package:path/path.dart';

class CliConstants {
CliConstants._();

static final buildFolder = Directory(join('build', 'impaktfull_cli'));

static String get buildFolderPath => buildFolder.path;
}
3 changes: 1 addition & 2 deletions lib/src/core/command/command/impaktfull_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ abstract class ImpaktfullCommand<T extends ConfigData> extends Command<void> {
} on ArgumentError catch (e) {
throw ImpaktfullCliArgumentError(
e.message.toString(),
argResults?.name,
argParser.usage,
argParser: argParser,
);
}
await runCommand(configData);
Expand Down
20 changes: 5 additions & 15 deletions lib/src/core/model/error/impaktfull_cli_argument_error.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import 'package:args/args.dart';
import 'package:impaktfull_cli/src/core/model/error/impaktfull_cli_error.dart';

class ImpaktfullCliArgumentError extends ImpaktfullCliError {
final String? command;
final String usage;
final ArgParser argParser;

ImpaktfullCliArgumentError(
super.message,
this.command,
this.usage,
);

@override
String toString() => '''Error while parsing arguments for `$command`:
$message
Usage fo `$command`:
$usage
''';
super.message, {
required this.argParser,
});
}
5 changes: 5 additions & 0 deletions lib/src/core/model/error/impaktfull_cli_exit_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:impaktfull_cli/src/core/model/error/impaktfull_cli_error.dart';

class ImpktfullCliExitError extends ImpaktfullCliError {
ImpktfullCliExitError(super.message);
}
10 changes: 10 additions & 0 deletions lib/src/core/model/error/impaktfull_cli_process_runner_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:impaktfull_cli/src/core/model/error/impaktfull_cli_error.dart';

class ImpaktfullCliProcessRunnerError extends ImpaktfullCliError {
final String errorOutput;

ImpaktfullCliProcessRunnerError(
super.message,
this.errorOutput,
);
}
59 changes: 19 additions & 40 deletions lib/src/core/util/args/env/impaktfull_cli_environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,27 @@ import 'package:path/path.dart';
class ImpaktfullCliEnvironment {
static late ImpaktfullCliEnvironment _instance;

final bool verboseLoggingEnabled;
final Directory workingDirectory;
final bool isFvmProject;
final List<InstalledCliTool> allCliTools;

static ImpaktfullCliEnvironment get instance => _instance;

List<InstalledCliTool> get installedCliTools =>
allCliTools.where((element) => element.isInstalled).toList();
List<InstalledCliTool> get installedCliTools => allCliTools.where((element) => element.isInstalled).toList();

List<InstalledCliTool> get notInstalledCliTools =>
allCliTools.where((element) => !element.isInstalled).toList();
List<InstalledCliTool> get notInstalledCliTools => allCliTools.where((element) => !element.isInstalled).toList();

const ImpaktfullCliEnvironment._({
required this.verboseLoggingEnabled,
required this.workingDirectory,
required this.isFvmProject,
required this.allCliTools,
});

static Future<void> init({
ProcessRunner processRunner = const CliProcessRunner(),
bool isVerboseLoggingEnabled = false,
}) async {
ImpaktfullCliLogger.init(isVerboseLoggingEnabled: isVerboseLoggingEnabled);
final workingDir = Directory.current;
_instance = ImpaktfullCliEnvironment._(
verboseLoggingEnabled: isVerboseLoggingEnabled,
workingDirectory: workingDir,
isFvmProject: await _checkIfActiveProjectIsFvm(workingDir),
allCliTools: await _checkInstalledTools(processRunner),
Expand All @@ -51,18 +44,11 @@ class ImpaktfullCliEnvironment {
return fvmConfigFile.exists();
}

static Future<List<InstalledCliTool>> _checkInstalledTools(
ProcessRunner processRunner) async =>
CliTool.values
.where((element) => element.supportedOperatingSystems
.contains(OperatingSystem.current))
.map((cliTool) => _isToolInstalled(processRunner, cliTool))
.wait;
static Future<List<InstalledCliTool>> _checkInstalledTools(ProcessRunner processRunner) async =>
CliTool.values.where((element) => element.supportedOperatingSystems.contains(OperatingSystem.current)).map((cliTool) => _isToolInstalled(processRunner, cliTool)).wait;

static Future<InstalledCliTool> _isToolInstalled(
ProcessRunner processRunner, CliTool cliTool) async {
final result =
await processRunner.runProcess(['which', cliTool.commandName]);
static Future<InstalledCliTool> _isToolInstalled(ProcessRunner processRunner, CliTool cliTool) async {
final result = await processRunner.runProcess(['which', cliTool.commandName]);
if (result.isEmpty) return InstalledCliTool.notInstalled(cliTool: cliTool);
return InstalledCliTool.installed(
cliTool: cliTool,
Expand All @@ -76,8 +62,7 @@ class ImpaktfullCliEnvironment {
}
}

static bool isInstalled(CliTool cliTool) =>
_instance.allCliTools.any((element) => element.cliTool == cliTool);
static bool isInstalled(CliTool cliTool) => _instance.allCliTools.any((element) => element.cliTool == cliTool);

static void requiresInstalledTools(List<CliTool> requiredTools) {
final requiredToolsFound = <CliTool>[];
Expand All @@ -88,40 +73,34 @@ class ImpaktfullCliEnvironment {
}
}
if (requiredToolsFound.length != requiredTools.length) {
final missingTools = requiredTools
.where((element) => !requiredToolsFound.contains(element));
throw ImpaktfullCliError(
'${missingTools.map((e) => '${e.commandName} (${e.name})').join(', ')} are not installed, but required for the next step');
final missingTools = requiredTools.where((element) => !requiredToolsFound.contains(element));
throw ImpaktfullCliError('${missingTools.map((e) => '${e.commandName} (${e.name})').join(', ')} are not installed, but required for the next step');
}
}

static void _printCurrentState() {
ImpaktfullCliLogger.debugSeperator();
ImpaktfullCliLogger.debug(
'Operating system: ${OperatingSystem.current.name}');
ImpaktfullCliLogger.debug(
'Working Dir: `${_instance.workingDirectory.path}`');
ImpaktfullCliLogger.debug('Is fvm project: `${_instance.isFvmProject}`');
ImpaktfullCliLogger.verboseSeperator();
ImpaktfullCliLogger.verbose('Operating system: ${OperatingSystem.current.name}');
ImpaktfullCliLogger.verbose('Working Dir: `${_instance.workingDirectory.path}`');
ImpaktfullCliLogger.verbose('Is fvm project: `${_instance.isFvmProject}`');
if (_instance.installedCliTools.isNotEmpty) {
ImpaktfullCliLogger.debug('Installed Tools:');
ImpaktfullCliLogger.verbose('Installed Tools:');
for (final clitool in _instance.installedCliTools) {
ImpaktfullCliLogger.debug(
'\t${clitool.cliTool.commandName} - ${clitool.path}');
ImpaktfullCliLogger.verbose('\t${clitool.cliTool.commandName} - ${clitool.path}');
}
}
if (_instance.notInstalledCliTools.isNotEmpty) {
ImpaktfullCliLogger.debug('Not Installed Tools:');
ImpaktfullCliLogger.verbose('Not Installed Tools:');
for (final notInstalledCliTool in _instance.notInstalledCliTools) {
final cliTool = notInstalledCliTool.cliTool;
var log = '\t${cliTool.commandName}';
final installationInstructions =
cliTool.installationInstructions[OperatingSystem.current];
final installationInstructions = cliTool.installationInstructions[OperatingSystem.current];
if (installationInstructions != null) {
log += ' - Install instructions: $installationInstructions';
}
ImpaktfullCliLogger.debug(log);
ImpaktfullCliLogger.verbose(log);
}
}
ImpaktfullCliLogger.debugSeperator();
ImpaktfullCliLogger.verboseSeperator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@ import 'package:impaktfull_cli/impaktfull_cli.dart';

typedef AsyncCallback = Future<void> Function();

class ForceQuitUtil {
class ForceQuitListener {
static var _isShuttingDown = false;
static final _listeners = <AsyncCallback>{};

static StreamSubscription<ProcessSignal>? _subscription;

static void _addListener(AsyncCallback listener) => _listeners.add(listener);

static void _removeListener(AsyncCallback listener) =>
_listeners.remove(listener);
static void _removeListener(AsyncCallback listener) => _listeners.remove(listener);

static void init() {
_subscription = ProcessSignal.sigint.watch().listen((signal) async {
if (_isShuttingDown) return;
_isShuttingDown = true;
ImpaktfullCliLogger.debug('');
ImpaktfullCliLogger.debug('Force quit detected. Cleaning up...');
ImpaktfullCliLogger.verbose(
'Cleaning up ${_listeners.length} listeners...');
ImpaktfullCliLogger.log('\nForce quit detected. Cleaning up...');
ImpaktfullCliLogger.verbose('Cleaning up ${_listeners.length} listeners...');
await Future.wait(_listeners.map((e) => e()));
exit(0);
});
Expand Down
24 changes: 24 additions & 0 deletions lib/src/core/util/input_listener/versbose_logging_listener.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:async';
import 'dart:io';

import 'package:impaktfull_cli/src/core/util/logger/logger.dart';

class VerboseLoggingListener {
static StreamSubscription<List<int>>? _subscription;

VerboseLoggingListener._();

static void startInputListener() {
_subscription = stdin.listen((data) {
final input = String.fromCharCodes(data).trim();

if (input.toLowerCase() == 'v' || input.toLowerCase() == 'verbose') {
ImpaktfullCliLogger.enableVerbose(isVerboseLoggingEnabled: true);
} else if (input.toLowerCase() == 'nv' || input.toLowerCase() == 'no-verbose') {
ImpaktfullCliLogger.enableVerbose(isVerboseLoggingEnabled: false);
}
});
}

static void stopListening() => _subscription?.cancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CliInputReader {
const CliInputReader._();

static Secret readSecret(String message) {
ImpaktfullCliLogger.debug('$message:');
ImpaktfullCliLogger.log('$message:');
stdin.echoMode = false;
final secretValue = stdin.readLineSync();
if (secretValue == null || secretValue.isEmpty) {
Expand Down
Loading

0 comments on commit 49274f1

Please sign in to comment.