Skip to content

Commit 45ae3be

Browse files
Add git shorthand for dependencies
1 parent 005dc1f commit 45ae3be

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

benchmark/dart2js_output.dart

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import 'dart:convert';
21
import 'dart:io';
32

43
import 'package:args/command_runner.dart';
54
import 'package:meta/meta.dart';
65

76
import 'dart2js_output/compile.dart';
87
import 'dart2js_output/dart2js_normalize.dart';
8+
import 'dart2js_output/dependency.dart';
99
import 'dart2js_output/logging.dart';
1010
import 'dart2js_output/source.dart' as source;
1111

@@ -21,16 +21,6 @@ Future<void> main(List<String> args) async {
2121
await runner.run(args);
2222
}
2323

24-
final _originMasterDep = jsonEncode({
25-
'git': {
26-
'url': Directory.current.uri.toString(),
27-
'ref': 'origin/HEAD',
28-
}
29-
});
30-
final _localPathDep = jsonEncode({
31-
'path': Directory.current.path,
32-
});
33-
3424
abstract class BaseCommand extends Command {
3525
BaseCommand() {
3626
argParser.addFlag('verbose', defaultsTo: true, negatable: true);
@@ -48,20 +38,21 @@ abstract class CompareCommand extends BaseCommand {
4838
argParser.addOption(
4939
'head',
5040
help: 'Head over_react dependency to compare to the base.'
41+
' Supports dependencies entry in JSON format, or `git:<ref>` shorthand.'
5142
' Defaults to the enclosing local working copy of over_react.',
52-
defaultsTo: _localPathDep,
43+
defaultsTo: localPathDepString,
5344
);
5445
argParser.addOption(
5546
'base',
5647
help: 'Base over_react dependency to compare against.'
57-
' Defaults to origin/master.',
58-
defaultsTo: _originMasterDep,
48+
' Supports dependencies entry in JSON format, or `git:<ref>` shorthand.',
49+
defaultsTo: originHeadDepString,
5950
);
6051
}
6152

62-
dynamic get _baseDep => jsonDecode(argResults!['base'] as String);
53+
dynamic get _baseDep => parseDependency(argResults!['base'] as String);
6354

64-
dynamic get _headDep => jsonDecode(argResults!['head'] as String);
55+
dynamic get _headDep => parseDependency(argResults!['head'] as String);
6556
}
6657

6758
class CompareSizeCommand extends CompareCommand {
@@ -128,12 +119,13 @@ class GetCodeCommand extends BaseCommand {
128119
argParser.addOption(
129120
'dependency',
130121
help: 'over_react dependency to compile with.'
122+
' Supports dependencies entry in JSON format, or `git:<ref>` shorthand.'
131123
' Defaults to the enclosing local working copy of over_react.',
132-
defaultsTo: _localPathDep,
124+
defaultsTo: localPathDepString,
133125
);
134126
}
135127

136-
dynamic get _dep => jsonDecode(argResults!['dependency'] as String);
128+
dynamic get _dep => parseDependency(argResults!['dependency'] as String);
137129

138130
@override
139131
Future<void> run() async {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
const originHeadDepString = 'git:origin/HEAD';
5+
final localPathDepString = jsonEncode({
6+
'path': Directory.current.path,
7+
});
8+
9+
dynamic parseDependency(String dependency) {
10+
final gitShorthandMatch = RegExp(r'^git:(.+)$').matchAsPrefix(dependency);
11+
if (gitShorthandMatch != null) {
12+
return {
13+
'git': {
14+
'url': Directory.current.uri.toString(),
15+
'ref': gitShorthandMatch.group(1)!,
16+
}
17+
};
18+
}
19+
try {
20+
return jsonDecode(dependency);
21+
} catch (e) {
22+
throw ArgumentError.value(
23+
dependency, 'dependency', 'must be a valid JSON String or a git:<ref> shorthand');
24+
}
25+
}

0 commit comments

Comments
 (0)