Add analyze command to moor cli tool, documentation site

This commit is contained in:
Simon Binder 2020-01-16 23:10:05 +01:00
parent 4a2184110f
commit d0cb2eec37
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 123 additions and 1 deletions

View File

@ -0,0 +1,71 @@
---
title: "Command line tools for moor"
description: A set of CLI tools to interact with moor files
url: /cli/
---
{{% alert title="Experimental" %}}
The moor cli tool is experimental at the moment. Please report all issues you can find.
{{% /alert %}}
## Usage
If your app depends on `moor_generator`, you're ready to use the CLI tool.
In this article, we'll use `pub run ...` to start the tool.
If you're using Flutter, you need to run `flutter pub run ...`.
In either case, the tool should be run from the same folder where you keep your
`pubspec.yaml`.
## Analyze
Runs moor's analyzer and linter across all `.moor` files in your project.
```
$ pub run moor_generator analyze
WARNING: For file test/data/tables/tables.moor:
WARNING: line 38, column 28: This table has columns without default values, so defaults can't be used for insert.
38 │ defaultConfig: INSERT INTO config DEFAULT VALUES;
│ ^^^^^^
INFO: Found 1 errors or problems
```
Exits with error code `1` if any error was found.
## Identify databases
This is more of a test command to test that moor's analyzer is working correctly.
It will identify all databases or daos defined in your project.
```
$ pub run moor_generator identify-databases
Starting to scan in /home/simon/IdeaProjects/moor/moor...
INFO: example/example.dart has moor databases or daos: Database
INFO: lib/src/data/database.dart has moor databases or daos: AppDatabase
INFO: test/fake_db.dart has moor databases or daos: TodoDb, SomeDao
```
## Schema tools
### Export
This subcommand expects two paths, a Dart file and a target. The Dart file should contain
excactly one class annotated with `@UseMoor`. Running the following command will export
the database schema to json.
```
$ pub run moor_generator schema dump path/to/databases.dart schema.json
```
The generated file (`schema.json` in this case) contains information about all
- tables, including detailed information about columns
- triggers
- indices
- `@create`-queries from included moor files
- dependecies thereof
The schema format is still work-in-progress and might change in the future.

View File

@ -2,6 +2,7 @@
- New `clientDefault` method for columns. It can be used for dynamic defaults that might be different for - New `clientDefault` method for columns. It can be used for dynamic defaults that might be different for
each row. For instance, you can generate a uuid for each row with `text().clientDefault(() => Uuid().v4()();` each row. For instance, you can generate a uuid for each row with `text().clientDefault(() => Uuid().v4()();`
- New CLI tool to analyze moor files: Learn more at [https://moor.simonbinder.eu/cli](https://moor.simonbinder.eu/cli)
- Ability to override the default `ValueSerializer` globally by using `moorRuntimeOptions.valueSerializer`. - Ability to override the default `ValueSerializer` globally by using `moorRuntimeOptions.valueSerializer`.
- Moor files: You can now explicitly declare column types in those cases that the analyzer can't - Moor files: You can now explicitly declare column types in those cases that the analyzer can't
infer it: infer it:

View File

@ -8,6 +8,7 @@ import 'package:moor_generator/src/backends/common/driver.dart';
import 'package:moor_generator/src/backends/standalone.dart'; import 'package:moor_generator/src/backends/standalone.dart';
import 'package:moor_generator/src/cli/project.dart'; import 'package:moor_generator/src/cli/project.dart';
import 'commands/analyze.dart';
import 'commands/debug_plugin.dart'; import 'commands/debug_plugin.dart';
import 'commands/identify_databases.dart'; import 'commands/identify_databases.dart';
import 'commands/schema.dart'; import 'commands/schema.dart';
@ -40,6 +41,7 @@ class MoorCli {
'CLI utilities for the moor package, currently in an experimental state.', 'CLI utilities for the moor package, currently in an experimental state.',
usageLineLength: 80, usageLineLength: 80,
) )
..addCommand(AnalyzeCommand(this))
..addCommand(IdentifyDatabases(this)) ..addCommand(IdentifyDatabases(this))
..addCommand(DebugPluginCommand(this)) ..addCommand(DebugPluginCommand(this))
..addCommand(SchemaCommand(this)); ..addCommand(SchemaCommand(this));

View File

@ -0,0 +1,45 @@
import 'dart:io';
import 'package:path/path.dart' as p;
import '../cli.dart';
class AnalyzeCommand extends MoorCommand {
AnalyzeCommand(MoorCli cli) : super(cli);
@override
String get description => 'Analyze and lint moor files';
@override
String get name => 'analyze';
@override
Future<void> run() async {
final driver = await cli.createMoorDriver();
var errorCount = 0;
await for (final file in cli.project.sourceFiles) {
if (p.extension(file.path) != '.moor') continue;
cli.logger.fine('Analyzing $file');
final parsed = await driver.waitFileParsed(file.path);
if (parsed.errors.errors.isNotEmpty) {
cli.logger.warning('For file ${p.relative(file.path)}:');
for (final error in parsed.errors.errors) {
error.writeDescription(cli.logger.warning);
errorCount++;
}
}
}
if (errorCount == 0) {
cli.logger.info('No errrors found');
} else {
cli.logger.info('Found $errorCount errors or problems');
exit(1);
}
}
}

View File

@ -16,7 +16,7 @@ class DumpSchemaCommand extends Command {
@override @override
String get invocation { String get invocation {
return '${runner.executableName} schema dump [arguments] <input <output>'; return '${runner.executableName} schema dump [arguments] <input> <output>';
} }
final MoorCli cli; final MoorCli cli;

View File

@ -43,6 +43,9 @@ dev_dependencies:
build_test: ^0.10.0 build_test: ^0.10.0
json_serializable: ^3.0.0 json_serializable: ^3.0.0
executables:
moor_generator:
dependency_overrides: dependency_overrides:
sqlparser: sqlparser:
path: ../sqlparser path: ../sqlparser