Skip to content

Commit 21f7679

Browse files
authored
[Flutter] Python script changes for file modification (#82)
* script * added two more files to erase
1 parent 4f3f963 commit 21f7679

File tree

1 file changed

+133
-27
lines changed

1 file changed

+133
-27
lines changed

scripts/script.py

Lines changed: 133 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,101 @@
22
import shutil
33

44
# README
5-
# This script should be run from the root of the project with the command:
5+
# This script should be run from the root of the project with the command:
66
# python script.py
77
#
88
# It will check operation constants, provided below, for information regarding what to erase or where to copy
99
# That information must be filles before running the script
1010
#
1111
# Versions
12-
# 0.1
12+
# 0.1
1313
# - can remove files if relative path to the script is provided
1414
# - can copy files if file and copy directory information is provided
15+
# 0.2
16+
# - can modify files
1517

1618
# OPERATION CONSTANTS
17-
FILES_TO_ERASE = ['../android/src/main/java/ly/count/dart/countly_flutter/CountlyMessagingService.java', '../ios/countly_flutter.podspec', '../ios/Classes/CountlyFLPushNotifications.h', '../ios/Classes/CountlyFLPushNotifications.m'] # array of string values. Relative path to the files. Something like: 'android/sth/sth.txt'
18-
FILES_TO_MOVE = [['no-push-files/AndroidManifest.xml','../android/src/main/AndroidManifest.xml'],['no-push-files/build.gradle','../android/build.gradle'],['no-push-files/pubspec.yaml','../pubspec.yaml'],['no-push-files/countly_flutter_np.podspec','../ios/countly_flutter_np.podspec'],['no-push-files/settings.gradle','../android/settings.gradle'],['no-push-files/README.md','../README.md']] # array of, arrays of string tuples. Relative path to the file and the relative path to the copy directory. Something like ['android/sth/sth.txt','android2/folder']
19-
# fileToModify = [] # write the file name to modify, currently expecting 1 file
20-
# lineToModify = '' # write the line to modify
21-
# modification = '' # write what to modify to
19+
FILES_TO_ERASE = [
20+
'../android/src/main/java/ly/count/dart/countly_flutter/CountlyMessagingService.java',
21+
'../ios/countly_flutter.podspec',
22+
'../ios/Classes/CountlyFLPushNotifications.h',
23+
'../ios/Classes/CountlyFLPushNotifications.m'
24+
] # array of string values. Relative path to the files. Something like: 'android/sth/sth.txt'
25+
FILES_TO_MOVE = [
26+
[
27+
'no-push-files/AndroidManifest.xml',
28+
'../android/src/main/AndroidManifest.xml'
29+
],
30+
[
31+
'no-push-files/build.gradle',
32+
'../android/build.gradle'
33+
],
34+
[
35+
'no-push-files/pubspec.yaml',
36+
'../pubspec.yaml'
37+
],
38+
[
39+
'no-push-files/countly_flutter_np.podspec',
40+
'../ios/countly_flutter_np.podspec'
41+
],
42+
[
43+
'no-push-files/settings.gradle',
44+
'../android/settings.gradle'
45+
],
46+
[
47+
'no-push-files/README.md',
48+
'../README.md'
49+
]
50+
] # array of, arrays of string tuples. Relative path to the file and the relative path to the copy directory. Something like ['android/sth/sth.txt','android2/folder']
51+
modPathAndroid = "../android/src/main/java/ly/count/dart/countly_flutter/CountlyFlutterPlugin.java"
52+
modPathIos = "../ios/Classes/CountlyFlutterPlugin.m"
53+
modPathCountly = "../lib/countly_flutter.dart"
54+
objectOfComModification = {
55+
modPathAndroid: {
56+
"modifications": {
57+
'import com.google.firebase.iid.FirebaseInstanceId;': "remove",
58+
'import com.google.firebase.iid.InstanceIdResult;': "remove",
59+
'import com.google.android.gms.tasks.Task;': "remove",
60+
'import com.google.android.gms.tasks.OnCompleteListener;': "remove",
61+
'import com.google.firebase.FirebaseApp;': "remove",
62+
"private final boolean BUILDING_WITH_PUSH_DISABLED = false;": "private final boolean BUILDING_WITH_PUSH_DISABLED = true;"
63+
},
64+
"consecutiveOmits": [
65+
"FirebaseApp.initializeApp(context);",
66+
"FirebaseInstanceId.getInstance().getInstanceId()",
67+
".addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {",
68+
"@Override",
69+
"public void onComplete(@NonNull Task<InstanceIdResult> task) {",
70+
"if (!task.isSuccessful()) {",
71+
'log("getInstanceId failed", task.getException(), LogLevel.WARNING);',
72+
"return;",
73+
"}",
74+
"String token = task.getResult().getToken();",
75+
"CountlyPush.onTokenRefresh(token);",
76+
"}",
77+
"});"
78+
]
79+
},
80+
modPathIos: {
81+
"modifications": {
82+
"BOOL BUILDING_WITH_PUSH_DISABLED = false;": "BOOL BUILDING_WITH_PUSH_DISABLED = true;",
83+
"// #define COUNTLY_EXCLUDE_PUSHNOTIFICATIONS": "#define COUNTLY_EXCLUDE_PUSHNOTIFICATIONS"
84+
},
85+
"consecutiveOmits": []
86+
},
87+
modPathCountly: {
88+
"modifications": {
89+
"import 'package:countly_flutter/countly_config.dart';": "import 'package:countly_flutter_np/countly_config.dart';",
90+
"static const bool BUILDING_WITH_PUSH_DISABLED = false;": " static const bool BUILDING_WITH_PUSH_DISABLED = true;"
91+
},
92+
"consecutiveOmits": []
93+
}
94+
} # 'modifications' is a map with keys are the lines to remove or modify. 'consecutiveOmits' are a block of lines to remove.
2295

23-
# walks through all files in the project and writes down the paths of the ones you are looking for. Can only work for unique files.
96+
# walks through all files in the project and writes down the paths of the ones you are looking for. Can only work for unique files.
2497
# TODO: Refactor so that it checks only the specified folder and files, like ['android-app', 'gradle']. Makes it easy to find files without the need of relative path information.
98+
99+
25100
def findFilesTo(src, array):
26101
paths = []
27102
for root, dirs, files in os.walk(src):
@@ -33,6 +108,8 @@ def findFilesTo(src, array):
33108
return paths
34109

35110
# loops through the provided array of relative paths and erases each file that exists
111+
112+
36113
def removeFiles(paths, cwd):
37114
for path in paths:
38115
path = os.path.join(cwd, path)
@@ -41,49 +118,78 @@ def removeFiles(paths, cwd):
41118
os.remove(path)
42119

43120
# loops through the provided array of relative paths and copies each file that exists
121+
122+
44123
def copyFiles(arrays, cwd):
45124
for tuple in arrays:
46125
file = os.path.join(cwd, tuple[0])
47126
folder = os.path.join(cwd, tuple[1])
48-
shutil.copy(file,folder)
127+
shutil.copy(file, folder)
128+
129+
# Modifies a given document
130+
# (String) filePath - relative path to the file to modify
131+
# (Obj) modificationInfo - object that contains file path as keys and modification and omittance info as values
132+
# (String) modificationType - 'bloc' for block removal, 'mod' for modification
49133

50-
# Modifies a line in a given document
51-
# TODO: Make it so that it can modify multiple lines
52-
def modifyFile(file, varName, replacementName):
134+
135+
def modifyFile(filePath, modificationInfo, modificationType):
53136
# reading operations
54-
with open(file, 'r') as f:
137+
with open(filePath, 'r') as f:
55138
content = f.readlines()
56139
fileLines = []
57140
for line in content:
58-
if varName in line:
59-
fileLines.append(line.replace(varName, replacementName))
60-
print("Replacing: "+varName+" with: "+replacementName)
61-
else:
62-
fileLines.append(line)
141+
if modificationType == 'mod':
142+
if line.strip("\n") in modificationInfo[filePath]['modifications']:
143+
if modificationInfo[filePath]['modifications'][line.strip("\n")] == 'remove':
144+
print("Removing line: ", line)
145+
else:
146+
fileLines.append(
147+
modificationInfo[filePath]['modifications'][line.strip("\n")] + "\n")
148+
print("Replacing: ["+line+"] with: ["+modificationInfo[filePath]
149+
['modifications'][line.strip("\n")]+"]")
150+
else:
151+
fileLines.append(line)
152+
elif modificationType == 'bloc':
153+
if modificationInfo[filePath]['consecutiveOmits']:
154+
if modificationInfo[filePath]['consecutiveOmits'][0] in line.strip("\n"):
155+
modificationInfo[filePath]['consecutiveOmits'].pop(0)
156+
print("Removing line: ", line)
157+
else:
158+
fileLines.append(line)
159+
else:
160+
fileLines.append(line)
63161
f.close()
64162

65163
# writing operations
66-
finalFile = open(file, 'w')
164+
finalFile = open(filePath, 'w')
67165
finalFile.writelines(fileLines)
68166
finalFile.close()
69-
print("Modified the file:"+file)
167+
print("Modified the file:"+filePath)
70168

71169

72170
def main():
73-
# give info in set constants
171+
# give info about set constants
74172
print("Paths to erase:")
75173
for i in FILES_TO_ERASE:
76-
print(i, end = '\n')
174+
print(i, end='\n')
77175
print("Paths to copy:")
78176
for i in FILES_TO_MOVE:
79-
print(i, end = '\n')
80-
177+
print(i, end='\n')
178+
print('Paths to modify:')
179+
print(modPathAndroid)
180+
print(modPathIos)
181+
print(modPathCountly)
182+
81183
# ask for permission to run the script
82184
start = input('Do you want to continue? (y/n)')
83185
if start == 'y' or start == 'Y' or start == 'yes' or start == 'YES':
84-
cwd = os.getcwd() # get current working directory
85-
removeFiles(FILES_TO_ERASE, cwd) # remove files
86-
copyFiles(FILES_TO_MOVE, cwd) # copies a file
186+
cwd = os.getcwd() # get current working directory
187+
removeFiles(FILES_TO_ERASE, cwd) # remove files
188+
copyFiles(FILES_TO_MOVE, cwd) # copies a file
189+
modifyFile(modPathAndroid, objectOfComModification, 'mod')
190+
modifyFile(modPathAndroid, objectOfComModification, 'bloc')
191+
modifyFile(modPathIos, objectOfComModification, 'mod')
192+
modifyFile(modPathCountly, objectOfComModification, 'mod')
87193
print("Done")
88194
else:
89195
print("Aborted")

0 commit comments

Comments
 (0)