-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-provision-service.ts
191 lines (173 loc) · 5.13 KB
/
ios-provision-service.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
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
import * as mobileprovision from "ios-mobileprovision-finder";
import { createTable, quoteString } from "../common/helpers";
import { IOptions } from "../declarations";
import * as _ from "lodash";
import { injector } from "../common/yok";
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
function formatDate(date: Date): string {
return `${date.getDay()} ${months[date.getMonth()]} ${date.getFullYear()}`;
}
export class IOSProvisionService {
constructor(
private $logger: ILogger,
private $options: IOptions,
private $devicesService: Mobile.IDevicesService,
private $mobileHelper: Mobile.IMobileHelper
) {}
public async pick(
uuidOrName: string,
projectId: string
): Promise<mobileprovision.provision.MobileProvision> {
const match = (await this.queryProvisioningProfilesAndDevices(projectId))
.match;
return (
match.eligable.find((prov) => prov.UUID === uuidOrName) ||
match.eligable.find((prov) => prov.Name === uuidOrName) ||
match.nonEligable.find((prov) => prov.UUID === uuidOrName) ||
match.nonEligable.find((prov) => prov.Name === uuidOrName)
);
}
public async listProvisions(projectId: string): Promise<void> {
const data = await this.queryProvisioningProfilesAndDevices(projectId);
const devices = data.devices;
const match = data.match;
function formatSupportedDeviceCount(
prov: mobileprovision.provision.MobileProvision
) {
if (devices.length > 0 && prov.Type === "Development") {
return (
prov.ProvisionedDevices.filter(
(device) => devices.indexOf(device) >= 0
).length +
"/" +
devices.length +
" targets"
);
} else {
return "";
}
}
function formatTotalDeviceCount(
prov: mobileprovision.provision.MobileProvision
) {
if (prov.Type === "Development" && prov.ProvisionedDevices) {
return prov.ProvisionedDevices.length + " total";
} else if (prov.Type === "AdHoc") {
return "all";
} else {
return "";
}
}
const table = createTable(
[
"Provision Name / Provision UUID / App Id",
"Team",
"Type / Due",
"Devices",
],
[]
);
function pushProvision(prov: mobileprovision.provision.MobileProvision) {
table.push(["", "", "", ""]);
table.push([
quoteString(prov.Name),
prov.TeamName,
prov.Type,
formatTotalDeviceCount(prov),
]);
table.push([
prov.UUID,
prov.TeamIdentifier && prov.TeamIdentifier.length > 0
? "(" + prov.TeamIdentifier[0] + ")"
: "",
formatDate(prov.ExpirationDate),
formatSupportedDeviceCount(prov),
]);
table.push([prov.Entitlements["application-identifier"], "", "", ""]);
}
match.eligable.forEach((prov) => pushProvision(prov));
this.$logger.info(table.toString());
this.$logger.info();
this.$logger.info(
"There are also " +
match.nonEligable.length +
" non-eligable provisioning profiles."
);
this.$logger.info();
}
public async listTeams(): Promise<void> {
const teams = await this.getDevelopmentTeams();
const table = createTable(
["Team Name", "Team ID"],
teams.map((team) => [quoteString(team.name), team.id])
);
this.$logger.info(table.toString());
}
private async queryProvisioningProfilesAndDevices(
projectId: string
): Promise<{ devices: string[]; match: mobileprovision.provision.Result }> {
const certificates = mobileprovision.cert.read();
const provisions = mobileprovision.provision.read();
const query: mobileprovision.provision.Query = {
Certificates: certificates.valid,
Unique: true,
AppId: projectId,
};
let devices: string[] = [];
if (this.$options.device) {
devices = [this.$options.device];
} else {
await this.$devicesService.initialize({
platform: "ios",
skipEmulatorStart: true,
});
devices = _(this.$devicesService.getDeviceInstances())
.filter((d) => this.$mobileHelper.isiOSPlatform(d.deviceInfo.platform))
.map((d) => d.deviceInfo.identifier)
.toJSON();
}
const match = mobileprovision.provision.select(provisions, query);
return { devices, match };
}
public async getDevelopmentTeams(): Promise<{ id: string; name: string }[]> {
const teams: { [teamName: string]: Set<string> } = {};
// NOTE: We are reading all provisioning profiles and collect team information from them.
// It would be better if we can check the Apple ID registered in Xcode and read the teams associated with it.
mobileprovision.provision.read().forEach(
(provision) =>
provision.TeamIdentifier &&
provision.TeamIdentifier.forEach((id) => {
if (!teams[provision.TeamName]) {
teams[provision.TeamName] = new Set();
}
teams[provision.TeamName].add(id);
})
);
const teamsArray = Object.keys(teams).reduce((arr, name) => {
teams[name].forEach((id) => arr.push({ id, name }));
return arr;
}, []);
return teamsArray;
}
public async getTeamIdsWithName(teamName: string): Promise<string[]> {
const allTeams = await this.getDevelopmentTeams();
const matchingTeamIds = allTeams
.filter((team) => team.name === teamName)
.map((team) => team.id);
return matchingTeamIds;
}
}
injector.register("iOSProvisionService", IOSProvisionService);