-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopeninstallConfig.js
277 lines (252 loc) · 10.1 KB
/
openinstallConfig.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
var fs = require('fs');
var spath = require('path');
var os = require('os');
//读取传入的appkey和schema
var appKey = process.argv[2];
if(appKey == undefined || appKey == null){
console.log("没有输入 appKey 参数");
return;
}
var schema = process.argv[3];
if(schema == undefined || schema == null){
console.log("没有输入 schema 参数,默认使用 appkey");
schema = appKey;
}
var podfileExsit;
var autoLink;
console.log("\n------------ 开始自动配置 ------------");
console.log("--- 如自动配置失败,请使用手动配置 ---\n");
//////////////// ios配置 ///////////////////
//导入头文件
function importHeaderFile(path){
var rf = fs.readFileSync(path,"utf-8");
// 插入 头文件
var hasImport = rf.match(/RCTOpenInstall.h/);
if (hasImport != null) {
return
}
if (podfileExsit != null) {
rf = rf.replace("#import <UIKit/UIKit.h>","#import <UIKit/UIKit.h>\n#import <openinstall-react-native/RCTOpenInstall.h>");
}else{
rf = rf.replace("#import <UIKit/UIKit.h>","#import <UIKit/UIKit.h>\n#import <RCTOpenInstall/RCTOpenInstall.h>");
}
fs.writeFileSync(path, rf, "utf-8");
console.log(path + " 头文件导入成功\n");
}
//编写代码
function codeAppDelegate(path){
var err = false;
var rf = fs.readFileSync(path,"utf-8");
var matchFinishLaunching = rf.match(/\n.*didFinishLaunchingWithOptions.*\n?\{/);
if(matchFinishLaunching != null){
var hasCoded = rf.match(/OpenInstallSDK initWithDelegate/);
if(hasCoded != null){
rf = rf.replace("[OpenInstallSDK initWithDelegate:[RCTOpenInstall allocWithZone:nil]];","\n");
console.log(path + "\n插件版本1.3.0开始,初始化方法将由用户主动调用的方式来进行,请参考最新文档\n");
//rf = rf.replace(matchFinishLaunching[0], matchFinishLaunching[0] + "\n\t"
//+ "[OpenInstallSDK initWithDelegate:[RCTOpenInstall allocWithZone:nil]];");
}
}
var matchOpenURL1 = rf.match(/\n.*openURL.*options.*\n?\{/);
if(matchOpenURL1 == null){
//rf = rf.replace("@end","- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{"
//+ "\n\t\/\/openURL1"
//+ "\n\t[OpenInstallSDK handLinkURL:url];\n\treturn YES;\n}"
//+ "\n@end");
}else{
var hasCodedTrash = rf.match(/OpenInstallSDK handLinkURL/);
if(hasCodedTrash != null){
rf = rf.replace("[OpenInstallSDK handLinkURL:url];","\n");
console.log(path + "\n插件版本1.3.0开始,新方法[RCTOpenInstall handLinkURL:]将替代原先的[OpenInstallSDK handLinkURL:]方法\n");
}
var hasCoded = rf.match(/RCTOpenInstall handLinkURL/);
if(hasCoded == null){
rf = rf.replace(matchOpenURL1[0], matchOpenURL1[0]
+ "\n\t\/\/openURL1"
+ "\n\t[RCTOpenInstall handLinkURL:url];");
}
}
var matchOpenURL2 = rf.match(/\n.*openURL.*sourceApplication.*\n?\{/);
if(matchOpenURL2 == null){
rf = rf.replace("@end","- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{"
+ "\n\t\/\/openURL2"
+ "\n\t[RCTOpenInstall handLinkURL:url];\n\treturn YES;\n}"
+ "\n@end");
}else{
var hasCodedTrash = rf.match(/OpenInstallSDK handLinkURL/);
if(hasCodedTrash != null){
rf = rf.replace("[OpenInstallSDK handLinkURL:url];","\n");
console.log(path + "\n插件版本1.3.0开始,新方法[RCTOpenInstall handLinkURL:]将替代原先的[OpenInstallSDK handLinkURL:]方法\n");
}
var hasCoded = rf.match(/openURL2\n.*RCTOpenInstall handLinkURL/);
if(hasCoded == null){
rf = rf.replace(matchOpenURL2[0], matchOpenURL2[0] + "\n\t\/\/openURL2" + "\n\t[RCTOpenInstall handLinkURL:url];");
}
}
var matchContinueUserActivity = rf.match(/\n.*continueUserActivity.*\n?\{/);
if(matchContinueUserActivity == null){
rf = rf.replace("@end","- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{"
+ "\n\t[RCTOpenInstall continueUserActivity:userActivity];"
+ "\n\treturn YES;\n}"
+ "\n@end");
}else{
var hasCodedTrash = rf.match(/OpenInstallSDK continueUserActivity/);
if(hasCodedTrash != null){
rf = rf.replace("[OpenInstallSDK continueUserActivity:userActivity];","\n");
console.log(path +"\n插件版本1.3.0开始,新方法[RCTOpenInstall continueUserActivity:]将替代原先的[OpenInstallSDK continueUserActivity:]方法\n");
}
var hasCoded = rf.match(/RCTOpenInstall continueUserActivity/);
if(hasCoded == null){
rf = rf.replace(matchContinueUserActivity[0], matchContinueUserActivity[0] + "\n\t" + "[RCTOpenInstall continueUserActivity:userActivity];");
}
}
fs.writeFileSync(path, rf, "utf-8");
if(err){
console.error(path + " 配置有问题,请参考文档手动修改\n");
}else{
console.log(path + " 修改成功\n");
}
}
//配置plist文件
function writePlist(path){
var rf = fs.readFileSync(path,"utf-8");
var matchStart = rf.match(/<plist .*>\n?<dict>/);
if(matchStart == null){
return;
}
var codeAppkey = rf.match(/<key>com.openinstall.APP_KEY<\/key>/);
if(codeAppkey == null){
rf = rf.replace(matchStart[0], matchStart[0]
+ "\n\t<key>com.openinstall.APP_KEY</key>"
+ "\n\t<string>" + appKey + "</string>");
}else{
console.log("iOS平台appkey已配置,如需修改请手动配置");
}
var codeScheme = rf.match(/<key>CFBundleURLName<\/key>\s*<string>openinstall<\/string>/);
if(codeScheme == null){
var matchBundleURL = rf.match(/<key>CFBundleURLTypes<\/key>\s*<array>/);
if(matchBundleURL == null){
rf = rf.replace(matchStart[0], matchStart[0]
+ "\n\t<key>CFBundleURLTypes</key>\n\t<array>\n\t\t<dict>"
+ "\n\t\t\t<key>CFBundleTypeRole</key>\n\t\t\t<string>Editor</string>"
+ "\n\t\t\t<key>CFBundleURLName</key>\n\t\t\t<string>openinstall</string>"
+ "\n\t\t\t<key>CFBundleURLSchemes</key>\n\t\t\t<array>\n\t\t\t\t<string>" + schema + "</string>\n\t\t\t</array>"
+ "\n\t\t</dict>\n\t</array>");
}else{
rf = rf.replace(matchBundleURL[0], matchBundleURL[0]
+ "\n\t\t<dict>"
+ "\n\t\t\t<key>CFBundleTypeRole</key>\n\t\t\t<string>Editor</string>"
+ "\n\t\t\t<key>CFBundleURLName</key>\n\t\t\t<string>openinstall</string>"
+ "\n\t\t\t<key>CFBundleURLSchemes</key>\n\t\t\t<array>\n\t\t\t\t<string>" + schema + "</string>\n\t\t\t</array>"
+ "\n\t\t</dict>");
}
}else{
console.log("iOS平台scheme已配置,如需修改请手动配置");
}
fs.writeFileSync(path, rf, "utf-8");
console.log(path + " 配置完成\n");
}
var projectName;
// find Project Dir
traversalFile(spath.resolve("./ios"), 1, function(f, s){
var xcodeproj = (/(\S+)?\.xcodeproj/im).exec(f);
var xcworkspace = (/(\S+)?\.xcworkspace/im).exec(f);
if(xcodeproj != null){
projectName = xcodeproj[1];
}
if(xcworkspace != null){
podfileExsit = xcworkspace[1];
//console.log("find xcworkspace: " + podfileExsit);
}
})
//遍历ios工程目录
traversalFile(projectName?projectName:spath.resolve("./ios/"), 10, function(f, s){
var AppDelegateHeader = f.match(/AppDelegate\.h/);
if (AppDelegateHeader != null) {
// console.log("find AppDelegate.h: " + f);
importHeaderFile(f);
}
var AppDelegateFile = f.match(/AppDelegate\.m/);
if (AppDelegateFile != null) {
// console.log("find AppDelegate.m : "+f);
codeAppDelegate(f);
}
var plistFile = f.match(/Info\.plist/);
if(plistFile != null){
// console.log("find Info.plist: "+f);
writePlist(f);
}
})
//////////////// Android配置 ///////////////
//配置AndroidManifest.xml中的appkey和scheme
function configManifestXml(path){
var err = false;
var rf = fs.readFileSync(path, "utf-8");
var alreadyConfigAppkey = rf.match(/com.openinstall.APP_KEY/);
if(alreadyConfigAppkey == null){
var matchApplication = rf.match(/\n.*<\/application>/);
if(matchApplication != null){
rf = rf.replace(matchApplication[0], "\n\t\t<meta-data "
+ "\n\t\t\tandroid:name=\"com.openinstall.APP_KEY\""
+ "\n\t\t\tandroid:value=\"" + appKey + "\"/>"
+ matchApplication[0]);
}else{
console.log("没有匹配到 </application>");
err = true;
}
}else{
console.log("Android平台appkey已配置,如需修改请手动配置");
}
var alreadyConfigScheme = rf.match(/<!-- openinstall scheme -->/);
if(alreadyConfigScheme == null){
var matchLauncherIntent = rf.match(/\n.*<intent-filter>\n.*android.intent.action.MAIN.*\n.*android.intent.category.LAUNCHER.*\n.*<\/intent-filter>/);
if(matchLauncherIntent != null){
rf = rf.replace(matchLauncherIntent[0], matchLauncherIntent[0]
+ "\n\t\t\t<!-- openinstall scheme -->"
+ "\n\t\t\t<intent-filter>"
+ "\n\t\t\t\t<action android:name=\"android.intent.action.VIEW\"/>"
+ "\n\t\t\t\t<category android:name=\"android.intent.category.DEFAULT\"/>"
+ "\n\t\t\t\t<category android:name=\"android.intent.category.BROWSABLE\"/>"
+ "\n\t\t\t\t<data android:scheme=\"" + schema + "\"/>"
+ "\n\t\t\t</intent-filter>");
}else{
console.log("没有匹配到 LAUNCHER intent-filter");
err =true;
}
}else{
console.log("Android平台scheme已配置,如需修改请手动配置");
}
fs.writeFileSync(path, rf, "utf-8");
if(err){
console.error(path + " 配置有问题,请参考文档手动修改\n");
}else{
console.log(path + " 配置完成\n");
}
}
//遍历 app module
traversalFile(spath.resolve("./android/app/src/main/"), 5, function(f, s){
var manifestXml = f.match(/AndroidManifest\.xml/);
if(manifestXml != null){
// console.log("find AndroidManifest.xml : " + f);
configManifestXml(f);
}
});
console.log("\n------------ 完成自动配置 ------------");
//////////////////////////////////////////////
function traversalFile(file, deep, handler){
var stats = fs.statSync(file);
handler(file, stats);
// 遍历子目录
if (deep > 0 && stats.isDirectory()) {
var files = fullPath(file, fs.readdirSync(file));
deep = deep-1;
files.forEach(function (f) {
traversalFile(f, deep, handler);
});
}
}
function fullPath(dir, files) {
return files.map(function (f) {
return spath.join(dir, f);
});
}