Skip to content

Commit 4fbf0cb

Browse files
committed
Add possibility to change files names
1 parent dd8131d commit 4fbf0cb

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:travel_app/screens/home.dart';
1+
import 'package:travel_app_package/screens/home.dart';
22
import 'package:flutter/material.dart';
33

44
void main() => runApp(MyApp());

example/lib/screens/home.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:travel_app/widgets/awesome_widget.dart';
1+
import 'package:travel_app_package/widgets/awesome_widget.dart';
22
import 'package:flutter/material.dart';
33

44
class Home extends StatelessWidget {

example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: travel_app
1+
name: travel_app_package
22
description: A new Flutter application.
33

44
# The following defines the version and build number for your application.
@@ -33,7 +33,7 @@ dev_dependencies:
3333

3434
flutter_rename_app:
3535
application_name: Travel app
36-
dart_package_name: travel_app
36+
dart_package_name: travel_app_package
3737
application_id: com.android.travel
3838
bundle_id: com.ios.travel
3939
android_package_name: com.android.travel
@@ -64,7 +64,7 @@ flutter:
6464
# in this "flutter" section. Each entry in this list should have a
6565
# "family" key with the font family name, and a "fonts" key with a
6666
# list giving the asset and other descriptors for the font. For
67-
# travel_app:
67+
# travel_app_package:
6868
# fonts:
6969
# - family: Schyler
7070
# fonts:

example/test/widget_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// This is a basic Flutter widget test.
22
//
33
// To perform an interaction with a widget in your test, use the WidgetTester
4-
// utility that Flutter provides. For travel_app, you can send tap and scroll
4+
// utility that Flutter provides. For travel_app_package, you can send tap and scroll
55
// gestures. You can also use WidgetTester to find child widgets in the widget
66
// tree, read text, and verify that the values of widget properties are correct.
77

88
import 'package:flutter/material.dart';
99
import 'package:flutter_test/flutter_test.dart';
1010

11-
import 'package:travel_app/main.dart';
11+
import 'package:travel_app_package/main.dart';
1212

1313
void main() {
1414
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter_rename_app/src/models/config.dart';
2+
import 'package:flutter_rename_app/src/models/required_change.dart';
3+
4+
List<RequiredChange> getFilesToModifyName(
5+
Config config,
6+
) {
7+
return [
8+
RequiredChange(
9+
regexp: RegExp(config.oldDartPackageName),
10+
replacement: config.newDartPackageName,
11+
paths: ["android/${config.oldDartPackageName}_android.iml"],
12+
),
13+
];
14+
}

lib/src/flutter_rename_app.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library flutter_rename_app;
22

33
import 'dart:io';
44

5+
import 'package:flutter_rename_app/src/changes/files_to_modify_name.dart';
56
import 'package:flutter_rename_app/src/utils/logger.dart';
67
import 'package:process_run/shell_run.dart';
78

@@ -29,8 +30,11 @@ renameApp() async {
2930
Logger.info("Current app android package name = ${config.oldAndroidPackageName}");
3031
Logger.info("New app android package name: ${config.newAndroidPackageName}");
3132

32-
final List<RequiredChange> requiredChanges = getFilesToModifyContent(config);
33-
_applyContentChanges(requiredChanges);
33+
final List<RequiredChange> contentChanges = getFilesToModifyContent(config);
34+
_applyContentChanges(contentChanges);
35+
36+
final List<RequiredChange> nameChanges = getFilesToModifyName(config);
37+
_applyNameChanges(nameChanges);
3438

3539
Logger.newLine();
3640
Logger.newLine();
@@ -70,14 +74,33 @@ _changeAllImportsIn(String directoryPath, Config config) async {
7074
}
7175
}
7276

77+
_applyNameChanges(List<RequiredChange> requiredChanges) async {
78+
await Future.forEach(requiredChanges, (RequiredChange change) async {
79+
for (final path in change.paths) {
80+
await _changeFileName(path, change.regexp, change.replacement);
81+
}
82+
});
83+
}
84+
7385
_applyContentChanges(List<RequiredChange> requiredChanges) async {
7486
await Future.forEach(requiredChanges, (RequiredChange change) async {
75-
for (var path in change.paths) {
87+
for (final path in change.paths) {
7688
await _changeContentInFile(path, change.regexp, change.replacement);
7789
}
7890
});
7991
}
8092

93+
_changeFileName(String filePath, RegExp regexp, String replacement) async {
94+
if (filePath.contains(regexp)) {
95+
try {
96+
final File file = File(filePath);
97+
file.rename(filePath.replaceAll(regexp, replacement));
98+
} on FileSystemException {
99+
Logger.error("File $filePath does not exist on this project");
100+
}
101+
}
102+
}
103+
81104
_changeContentInFile(String filePath, RegExp regexp, String replacement) async {
82105
try {
83106
final File file = File(filePath);

0 commit comments

Comments
 (0)