Skip to content

Commit 4fb0daa

Browse files
committed
Refactor a bit and skip useless changes
1 parent 60545a5 commit 4fb0daa

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

lib/src/changes/files_to_modify_content.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,31 @@ List<RequiredChange> getFilesToModifyContent(
99
regexp: RegExp(config.oldDartPackageName),
1010
replacement: config.newDartPackageName,
1111
paths: ["pubspec.yaml"],
12+
needChanges: config.oldDartPackageName != config.newDartPackageName,
1213
),
1314
RequiredChange(
1415
regexp: RegExp('android:label="${config.oldAppName}"'),
1516
replacement: 'android:label="${config.newAppName}"',
1617
paths: ["android/app/src/main/AndroidManifest.xml"],
18+
needChanges: config.oldAppName != config.newAppName,
1719
),
1820
RequiredChange(
1921
regexp: RegExp('applicationId "${config.oldApplicationId}"'),
2022
replacement: 'applicationId "${config.newApplicationId}"',
2123
paths: ["android/app/build.gradle"],
24+
needChanges: config.oldApplicationId != config.newApplicationId,
2225
),
2326
RequiredChange(
2427
regexp: RegExp(config.oldBundleId),
2528
replacement: config.newBundleId,
2629
paths: ["ios/Runner.xcodeproj/project.pbxproj"],
30+
needChanges: config.oldBundleId != config.newBundleId,
2731
),
2832
RequiredChange(
2933
regexp: RegExp(config.oldAppName),
3034
replacement: config.newAppName,
3135
paths: ["ios/Runner/Info.plist"],
36+
needChanges: config.oldAppName != config.newAppName,
3237
),
3338
RequiredChange(
3439
regexp: RegExp(config.oldAndroidPackageName),
@@ -37,6 +42,7 @@ List<RequiredChange> getFilesToModifyContent(
3742
"android/app/src",
3843
],
3944
isDirectory: true,
45+
needChanges: config.oldAndroidPackageName != config.newAndroidPackageName,
4046
),
4147
];
4248
}

lib/src/changes/files_to_modify_name.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ List<RequiredChange> getFilesToModifyName(
99
regexp: RegExp(config.oldDartPackageName),
1010
replacement: config.newDartPackageName,
1111
paths: ["android/${config.oldDartPackageName}_android.iml"],
12+
needChanges: config.oldDartPackageName != config.newDartPackageName,
1213
),
1314
];
1415
}

lib/src/flutter_rename_app.dart

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'utils/get_config.dart';
1616
renameApp() async {
1717
try {
1818
final Config config = await getConfig();
19+
1920
Logger.info("Current app name: ${config.oldAppName}");
2021
Logger.info("New name will be: ${config.newAppName}");
2122

@@ -62,6 +63,8 @@ renameApp() async {
6263
}
6364
}
6465

66+
/// Change all imports in given path (recursively)
67+
/// Needed in order to change dart package name in lib or test
6568
_changeAllImportsIn(String directoryPath, Config config) {
6669
final Directory directory = Directory(directoryPath);
6770
if (directory.existsSync()) {
@@ -80,29 +83,36 @@ _changeAllImportsIn(String directoryPath, Config config) {
8083
}
8184
}
8285

86+
/// Apply the list of required files name changes
8387
_applyNameChanges(List<RequiredChange> requiredChanges) {
8488
requiredChanges.forEach((RequiredChange change) {
85-
for (final path in change.paths) {
86-
_changeFileName(path, change.regexp, change.replacement);
89+
if (change.needChanges) {
90+
for (final path in change.paths) {
91+
_changeFileName(path, change.regexp, change.replacement);
92+
}
8793
}
8894
});
8995
}
9096

97+
/// Apply the list of required files content changes
9198
_applyContentChanges(List<RequiredChange> requiredChanges) {
9299
requiredChanges.forEach((RequiredChange change) {
93-
for (final path in change.paths) {
94-
if (change.isDirectory) {
95-
final Directory directory = Directory(path);
96-
directory.listSync(recursive: true).forEach((FileSystemEntity entity) {
97-
_changeContentInFile(entity.path, change.regexp, change.replacement);
98-
});
99-
} else {
100-
_changeContentInFile(path, change.regexp, change.replacement);
100+
if (change.needChanges) {
101+
for (final path in change.paths) {
102+
if (change.isDirectory) {
103+
final Directory directory = Directory(path);
104+
directory.listSync(recursive: true).forEach((FileSystemEntity entity) {
105+
_changeContentInFile(entity.path, change.regexp, change.replacement);
106+
});
107+
} else {
108+
_changeContentInFile(path, change.regexp, change.replacement);
109+
}
101110
}
102111
}
103112
});
104113
}
105114

115+
/// Change the name of the File at the given path by the given replacement name
106116
_changeFileName(String filePath, RegExp regexp, String replacement) {
107117
if (filePath.contains(regexp)) {
108118
try {
@@ -112,14 +122,15 @@ _changeFileName(String filePath, RegExp regexp, String replacement) {
112122
}
113123
}
114124

125+
/// Change content of the given File by the given content
115126
_changeContentInFile(String filePath, RegExp regexp, String replacement) {
116127
try {
117128
final File file = File(filePath);
118129
final String content = file.readAsStringSync();
119130
if (content.contains(regexp)) {
120131
final String newContent = content.replaceAll(regexp, replacement);
121132
file.writeAsStringSync(newContent);
122-
Logger.info("Changed file $filePath");
133+
Logger.info("$filePath has been modified");
123134
}
124135
} catch (error) {}
125136
}

lib/src/models/required_change.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ class RequiredChange {
33
final String replacement;
44
final List<String> paths;
55
final bool isDirectory;
6+
final bool needChanges;
67

78
RequiredChange({
89
this.regexp,
910
this.replacement,
1011
this.paths,
1112
this.isDirectory = false,
13+
this.needChanges = true,
1214
});
1315
}

lib/src/utils/change_android_package_name.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ changeAndroidPackageName(Config config) async {
3535
});
3636

3737
/// Deleting all inside old Android package
38-
print("Working directory = ${workingDirectory.path}");
39-
4038
final Directory directoryToDelete = _getFirstDifferentDirectory(
4139
workingDirectory.path,
4240
oldPackageNameParts,
@@ -66,13 +64,18 @@ Directory _getFirstDifferentDirectory(
6664
List<String> newPackageParts,
6765
) {
6866
String path = "";
67+
bool foundADifference = false;
6968
for (int i = 0; i < oldPackageParts.length; i++) {
7069
path += "${oldPackageParts[i]}/";
7170
if (!newPackageParts.contains(oldPackageParts[i])) {
71+
foundADifference = true;
7272
break;
7373
}
7474
}
7575

76-
print("PATH = $path");
77-
return Directory("$workingDirectoryPath/$path");
76+
/// In case of the new package_name being longer than previous one like :
77+
/// com.example.one becoming com.example.one.two
78+
if (foundADifference) {
79+
return Directory("$workingDirectoryPath/$path");
80+
}
7881
}

0 commit comments

Comments
 (0)