Skip to content

Commit 00fe326

Browse files
authored
feat: Convert the package into a proper Command Line application (#6)
The package can now be activated and then run as a CLI tool. Closes #4
1 parent 7d99969 commit 00fe326

File tree

5 files changed

+90
-73
lines changed

5 files changed

+90
-73
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
4+
## 0.1.0
5+
6+
- Initial release.

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ This is a command line tool that can parse your Dart files to extract API inform
44
save that information into a file in JSON format. This output can then be consumed by downstream
55
tools for a variety of purposes, for example to generate documentation.
66

7+
Install:
8+
```shell
9+
dart pub global activate dartdoc_json
10+
```
11+
712
Usage:
813
```shell
9-
dart run dartdoc_json.dart FILENAME(s)
14+
dartdoc_json FILENAME(s)
1015
```
1116

1217

bin/main.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
import 'package:analyzer/dart/analysis/features.dart';
4+
import 'package:analyzer/dart/analysis/utilities.dart';
5+
import 'package:args/args.dart';
6+
import 'package:dartdoc_json/dartdoc_json.dart';
7+
import 'package:path/path.dart' as path;
8+
9+
void main(List<String> args) {
10+
exitCode = 0;
11+
final parser = ArgParser(usageLineLength: 80)
12+
..addOption(
13+
'root',
14+
abbr: 'r',
15+
defaultsTo: '.',
16+
help: 'The path to the root folder of the package. The input file names '
17+
'will be resolved relative to this folder.',
18+
)
19+
..addOption(
20+
'output',
21+
abbr: 'o',
22+
defaultsTo: 'out.json',
23+
help: 'Output file where the JSON will be written to.',
24+
)
25+
..addFlag(
26+
'pretty',
27+
negatable: false,
28+
help: 'Pretty-print the output JSON file.',
29+
);
30+
final arguments = parser.parse(args);
31+
final root = arguments['root'] as String;
32+
final output = arguments['output'] as String;
33+
final pretty = arguments['pretty'] as bool;
34+
final inputs = arguments.rest;
35+
if (inputs.isEmpty) {
36+
stdout.writeln(
37+
'A command-line utility that can extract the API of dart file(s) and\n'
38+
'save it in JSON format.',
39+
);
40+
stdout.writeln();
41+
stdout.writeln('Usage: dart run dartdoc_json.dart <input> [Parameters]');
42+
stdout.writeln();
43+
stdout.writeln('Parameters:');
44+
stdout.writeln(parser.usage);
45+
exitCode = 1;
46+
return;
47+
}
48+
if (!Directory(root).existsSync()) {
49+
stderr.writeln('Directory `$root` does not exist');
50+
exitCode = 2;
51+
return;
52+
}
53+
final encoder =
54+
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
55+
56+
final result = <Map<String, dynamic>>[];
57+
for (final input in inputs) {
58+
final fullPath = path.canonicalize(path.join(root, input));
59+
if (!File(fullPath).existsSync()) {
60+
stderr.writeln('File `$fullPath` does not exist');
61+
exitCode = 2;
62+
return;
63+
}
64+
final parsed = parseFile(
65+
path: fullPath,
66+
featureSet: FeatureSet.latestLanguageVersion(),
67+
);
68+
final unit = serializeCompilationUnit(parsed.unit);
69+
unit['source'] = input;
70+
result.add(unit);
71+
}
72+
final json = encoder.convert(result);
73+
File(output).writeAsStringSync(json);
74+
}

lib/dartdoc_json.dart

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1 @@
1-
import 'dart:convert';
2-
import 'dart:io';
3-
import 'package:analyzer/dart/analysis/features.dart';
4-
import 'package:analyzer/dart/analysis/utilities.dart';
5-
import 'package:args/args.dart';
6-
import 'package:dartdoc_json/src/compilation_unit.dart';
7-
import 'package:path/path.dart' as path;
8-
9-
int main(List<String> args) {
10-
final parser = ArgParser(usageLineLength: 80)
11-
..addOption(
12-
'root',
13-
abbr: 'r',
14-
defaultsTo: '.',
15-
help: 'The path to the root folder of the package. The input file names '
16-
'will be resolved relative to this folder.',
17-
)
18-
..addOption(
19-
'output',
20-
abbr: 'o',
21-
defaultsTo: 'out.json',
22-
help: 'Output file where the JSON will be written to.',
23-
)
24-
..addFlag(
25-
'pretty',
26-
negatable: false,
27-
help: 'Pretty-print the output JSON file.',
28-
);
29-
final arguments = parser.parse(args);
30-
final root = arguments['root'] as String;
31-
final output = arguments['output'] as String;
32-
final pretty = arguments['pretty'] as bool;
33-
final inputs = arguments.rest;
34-
if (inputs.isEmpty) {
35-
// ignore_for_file: avoid_print
36-
print(
37-
'A command-line utility that can extract the API of dart file(s) and\n'
38-
'save it in JSON format.',
39-
);
40-
print('');
41-
print('Usage: dart run dartdoc_json.dart <input> [Parameters]');
42-
print('');
43-
print('Parameters:');
44-
print(parser.usage);
45-
return 1;
46-
}
47-
if (!Directory(root).existsSync()) {
48-
print('Directory `$root` does not exist');
49-
return 2;
50-
}
51-
final encoder =
52-
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
53-
54-
final result = <Map<String, dynamic>>[];
55-
for (final input in inputs) {
56-
final fullPath = path.canonicalize(path.join(root, input));
57-
if (!File(fullPath).existsSync()) {
58-
print('File `$fullPath` does not exist');
59-
return 2;
60-
}
61-
final parsed = parseFile(
62-
path: fullPath,
63-
featureSet: FeatureSet.latestLanguageVersion(),
64-
);
65-
final unit = serializeCompilationUnit(parsed.unit);
66-
unit['source'] = input;
67-
result.add(unit);
68-
}
69-
final json = encoder.convert(result);
70-
File(output).writeAsStringSync(json);
71-
return 0;
72-
}
1+
export 'src/compilation_unit.dart' show serializeCompilationUnit;

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ dependencies:
1818
dev_dependencies:
1919
flame_lint: ^0.1.3
2020
test: any
21+
22+
executables:
23+
dartdoc_json: main

0 commit comments

Comments
 (0)