-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathManifestBuilder.ts
149 lines (134 loc) · 4.41 KB
/
ManifestBuilder.ts
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
import * as path from 'path';
import {Interfaces} from "../../Interfaces";
import IEnv = Interfaces.IEnv;
import IDeepLink = Interfaces.IDeepLink;
let header = `<?xml version="1.0" encoding="utf-8" standalone="no"?>\n`;
class Node {
name: string = '';
keys: {[key:string]: string};
children: Array<Node> = [];
constructor({name, keys={}}) {
this.name = name;
this.keys = {...keys};
}
render({padding, paddingValue}): string {
let data = '';
data += ' '.repeat(padding) + `<${this.name} `;
for(const key in this.keys) {
data += `${key}="${this.keys[key]}" `;
}
if(this.children.length === 0) {
data += `/>\n`;
}else{
data += `>\n`;
}
for(const i in this.children){
data += this.children[i].render({padding:padding+paddingValue, paddingValue});
}
if(this.children.length !== 0) {
data += ' '.repeat(padding) + `</${this.name}>\n`
}
return data;
}
}
function createPermission(permission){
return new Node({
name: 'uses-permission',
keys: {
'android:name': permission
}
});
}
function createDeepLink(deep_link:IDeepLink): Node {
let filter = new Node({name: 'intent-filter'});
let action = new Node({
name: 'action',
keys: {
'android:name': 'android.intent.action.VIEW'
}
});
let data = new Node({
name: 'data',
keys: {
'android:scheme': deep_link.scheme,
'android:host': deep_link.host
}
});
let category1 = new Node({
name: 'category',
keys: {
'android:name': 'android.intent.category.DEFAULT'
}
});
let category2 = new Node({
name: 'category',
keys: {
'android:name': 'android.intent.category.BROWSABLE'
}
});
filter.children.push(action);
filter.children.push(data);
filter.children.push(category1);
filter.children.push(category2);
return filter;
}
export function getManifest(env: IEnv, args, permissions: Array<string>, deep_links: Array<Interfaces.IDeepLink>, screenOrientation: String = null): string {
let package_name = env.project.package["package-name"];
let env_manifist = args.manifist;
const sdkPath = path.join(env.builder.cache, args.sdk.repo);
if(!package_name){
console.log("can not find package-name");
process.exit();
}
let manifest = new Node({
name: 'manifest',
keys: {
'xmlns:android': "http://schemas.android.com/apk/res/android",
'package': package_name,
platformBuildVersionCode: env_manifist.platformBuildVersionCode,
platformBuildVersionName: env_manifist.platformBuildVersionName
}
});
let application = new Node({
name: 'application',
keys: env_manifist.application
});
let activity = new Node({
name: 'activity',
keys: env_manifist.activity
});
if(screenOrientation !== null) {
// @ts-ignore
activity.keys['android:screenOrientation'] = screenOrientation;
}
let intent_filter = new Node({name: 'intent-filter'});
intent_filter.children.push(new Node({
name: 'action',
keys: {
'android:name':"android.intent.action.MAIN"
}
}));
intent_filter.children.push(new Node({
name: 'category',
keys: {
'android:name':"android.intent.category.LAUNCHER"
}
}));
manifest.children.push(application);
application.children.push(activity);
activity.children.push(intent_filter);
for(const i in permissions) {
manifest.children.push(createPermission(permissions[i]));
if(env.builder.debug)console.log('Adding:', permissions[i]);
}
for(const i in deep_links){
let deepLink = createDeepLink(deep_links[i]);
activity.children.push(deepLink)
}
if(env.builder.debug) {
console.log("Built AndroidManifest.xml")
console.log(" platformBuildVersionCode:", env_manifist.platformBuildVersionCode)
console.log(" platformBuildVersionName:", env_manifist.platformBuildVersionName)
}
return header + manifest.render({padding:0, paddingValue: 4});
}