Skip to content

Commit fef2c54

Browse files
Merge #403
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 <[email protected]>
2 parents 1d22b74 + e1e362f commit fef2c54

File tree

10 files changed

+178
-359
lines changed

10 files changed

+178
-359
lines changed

lib/src/results/matching_strategy_enum.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ extension MatchingStrategyExtension on MatchingStrategy {
1010
return 'all';
1111
case MatchingStrategy.last:
1212
return 'last';
13-
default:
14-
return 'last';
1513
}
1614
}
1715
}

pubspec.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ dependencies:
1414
collection: ^1.17.0
1515
json_annotation: ^4.8.1
1616
meta: ^1.9.1
17+
platform: ^3.1.0
18+
colorize: ^3.0.0
19+
http: ^1.1.0
20+
yaml_edit: ^2.1.1
1721

1822
dev_dependencies:
1923
test: ^1.0.0
2024
dart_jsonwebtoken: ^2.12.2
2125
lints: ">=2.1.0 <4.0.0"
2226
json_serializable: ^6.7.1
2327
build_runner: ^2.4.6
28+
args: ^2.4.2
29+
path: ^1.8.3
2430

2531
screenshots:
2632
- description: The Meilisearch logo.

test/utils/wait_for.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,24 @@ extension TaskWaiterForLists on Iterable<Task> {
4040
bool throwFailed = true,
4141
}) async {
4242
final endingTime = DateTime.now().add(timeout);
43-
final originalUids = toList();
44-
final remainingUids = map((e) => e.uid).whereNotNull().toList();
43+
final originalUids = List<Task>.from(this);
44+
final remainingUids = <int>[];
45+
for (final task in this) {
46+
if (task.uid != null) {
47+
remainingUids.add(task.uid!);
48+
}
49+
}
4550
final completedTasks = <int, Task>{};
4651
final statuses = ['enqueued', 'processing'];
4752

4853
while (DateTime.now().isBefore(endingTime)) {
4954
final taskRes =
5055
await client.getTasks(params: TasksQuery(uids: remainingUids));
5156
final tasks = taskRes.results;
52-
final completed = tasks.where((e) => !statuses.contains(e.status));
57+
final completed = tasks.where((Task e) => !statuses.contains(e.status));
5358
if (throwFailed) {
5459
final failed = completed
55-
.firstWhereOrNull((element) => element.status != 'succeeded');
60+
.firstWhereOrNull((Task element) => element.status != 'succeeded');
5661
if (failed != null) {
5762
throw MeiliSearchApiException(
5863
"Task (${failed.uid}) failed",
@@ -63,14 +68,14 @@ extension TaskWaiterForLists on Iterable<Task> {
6368
}
6469
}
6570

66-
completedTasks.addEntries(completed.map((e) => MapEntry(e.uid!, e)));
71+
completedTasks.addEntries(completed.map((Task e) => MapEntry(e.uid!, e)));
6772
remainingUids
68-
.removeWhere((element) => completedTasks.containsKey(element));
73+
.removeWhere((int element) => completedTasks.containsKey(element));
6974

7075
if (remainingUids.isEmpty) {
7176
return originalUids
72-
.map((e) => completedTasks[e.uid])
73-
.whereNotNull()
77+
.map((Task e) => completedTasks[e.uid])
78+
.nonNulls
7479
.toList();
7580
}
7681
await Future<void>.delayed(interval);

tool/bin/meili.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export 'package:meili_tool/src/main.dart';
1+
import 'package:meili_tool/src/main.dart' as meili;
2+
3+
void main(List<String> args) async {
4+
await meili.main(args);
5+
}

tool/lib/src/command_base.dart

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
import 'package:args/command_runner.dart';
2-
import 'package:file/file.dart';
3-
import 'package:meili_tool/src/result.dart';
4-
import 'package:platform/platform.dart';
5-
import 'package:path/path.dart' as p;
2+
import 'result.dart';
63

7-
abstract class MeiliCommandBase extends Command<PackageResult> {
8-
final Directory packageDirectory;
4+
/// Base class for package commands.
5+
abstract class PackageCommand extends Command<PackageResult> {
6+
@override
7+
final String name;
98

10-
MeiliCommandBase(
11-
this.packageDirectory, {
12-
this.platform = const LocalPlatform(),
13-
});
14-
15-
/// The current platform.
16-
///
17-
/// This can be overridden for testing.
18-
final Platform platform;
9+
@override
10+
final String description;
1911

20-
/// A context that matches the default for [platform].
21-
p.Context get path => platform.isWindows ? p.windows : p.posix;
22-
// Returns the relative path from [from] to [entity] in Posix style.
23-
///
24-
/// This should be used when, for example, printing package-relative paths in
25-
/// status or error messages.
26-
String getRelativePosixPath(
27-
FileSystemEntity entity, {
28-
required Directory from,
29-
}) =>
30-
p.posix.joinAll(path.split(path.relative(entity.path, from: from.path)));
31-
32-
String get indentation => ' ';
12+
PackageCommand({
13+
required this.name,
14+
required this.description,
15+
});
3316

34-
bool getBoolArg(String key) {
35-
return (argResults![key] as bool?) ?? false;
36-
}
17+
@override
18+
Future<PackageResult> run();
3719
}

tool/lib/src/main.dart

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,38 @@
1-
import 'dart:io' as io;
1+
import 'dart:io';
22

33
import 'package:args/command_runner.dart';
4-
import 'package:file/file.dart';
5-
import 'package:file/local.dart';
6-
import 'package:meili_tool/src/output_utils.dart';
7-
import 'package:meili_tool/src/result.dart';
8-
9-
import 'core.dart';
4+
import 'output_utils.dart';
5+
import 'result.dart';
106
import 'update_samples_command.dart';
117

12-
void main(List<String> arguments) {
13-
const FileSystem fileSystem = LocalFileSystem();
14-
final Directory scriptDir =
15-
fileSystem.file(io.Platform.script.toFilePath()).parent;
16-
final Directory toolsDir =
17-
scriptDir.basename == 'bin' ? scriptDir.parent : scriptDir.parent.parent;
18-
19-
final Directory meilisearchDirectory = toolsDir.parent;
8+
Future<void> main(List<String> args) async {
9+
final runner = CommandRunner<PackageResult>(
10+
'meili',
11+
'Tool for managing Meilisearch Dart SDK.',
12+
);
2013

21-
final commandRunner = CommandRunner<PackageResult>(
22-
'dart run ./tool/bin/meili.dart', 'Productivity utils for meilisearch.')
23-
..addCommand(UpdateSamplesCommand(meilisearchDirectory));
14+
runner.addCommand(UpdateSamplesCommand());
2415

25-
commandRunner.run(arguments).then((value) {
26-
if (value == null) {
27-
print('MUST output either a success or fail.');
28-
assert(false);
29-
io.exit(255);
16+
try {
17+
final result = await runner.run(args);
18+
if (result == null) {
19+
// help command or similar was run
20+
exit(0);
3021
}
31-
switch (value.state) {
32-
case RunState.succeeded:
33-
printSuccess('Success!');
34-
break;
35-
case RunState.failed:
36-
printError('Failed!');
37-
if (value.details.isNotEmpty) {
38-
printError(value.details.join('\n'));
22+
23+
switch (result.state) {
24+
case RunState.success:
25+
printSuccess('Command completed successfully');
26+
exit(0);
27+
case RunState.failure:
28+
printError('Command failed');
29+
if (result.details.isNotEmpty) {
30+
printError('Details: ${result.details}');
3931
}
40-
io.exit(255);
41-
}
42-
}).catchError((Object e) {
43-
final ToolExit toolExit = e as ToolExit;
44-
int exitCode = toolExit.exitCode;
45-
// This should never happen; this check is here to guarantee that a ToolExit
46-
// never accidentally has code 0 thus causing CI to pass.
47-
if (exitCode == 0) {
48-
assert(false);
49-
exitCode = 255;
32+
exit(1);
5033
}
51-
io.exit(exitCode);
52-
}, test: (Object e) => e is ToolExit);
34+
} catch (e, stack) {
35+
printError('Unexpected error: $e\n$stack');
36+
exit(1);
37+
}
5338
}

tool/lib/src/output_utils.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,26 @@ String _colorizeIfAppropriate(String string, Styles color) {
2424

2525
/// Prints [message] in green, if the environment supports color.
2626
void printSuccess(String message) {
27-
print(_colorizeIfAppropriate(message, Styles.GREEN));
27+
final colorized = Colorize(message)..green();
28+
print(colorized);
2829
}
2930

3031
/// Prints [message] in yellow, if the environment supports color.
3132
void printWarning(String message) {
32-
print(_colorizeIfAppropriate(message, Styles.YELLOW));
33+
final colorized = Colorize(message)..yellow();
34+
print(colorized);
3335
}
3436

3537
/// Prints [message] in red, if the environment supports color.
3638
void printError(String message) {
37-
print(_colorizeIfAppropriate(message, Styles.RED));
39+
final colorized = Colorize(message)..red();
40+
print(colorized);
41+
}
42+
43+
/// Prints [message] in blue, if the environment supports color.
44+
void printInfo(String message) {
45+
final colorized = Colorize(message)..blue();
46+
print(colorized);
3847
}
3948

4049
/// Returns [message] with escapes to print it in [color], if the environment

tool/lib/src/result.dart

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
/// Possible outcomes of a command run for a package.
22
enum RunState {
33
/// The command succeeded for the package.
4-
succeeded,
4+
success,
55

66
/// The command failed for the package.
7-
failed,
7+
failure,
88
}
99

1010
/// The result of a [runForPackage] call.
1111
class PackageResult {
1212
/// A successful result.
13-
PackageResult.success() : this._(RunState.succeeded);
13+
PackageResult.success() : this._(RunState.success, []);
1414

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

22-
const PackageResult._(this.state, [this.details = const <String>[]]);
21+
const PackageResult._(this.state, this.details);
2322

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

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

0 commit comments

Comments
 (0)