-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathGetuiConfiguration.js
250 lines (203 loc) · 7.4 KB
/
GetuiConfiguration.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
// 这个地方是更新配置文件 的脚本
var fs = require('fs');
var spath = require('path');
var os = require('os');
// add other link flag
var moduleName = process.argv[5];
if (moduleName == undefined || moduleName == null) {
console.log("没有输入 moduleName, 将使用默认模块名: app");
moduleName = "app";
};
var appId = process.argv[2];
if (appId == undefined || appId == null) {
console.log("error 没有输入 appId 参数");
return;
}
var appKey = process.argv[3];
if (appKey == undefined || appKey == null) {
console.log("error 没有输入 appKey 参数");
return;
}
var appSecret = process.argv[4];
if (appSecret == undefined || appSecret == null) {
console.log("error 没有输入 appSecret 参数");
return;
}
function insertIOSImplCode(path) {
// 这个是插入代码的脚本 install
if (isFile(path) == false) {
console.log("configuration Getui error!!");
return;
}
var rf = fs.readFileSync(path, "utf-8");
// 不做删除工作,默认认为没有 Getui 相关代码
// 插入 注册推送 和启动 Getui SDK
// - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// {
var rf = fs.readFileSync(path, "utf-8");
var searchDidlaunch = rf.match(/\n.*didFinishLaunchingWithOptions.*\n?\{/);
if (searchDidlaunch == null) {
console.log("没有匹配到 didFinishLaunchingWithOptions");
console.log(rf);
} else {
// console.log(searchDidlaunch[0]);
var oldValue = rf.match(/GeTuiSdk registerRemoteNotification/)
if (oldValue == null) {
rf = rf.replace(searchDidlaunch[0], searchDidlaunch[0] + "\n \/\/ 接入个推\n \[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:\[RCTGetuiModule sharedGetuiModule\] launchingOptions:launchOptions\]\;\n \/\/ APNs\n \[GeTuiSdk registerRemoteNotification: \(UNAuthorizationOptionSound \| UNAuthorizationOptionAlert \| UNAuthorizationOptionBadge\)\]\;");
fs.writeFileSync(path, rf, "utf-8");
}
}
}
// 插入 Appdelegate.h 文件代码
function insertIOSHeaderCode(path) {
if (isFile(path) == false) {
console.log("configuration Getui error!!");
return;
}
var rf = fs.readFileSync(path, "utf-8");
// 不做删除工作,默认认为没有 Getui 相关代码
// 插入 头文件
var oldValue = rf.match(/RCTGetuiModule.h/)
if (oldValue != null) {
return
}
rf = rf.replace("\#import \<UIKit\/UIKit.h\>", "\#import \<UIKit\/UIKit.h\>\n\#if __has_include\(\<RCTGetuiModule\/RCTGetuiModule.h\>\)\n\#import \<RCTGetuiModule\/RCTGetuiModule.h\>\n\#elif __has_include\(\"RCTGetuiModule.h\"\)\n\#import \"RCTGetuiModule.h\"\n\#elif __has_include\(\<GtSdkRN\/RCTGetuiModule.h\>\)\n\#import \<GtSdkRN\/RCTGetuiModule.h\>\n\#endif\n#define kGtAppId \@\"" + appId + "\"\n\#define kGtAppKey \@\"" + appKey + "\"\n\#define kGtAppSecret \@\"" + appSecret + "\"\n");
fs.writeFileSync(path, rf, "utf-8");
}
// 判断文件
function exists(path) {
return fs.existsSync(path) || path.existsSync(path);
}
function isFile(path) {
return exists(path) && fs.statSync(path).isFile();
}
function isDir(path) {
return exists(path) && fs.statSync(path).isDirectory();
}
// 深度遍历所有文件,
getAllfiles("./ios", function (f, s) {
//匹配路径中有/AppDelegate.m的文件,避免修改RCTAppDelegate.文件
var isAppdelegateImpl = f.match(/\/AppDelegate\.m/);
// 找到Appdelegate.m 文件 插入代码
if (isAppdelegateImpl != null) {
console.log("the file is appdelegate:" + f);
insertIOSImplCode(f);
}
var isAppdelegateHeader = f.match(/\/AppDelegate\.h$/);
if (isAppdelegateHeader != null) {
console.log("the file is appdelegate:" + f);
insertIOSHeaderCode(f);
}
});
getAndroidManifest("./android/" + moduleName, function (f, s) {
var isAndroidManifest = f.match(/AndroidManifest\.xml/);
if (isAndroidManifest != null) {
console.log("find AndroidManifest in " + moduleName);
configureAndroidManifest(f);
};
});
getConfigureFiles("./android", function (f, s) {
//找到settings.gradle
var isSettingGradle = f.match(/settings\.gradle/);
if (isSettingGradle != null) {
console.log("find settings.gradle in android project " + f);
configureSetting(f);
}
//找到project下的build.gradle
var isProjectGradle = f.match(/.*\/build\.gradle/);
if (isProjectGradle != null) {
console.log("find build.gradle in android project " + f);
configureGradle(f);
}
});
function getAllfiles(dir, findOne) {
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function eachFileSync(dir, findOne) {
var stats = fs.statSync(dir);
findOne(dir, stats);
// 遍历子目录
if (stats.isDirectory()) {
var files = fullPath(dir, fs.readdirSync(dir));
// console.log(dir);
files.forEach(function (f) {
eachFileSync(f, findOne);
});
}
}
function fullPath(dir, files) {
return files.map(function (f) {
return spath.join(dir, f);
});
}
// android
function getGradleFile(dir, findOne) {
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function getAndroidManifest(dir, findOne) {
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function getConfigureFiles(dir, findOne) {
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function configureAndroidManifest(path) {
if (isFile(path) == false) {
console.log("configuration Getui error!!");
return;
}
var rf = fs.readFileSync(path, "utf-8");
var isAlreadyWrite = rf.match(/.*android\:value=\"\$\{PUSH_APPID\}\"/);
if (isAlreadyWrite == null) {
var searchKey = rf.match(/\n.*\<\/activity\>/);
if (searchKey != null) {
rf = rf.replace(searchKey[0], searchKey[0] + "\n\n\<meta-data android\:name=\"PUSH_APPID\" android\:value=\"" + appId + "\"\/\>\n\<meta-data android\:name=\"PUSH_APPKEY\" android\:value=\"" + appKey + "\"\/\>\n\<meta-data android\:name=\"PUSH_APPSECRET\" android\:value=\"" + appSecret + "\"\/\>\n");
fs.writeFileSync(path, rf, "utf-8");
}
}
}
function configureSetting(path) {
if (isFile(path) == false) {
console.log("configuration Getui error!!");
return;
}
var rf = fs.readFileSync(path, "utf-8");
var isAlreadyWrite = rf.match(/.*react-native-getui.*/);
if (isAlreadyWrite == null) {
var searchKey = rf.match(/\n.*include.*/);
if (searchKey != null) {
rf = rf.replace(searchKey[0], searchKey[0] + "\, \'\:react-native-getui\'\nproject\(\'\:react-native-getui\'\)\.projectDir = new File\(rootProject\.projectDir\, \'\.\.\/node_modules\/react-native-getui\/android\'\)\n");
fs.writeFileSync(path, rf, "utf-8");
} else {
console.log("Did not find include in settings.gradle: " + path);
}
}
}
function configureGradle(path) {
if (isFile(path) == false) {
console.log("configuration Getui error!!");
return;
}
var rf = fs.readFileSync(path, "utf-8");
var isAlreadyWrite = rf.match(/.*react-native-getui.*/);
if (isAlreadyWrite == null) {
var searchKey = rf.match(/\n.*compile fileTree.*\n/);
if (searchKey != null) {
rf = rf.replace(searchKey[0], searchKey[0] + " compile project\(\'\:react-native-getui\'\)\n");
fs.writeFileSync(path, rf, "utf-8");
} else {
console.log("Did not find \"compile\" in path: " + path);
}
}
}