-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.dart
352 lines (334 loc) · 11.4 KB
/
generate.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import 'dart:io';
Future<void> main(List<String> args) async {
if (args.length != 3 && args.length != 4) {
print('Expecting 4 or 3 parameters! See README!');
return;
}
String pluginName = args[0];
print('Plugin name: $pluginName');
String classCase = toClassCase(pluginName);
print('Class name: $classCase');
String bundle = args[1];
print('Bundle: $bundle');
String version = args[2];
print('Version: $version');
String? repository;
if (args.length == 4) {
repository = args[3];
print('Repository: $repository');
} else {
print('Repository: Not set!');
}
bool ios = false;
bool android = false;
bool web = false;
bool windows = false;
bool foundPlatformInterface = false;
bool foundMain = false;
File? licenseTemplate;
File? readmeTemplate;
File? changelogTemplate;
await for (FileSystemEntity entity in Directory.current.list()) {
String name = entityName(entity);
if (entity is Directory) {
if (name == 'NAME_ios') {
print('Targeting ios');
ios = true;
} else if (name == 'NAME_android') {
print('Targeting android');
android = true;
} else if (name == 'NAME_web') {
print('Targeting web');
web = true;
} else if (name == 'NAME_windows') {
print('Targeting windows');
windows = true;
} else if (name == 'NAME_platform_interface') {
foundPlatformInterface = true;
} else if (name == 'NAME') {
foundMain = true;
}
} else if (entity is File) {
if (name == 'FEDERATED_CHANGELOG_TEMPLATE.md') {
changelogTemplate = entity;
} else if (name == 'FEDERATED_README_TEMPLATE.md') {
readmeTemplate = entity;
} else if (name == 'FEDERATED_LICENSE_TEMPLATE') {
licenseTemplate = entity;
}
}
}
if (!foundMain) {
print('NAME template foulder not fond!');
return;
}
if (!foundPlatformInterface) {
print('NAME_platform_interface foulder not found!');
return;
}
if (licenseTemplate == null) {
print('FEDERATED_LICENSE_TEMPLATE not found!');
return;
}
if (readmeTemplate == null) {
print('FEDERATED_README_TEMPLATE not found!');
return;
}
if (changelogTemplate == null) {
print('FEDERATED_CHANGELOG_TEMPLATE not found!');
return;
}
if (!(ios || android || web || windows)) {
print('No target!');
return;
}
List<String> platforms = [];
if (ios) {
platforms.add('iOS');
}
if (android) {
platforms.add('Android');
}
if (web) {
platforms.add('Web');
}
if (windows) {
platforms.add('Windows');
}
List<Directory> flutterGetDirs = [];
for (String platform in platforms) {
String platformLower = platform.toLowerCase();
Directory directory = await Directory.current
.list()
.where((event) => event is Directory)
.cast<Directory>()
.firstWhere(
(Directory dir) => entityName(dir) == 'NAME_' + platformLower);
String newName = pluginName + '_' + platformLower;
directory = await directory.rename(newName);
await licenseTemplate.copy(directory.path + '/LICENSE');
File platformReadme =
await readmeTemplate.copy(directory.path + '/README.md');
await rewriteReadme(
platformReadme, pluginName, platform, version, classCase);
File platformChangelog =
await changelogTemplate.copy(directory.path + '/CHANGELOG.md');
await rewriteChangelog(platformChangelog, version);
await rewritePubspec(new File(directory.path + '/pubspec.yaml'), pluginName,
classCase, bundle, version, repository);
await for (FileSystemEntity entity in directory.list(recursive: true)) {
if (entity is File) {
if (entity.path.endsWith('.dart')) {
await rewriteDartFile(entity, pluginName, classCase);
}
}
}
await flutterCreatePlugin(directory, platform, bundle);
flutterGetDirs.add(directory);
}
Directory interfaceDirectory =
new Directory(Directory.current.path + '/NAME_platform_interface');
interfaceDirectory =
await interfaceDirectory.rename(pluginName + '_platform_interface');
File interfaceChangelog =
await changelogTemplate.copy(interfaceDirectory.path + '/CHANGELOG.md');
await rewriteChangelog(interfaceChangelog, version);
await rewritePlatformInterfaceReadme(
new File(interfaceDirectory.path + '/README.md'), pluginName, classCase);
await rewritePubspec(new File(interfaceDirectory.path + '/pubspec.yaml'),
pluginName, classCase, bundle, version, repository);
await licenseTemplate.copy(interfaceDirectory.path + '/LICENSE');
await for (FileSystemEntity entity
in interfaceDirectory.list(recursive: true)) {
if (entity is File) {
if (entity.path.endsWith('.dart')) {
await rewriteDartFile(entity, pluginName, classCase);
}
}
}
flutterGetDirs.add(interfaceDirectory);
Directory mainDirectory = new Directory(Directory.current.path + '/NAME');
mainDirectory = await mainDirectory.rename(pluginName);
File mainChangelog =
await changelogTemplate.copy(mainDirectory.path + '/CHANGELOG.md');
await rewriteChangelog(mainChangelog, version);
await rewriteMainPubspec(new File(mainDirectory.path + '/pubspec.yaml'),
pluginName, version, repository, ios, android, windows, web);
await licenseTemplate.copy(mainDirectory.path + '/LICENSE');
await for (FileSystemEntity entity in mainDirectory.list(recursive: true)) {
if (entity is File) {
if (entity.path.endsWith('.dart')) {
await rewriteDartFile(entity, pluginName, classCase);
}
}
}
flutterGetDirs.add(mainDirectory);
await flutterCreatePackage(mainDirectory);
await Future.wait(flutterGetDirs.reversed.map(flutterPackagesGet));
await format();
await cleanUp();
}
Future<void> flutterPackagesGet(Directory dir) async {
await Process.run('flutter', ['packages', 'get'],
workingDirectory: dir.absolute.path, runInShell: true);
}
Future<void> format() async {
await Process.run('dartfmt', ['-w', '.'],
workingDirectory: Directory.current.absolute.path, runInShell: true);
}
Future<void> flutterCreatePlugin(
Directory dir, String platform, String bundle) async {
platform = platform.toLowerCase();
await Process.run(
'flutter',
[
'create',
'--template=plugin',
'--platforms=$platform',
'--android-language=java',
'--org=$bundle',
'.'
],
runInShell: true,
workingDirectory: dir.absolute.path);
await new Directory(dir.path + '/test').delete(recursive: true);
if (platform == 'web') {
File f = await new Directory(dir.path + '/lib')
.list()
.where((FileSystemEntity e) => entityName(e).endsWith('_web_web.dart'))
.cast<File>()
.first;
await f.delete();
}
}
Future<void> flutterCreatePackage(Directory dir) async {
await Process.run('flutter', ['create', '--template=package', '.'],
runInShell: true, workingDirectory: dir.absolute.path);
await new Directory(dir.path + '/test').delete(recursive: true);
}
String entityName(FileSystemEntity entity) {
String name = entity.path.replaceAll('\\', '/');
int index = name.lastIndexOf('/');
if (index >= 0) {
name = name.substring(index + 1);
}
return name;
}
String toClassCase(String name) {
return name.split('_').map(capitalize).join();
}
String capitalize(String s) {
if (s.length == 0) {
return s;
} else if (s.length == 1) {
return s.toUpperCase();
} else {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
Future<void> rewriteReadme(File readme, String name, String platform,
String version, String className) async {
String underscorePlatform = '_' + platform.toLowerCase();
String content = await readme.readAsString();
content = content
.replaceAll('NAME', name)
.replaceAll('_PLATFORM', underscorePlatform)
.replaceAll('PLATFORM', platform)
.replaceAll('VERSION', version)
.replaceAll('CLASS', className);
await readme.writeAsString(content);
}
Future<void> rewritePlatformInterfaceReadme(
File readme, String name, String className) async {
String content = await readme.readAsString();
content = content.replaceAll('NAME', name).replaceAll('CLASS', className);
await readme.writeAsString(content);
}
Future<void> rewriteChangelog(File changelog, String version) async {
String content = await changelog.readAsString();
content = content.replaceAll('VERSION', version);
await changelog.writeAsString(content);
}
Future<void> rewriteDartFile(File file, String name, String className) async {
String content = await file.readAsString();
content = content.replaceAll('NAME', name).replaceAll('CLASS', className);
String fileName = entityName(file);
if (fileName.startsWith('NAME')) {
fileName = fileName.replaceAll('NAME', name);
await file.delete();
file = new File(file.parent.path + '/' + fileName);
}
await file.writeAsString(content);
}
Future<void> rewritePubspec(File file, String name, String className,
String bundleIdentifier, String version, String? repository) async {
String content = await file.readAsString();
content = content
.replaceAll('NAME', name)
.replaceAll('CLASS', className)
.replaceAll('BUNDLE', bundleIdentifier)
.replaceAll('VERSION', version);
if (repository != null) {
content = content.replaceAll('GITHUB', repository);
} else {
List<String> lines = content.split('\n');
lines.removeAt(2);
content = lines.join('\n');
}
await file.writeAsString(content, flush: true);
}
Future<void> rewriteMainPubspec(File file, String name, String version,
String? repository, bool ios, bool android, bool windows, bool web) async {
await rewritePubspec(file, name, '', '', version, repository);
String content = await file.readAsString();
List<String> lines = [];
Iterator<String> iterator = content.split('\n').iterator;
bool inFlutterSection = false;
while (iterator.moveNext()) {
if (inFlutterSection) {
String label = iterator.current.trim();
if (label == 'ios:' && !ios) {
iterator.moveNext();
continue;
} else if (label == 'android:' && !android) {
iterator.moveNext();
continue;
} else if (label == 'web:' && !web) {
iterator.moveNext();
continue;
} else if (label == 'windows:' && !windows) {
iterator.moveNext();
continue;
}
} else if (iterator.current.trimRight() == 'flutter:') {
inFlutterSection = true;
} else {
if (iterator.current.startsWith(' ' + name + '_ios') && !ios) {
iterator.moveNext();
continue;
} else if (iterator.current.startsWith(' ' + name + '_android') &&
!android) {
iterator.moveNext();
continue;
} else if (iterator.current.startsWith(' ' + name + '_web') && !web) {
iterator.moveNext();
continue;
} else if (iterator.current.startsWith(' ' + name + '_windows') &&
!windows) {
iterator.moveNext();
continue;
}
}
lines.add(iterator.current);
}
content = lines.join('\n');
await file.writeAsString(content);
}
Future<void> cleanUp() async {
await new File('README.md').delete();
await new File('FEDERATED_CHANGELOG_TEMPLATE.md').delete();
await new File('FEDERATED_README_TEMPLATE.md').delete();
await new File('FEDERATED_LICENSE_TEMPLATE').delete();
await new File('LICENSE').delete();
await new File('generate.dart').delete();
}