Skip to content

Commit

Permalink
Verify analyzer target directory exists (#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
freemansoft authored Jul 25, 2023
1 parent b9f9586 commit 95fca24
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
26 changes: 26 additions & 0 deletions packages/riverpod_graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,29 @@ The graph generated is generated using [Mermaid](https://mermaid-js.github.io/me
Here is graph example, generated from the Flutter Devtool project (which uses Riverpod).

![Devtool graph](../../resources/devtool_graph.jpeg)

## Generating a graph

Assuming you are working on `riverpod_graph` in this repo. You can test against other projects with relative references. Generating a graph against the `examples/todos` project would look like:

```
cd <the riverpod_graph directory>
```

mermaid.js markup
```bash
dart run riverpod_graph path/to/folder
```

d2 markup
```bash
dart run riverpod_graph path/to/folder -f d2
```

Assuming you have activated, installed, riverpod_graph in the global dart cache:

mermaid.js markup
```bash
cd <the lib directory of the program you wish to analyze>
$ dart pub global run riverpod_graph .
```
32 changes: 29 additions & 3 deletions packages/riverpod_graph/lib/src/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Future<void> analyze(
resourceProvider: PhysicalResourceProvider.INSTANCE,
);

verifyRootDirectoryExists(rootDirectory);

// Often one context is returned, but depending on the project structure we
// can see multiple contexts.
for (final context in collection.contexts) {
Expand Down Expand Up @@ -88,6 +90,18 @@ Future<void> analyze(
}
}

///
///Throws an exception if the directory doesn't exist
///
bool verifyRootDirectoryExists(String rootDirectory) {
if (!Directory(rootDirectory).existsSync()) {
throw FileSystemException(
'Requested scanning target directory does not exist $rootDirectory',
);
}
return true;
}

/// Output formats supported by riverpod_graph
enum SupportFormat {
/// Mermaid.js format
Expand All @@ -101,7 +115,19 @@ String _buildD2(ProviderGraph providerGraph) {
const _watchLineStyle = '{style.stroke-width: 4}';
const _readLineStyle = '{style.stroke-dash: 4}';

final buffer = StringBuffer();
final buffer = StringBuffer('''
Legend: {
Type: {
Widget.shape: circle
Provider
}
Arrows: {
"." -> "..": read: {style.stroke-dash: 4}
"." -> "..": listen
"." -> "..": watch: {style.stroke-width: 4}
}
}
''');

for (final node in providerGraph.consumerWidgets) {
buffer.writeln('${node.definition.name}.shape: Circle');
Expand Down Expand Up @@ -156,10 +182,10 @@ flowchart TB
style stop1 height:0px;
start2[ ] --->|listen| stop2[ ]
style start2 height:0px;
style stop2 height:0px;
style stop2 height:0px;
start3[ ] ===>|watch| stop3[ ]
style start3 height:0px;
style stop3 height:0px;
style stop3 height:0px;
end
subgraph Type
Expand Down
20 changes: 20 additions & 0 deletions packages/riverpod_graph/test/analyze_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:io';

import 'package:riverpod_graph/src/analyze.dart';

import 'package:test/test.dart';

void main() {
group('flutter graph analyzer tests', () {
test('throw exception if analysis target directory does not exist', () {
expect(
() => verifyRootDirectoryExists('dogfood'),
throwsA(isA<FileSystemException>()),
);
});

test('returns true if analysis target directory does exist', () {
expect(verifyRootDirectoryExists('.'), true);
});
});
}

0 comments on commit 95fca24

Please sign in to comment.