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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.dart_tool/

.idea/
*.iml

pubspec.lock
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ reporting.
## Contributing Guidelines

To contribute to cli_tools, see the [contribution guidelines](CONTRIBUTING.md).

### Development workflow

This repo uses [melos](https://melos.invertase.dev/) to aid in development,
test, and publishing.

After cloning the repo, run `melos bootstrap` (or `melos bs`) to initialize it
(this will also run `dart pub get`).

Run `melos test` to run all the tests.
19 changes: 19 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: cli_tools_repo

packages:
- packages/**

ide:
intellij:
enabled: false

command:
version:
workspaceChangelog: false

scripts:
test:
description: "Run all tests"
run: dart test
exec:
concurrency: 1
2 changes: 2 additions & 0 deletions packages/cli_tools/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

pubspec.lock
4 changes: 3 additions & 1 deletion packages/cli_tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog
## 0.7.1

- **CHORE**(cli_tools): Bumped `config` dependency.

## 0.7.0
- refactor!: Moved out the `config` library from the `cli_tools` package and into its own package, to be published as `config` on pub.dev.
Expand Down
9 changes: 8 additions & 1 deletion packages/cli_tools/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
include: package:serverpod_lints/cli.yaml

analyzer:
language:
strict-raw-types: false
errors:
unnecessary_final: false
inference_failure_on_instance_creation: ignore
inference_failure_on_function_invocation: ignore

linter:
rules:
prefer_relative_imports: true
12 changes: 6 additions & 6 deletions packages/cli_tools/example/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:cli_tools/cli_tools.dart';
import 'package:config/config.dart';

Future<int> main(List<String> args) async {
var commandRunner = BetterCommandRunner(
Future<int> main(final List<String> args) async {
final commandRunner = BetterCommandRunner(
'example',
'Example CLI command',
globalOptions: [
Expand All @@ -29,7 +29,7 @@ Future<int> main(List<String> args) async {
} else {
logLevel = LogLevel.info;
}
var logger = StdOutLogger(logLevel);
final logger = StdOutLogger(logLevel);

logger.info('An info message');
logger.error('An error message');
Expand Down Expand Up @@ -97,12 +97,12 @@ class TimeSeriesCommand extends BetterCommand<TimeSeriesOption, void> {
String get description => 'Generate a series of time stamps';

@override
void runWithConfig(Configuration<TimeSeriesOption> commandConfig) {
void runWithConfig(final Configuration<TimeSeriesOption> commandConfig) {
var start = DateTime.now();
var until = commandConfig.value(TimeSeriesOption.until);
final until = commandConfig.value(TimeSeriesOption.until);

// exactly one of these options is set
var length = commandConfig.optionalValue(TimeSeriesOption.length);
final length = commandConfig.optionalValue(TimeSeriesOption.length);
var interval = commandConfig.optionalValue(TimeSeriesOption.interval);
interval ??= (until.difference(start) ~/ length!);
if (interval < const Duration(milliseconds: 1)) {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli_tools/example/simple_command_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'package:config/config.dart';
/// ```sh
/// INTERVAL=1s dart run example/simple_command_example.dart show
/// ```
Future<int> main(List<String> args) async {
var commandRunner = BetterCommandRunner(
Future<int> main(final List<String> args) async {
final commandRunner = BetterCommandRunner(
'example',
'Example CLI command',
);
Expand Down Expand Up @@ -56,8 +56,8 @@ class ShowCommand extends BetterCommand<ShowOption, void> {
String get description => 'Show the configured interval';

@override
void runWithConfig(Configuration<ShowOption> commandConfig) {
var interval = commandConfig.value(ShowOption.interval);
void runWithConfig(final Configuration<ShowOption> commandConfig) {
final interval = commandConfig.value(ShowOption.interval);
print('interval: $interval');
}
}
2 changes: 1 addition & 1 deletion packages/cli_tools/lib/logger.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'src/logger/helpers/ansi_style.dart';
export 'src/logger/logger.dart';
export 'src/logger/loggers/std_out_logger.dart';
export 'src/logger/loggers/void_logger.dart';
export 'src/logger/helpers/ansi_style.dart';
2 changes: 1 addition & 1 deletion packages/cli_tools/lib/package_version.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export 'src/package_version/package_version.dart';
export 'src/package_version/pub_api_client_exceptions.dart';
export 'src/package_version/pub_api_client.dart';
export 'src/package_version/pub_api_client_exceptions.dart';
14 changes: 7 additions & 7 deletions packages/cli_tools/lib/src/analytics/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract interface class Analytics {
void cleanUp();

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

/// Analytics service for MixPanel.
Expand All @@ -21,9 +21,9 @@ class MixPanelAnalytics implements Analytics {
final String _version;

MixPanelAnalytics({
required String uniqueUserId,
required String projectToken,
required String version,
required final String uniqueUserId,
required final String projectToken,
required final String version,
}) : _uniqueUserId = uniqueUserId,
_projectToken = projectToken,
_version = version;
Expand All @@ -32,8 +32,8 @@ class MixPanelAnalytics implements Analytics {
void cleanUp() {}

@override
void track({required String event}) {
var payload = jsonEncode({
void track({required final String event}) {
final payload = jsonEncode({
'event': event,
'properties': {
'distinct_id': _uniqueUserId,
Expand All @@ -60,7 +60,7 @@ class MixPanelAnalytics implements Analytics {
}
}

Future<void> _quietPost(String payload) async {
Future<void> _quietPost(final String payload) async {
try {
await http.post(
Uri.parse(_endpoint),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ abstract class BetterCommand<O extends OptionDefinition, T> extends Command<T> {
/// }
/// ```
BetterCommand({
MessageOutput? messageOutput = _defaultMessageOutput,
int? wrapTextColumn,
final MessageOutput? messageOutput = _defaultMessageOutput,
final int? wrapTextColumn,
this.options = const [],
Map<String, String>? env,
final Map<String, String>? env,
}) : _messageOutput = messageOutput,
_argParser = ArgParser(usageLineLength: wrapTextColumn),
envVariables = env ?? Platform.environment {
Expand All @@ -71,7 +71,7 @@ abstract class BetterCommand<O extends OptionDefinition, T> extends Command<T> {
if (_messageOutput != _defaultMessageOutput) {
return _messageOutput;
}
if (runner case BetterCommandRunner<O, T> runner) {
if (runner case final BetterCommandRunner<O, T> runner) {
return runner.messageOutput;
}
return _messageOutput;
Expand Down Expand Up @@ -120,7 +120,7 @@ abstract class BetterCommand<O extends OptionDefinition, T> extends Command<T> {
///
/// This method can be overridden to change the configuration resolution
/// behavior.
Configuration<O> resolveConfiguration(ArgResults? argResults) {
Configuration<O> resolveConfiguration(final ArgResults? argResults) {
return Configuration.resolveNoExcept(
options: options,
argResults: argResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ final class MessageOutput {

/// Logs a usage exception.
/// If the function has not been provided then nothing will happen.
void logUsageException(UsageException exception) {
void logUsageException(final UsageException exception) {
usageExceptionLogger?.call(exception);
}

/// Logs a usage message.
/// If the function has not been provided then nothing will happen.
void logUsage(String usage) {
void logUsage(final String usage) {
usageLogger?.call(usage);
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>

/// Sets the global configuration, by default called by [parse].
/// It must be set before [runCommand] is called.
set globalConfiguration(Configuration<O> configuration) {
set globalConfiguration(final Configuration<O> configuration) {
_globalConfiguration = configuration;
}

Expand Down Expand Up @@ -140,13 +140,14 @@ class BetterCommandRunner<O extends OptionDefinition, T>
super.executableName,
super.description, {
super.suggestionDistanceLimit,
MessageOutput? messageOutput = const MessageOutput(usageLogger: print),
SetLogLevel? setLogLevel,
OnBeforeRunCommand? onBeforeRunCommand,
OnAnalyticsEvent? onAnalyticsEvent,
int? wrapTextColumn,
List<O>? globalOptions,
Map<String, String>? env,
final MessageOutput? messageOutput =
const MessageOutput(usageLogger: print),
final SetLogLevel? setLogLevel,
final OnBeforeRunCommand? onBeforeRunCommand,
final OnAnalyticsEvent? onAnalyticsEvent,
final int? wrapTextColumn,
final List<O>? globalOptions,
final Map<String, String>? env,
}) : _messageOutput = messageOutput,
_setLogLevel = setLogLevel,
_onBeforeRunCommand = onBeforeRunCommand,
Expand Down Expand Up @@ -176,8 +177,8 @@ class BetterCommandRunner<O extends OptionDefinition, T>
MessageOutput? get messageOutput => _messageOutput;

/// Adds a list of commands to the command runner.
void addCommands(List<Command<T>> commands) {
for (var command in commands) {
void addCommands(final List<Command<T>> commands) {
for (final command in commands) {
addCommand(command);
}
}
Expand All @@ -195,9 +196,9 @@ class BetterCommandRunner<O extends OptionDefinition, T>
/// If this method is overridden, the overriding method must ensure that
/// the global configuration is set, see [globalConfiguration].
@override
Future<T?> run(Iterable<String> args) {
Future<T?> run(final Iterable<String> args) {
return Future.sync(() {
var argResults = parse(args);
final argResults = parse(args);
globalConfiguration = resolveConfiguration(argResults);

try {
Expand All @@ -219,7 +220,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>

/// Parses the command line arguments and returns the result.
@override
ArgResults parse(Iterable<String> args) {
ArgResults parse(final Iterable<String> args) {
try {
return super.parse(args);
} on UsageException catch (e) {
Expand Down Expand Up @@ -247,7 +248,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
///
/// This returns the return value of [Command.run].
@override
Future<T?> runCommand(ArgResults topLevelResults) async {
Future<T?> runCommand(final ArgResults topLevelResults) async {
_setLogLevel?.call(
parsedLogLevel: _determineLogLevel(globalConfiguration),
commandName: topLevelResults.command?.name,
Expand All @@ -261,7 +262,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>

unawaited(
Future(() async {
var command = topLevelResults.command;
final 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
Expand All @@ -280,7 +281,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
// 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;
final noUnexpectedArgs = topLevelResults.rest.isEmpty;
if (noUnexpectedArgs) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help);
}
Expand All @@ -302,7 +303,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
///
/// This method can be overridden to change the configuration resolution
/// behavior.
Configuration<O> resolveConfiguration(ArgResults? argResults) {
Configuration<O> resolveConfiguration(final ArgResults? argResults) {
return Configuration.resolveNoExcept(
options: _globalOptions,
argResults: argResults,
Expand All @@ -311,7 +312,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
);
}

static CommandRunnerLogLevel _determineLogLevel(Configuration config) {
static CommandRunnerLogLevel _determineLogLevel(final Configuration config) {
if (config.findValueOf(argName: BetterCommandRunnerFlags.verbose) == true) {
return CommandRunnerLogLevel.verbose;
} else if (config.findValueOf(argName: BetterCommandRunnerFlags.quiet) ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class CommandDocumentationGenerator {
CommandDocumentationGenerator(this.commandRunner);

Map<String, String> generateMarkdown() {
var commands = commandRunner.commands.values;
final commands = commandRunner.commands.values;

var files = <String, String>{};
final files = <String, String>{};

for (var command in commands) {
StringBuffer markdown = StringBuffer();
for (final command in commands) {
final StringBuffer markdown = StringBuffer();
markdown.writeln('## Usage\n');

if (command.argParser.options.isNotEmpty) {
Expand All @@ -21,9 +21,9 @@ class CommandDocumentationGenerator {
}

if (command.subcommands.isNotEmpty) {
var numberOfSubcommands = command.subcommands.length;
final numberOfSubcommands = command.subcommands.length;
markdown.writeln('### Sub commands\n');
for (var (i, subcommand) in command.subcommands.entries.indexed) {
for (final (i, subcommand) in command.subcommands.entries.indexed) {
markdown.writeln('#### `${subcommand.key}`\n');
markdown.writeln('```console');
markdown.writeln(subcommand.value.usage);
Expand Down
Loading