Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Chore: fix linting issues" & fix linting #405

Merged
merged 3 commits into from
Jan 10, 2025
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
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ You can set up your local environment natively or using `docker`, check out the

Example of running all the checks with docker:
```bash
docker-compose run --rm package bash -c "dart pub get && dart run test --concurrency=4 && dart analyze && dart format . --set-exit-if-changed"
docker-compose run --rm package bash -c "dart pub get && dart pub get -C tool && dart run test --concurrency=4 && dart analyze && dart format . --set-exit-if-changed"
```

To install dependencies:

```bash
dart pub get
dart pub get -C tool
```

This package relies on [build_runner](https://pub.dev/packages/build_runner) to generate serialization information for some models, to re-generate files after making any changes, run:
Expand Down Expand Up @@ -91,14 +92,14 @@ The process to define a new code sample is as follows:
```
// #docregion meilisearch_contributing_1
final client = MeilisearchClient();
anything();
anything();
// #enddocregion
```
3. run this command to update the code samples
```bash
dart run ./tool/bin/meili.dart update-samples
```
4. to test if the code samples are updated correctly, run:
4. to test if the code samples are updated correctly, run:
```bash
dart run ./tool/bin/meili.dart update-samples --fail-on-change
```
Expand Down
6 changes: 0 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ 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
22 changes: 7 additions & 15 deletions test/utils/wait_for.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,19 @@ extension TaskWaiterForLists on Iterable<Task> {
bool throwFailed = true,
}) async {
final endingTime = DateTime.now().add(timeout);
final originalUids = List<Task>.from(this);
final remainingUids = <int>[];
for (final task in this) {
if (task.uid != null) {
remainingUids.add(task.uid!);
}
}
final originalUids = toList();
final remainingUids = map((e) => e.uid).nonNulls.toList();
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((Task e) => !statuses.contains(e.status));
final completed = tasks.where((e) => !statuses.contains(e.status));
if (throwFailed) {
final failed = completed
.firstWhereOrNull((Task element) => element.status != 'succeeded');
.firstWhereOrNull((element) => element.status != 'succeeded');
if (failed != null) {
throw MeiliSearchApiException(
"Task (${failed.uid}) failed",
Expand All @@ -68,15 +63,12 @@ extension TaskWaiterForLists on Iterable<Task> {
}
}

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

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

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

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

@override
final String description;

PackageCommand({
required this.name,
required this.description,
MeiliCommandBase(
this.packageDirectory, {
this.platform = const LocalPlatform(),
});

@override
Future<PackageResult> run();
/// The current platform.
///
/// This can be overridden for testing.
final Platform platform;

/// 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 => ' ';

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

import 'package:args/command_runner.dart';
import 'output_utils.dart';
import 'result.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 'update_samples_command.dart';

Future<void> main(List<String> args) async {
final runner = CommandRunner<PackageResult>(
'meili',
'Tool for managing Meilisearch Dart SDK.',
);
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;

runner.addCommand(UpdateSamplesCommand());
final Directory meilisearchDirectory = toolsDir.parent;

try {
final result = await runner.run(args);
if (result == null) {
// help command or similar was run
exit(0);
}
final commandRunner = CommandRunner<PackageResult>(
'dart run ./tool/bin/meili.dart', 'Productivity utils for meilisearch.')
..addCommand(UpdateSamplesCommand(meilisearchDirectory));

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}');
commandRunner.run(arguments).then((value) {
if (value == null) {
print('MUST output either a success or fail.');
assert(false);
io.exit(255);
}
switch (value.state) {
case RunState.succeeded:
printSuccess('Success!');
break;
case RunState.failed:
printError('Failed!');
if (value.details.isNotEmpty) {
printError(value.details.join('\n'));
}
exit(1);
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;
}
} catch (e, stack) {
printError('Unexpected error: $e\n$stack');
exit(1);
}
io.exit(exitCode);
}, test: (Object e) => e is ToolExit);
}
15 changes: 3 additions & 12 deletions tool/lib/src/output_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,17 @@ String _colorizeIfAppropriate(String string, Styles color) {

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

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

/// Prints [message] in red, if the environment supports color.
void printError(String message) {
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);
print(_colorizeIfAppropriate(message, Styles.RED));
}

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

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

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

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

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

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

/// Information about the result:
/// - For `success`, this is empty.
/// - For `failure`, it contains zero or more specific error details to be
/// - 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
/// shown in the summary.
final List<String> details;
}
Loading
Loading