-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pbx.ts
101 lines (82 loc) · 3.01 KB
/
pbx.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
import * as ast from "./ast";
export function isProject(obj: DocumentObject): obj is PBXProject {
return obj.isa === "PBXProject";
}
export function isTarget(obj: DocumentObject): obj is PBXNativeTarget {
return obj.isa === "PBXNativeTarget";
}
export type ISA = "PBXProject" | "PBXNativeTarget" | "XCBuildConfiguration" | "XCConfigurationList";
const pbxConstructors: { [key: string]: typeof DocumentObject } = {}
function pbx(target: typeof DocumentObject): void {
target.prototype.isa = <ISA>target.name;
pbxConstructors[target.prototype.isa] = target;
}
export class DocumentObject {
isa: ISA;
constructor(protected document: Document, public ast: ast.KeyValuePair<ast.Dictionary>) {
}
get key() { return this.ast.key.json; }
public patch(json: any) {
this.ast.value.patch(json);
}
/**
* Override and use the ast and the document to resolve any objects referenced by hash.
*/
protected resolve() {
}
toString() {
return this.ast.toString();
}
}
@pbx export class PBXProject extends DocumentObject {
get targets(): PBXNativeTarget[] {
return this.ast.value.get("targets").items.map(key => this.document[key.text]);
}
get buildConfigurationsList(): XCConfigurationList {
return this.document[this.ast.value.get("buildConfigurationList").text];
}
}
@pbx export class PBXNativeTarget extends DocumentObject {
get name() { return this.ast.value.get("name").text; }
get buildConfigurationsList(): XCConfigurationList {
return this.document[this.ast.value.get("buildConfigurationList").text];
}
get productType() { return this.ast.value.get("productType").text; }
}
@pbx export class XCBuildConfiguration extends DocumentObject {
get name() {
return this.ast.value.get("name").text;
}
}
@pbx export class XCConfigurationList extends DocumentObject {
get buildConfigurations(): XCBuildConfiguration[] {
return this.ast.value.get("buildConfigurations").items.map(key => this.document[key.text]);
}
}
export class Document {
[id: string]: DocumentObject | any;
private constructor(protected ast: ast.Document) {
}
get objects(): DocumentObject[] { return Object.keys(this).map(key => this[key]); }
get targets(): PBXNativeTarget[] { return <any>this.objects.filter(isTarget); }
get projects(): PBXProject[] { return <any>this.objects.filter(isProject); }
static fromAST(fromAST: ast.Document): Document {
const doc = new Document(fromAST);
fromAST.get("objects").kvps.forEach(kvp => {
if (ast.isKVPDictionary(kvp)) {
const Ctor = pbxConstructors[kvp.value.get("isa").json];
if (Ctor) {
const obj = new Ctor(doc, kvp);
doc[kvp.key.json] = obj;
}
}
});
return doc;
}
toString() {
return this.ast.toString();
}
}
export function parse(project: ast.Document): Document {
return Document.fromAST(project);
}