-
Notifications
You must be signed in to change notification settings - Fork 1
/
smisdk_integration.js
306 lines (256 loc) · 12.6 KB
/
smisdk_integration.js
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
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const xml = require('xmldoc');
const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer;
function findAndroidAppFolder(folder) {
const flat = 'android';
const nested = path.join('android', 'app');
if (fs.existsSync(path.join(folder, nested))) {
return nested;
}
if (fs.existsSync(path.join(folder, flat))) {
return flat;
}
return null;
};
function findManifest(folder) {
const manifestPath = glob.sync(path.join('**', 'AndroidManifest.xml'), {
cwd: folder,
ignore: ['node_modules/**', '**/build/**', 'Examples/**', 'examples/**', '**/debug/**'],
})[0];
return manifestPath ? path.join(folder, manifestPath) : null;
};
function readManifest(manifestPath) {
return new xml.XmlDocument(fs.readFileSync(manifestPath, 'utf8'));
};
const getPackageName = (manifest) => manifest.attr.package;
function getApplicationClassName(folder) {
const files = glob.sync('**/*.java', {
cwd: folder
});
const packages = files
.map(filePath => fs.readFileSync(path.join(folder, filePath), 'utf8'))
.map(file => file.match(/class (.*) implements/))
.filter(match => match);
return packages.length ? packages[0][1] : null;
};
// String append
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
function findStringsXml(folder) {
console.log('findStringsXml()');
const stringsXmlPath = glob.sync(path.join('**', 'strings.xml'), {
cwd: folder,
ignore: ['node_modules/**', '**/build/**', 'Examples/**', 'examples/**', '**/debug/**'],
})[0];
return stringsXmlPath ? path.join(folder, stringsXmlPath) : null;
};
// update string.xml file
function updateConfigurationFile(confFilePath) {
console.log('updateConfigurationFile()');
const smisdkApikey = '\n<string name="smisdk_apikey"></string>';
const smisdkShowMessaging = '\n<bool name="smisdk_show_messaging">true</bool>';
const smisdkExclusionDomin = '\n<array name="smisdk_exclusion_domin"></array>';
var stringsXmlDoc = fs.readFileSync(confFilePath, 'utf8');
var resourcesEndIndex = stringsXmlDoc.search("</resources>")
if (stringsXmlDoc.search('smisdk_apikey') < 0) {
stringsXmlDoc = insert(stringsXmlDoc, resourcesEndIndex - 1, smisdkApikey);
}
if (stringsXmlDoc.search('smisdk_show_messaging') < 0) {
stringsXmlDoc = insert(stringsXmlDoc, resourcesEndIndex - 1, smisdkShowMessaging);
}
if (stringsXmlDoc.search('smisdk_exclusion_domin') < 0) {
stringsXmlDoc = insert(stringsXmlDoc, resourcesEndIndex - 1, smisdkExclusionDomin);
}
fs.writeFileSync(confFilePath, stringsXmlDoc, 'utf8');
}
// update manifest file with app name
function updateManifestFile(manifestPath, applicationClassName) {
console.log('updateManifestFile()');
var manifestXmlDoc = new DOMParser().parseFromString(fs.readFileSync(manifestPath, 'utf8'));
var attrApplication = manifestXmlDoc.getElementsByTagName("application");
// console.log('attrApplication:' + attrApplication[0]);
const attrApplicationLength = attrApplication[0].attributes.length;
console.log('attrApplicationLength:' + attrApplicationLength);
// insert/update android:name attribute to manifest
var i;
for (i = 0; i < attrApplicationLength; i++) {
var attrNodeName = attrApplication[0].attributes[i].nodeName;
console.log('attrNodeName:' + attrNodeName);
if (attrNodeName.search('android:name') >= 0) {
var attrNodeValue = attrApplication[0].attributes[i].nodeValue;
console.log('attrNodeValue:' + attrNodeValue);
if (attrNodeValue === ('.' + applicationClassName)) {
console.log('app class name matched');
} else {
console.log('app class name not matched:' + applicationClassName);
attrApplication[0].removeAttribute(attrApplication[0].attributes[i].nodeName);
attrApplication[0].setAttribute('android:name ', '.' + applicationClassName);
fs.writeFileSync(manifestPath, manifestXmlDoc, 'utf8');
}
break;
} else if (i == attrApplicationLength - 1) {
// android:name does not exist
console.log('android:name does not exist in manifest file');
attrApplication[0].setAttribute('android:name ', '.' + applicationClassName);
fs.writeFileSync(manifestPath, manifestXmlDoc, 'utf8');
}
}
}
//// Main function to perform integration
function projectConfigAndroid(folder) {
const androidAppFolder = findAndroidAppFolder(folder);
if (!androidAppFolder) {
console.log('App folder not available.');
return null;
}
const sourceDir = path.join(folder, androidAppFolder);
console.log('sourceDir: ' + sourceDir);
const manifestPath = findManifest(sourceDir);
console.log('manifestPath: ' + manifestPath);
if (!manifestPath) {
return null;
}
// check app class Availability
var applicationClassName = getApplicationClassName(sourceDir + '/src/main')
console.log('applicationClassName before:' + applicationClassName);
if (applicationClassName == null) {
// Application class not available.
//Copy application class and update manifest with application name
const manifest = readManifest(manifestPath);
const packageName = getPackageName(manifest);
const packageNameStr = "package " + packageName + ';\n';
const packageFolder = packageName.replace(/\./g, path.sep);
const appPackagePath = path.join(sourceDir, `src/main/java/${packageFolder}`);
var datamiAppFile = fs.readFileSync('ApplicationClass.txt', 'utf8')
if (datamiAppFile.search(packageNameStr) < 0) {
var datamiAppFile = insert(datamiAppFile, 0, packageNameStr);
}
var datamiApplicationClassName = 'MainApplication';
fs.writeFileSync(appPackagePath + '/' + datamiApplicationClassName + '.java', datamiAppFile, 'utf8');
// update manifest with app name
updateManifestFile(manifestPath, datamiApplicationClassName);
// update configuration file
const stringsXmlPath = findStringsXml(sourceDir);
console.log('stringsXmlPath: ' + stringsXmlPath);
if (stringsXmlPath != null) {
updateConfigurationFile(stringsXmlPath);
}
} else {
// Application class available
var applicationClassNameSplit = applicationClassName.split(' ');
if (applicationClassNameSplit.length > 0) {
applicationClassName = applicationClassNameSplit[0];
console.log('applicationClassName after:' + applicationClassName);
const manifest = readManifest(manifestPath);
const packageName = getPackageName(manifest);
// console.log('packageName: ' + packageName);
const packageFolder = packageName.replace(/\./g, path.sep);
// console.log('packageFolder: ' + packageFolder);
const mainApplicationPath = path.join(sourceDir,
`src/main/java/${packageFolder}/${applicationClassName}.java`);
console.log('mainApplicationPath: ' + mainApplicationPath);
//Read application class
const appFile = fs.readFileSync(mainApplicationPath, 'utf8');
var isPackageExist = appFile.search('SmiSdkReactPackage');
console.log('isPackageExist: ' + isPackageExist);
if (isPackageExist < 0) {
const smiPackageName = ', new SmiSdkReactPackage()';
const smiPackageNameFor62 = 'packages.add(new SmiSdkReactPackage());';
const packageImport = 'import com.datami.smi.SdStateChangeListener; \nimport com.datami.smi.SmiResult; \nimport com.datami.smi.SmiSdk; \nimport com.datami.smisdk_plugin.SmiSdkReactModule; \nimport com.datami.smisdk_plugin.SmiSdkReactPackage; \n';
const initSponsoredDataAPI = '\nSmiSdk.initSponsoredData(getResources().getString(R.string.smisdk_apikey), \nthis, null, R.mipmap.ic_launcher,\ngetResources().getBoolean(R.bool.smisdk_show_messaging),\nArrays.asList(getResources().getStringArray(R.array.smisdk_exclusion_domin)));';
const onCreateMethod = '\n @Override \n public void onCreate() { \n super.onCreate();' + initSponsoredDataAPI + ' \n}';
const stateChangeListnerStr = ' SdStateChangeListener, ';
const onChangeMethod = '\n@Override \n public void onChange(SmiResult smiResult) {\n SmiSdkReactModule.setSmiResultToModule(smiResult);\n}';
var intMainPackageIndex = appFile.search("new MainReactPackage()")
console.log('intMainPackageIndex: ' + intMainPackageIndex);
const packageImportArray = 'import java.util.Arrays; \n';
var intMainPackageIndexLatest = appFile.search("return packages;")
console.log('intMainPackageIndexLatest: ' + intMainPackageIndexLatest);
if (intMainPackageIndex > 0) {
// add package name
var appfileNew = insert(appFile, intMainPackageIndex + 22, smiPackageName);
// add import
var intImportIndex = appFile.search("import")
appfileNew = insert(appfileNew, intImportIndex, packageImport);
//add sdStateChangeListner
var implementsIndex = appfileNew.search("implements");
if (implementsIndex > 0) {
appfileNew = insert(appfileNew, implementsIndex + 10, stateChangeListnerStr);
}
// add initSponsoredData Api
var intSuperIndex = appfileNew.search("super.onCreate()");
console.log('intSuperIndex: ' + intSuperIndex);
if (intSuperIndex > 0) {
appfileNew = insert(appfileNew, intSuperIndex + 17, initSponsoredDataAPI);
} else {
// add onCreate method with initSponsoredData API
var n = appfileNew.lastIndexOf("}");
console.log('lastIndexOf }: ' + n);
appfileNew = insert(appfileNew, n - 1, onCreateMethod);
}
// add onChange method
var lastchar = appfileNew.lastIndexOf("}");
console.log('lastchar: ' + n);
appfileNew = insert(appfileNew, lastchar - 1, onChangeMethod);
fs.writeFileSync(mainApplicationPath, appfileNew, 'utf8');
// const appFileNew2 = fs.readFileSync(mainApplicationPath, 'utf8')
updateManifestFile(manifestPath, applicationClassName);
// update configuration file
const stringsXmlPath = findStringsXml(sourceDir);
console.log('stringsXmlPath: ' + stringsXmlPath);
if (stringsXmlPath != null) {
updateConfigurationFile(stringsXmlPath);
}
} else if (intMainPackageIndexLatest > 0) {
// add package name
var appfileNew = insert(appFile, intMainPackageIndexLatest, smiPackageNameFor62);
// add import
var intImportIndex = appFile.search("import")
appfileNew = insert(appfileNew, intImportIndex, packageImport);
appfileNew = insert(appfileNew, intImportIndex, packageImportArray);
//add sdStateChangeListner
var implementsIndex = appfileNew.search("implements");
if (implementsIndex > 0) {
appfileNew = insert(appfileNew, implementsIndex + 10, stateChangeListnerStr);
}
// add initSponsoredData Api
var intSuperIndex = appfileNew.search("super.onCreate()");
console.log('intSuperIndex: ' + intSuperIndex);
if (intSuperIndex > 0) {
appfileNew = insert(appfileNew, intSuperIndex + 17, initSponsoredDataAPI);
} else {
// add onCreate method with initSponsoredData API
var n = appfileNew.lastIndexOf("}");
console.log('lastIndexOf }: ' + n);
appfileNew = insert(appfileNew, n - 1, onCreateMethod);
}
// add onChange method
var lastchar = appfileNew.lastIndexOf("}");
console.log('lastchar: ' + n);
appfileNew = insert(appfileNew, lastchar - 1, onChangeMethod);
fs.writeFileSync(mainApplicationPath, appfileNew, 'utf8');
// const appFileNew2 = fs.readFileSync(mainApplicationPath, 'utf8')
updateManifestFile(manifestPath, applicationClassName);
// update configuration file
const stringsXmlPath = findStringsXml(sourceDir);
console.log('stringsXmlPath: ' + stringsXmlPath);
if (stringsXmlPath != null) {
updateConfigurationFile(stringsXmlPath);
}
} else {
console.log('Error MainReactPackage does not exist.');
}
} else {
console.log('SmiSdkReactPackage already exist.');
}
} else {
console.log('Error in getting applicationClassName.');
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
};
projectConfigAndroid('../..')