Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/policies/render.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { NlsString, LanguageTranslations, Category, Policy, Translations, ProductJson } from './types.ts';
7
8
export function renderADMLString(prefix: string, moduleName: string, nlsString: NlsString, translations?: LanguageTranslations): string {
9
let value: string | undefined;
10
11
if (translations) {
12
const moduleTranslations = translations[moduleName];
13
14
if (moduleTranslations) {
15
value = moduleTranslations[nlsString.nlsKey];
16
}
17
}
18
19
if (!value) {
20
value = nlsString.value;
21
}
22
23
return `<string id="${prefix}_${nlsString.nlsKey.replace(/\./g, '_')}">${value}</string>`;
24
}
25
26
export function renderProfileString(_prefix: string, moduleName: string, nlsString: NlsString, translations?: LanguageTranslations): string {
27
let value: string | undefined;
28
29
if (translations) {
30
const moduleTranslations = translations[moduleName];
31
32
if (moduleTranslations) {
33
value = moduleTranslations[nlsString.nlsKey];
34
}
35
}
36
37
if (!value) {
38
value = nlsString.value;
39
}
40
41
return value;
42
}
43
44
export function renderADMX(regKey: string, versions: string[], categories: Category[], policies: Policy[]) {
45
versions = versions.map(v => v.replace(/\./g, '_'));
46
47
return `<?xml version="1.0" encoding="utf-8"?>
48
<policyDefinitions revision="1.1" schemaVersion="1.0">
49
<policyNamespaces>
50
<target prefix="${regKey}" namespace="Microsoft.Policies.${regKey}" />
51
</policyNamespaces>
52
<resources minRequiredRevision="1.0" />
53
<supportedOn>
54
<definitions>
55
${versions.map(v => `<definition name="Supported_${v}" displayName="$(string.Supported_${v})" />`).join(`\n `)}
56
</definitions>
57
</supportedOn>
58
<categories>
59
<category displayName="$(string.Application)" name="Application" />
60
${categories.map(c => `<category displayName="$(string.Category_${c.name.nlsKey})" name="${c.name.nlsKey}"><parentCategory ref="Application" /></category>`).join(`\n `)}
61
</categories>
62
<policies>
63
${policies.map(p => p.renderADMX(regKey)).flat().join(`\n `)}
64
</policies>
65
</policyDefinitions>
66
`;
67
}
68
69
export function renderADML(appName: string, versions: string[], categories: Category[], policies: Policy[], translations?: LanguageTranslations) {
70
return `<?xml version="1.0" encoding="utf-8"?>
71
<policyDefinitionResources revision="1.0" schemaVersion="1.0">
72
<displayName />
73
<description />
74
<resources>
75
<stringTable>
76
<string id="Application">${appName}</string>
77
${versions.map(v => `<string id="Supported_${v.replace(/\./g, '_')}">${appName} &gt;= ${v}</string>`).join(`\n `)}
78
${categories.map(c => renderADMLString('Category', c.moduleName, c.name, translations)).join(`\n `)}
79
${policies.map(p => p.renderADMLStrings(translations)).flat().join(`\n `)}
80
</stringTable>
81
<presentationTable>
82
${policies.map(p => p.renderADMLPresentation()).join(`\n `)}
83
</presentationTable>
84
</resources>
85
</policyDefinitionResources>
86
`;
87
}
88
89
export function renderProfileManifest(appName: string, bundleIdentifier: string, _versions: string[], _categories: Category[], policies: Policy[], translations?: LanguageTranslations) {
90
91
const requiredPayloadFields = `
92
<dict>
93
<key>pfm_default</key>
94
<string>Configure ${appName}</string>
95
<key>pfm_name</key>
96
<string>PayloadDescription</string>
97
<key>pfm_title</key>
98
<string>Payload Description</string>
99
<key>pfm_type</key>
100
<string>string</string>
101
</dict>
102
<dict>
103
<key>pfm_default</key>
104
<string>${appName}</string>
105
<key>pfm_name</key>
106
<string>PayloadDisplayName</string>
107
<key>pfm_require</key>
108
<string>always</string>
109
<key>pfm_title</key>
110
<string>Payload Display Name</string>
111
<key>pfm_type</key>
112
<string>string</string>
113
</dict>
114
<dict>
115
<key>pfm_default</key>
116
<string>${bundleIdentifier}</string>
117
<key>pfm_name</key>
118
<string>PayloadIdentifier</string>
119
<key>pfm_require</key>
120
<string>always</string>
121
<key>pfm_title</key>
122
<string>Payload Identifier</string>
123
<key>pfm_type</key>
124
<string>string</string>
125
</dict>
126
<dict>
127
<key>pfm_default</key>
128
<string>${bundleIdentifier}</string>
129
<key>pfm_name</key>
130
<string>PayloadType</string>
131
<key>pfm_require</key>
132
<string>always</string>
133
<key>pfm_title</key>
134
<string>Payload Type</string>
135
<key>pfm_type</key>
136
<string>string</string>
137
</dict>
138
<dict>
139
<key>pfm_default</key>
140
<string></string>
141
<key>pfm_name</key>
142
<string>PayloadUUID</string>
143
<key>pfm_require</key>
144
<string>always</string>
145
<key>pfm_title</key>
146
<string>Payload UUID</string>
147
<key>pfm_type</key>
148
<string>string</string>
149
</dict>
150
<dict>
151
<key>pfm_default</key>
152
<integer>1</integer>
153
<key>pfm_name</key>
154
<string>PayloadVersion</string>
155
<key>pfm_range_list</key>
156
<array>
157
<integer>1</integer>
158
</array>
159
<key>pfm_require</key>
160
<string>always</string>
161
<key>pfm_title</key>
162
<string>Payload Version</string>
163
<key>pfm_type</key>
164
<string>integer</string>
165
</dict>
166
<dict>
167
<key>pfm_default</key>
168
<string>Microsoft</string>
169
<key>pfm_name</key>
170
<string>PayloadOrganization</string>
171
<key>pfm_title</key>
172
<string>Payload Organization</string>
173
<key>pfm_type</key>
174
<string>string</string>
175
</dict>`;
176
177
const profileManifestSubkeys = policies.map(policy => {
178
return policy.renderProfileManifest(translations);
179
}).join('');
180
181
return `<?xml version="1.0" encoding="UTF-8"?>
182
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
183
<plist version="1.0">
184
<dict>
185
<key>pfm_app_url</key>
186
<string>https://code.visualstudio.com/</string>
187
<key>pfm_description</key>
188
<string>${appName} Managed Settings</string>
189
<key>pfm_documentation_url</key>
190
<string>https://code.visualstudio.com/docs/setup/enterprise</string>
191
<key>pfm_domain</key>
192
<string>${bundleIdentifier}</string>
193
<key>pfm_format_version</key>
194
<integer>1</integer>
195
<key>pfm_interaction</key>
196
<string>combined</string>
197
<key>pfm_last_modified</key>
198
<date>${new Date().toISOString().replace(/\.\d+Z$/, 'Z')}</date>
199
<key>pfm_platforms</key>
200
<array>
201
<string>macOS</string>
202
</array>
203
<key>pfm_subkeys</key>
204
<array>
205
${requiredPayloadFields}
206
${profileManifestSubkeys}
207
</array>
208
<key>pfm_title</key>
209
<string>${appName}</string>
210
<key>pfm_unique</key>
211
<true/>
212
<key>pfm_version</key>
213
<integer>1</integer>
214
</dict>
215
</plist>`;
216
}
217
218
export function renderMacOSPolicy(product: ProductJson, policies: Policy[], translations: Translations) {
219
const appName = product.nameLong;
220
const bundleIdentifier = product.darwinBundleIdentifier;
221
const payloadUUID = product.darwinProfilePayloadUUID;
222
const UUID = product.darwinProfileUUID;
223
224
const versions = [...new Set(policies.map(p => p.minimumVersion)).values()].sort();
225
const categories = [...new Set(policies.map(p => p.category))];
226
227
const policyEntries =
228
policies.map(policy => policy.renderProfile())
229
.flat()
230
.map(entry => `\t\t\t\t${entry}`)
231
.join('\n');
232
233
234
return {
235
profile: `<?xml version="1.0" encoding="UTF-8"?>
236
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
237
<plist version="1.0">
238
<dict>
239
<key>PayloadContent</key>
240
<array>
241
<dict>
242
<key>PayloadDisplayName</key>
243
<string>${appName}</string>
244
<key>PayloadIdentifier</key>
245
<string>${bundleIdentifier}.${UUID}</string>
246
<key>PayloadType</key>
247
<string>${bundleIdentifier}</string>
248
<key>PayloadUUID</key>
249
<string>${UUID}</string>
250
<key>PayloadVersion</key>
251
<integer>1</integer>
252
${policyEntries}
253
</dict>
254
</array>
255
<key>PayloadDescription</key>
256
<string>This profile manages ${appName}. For more information see https://code.visualstudio.com/docs/setup/enterprise</string>
257
<key>PayloadDisplayName</key>
258
<string>${appName}</string>
259
<key>PayloadIdentifier</key>
260
<string>${bundleIdentifier}</string>
261
<key>PayloadOrganization</key>
262
<string>Microsoft</string>
263
<key>PayloadType</key>
264
<string>Configuration</string>
265
<key>PayloadUUID</key>
266
<string>${payloadUUID}</string>
267
<key>PayloadVersion</key>
268
<integer>1</integer>
269
<key>TargetDeviceType</key>
270
<integer>5</integer>
271
</dict>
272
</plist>`,
273
manifests: [{ languageId: 'en-us', contents: renderProfileManifest(appName, bundleIdentifier, versions, categories, policies) },
274
...translations.map(({ languageId, languageTranslations }) =>
275
({ languageId, contents: renderProfileManifest(appName, bundleIdentifier, versions, categories, policies, languageTranslations) }))
276
]
277
};
278
}
279
280
export function renderGP(product: ProductJson, policies: Policy[], translations: Translations) {
281
const appName = product.nameLong;
282
const regKey = product.win32RegValueName;
283
284
const versions = [...new Set(policies.map(p => p.minimumVersion)).values()].sort();
285
const categories = [...Object.values(policies.reduce((acc, p) => ({ ...acc, [p.category.name.nlsKey]: p.category }), {}))] as Category[];
286
287
return {
288
admx: renderADMX(regKey, versions, categories, policies),
289
adml: [
290
{ languageId: 'en-us', contents: renderADML(appName, versions, categories, policies) },
291
...translations.map(({ languageId, languageTranslations }) =>
292
({ languageId, contents: renderADML(appName, versions, categories, policies, languageTranslations) }))
293
]
294
};
295
}
296
297
export function renderJsonPolicies(policies: Policy[]) {
298
const policyObject: { [key: string]: string | number | boolean | object | null } = {};
299
for (const policy of policies) {
300
policyObject[policy.name] = policy.renderJsonValue();
301
}
302
return policyObject;
303
}
304
305