-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_generator.dart
130 lines (108 loc) · 3.77 KB
/
config_generator.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
import 'dart:io';
import 'dart:convert';
void main() {
final cg = ConfigGenerator();
final props = cg.readUserConfigValues();
// TODO: generate navigation bars and routes
cg.writeUserConfigValues(props);
}
/// Reads configuration values from users and generates a Dart config class.
/// See: ./lib/config.dart
class ConfigGenerator {
static const String doYouWantToUseX = 'Do you want to use';
static const String YES = 'yes';
static const String NO = 'no';
/// Prompts the user for all necessary properties and read them
/// from command line.
InfluxConfigProperties readUserConfigValues() {
final props = InfluxConfigProperties();
try {
var q = '$doYouWantToUseX an \'RSS Feed Widget\'?';
props.useRssFeedWidget = _readBoolValue(q, true, YES);
if (props.useRssFeedWidget) {
q = 'Enter the RSS feed\'s URL (e.g. \'http://rss.cnn.com/rss/edition.rss\'):';
props.rssFeedUrl = _readStringValue(q, null);
}
stdout.write('\n');
q = '$doYouWantToUseX a \'Youtube Channel Widget\'?';
props.useYoutubeChannelWidget = _readBoolValue(q, true, YES);
if (props.useRssFeedWidget) {
// TODO: read all Youtube Channel props
}
stdout.write('\n');
// TODO: read all other props...
} catch (e) {
stderr.writeln('Malformed input! Could not setup your App.');
}
return props;
}
/// Writes the properties object into the config.dart file.
void writeUserConfigValues(InfluxConfigProperties props) async {
final dartConfigFile = File('./lib/config.dart');
final Stream<String> dartConfigFileStream = dartConfigFile
.openRead()
.transform(utf8.decoder)
.transform(LineSplitter());
// match type/name tuples in the existing config class and replace their
// assigned values with the ones provided by the user:
final List<String> customizedLines = await dartConfigFileStream
.map((l) => _replaceIfContains(l, RegExp(r'MaterialColor primaryColor'), RegExp(r'Colors.blue'), props.primaryColor))
// TODO: also replace the other values...
.map((l) => _replaceIfContains(l, RegExp(r'String rssFeedUrl'), RegExp(r"'http://rss.cnn.com/rss/edition.rss'"), "'${props.rssFeedUrl}'"))
.toList();
final IOSink sink = dartConfigFile.openWrite();
customizedLines.forEach((line) {
sink.writeln(line);
});
await sink.flush();
await sink.close();
}
bool _readBoolValue(
String question, bool defaultValue, String defaultValueToDisplay) {
stdout.write(question + ' [$defaultValueToDisplay] ');
String input = stdin.readLineSync();
if (input.isEmpty) {
return defaultValue;
} else if (input.trim().toLowerCase() == YES) {
return true;
} else if (input.trim().toLowerCase() == NO) {
return false;
} else {
throw TypeError();
}
}
String _readStringValue(String question, String defaultValue) {
if (defaultValue != null) {
stdout.write(question + ' [$defaultValue] ');
} else {
stdout.write(question + ' ');
}
String input = stdin.readLineSync();
if (input.isEmpty) {
if (defaultValue != null) {
return defaultValue;
} else {
throw ArgumentError();
}
} else {
return input;
}
}
String _replaceIfContains(
String l, RegExp typeNameTuple, RegExp oldValue, String newValue) {
if (l.contains(typeNameTuple)) {
return l.replaceAll(oldValue, newValue);
}
return l;
}
}
class InfluxConfigProperties {
// RSS feed props:
bool useRssFeedWidget;
String rssFeedUrl = '\'\'';
// Youtube channel props:
bool useYoutubeChannelWidget;
// styling:
String primaryColor = 'Colors.red';
InfluxConfigProperties({this.useRssFeedWidget, this.useYoutubeChannelWidget});
}