Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ void main() async {
);
await logger.progress(
'A progress message',
() async => Future.delayed(
const Duration(seconds: 3),
() => true,
),
() async => Future.delayed(const Duration(seconds: 3), () => true),
);
}
34 changes: 16 additions & 18 deletions lib/src/analytics/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ abstract interface class Analytics {
void cleanUp();

/// Track an event.
void track({
required String event,
});
void track({required String event});
}

/// Analytics service for MixPanel.
Expand All @@ -26,17 +24,15 @@ class MixPanelAnalytics implements Analytics {
required String uniqueUserId,
required String projectToken,
required String version,
}) : _uniqueUserId = uniqueUserId,
_projectToken = projectToken,
_version = version;
}) : _uniqueUserId = uniqueUserId,
_projectToken = projectToken,
_version = version;

@override
void cleanUp() {}

@override
void track({
required String event,
}) {
void track({required String event}) {
var payload = jsonEncode({
'event': event,
'properties': {
Expand All @@ -46,7 +42,7 @@ class MixPanelAnalytics implements Analytics {
'dart_version': Platform.version,
'is_ci': ci.isCI,
'version': _version,
}
},
});

_quietPost(payload);
Expand All @@ -66,14 +62,16 @@ class MixPanelAnalytics implements Analytics {

Future<void> _quietPost(String payload) async {
try {
await http.post(
Uri.parse(_endpoint),
body: 'data=$payload',
headers: {
'Accept': 'text/plain',
'Content-Type': 'application/x-www-form-urlencoded',
},
).timeout(const Duration(seconds: 2));
await http
.post(
Uri.parse(_endpoint),
body: 'data=$payload',
headers: {
'Accept': 'text/plain',
'Content-Type': 'application/x-www-form-urlencoded',
},
)
.timeout(const Duration(seconds: 2));
} catch (e) {
return;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/src/better_command_runner/better_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ abstract class BetterCommand extends Command {
final PassMessage? _logInfo;
final ArgParser _argParser;

BetterCommand({
PassMessage? logInfo,
int? wrapTextColumn,
}) : _logInfo = logInfo,
_argParser = ArgParser(usageLineLength: wrapTextColumn);
BetterCommand({PassMessage? logInfo, int? wrapTextColumn})
: _logInfo = logInfo,
_argParser = ArgParser(usageLineLength: wrapTextColumn);

@override
ArgParser get argParser => _argParser;
Expand Down
83 changes: 43 additions & 40 deletions lib/src/better_command_runner/better_command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import 'package:args/command_runner.dart';
import 'package:cli_tools/src/better_command_runner/exit_exception.dart';

/// A function type for executing code before running a command.
typedef OnBeforeRunCommand = Future<void> Function(
BetterCommandRunner runner,
);
typedef OnBeforeRunCommand = Future<void> Function(BetterCommandRunner runner);

/// A function type for passing log messages.
typedef PassMessage = void Function(String message);
Expand All @@ -16,10 +14,11 @@ typedef PassMessage = void Function(String message);
/// The [logLevel] is the log level to set.
/// The [commandName] is the name of the command if custom rules for log
/// levels are needed.
typedef SetLogLevel = void Function({
required CommandRunnerLogLevel parsedLogLevel,
String? commandName,
});
typedef SetLogLevel =
void Function({
required CommandRunnerLogLevel parsedLogLevel,
String? commandName,
});

/// A function type for tracking events.
typedef OnAnalyticsEvent = void Function(String event);
Expand Down Expand Up @@ -64,18 +63,19 @@ class BetterCommandRunner extends CommandRunner {
OnBeforeRunCommand? onBeforeRunCommand,
OnAnalyticsEvent? onAnalyticsEvent,
int? wrapTextColumn,
}) : _logError = logError,
_logInfo = logInfo,
_onBeforeRunCommand = onBeforeRunCommand,
_setLogLevel = setLogLevel,
_onAnalyticsEvent = onAnalyticsEvent,
_argParser = ArgParser(usageLineLength: wrapTextColumn) {
}) : _logError = logError,
_logInfo = logInfo,
_onBeforeRunCommand = onBeforeRunCommand,
_setLogLevel = setLogLevel,
_onAnalyticsEvent = onAnalyticsEvent,
_argParser = ArgParser(usageLineLength: wrapTextColumn) {
argParser.addFlag(
BetterCommandRunnerFlags.quiet,
abbr: BetterCommandRunnerFlags.quietAbbr,
defaultsTo: false,
negatable: false,
help: 'Suppress all cli output. Is overridden by '
help:
'Suppress all cli output. Is overridden by '
' -${BetterCommandRunnerFlags.verboseAbbr}, --${BetterCommandRunnerFlags.verbose}.',
);

Expand All @@ -84,7 +84,8 @@ class BetterCommandRunner extends CommandRunner {
abbr: BetterCommandRunnerFlags.verboseAbbr,
defaultsTo: false,
negatable: false,
help: 'Prints additional information useful for development. '
help:
'Prints additional information useful for development. '
'Overrides --${BetterCommandRunnerFlags.quietAbbr}, --${BetterCommandRunnerFlags.quiet}.',
);

Expand Down Expand Up @@ -140,31 +141,33 @@ class BetterCommandRunner extends CommandRunner {
_onAnalyticsEvent = null;
}

unawaited(Future(() async {
var command = topLevelResults.command;
if (command != null) {
// Command name can only be null for top level results.
// But since we are taking the name of a command from the top level
// results there should always be a name specified.
assert(command.name != null, 'Command name should never be null.');
_onAnalyticsEvent?.call(
command.name ?? BetterCommandRunnerAnalyticsEvents.invalid,
);
return;
}

// Checks if the command is valid (i.e. no unexpected arguments).
// If there are unexpected arguments this will trigger a [UsageException]
// which will be caught in the try catch around the super.runCommand call.
// Therefore, this ensures that the help event is not sent for
// commands that are invalid.
// Note that there are other scenarios that also trigger a [UsageException]
// so the try/catch statement can't be fully compensated for handled here.
var noUnexpectedArgs = topLevelResults.rest.isEmpty;
if (noUnexpectedArgs) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help);
}
}));
unawaited(
Future(() async {
var command = topLevelResults.command;
if (command != null) {
// Command name can only be null for top level results.
// But since we are taking the name of a command from the top level
// results there should always be a name specified.
assert(command.name != null, 'Command name should never be null.');
_onAnalyticsEvent?.call(
command.name ?? BetterCommandRunnerAnalyticsEvents.invalid,
);
return;
}

// Checks if the command is valid (i.e. no unexpected arguments).
// If there are unexpected arguments this will trigger a [UsageException]
// which will be caught in the try catch around the super.runCommand call.
// Therefore, this ensures that the help event is not sent for
// commands that are invalid.
// Note that there are other scenarios that also trigger a [UsageException]
// so the try/catch statement can't be fully compensated for handled here.
var noUnexpectedArgs = topLevelResults.rest.isEmpty;
if (noUnexpectedArgs) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help);
}
}),
);

await _onBeforeRunCommand?.call(this);

Expand Down
9 changes: 5 additions & 4 deletions lib/src/logger/helpers/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ProgressAnimation {
'⠦',
'⠧',
'⠇',
'⠏'
'⠏',
];

/// The list of animation frames.
Expand All @@ -64,8 +64,8 @@ class Progress {
this._message,
this._stdout, {
ProgressOptions options = const ProgressOptions(),
}) : _stopwatch = Stopwatch(),
_options = options {
}) : _stopwatch = Stopwatch(),
_options = options {
_stopwatch
..reset()
..start();
Expand Down Expand Up @@ -121,7 +121,8 @@ class Progress {
void fail([String? update]) {
_timer?.cancel();
_write(
'$_clearLine${AnsiStyle.red.wrap('✗')} ${update ?? _message} $_time\n');
'$_clearLine${AnsiStyle.red.wrap('✗')} ${update ?? _message} $_time\n',
);
_stopwatch.stop();
}

Expand Down
33 changes: 5 additions & 28 deletions lib/src/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,17 @@ abstract class Logger {
/// Display debug [message] to the user.
/// Commands should use this for information that is important for
/// debugging purposes.
void debug(
String message, {
bool newParagraph,
LogType type,
});
void debug(String message, {bool newParagraph, LogType type});

/// Display a normal [message] to the user.
/// Command should use this as the standard communication channel for
/// success, progress or information messages.
void info(
String message, {
bool newParagraph,
LogType type,
});
void info(String message, {bool newParagraph, LogType type});

/// Display a warning [message] to the user.
/// Commands should use this if they have important but not critical
/// information for the user.
void warning(
String message, {
bool newParagraph,
LogType type,
});
void warning(String message, {bool newParagraph, LogType type});

/// Display an error [message] to the user.
/// Commands should use this if they want to inform a user that an error
Expand Down Expand Up @@ -86,15 +74,7 @@ enum LogLevel {
final String name;
}

enum TextLogStyle {
init,
normal,
hint,
header,
bullet,
command,
success,
}
enum TextLogStyle { init, normal, hint, header, bullet, command, success }

abstract class LogType {
const LogType();
Expand All @@ -110,10 +90,7 @@ class RawLogType extends LogType {
/// If [title] is set the box will have a title row.
class BoxLogType extends LogType {
final String? title;
const BoxLogType({
this.title,
bool newParagraph = true,
});
const BoxLogType({this.title, bool newParagraph = true});
}

/// Abstract style console formatting.
Expand Down
Loading