-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator_generate_command.dart
293 lines (238 loc) · 9.75 KB
/
configurator_generate_command.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
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:path/path.dart' as path;
import 'package:webtrit_phone_tools/src/commands/constants.dart';
import 'package:webtrit_phone_tools/src/extension/extension.dart';
const _bundleIdAndroid = 'bundleIdAndroid';
const _bundleIdIos = 'bundleIdIos';
const _keystorePath = 'keystore-path';
const _cacheSessionDataPath = 'cache-session-data-path';
const _directoryParameterName = '<directory>';
const _firebaseServiceAccountFileName = 'push_notifications/firebase-service-account.json';
class ConfiguratorGenerateCommand extends Command<int> {
ConfiguratorGenerateCommand({
required Logger logger,
}) : _logger = logger {
argParser
..addOption(
_keystorePath,
help: "Path to the project's keystore folder.",
)
..addOption(
_bundleIdAndroid,
help: 'Android application identifier.',
)
..addOption(
_bundleIdIos,
help: 'iOS application identifier.',
)
..addOption(
_cacheSessionDataPath,
help: 'Path to file which cache temporarily stores user session data to enhance performance '
'and maintain state across different processes.',
);
}
@override
String get name => 'configurator-generate';
@override
String get description {
final buffer = StringBuffer()
..writeln(
'Generate resources for customize application',
);
return buffer.toString();
}
final Logger _logger;
late String workingDirectoryPath;
@override
Future<int> run() async {
final commandArgResults = argResults!;
if (commandArgResults.rest.isEmpty) {
workingDirectoryPath = Directory.current.path;
} else if (commandArgResults.rest.length == 1) {
workingDirectoryPath = commandArgResults.rest[0];
} else {
_logger.err('Only one "$_directoryParameterName" parameter can be passed.');
return ExitCode.usage.code;
}
final cacheSessionDataPath = (commandArgResults[_cacheSessionDataPath] as String?) ?? defaultCacheSessionDataPath;
if (!File(cacheSessionDataPath).existsSync()) {
_logger.err(
'- The default cache_session_data.json file was not used, generated, or prepared before running this script, '
'or the custom path to cache session data was not provided correctly.');
return ExitCode.data.code;
}
final cacheSessionData = File(_workingDirectory(relativePath: cacheSessionDataPath)).readAsStringSync().toMap();
final projectKeystorePathArg = commandArgResults[_keystorePath] as String?;
final projectKeystorePathBuildConfig = cacheSessionData[keystorePathField] as String?;
final projectKeystorePath = projectKeystorePathArg ?? projectKeystorePathBuildConfig ?? '';
final bundleIdAndroid =
(commandArgResults[_bundleIdAndroid] as String?) ?? cacheSessionData[bundleIdAndroidField] as String?;
if ((bundleIdAndroid ?? '').isEmpty) {
_logger.err('Option "$_bundleIdAndroid" can not be empty.');
return ExitCode.usage.code;
}
final bundleIdIos = (commandArgResults[_bundleIdIos] as String?) ?? cacheSessionData[bundleIdIosField] as String?;
if ((bundleIdIos ?? '').isEmpty) {
_logger.err('Option "$_bundleIdIos" can not be empty.');
return ExitCode.usage.code;
}
if (projectKeystorePath.isEmpty) {
_logger.err(
'The option $_keystorePath cannot be empty and must be provided as a parameter or through $defaultCacheSessionDataPath',
);
return ExitCode.usage.code;
}
_logger.info('- Keystore path: $projectKeystorePath');
if (!Directory(projectKeystorePath).existsSync()) {
_logger.err('- Directory does not exist: $projectKeystorePath');
return ExitCode.data.code;
}
if ((Directory(projectKeystorePath).statSync().mode & 0x124) == 0) {
_logger.err('- No read permissions for file: $projectKeystorePath');
return ExitCode.data.code;
}
final firebaseServiceAccountPath = path.join(projectKeystorePath, _firebaseServiceAccountFileName);
final firebaseServiceAccount = File(firebaseServiceAccountPath).readAsStringSync().toMap();
final firebaseAccountId = firebaseServiceAccount[projectIdField] as String;
final workingDirectory = _workingDirectory();
// Setup dependencies for the proper functioning of the configuration script
await _setupDependencies(workingDirectory);
_logger
..info('- Platform identifier android: $bundleIdAndroid')
..info('- Platform identifier ios: $bundleIdIos')
..info('- Scripts working directory: $workingDirectory')
..info('- Service account path: $firebaseServiceAccountPath');
// Configure firebase for all supported platforms
await _configureFirebase(firebaseAccountId, bundleIdAndroid, bundleIdIos, firebaseServiceAccountPath);
// Configure launch icons for all supported platforms
await _configureLaunchIcons(workingDirectory);
// Configure splash screen for all supported platforms
await _configureSplash(workingDirectory);
// Configure platforms bundle id and package name
await _configurePlatformsIdentifiers(workingDirectory);
// Configure localization for support languages
await _configureLocalization(workingDirectory);
// Configure assets, especially if they are removed unexpectedly
await _configureAssets(workingDirectory);
return ExitCode.success.code;
}
Future<void> _setupDependencies(String workingDirectory) async {
final flutterGenDependencyProcess = await Process.run(
'dart',
['pub', 'global', 'activate', 'flutter_gen'],
workingDirectory: workingDirectory,
);
_logger
..info(flutterGenDependencyProcess.stdout.toString())
..err(flutterGenDependencyProcess.stderr.toString())
..info('Flutter gen generation finished with: ${flutterGenDependencyProcess.exitCode}');
final firebaseDependencyProgress = await Process.run(
'dart',
['pub', 'global', 'activate', 'flutterfire_cli'],
workingDirectory: workingDirectory,
);
_logger
..info(firebaseDependencyProgress.stdout.toString())
..err(firebaseDependencyProgress.stderr.toString())
..info('Package renaming finished with: ${firebaseDependencyProgress.exitCode}');
final packageRenameDependencyProgress = await Process.run(
'dart',
['pub', 'add', 'package_rename'],
workingDirectory: workingDirectory,
);
_logger
..info(packageRenameDependencyProgress.stdout.toString())
..err(packageRenameDependencyProgress.stderr.toString())
..info('Package renaming finished with: ${packageRenameDependencyProgress.exitCode}');
}
Future<void> _configureFirebase(
String firebaseAccountId,
String? bundleIdAndroid,
String? bundleIdIos,
String firebaseServiceAccountPath,
) async {
final workingDirectory = _workingDirectory();
_logger.info('Configure $firebaseAccountId google services');
final process = await Process.run(
'flutterfire',
[
'configure',
'--yes',
'--project=$firebaseAccountId',
'--android-package-name=$bundleIdAndroid',
'--ios-bundle-id=$bundleIdIos',
'--macos-bundle-id=',
'--web-app-id=',
'--windows-app-id=',
'--service-account=$firebaseServiceAccountPath',
'--platforms',
'android,ios',
],
workingDirectory: workingDirectory,
);
_logger
..info(process.stdout.toString())
..err(process.stderr.toString())
..info('flutterfire finished with: ${process.exitCode}');
}
Future<void> _configureSplash(String workingDirectory) async {
final nativeSplashProcess = await Process.run(
'dart',
['run', 'flutter_native_splash:create'],
workingDirectory: workingDirectory,
);
_logger
..info(nativeSplashProcess.stdout.toString())
..err(nativeSplashProcess.stderr.toString())
..info('Native splash generation finished with: ${nativeSplashProcess.exitCode}');
}
Future<void> _configureLaunchIcons(String workingDirectory) async {
final flutterIconsProcess = await Process.run(
'flutter',
['pub', 'run', 'flutter_launcher_icons'],
workingDirectory: workingDirectory,
);
_logger
..info(flutterIconsProcess.stdout.toString())
..err(flutterIconsProcess.stderr.toString())
..info('Flutter icons generation finished with: ${flutterIconsProcess.exitCode}');
}
Future<void> _configureAssets(String workingDirectory) async {
final flutterGenProcess = await Process.run(
'fluttergen',
[],
workingDirectory: workingDirectory,
);
_logger
..info(flutterGenProcess.stdout.toString())
..err(flutterGenProcess.stderr.toString())
..info('Flutter gen finished with: ${flutterGenProcess.exitCode}');
}
Future<void> _configureLocalization(String workingDirectory) async {
final flutterL10nProcess = await Process.run(
'flutter',
['gen-l10n'],
workingDirectory: workingDirectory,
);
_logger
..info(flutterL10nProcess.stdout.toString())
..err(flutterL10nProcess.stderr.toString())
..info('Localization generation finished with: ${flutterL10nProcess.exitCode}');
}
Future<void> _configurePlatformsIdentifiers(String workingDirectory) async {
final packageRenameProcess = await Process.run(
'dart',
['run', 'package_rename'],
workingDirectory: workingDirectory,
);
_logger
..info(packageRenameProcess.stdout.toString())
..err(packageRenameProcess.stderr.toString())
..info('Package renaming finished with: ${packageRenameProcess.exitCode}');
}
String _workingDirectory({String relativePath = ''}) {
return path.normalize(path.join(workingDirectoryPath, relativePath));
}
}