Skip to content

Commit

Permalink
Merge #403
Browse files Browse the repository at this point in the history
403: Chore: fix linting issues r=curquiza a=Strift

# Pull Request

Fixes linting issues blocking #402 

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Strift <lau.cazanove@gmail.com>
  • Loading branch information
meili-bors[bot] and Strift authored Jan 10, 2025
2 parents 1d22b74 + e1e362f commit fef2c54
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 359 deletions.
2 changes: 0 additions & 2 deletions lib/src/results/matching_strategy_enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ extension MatchingStrategyExtension on MatchingStrategy {
return 'all';
case MatchingStrategy.last:
return 'last';
default:
return 'last';
}
}
}
6 changes: 6 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ dependencies:
collection: ^1.17.0
json_annotation: ^4.8.1
meta: ^1.9.1
platform: ^3.1.0
colorize: ^3.0.0
http: ^1.1.0
yaml_edit: ^2.1.1

dev_dependencies:
test: ^1.0.0
dart_jsonwebtoken: ^2.12.2
lints: ">=2.1.0 <4.0.0"
json_serializable: ^6.7.1
build_runner: ^2.4.6
args: ^2.4.2
path: ^1.8.3

screenshots:
- description: The Meilisearch logo.
Expand Down
21 changes: 13 additions & 8 deletions test/utils/wait_for.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ extension TaskWaiterForLists on Iterable<Task> {
bool throwFailed = true,
}) async {
final endingTime = DateTime.now().add(timeout);
final originalUids = toList();
final remainingUids = map((e) => e.uid).whereNotNull().toList();
final originalUids = List<Task>.from(this);
final remainingUids = <int>[];
for (final task in this) {
if (task.uid != null) {
remainingUids.add(task.uid!);
}
}
final completedTasks = <int, Task>{};
final statuses = ['enqueued', 'processing'];

while (DateTime.now().isBefore(endingTime)) {
final taskRes =
await client.getTasks(params: TasksQuery(uids: remainingUids));
final tasks = taskRes.results;
final completed = tasks.where((e) => !statuses.contains(e.status));
final completed = tasks.where((Task e) => !statuses.contains(e.status));
if (throwFailed) {
final failed = completed
.firstWhereOrNull((element) => element.status != 'succeeded');
.firstWhereOrNull((Task element) => element.status != 'succeeded');
if (failed != null) {
throw MeiliSearchApiException(
"Task (${failed.uid}) failed",
Expand All @@ -63,14 +68,14 @@ extension TaskWaiterForLists on Iterable<Task> {
}
}

completedTasks.addEntries(completed.map((e) => MapEntry(e.uid!, e)));
completedTasks.addEntries(completed.map((Task e) => MapEntry(e.uid!, e)));
remainingUids
.removeWhere((element) => completedTasks.containsKey(element));
.removeWhere((int element) => completedTasks.containsKey(element));

if (remainingUids.isEmpty) {
return originalUids
.map((e) => completedTasks[e.uid])
.whereNotNull()
.map((Task e) => completedTasks[e.uid])
.nonNulls
.toList();
}
await Future<void>.delayed(interval);
Expand Down
6 changes: 5 additions & 1 deletion tool/bin/meili.dart
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export 'package:meili_tool/src/main.dart';
import 'package:meili_tool/src/main.dart' as meili;

void main(List<String> args) async {
await meili.main(args);
}
44 changes: 13 additions & 31 deletions tool/lib/src/command_base.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:meili_tool/src/result.dart';
import 'package:platform/platform.dart';
import 'package:path/path.dart' as p;
import 'result.dart';

abstract class MeiliCommandBase extends Command<PackageResult> {
final Directory packageDirectory;
/// Base class for package commands.
abstract class PackageCommand extends Command<PackageResult> {
@override
final String name;

MeiliCommandBase(
this.packageDirectory, {
this.platform = const LocalPlatform(),
});

/// The current platform.
///
/// This can be overridden for testing.
final Platform platform;
@override
final String description;

/// A context that matches the default for [platform].
p.Context get path => platform.isWindows ? p.windows : p.posix;
// Returns the relative path from [from] to [entity] in Posix style.
///
/// This should be used when, for example, printing package-relative paths in
/// status or error messages.
String getRelativePosixPath(
FileSystemEntity entity, {
required Directory from,
}) =>
p.posix.joinAll(path.split(path.relative(entity.path, from: from.path)));

String get indentation => ' ';
PackageCommand({
required this.name,
required this.description,
});

bool getBoolArg(String key) {
return (argResults![key] as bool?) ?? false;
}
@override
Future<PackageResult> run();
}
71 changes: 28 additions & 43 deletions tool/lib/src/main.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,38 @@
import 'dart:io' as io;
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:meili_tool/src/output_utils.dart';
import 'package:meili_tool/src/result.dart';

import 'core.dart';
import 'output_utils.dart';
import 'result.dart';
import 'update_samples_command.dart';

void main(List<String> arguments) {
const FileSystem fileSystem = LocalFileSystem();
final Directory scriptDir =
fileSystem.file(io.Platform.script.toFilePath()).parent;
final Directory toolsDir =
scriptDir.basename == 'bin' ? scriptDir.parent : scriptDir.parent.parent;

final Directory meilisearchDirectory = toolsDir.parent;
Future<void> main(List<String> args) async {
final runner = CommandRunner<PackageResult>(
'meili',
'Tool for managing Meilisearch Dart SDK.',
);

final commandRunner = CommandRunner<PackageResult>(
'dart run ./tool/bin/meili.dart', 'Productivity utils for meilisearch.')
..addCommand(UpdateSamplesCommand(meilisearchDirectory));
runner.addCommand(UpdateSamplesCommand());

commandRunner.run(arguments).then((value) {
if (value == null) {
print('MUST output either a success or fail.');
assert(false);
io.exit(255);
try {
final result = await runner.run(args);
if (result == null) {
// help command or similar was run
exit(0);
}
switch (value.state) {
case RunState.succeeded:
printSuccess('Success!');
break;
case RunState.failed:
printError('Failed!');
if (value.details.isNotEmpty) {
printError(value.details.join('\n'));

switch (result.state) {
case RunState.success:
printSuccess('Command completed successfully');
exit(0);
case RunState.failure:
printError('Command failed');
if (result.details.isNotEmpty) {
printError('Details: ${result.details}');
}
io.exit(255);
}
}).catchError((Object e) {
final ToolExit toolExit = e as ToolExit;
int exitCode = toolExit.exitCode;
// This should never happen; this check is here to guarantee that a ToolExit
// never accidentally has code 0 thus causing CI to pass.
if (exitCode == 0) {
assert(false);
exitCode = 255;
exit(1);
}
io.exit(exitCode);
}, test: (Object e) => e is ToolExit);
} catch (e, stack) {
printError('Unexpected error: $e\n$stack');
exit(1);
}
}
15 changes: 12 additions & 3 deletions tool/lib/src/output_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ String _colorizeIfAppropriate(String string, Styles color) {

/// Prints [message] in green, if the environment supports color.
void printSuccess(String message) {
print(_colorizeIfAppropriate(message, Styles.GREEN));
final colorized = Colorize(message)..green();
print(colorized);
}

/// Prints [message] in yellow, if the environment supports color.
void printWarning(String message) {
print(_colorizeIfAppropriate(message, Styles.YELLOW));
final colorized = Colorize(message)..yellow();
print(colorized);
}

/// Prints [message] in red, if the environment supports color.
void printError(String message) {
print(_colorizeIfAppropriate(message, Styles.RED));
final colorized = Colorize(message)..red();
print(colorized);
}

/// Prints [message] in blue, if the environment supports color.
void printInfo(String message) {
final colorized = Colorize(message)..blue();
print(colorized);
}

/// Returns [message] with escapes to print it in [color], if the environment
Expand Down
19 changes: 8 additions & 11 deletions tool/lib/src/result.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
/// Possible outcomes of a command run for a package.
enum RunState {
/// The command succeeded for the package.
succeeded,
success,

/// The command failed for the package.
failed,
failure,
}

/// The result of a [runForPackage] call.
class PackageResult {
/// A successful result.
PackageResult.success() : this._(RunState.succeeded);
PackageResult.success() : this._(RunState.success, []);

/// A run that failed.
///
/// If [errors] are provided, they will be listed in the summary, otherwise
/// If [details] are provided, they will be listed in the summary, otherwise
/// the summary will simply show that the package failed.
PackageResult.fail([List<String> errors = const <String>[]])
: this._(RunState.failed, errors);
PackageResult.failure(String detail) : this._(RunState.failure, [detail]);

const PackageResult._(this.state, [this.details = const <String>[]]);
const PackageResult._(this.state, this.details);

/// The state the package run completed with.
final RunState state;

/// Information about the result:
/// - For `succeeded`, this is empty.
/// - For `skipped`, it contains a single entry describing why the run was
/// skipped.
/// - For `failed`, it contains zero or more specific error details to be
/// - For `success`, this is empty.
/// - For `failure`, it contains zero or more specific error details to be
/// shown in the summary.
final List<String> details;
}
Loading

0 comments on commit fef2c54

Please sign in to comment.