-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename.dart
400 lines (337 loc) · 13.6 KB
/
rename.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import 'dart:io';
import 'dart:async';
class Path{
String androidManifestPath = ".\\android\\app\\src\\main\\AndroidManifest.xml";
String androidProfilePath = ".\\android\\app\\src\\profile\\AndroidManifest.xml";
String androidDebugPath = ".\\android\\app\\src\\debug\\AndroidManifest.xml";
String androidKotlinPath = ".\\android\\app\\src\\main\\kotlin\\"; //additional
String iosInfoPlistPath = ".\\ios\\Runner\\Info.plist";
String androidAppBuildGradlePath = ".\\android\\app\\build.gradle";
String iosProjectPbxprojPath = ".\\ios\\Runner.xcodeproj\\project.pbxproj";
String macosAppInfoxprojPath = ".\\macos\\Runner\\Configs\\AppInfo.xcconfig";
String macosRootPath = ".\\macos";
String pubspec = ".\\pubspec.yaml";
String appLibPath = ".\\lib\\";
Path._privateConstructor();
static final Path _instance = Path._privateConstructor();
static Path get instance {
if (Platform.isMacOS || Platform.isLinux) {
_instance.androidManifestPath = "android/app/src/main/AndroidManifest.xml";
_instance.androidProfilePath = "android/app/src/profile/AndroidManifest.xml";
_instance.androidDebugPath = "android/app/src/debug/AndroidManifest.xml";
_instance.androidKotlinPath = "android/app/src/main/kotlin/"; //additional
_instance.iosInfoPlistPath = "ios/Runner/Info.plist";
_instance.androidAppBuildGradlePath = "android/app/build.gradle";
_instance.iosProjectPbxprojPath = "ios/Runner.xcodeproj/project.pbxproj";
_instance.macosAppInfoxprojPath = "macos/Runner/Configs/AppInfo.xcconfig";
_instance.macosRootPath = "macos";
_instance.pubspec = "pubspec.yaml";
_instance.appLibPath = "lib/";
}
return _instance;
}
}
class Utils{
static Future<List<String>> readFileAsLineByline({String filePath}) async {
String fileAsString = await File(filePath).readAsString();
return fileAsString.split("\n");
}
static Future<File> writeFile({String filePath, String content}) {
return File(filePath).writeAsString(content);
}
// ignore: missing_return
static Future<String> getCurrentIosAppName() async {
List contentLineByLine = await readFileAsLineByline(
filePath: Path.instance.iosInfoPlistPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("<key>CFBundleName</key>")) {
return (contentLineByLine[i + 1] as String).trim().substring(5, 5);
}
}
}
// ignore: missing_return
static Future<String> getCurrentAndroidAppName() async {
List contentLineByLine = await readFileAsLineByline(
filePath: Path.instance.androidManifestPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("android:label")) {
return (contentLineByLine[i] as String).split('"')[1];
}
}
}
// ignore: missing_return
static Future<String> getCurrentAndroidBundleId() async {
List contentLineByLine = await readFileAsLineByline(
filePath: Path.instance.androidManifestPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("package")) {
return (contentLineByLine[i] as String).split('"')[1];
}
}
}
}
class Android{
Future<File> changeAndroidAppName(String appName) async {
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.androidManifestPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("android:label")) {
contentLineByLine[i] = " android:label=\"${appName}\"";
break;
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.androidManifestPath,
content: contentLineByLine.join('\n'),
);
print("Android appname changed successfully to : $appName");
return writtenFile;
}
Future<void> changeAndroidBundleId(String oldId, String newId) async{
print("OLD: $oldId >> NEW: $newId");
var currentPackageDir = oldId.replaceAll('.', '\\') + '\\';
var newPackageDir = newId.replaceAll('.', '\\') + '\\';
if (Platform.isMacOS || Platform.isLinux) {
currentPackageDir = oldId.replaceAll('.', '/') + '/';
newPackageDir = oldId.replaceAll('.', '/') + '/';
}
print("CurrentPkgDir:: $currentPackageDir");
var mainActivityPath = Path.instance.androidKotlinPath + currentPackageDir + "MainActivity.kt";
var mainActivityRead = await Utils.readFileAsLineByline(
filePath: mainActivityPath,
);
for (int i = 0; i < mainActivityRead.length; i++) {
if (mainActivityRead[i].contains("package $oldId")) {
mainActivityRead[i] = "package $newId";
break;
}
}
var findFirstDomainNameForDir = oldId.split('.')[0].toString();
var domainDirForDelete = Directory(Path.instance.androidKotlinPath + findFirstDomainNameForDir);
domainDirForDelete.deleteSync(recursive: true);
var newDomainDirectoryPath = Path.instance.androidKotlinPath + newPackageDir;
print("newDomainDirectoryPath: ${newDomainDirectoryPath.toString()}");
new Directory(newDomainDirectoryPath).create(recursive: true).then((Directory newDir) async{
print("CreatednewPath:: ${newDir.path}");
await Utils.writeFile(
filePath: newDir.path + "MainActivity.kt",
content: mainActivityRead.join('\n'),
);
});
print("Android bundleId package changed with folder system");
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.androidAppBuildGradlePath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("applicationId")) {
contentLineByLine[i] = " applicationId \"${newId.toString()}\"";
break;
}
}
await Utils.writeFile(
filePath: Path.instance.androidAppBuildGradlePath,
content: contentLineByLine.join('\n'),
);
print("Android bundleId changed successfully to : $newId");
List contentLineByLine2 = await Utils.readFileAsLineByline(
filePath: Path.instance.androidManifestPath,
);
for (int i = 0; i < contentLineByLine2.length; i++) {
if (contentLineByLine2[i].contains("package")) {
contentLineByLine2[i] = " package=\"${newId.toString()}\">";
break;
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.androidManifestPath,
content: contentLineByLine2.join('\n'),
);
print("Android manifest package name changed to : $newId");
List contentLineByLine3 = await Utils.readFileAsLineByline(
filePath: Path.instance.androidProfilePath,
);
for (int i = 0; i < contentLineByLine3.length; i++) {
if (contentLineByLine3[i].contains("package")) {
contentLineByLine3[i] = " package=\"${newId.toString()}\">";
break;
}
}
File writtenFile3 = await Utils.writeFile(
filePath: Path.instance.androidProfilePath,
content: contentLineByLine3.join('\n'),
);
print("Android profile package name changed to : $newId");
List contentLineByLine4 = await Utils.readFileAsLineByline(
filePath: Path.instance.androidDebugPath,
);
for (int i = 0; i < contentLineByLine4.length; i++) {
if (contentLineByLine4[i].contains("package")) {
contentLineByLine4[i] = " package=\"${newId.toString()}\">";
break;
}
}
File writtenFile4 = await Utils.writeFile(
filePath: Path.instance.androidDebugPath,
content: contentLineByLine4.join('\n'),
);
print("Android debug package name changed to : $newId");
}
}
class Apple {
Future<File> changeIosBundleId({String bundleId}) async {
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.iosProjectPbxprojPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("PRODUCT_BUNDLE_IDENTIFIER")) {
contentLineByLine[i] = " PRODUCT_BUNDLE_IDENTIFIER = $bundleId;";
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.iosProjectPbxprojPath,
content: contentLineByLine.join('\n'),
);
print("IOS BundleIdentifier changed successfully to : $bundleId");
return writtenFile;
}
Future<File> changeMacOsBundleId({String bundleId}) async {
bool dirExist = await Directory(Path.instance.macosRootPath).exists();
if (dirExist){
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.macosAppInfoxprojPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("PRODUCT_BUNDLE_IDENTIFIER")) {
contentLineByLine[i] = "PRODUCT_BUNDLE_IDENTIFIER = $bundleId;";
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.macosAppInfoxprojPath,
content: contentLineByLine.join('\n'),
);
print("MacOS BundleIdentifier changed successfully to : $bundleId");
return writtenFile;
}
return null;
}
Future<File> changeIosAppName(String appName) async {
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.iosInfoPlistPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("<key>CFBundleName</key>")) {
contentLineByLine[i + 1] = "\t<string>${appName.toString()}</string>\r";
break;
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.iosInfoPlistPath,
content: contentLineByLine.join('\n'),
);
print("IOS appname changed successfully to : $appName");
return writtenFile;
}
Future<File> changeMacOsAppName(String appName) async {
bool dirExist = await Directory(Path.instance.macosRootPath).exists();
if (dirExist){
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.macosAppInfoxprojPath,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("PRODUCT_NAME")) {
contentLineByLine[i] = "PRODUCT_NAME = $appName;";
break;
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.macosAppInfoxprojPath,
content: contentLineByLine.join('\n'),
);
print("MacOS appname changed successfully to : $appName");
return writtenFile;
}
return null;
}
}
class PubSpec{
Future<File> change(String appName, String description) async {
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: Path.instance.pubspec,
);
int j = 0;
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("name:")) {
contentLineByLine[i] = "name: $appName";
j++;
}
if (contentLineByLine[i].contains("description:")) {
contentLineByLine[i] = "description: $description";
j++;
}
if (j == 2){
break;
}
}
File writtenFile = await Utils.writeFile(
filePath: Path.instance.pubspec,
content: contentLineByLine.join('\n'),
);
print("Updated pubspec.yaml: $appName | $description");
return writtenFile;
}
Future<void> renameAllPackageNamesUnderLibFolder(String oldPackageName, String newPackageName) async{
Directory lib = new Directory(Path.instance.appLibPath);
lib.list(recursive: true).forEach((file) async{
if (file.path.split('.').last == "dart"){
List contentLineByLine = await Utils.readFileAsLineByline(
filePath: file.path,
);
for (int i = 0; i < contentLineByLine.length; i++) {
if (contentLineByLine[i].contains("import 'package:$oldPackageName")) {
contentLineByLine[i] = contentLineByLine[i].toString().replaceAll("package:$oldPackageName", "package:$newPackageName");
// print("${file.path}:: " + contentLineByLine[i]);
// print("REPLACE_TEST:: " + contentLineByLine[i].toString().replaceAll("package:$oldPackageName", "package:$newPackageName"));
}
}
File writtenFile = await Utils.writeFile(
filePath: file.path,
content: contentLineByLine.join('\n'),
);
}
});
print("Updated lib under dart files' import package names: $oldPackageName -> $newPackageName");
}
}
// store build.enb
Map<String, String> build;
void main(List<String> args) async{
// init empty build.env object
build = Map();
// Storage to build variable from build.env file
List<String> lines = new File('build.env').readAsLinesSync();
for (var line in lines) {
if (line.isNotEmpty && line[0] != "#"){ //skip comment line and empty line
var splitted = line.split("=");
build[splitted[0]] = splitted[1];
}
}
Android android = new Android();
Apple apple = new Apple();
PubSpec pubspec = new PubSpec();
var currentPackageName = await Utils.getCurrentAndroidBundleId();
await android.changeAndroidAppName(build["APPLICATION_NAME"]);
await android.changeAndroidBundleId(currentPackageName, build["DOMAIN_NAME"]);
await apple.changeIosAppName(build["APPLICATION_NAME"]);
await apple.changeIosBundleId(bundleId: build["DOMAIN_NAME"]);
await apple.changeMacOsAppName(build["APPLICATION_NAME"]);
await apple.changeMacOsBundleId(bundleId: build["DOMAIN_NAME"]);
await pubspec.change(build["FLUTTER_PACKAGE_NAME"], build["APPLICATON_DESCRIPTION"]);
await pubspec.renameAllPackageNamesUnderLibFolder(currentPackageName, build["FLUTTER_PACKAGE_NAME"]);
print("----------------------------------");
print("COMPLETED RENAME PROCESSING");
print("----------------------------------");
}