This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
gen_scaffold_spec.dart
133 lines (117 loc) · 3.16 KB
/
gen_scaffold_spec.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* This utility generates a sample application descriptor that follows the
* standard mobile app template:
*
* - a sidebar containing a configurable number of tabs
* - for each tab a component of configurable depth and width
*/
library benchmark_generator.gen_scaffold_spec;
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart' as yaml;
// parsed command-line arguments
int tabCount;
int depth;
int branchingFactor;
int conditionalBranchingFactor;
// internals
final _argParser = new ArgParser();
int _componentIndex = 0;
final _components = <String>[];
main(List<String> rawArgs) {
if(!_parseArgs(rawArgs)) return;
final String tabs = new List.generate(tabCount, (int i) {
_generateTree('Tab${i}');
return '''
- Tab${i}:
branch: if
props: 1''';
}).join('\n');
print('''
entrypoint: Shell
Shell:
template:
${tabs}
${_components.join('\n\n')}
''');
}
void _generateTree(String rootComponent) {
_components.add('''
${rootComponent}:
template:
- div
${_generateChildren(branchingFactor).map((String s) => ' - ${s}').join('\n')}
''');
}
List<String> _generateChildren(int count, [int level = 0]) {
if (level == depth) return [];
final res = <String>[];
String childComponent(String name) => ' - ${name}';
String conditionalChildComponent(String name) {
return
''' - ${name}:
branch: if''';
}
for (int i = 0; i < count; i++) {
var cmpId = _componentIndex++;
var cmpName = 'Component${cmpId}';
res.add(cmpName);
_components.add('''
${cmpName}:
template:
- div:
props: 1
- div:
textBindings: 1
${_generateChildren(branchingFactor, level + 1).map(childComponent).join('\n')}
${_generateChildren(conditionalBranchingFactor, level + 1).map(conditionalChildComponent).join('\n')}
''');
}
return res;
}
bool _parseArgs(List<String> rawArgs) {
_addOption('tabs',
'number of tabs in sidebar',
abbr: 't',
callback: (val) {
tabCount = int.parse(val);
});
_addOption('depth',
'how deep is the compnent hierarchy for each tab',
abbr: 'd',
callback: (val) {
depth = int.parse(val);
});
_addOption('branching-factor',
'number of child components at each intermediate level',
abbr: 'b',
callback: (val) {
branchingFactor = int.parse(val);
});
_addOption('conditional-branching-factor',
'number of child components at intermediate levels guarded by "if"',
abbr: 'c',
callback: (val) {
conditionalBranchingFactor = int.parse(val);
});
try {
_argParser.parse(rawArgs);
return true;
} catch (e) {
print('Bad arguments: ${e}\n');
print('Usage:\n');
print(_argParser.usage);
return false;
}
}
void _addOption(String option, String description, {String abbr,
callback(String value)}) {
_argParser.addOption(option, abbr: abbr, valueHelp: description,
callback: (String value) {
if (value == null || value.isEmpty) {
throw '${option}${abbr != null ? ' (${abbr})' : ''} required: ${description}';
}
callback(value);
});
}