forked from NativeScript/pbxproj-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcode.ts
156 lines (143 loc) · 5.81 KB
/
xcode.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
150
151
152
153
154
155
156
import * as parser from "./parser";
import * as pbx from "./pbx";
import * as fs from "fs";
export interface ManualSigning {
team: string;
uuid: string;
name: string;
identity?: "iPhone Developer" | "iPhone Distribution" | string;
}
export interface AutomaticSigning {
team: string;
}
/**
* Facade encapsulating common Xcode interractions.
*/
export class Xcode {
/**
* Readonly, pbx project dom.
*/
public document: pbx.Document;
/**
* Readonly, project file path.
*/
public path: string;
/**
* Opens an existing Xcode project file.
*/
public static open(path: string): Xcode {
const xcode = new Xcode();
xcode.document = pbx.parse(parser.parse(fs.readFileSync(path).toString()));
xcode.path = path;
return xcode;
}
/**
* Save the project back to the project file.
*/
public save() {
fs.writeFileSync(this.path, this.toString(), { encoding: 'utf8' });
}
/**
* Sets Manual signing style for a target in the pbx.Document.
*/
setManualSigningStyle(targetName: string, {team, uuid, name, identity}: ManualSigning = { team: undefined, uuid: undefined, name: undefined }) {
this.document.targets
.filter(target => target.name === targetName)
.forEach(target => {
this.document.projects
.filter(project => project.targets.indexOf(target) >= 0)
.forEach(project => {
project.patch({
attributes: {
TargetAttributes: {
[target.key]: {
DevelopmentTeam: team,
ProvisioningStyle: "Manual"
}
}
}
});
});
target.buildConfigurationsList.buildConfigurations.forEach(config => {
config.patch({
buildSettings: {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]": identity /* delete or set the CODE_SIGN_IDENTITY[sdk=iphoneos*] */,
DEVELOPMENT_TEAM: team || "",
PROVISIONING_PROFILE: uuid,
PROVISIONING_PROFILE_SPECIFIER: name
}
});
});
});
}
/**
* Sets Automatic signing style for a target in the pbx.Document.
*/
setAutomaticSigningStyle(targetName: string, developmentTeam: string) {
this.document.targets
.filter(target => target.name === targetName)
.forEach(target => {
this.document.projects
.filter(project => project.targets.indexOf(target) >= 0)
.forEach(project => {
project.patch({
attributes: {
TargetAttributes: {
[target.key]: {
DevelopmentTeam: developmentTeam,
ProvisioningStyle: "Automatic"
}
}
}
});
});
target.buildConfigurationsList.buildConfigurations.forEach(config => {
config.patch({
buildSettings: {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]": "iPhone Developer",
DEVELOPMENT_TEAM: developmentTeam,
PROVISIONING_PROFILE: undefined,
PROVISIONING_PROFILE_SPECIFIER: undefined
}
});
});
});
}
/**
* Read the signing configuration for a target.
*/
getSigning(targetName: string): { style: "Automatic", team: string } | { style: "Manual", configurations: { [config: string]: ManualSigning } } | undefined {
for (let project of this.document.projects) {
for (let target of project.targets) {
if (target.name === targetName) {
const targetAttributes = project.ast.get("attributes").get("TargetAttributes").get(target.key);
const style = targetAttributes.get("ProvisioningStyle").text;
const team = targetAttributes.get("DevelopmentTeam").text;
if (style === "Automatic") {
return { style, team };
} else if (style === "Manual") {
const configurations: { [config: string]: ManualSigning } = {};
for (let config of target.buildConfigurationsList.buildConfigurations) {
const buildSettings = config.ast.get("buildSettings");
const uuid = buildSettings.get("PROVISIONING_PROFILE").text;
const name = buildSettings.get("PROVISIONING_PROFILE_SPECIFIER").text;
const identity = buildSettings.get("CODE_SIGN_IDENTITY[sdk=iphoneos*]").text;
const team = buildSettings.get("DEVELOPMENT_TEAM").text;
configurations[config.name] = { uuid, name, identity, team };
}
return { style, configurations };
} else {
return undefined;
}
}
}
}
return undefined;
}
/**
* Serializes the project back to string format.
*/
toString() {
return this.document.toString();
}
}