-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
177 lines (142 loc) · 6.24 KB
/
configure.py
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
import os
from pathlib import Path
import json
import plistlib
# Utils ===============================================================================================
def replace_text(file_path, original, replacement):
with open(Path(file_path), "r") as f:
file_data = f.read()
with open(Path(file_path), "w") as f:
f.write(file_data.replace(original, replacement))
def move_file_and_delete_directory(file_path, new_path):
Path(new_path).parent.mkdir(parents=True, exist_ok=True)
os.rename(Path(file_path), Path(new_path))
def delete_lines_containing(file_path, text):
with open(Path(file_path), "r") as f:
file_data = f.read()
with open(Path(file_path), "w") as f:
f.write(file_data.replace(text, ""))
def file_exists(file_path):
return os.path.isfile(Path(file_path))
def recursively_replace_text(directory, original, replacement):
# Recursively replace in all directories inside directory
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".dart"):
replace_text(os.path.join(root, file), original, replacement)
# Verify ===============================================================================================
class FlutterFirebaseTemplate:
def __init__(self, flutter_package_name):
self.android_application_id = None
self.ios_bundle_name = None
self.app_display_name = None
self.flutter_package_name = flutter_package_name
app_display_name_parts = flutter_package_name.split("_")
self.app_display_name = " ".join(map(str.capitalize, app_display_name_parts))
def verify(self):
for file in [
"lib/firebase_options.dart",
"android/app/google-services.json",
"ios/firebase_app_id_file.json",
"ios/Runner/GoogleService-Info.plist",
]:
if not file_exists(file):
print(f"{file} is missing")
print("Please configure firebase in your project using 'flutterfire configure'")
print("https://firebase.flutter.dev/docs/overview/#using-the-flutterfire-cli")
print("Please note that you may neeed to use atleast flutterfire 0.2.4")
raise Exception("Missing firebase configuration")
print("Extracting android application id from google-services.json")
with open("android/app/google-services.json", "r") as f:
google_services_json = json.load(f)
client_data = google_services_json["client"][0]
self.android_application_id = client_data["client_info"]["android_client_info"]["package_name"]
print("Extracting ios bundle name from GoogleService-Info.plist")
with open("ios/Runner/GoogleService-Info.plist", "rb") as f:
google_service_info_plist = plistlib.load(f)
self.ios_bundle_name = google_service_info_plist["BUNDLE_ID"]
print("==== Project verification complete ====")
print(f"Android application id: {self.android_application_id}")
print(f"iOS bundle name: {self.ios_bundle_name}")
print(f"Flutter package name: {self.flutter_package_name}")
print(f"App display name: {self.app_display_name}")
def configure(self):
# Android
print("Fixing android folders")
for file in [
"android/app/build.gradle",
"android/app/src/main/AndroidManifest.xml",
"android/app/src/debug/AndroidManifest.xml",
"android/app/src/profile/AndroidManifest.xml",
"android/app/src/main/kotlin/com/example/flutter_firebase_template/MainActivity.kt",
]:
replace_text(file, "com.example.flutter_firebase_template", self.android_application_id)
replace_text(
"android/app/src/main/AndroidManifest.xml",
"flutter_firebase_template",
self.android_application_id.split(".")[-1],
)
move_file_and_delete_directory(
"android/app/src/main/kotlin/com/example/flutter_firebase_template/MainActivity.kt",
f"android/app/src/main/kotlin/{self.android_application_id.replace('.', '/')}/MainActivity.kt",
)
# iOS
print("Fixing iOS folders")
replace_text(
"ios/Runner.xcodeproj/project.pbxproj",
"com.example.flutterFirebaseTemplate",
self.ios_bundle_name,
)
replace_text(
"ios/Runner/Info.plist",
"Flutter Firebase Template",
self.app_display_name,
)
replace_text(
"ios/Runner/Info.plist",
"flutter_firebase_template",
self.flutter_package_name,
)
# Flutter
print("Fixing Flutter folders")
replace_text(
"pubspec.yaml",
"flutter_firebase_template",
self.flutter_package_name,
)
recursively_replace_text(
"lib",
"package:flutter_firebase_template",
f"package:{self.flutter_package_name}",
)
recursively_replace_text(
"test",
"package:flutter_firebase_template",
f"package:{self.flutter_package_name}",
)
# Fix gitignore
print("Fixing .gitignore")
with open(".gitignore", "r") as f:
gitignore = f.read()
for remove_line in [
"# Remove following after configuring firebase",
"firebase_app_id_file.json",
"GoogleService-Info.plist",
"google-services.json",
]:
for line in gitignore.split("\n"):
if line.startswith(remove_line):
delete_lines_containing(".gitignore", line)
# Main ===============================================================================================
def main():
try:
flutter_package_name = input("Enter your flutter package name: ").strip()
if not flutter_package_name:
raise Exception("Flutter package name is required")
project = FlutterFirebaseTemplate(flutter_package_name)
project.verify()
project.configure()
except Exception as e:
print("Something went wrong:", e)
if __name__ == "__main__":
main()