Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/policies/stringEnumPolicy.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 { BasePolicy } from './basePolicy.ts';
7
import type { CategoryDto, PolicyDto } from './policyDto.ts';
8
import { renderProfileString } from './render.ts';
9
import { type Category, type NlsString, PolicyType, type LanguageTranslations } from './types.ts';
10
11
export class StringEnumPolicy extends BasePolicy {
12
13
static from(category: CategoryDto, policy: PolicyDto): StringEnumPolicy | undefined {
14
const { type, name, minimumVersion, enum: enumValue, localization } = policy;
15
16
if (type !== 'string') {
17
return undefined;
18
}
19
20
const enum_ = enumValue;
21
22
if (!enum_) {
23
return undefined;
24
}
25
26
if (!localization.enumDescriptions || !Array.isArray(localization.enumDescriptions) || localization.enumDescriptions.length !== enum_.length) {
27
throw new Error(`Invalid policy data: enumDescriptions must exist and have the same length as enum_ for policy "${name}".`);
28
}
29
const enumDescriptions = localization.enumDescriptions.map((e) => ({ nlsKey: e.key, value: e.value }));
30
return new StringEnumPolicy(
31
name,
32
{ moduleName: '', name: { nlsKey: category.name.key, value: category.name.value } },
33
minimumVersion,
34
{ nlsKey: localization.description.key, value: localization.description.value },
35
'',
36
enum_,
37
enumDescriptions
38
);
39
}
40
41
protected enum_: string[];
42
protected enumDescriptions: NlsString[];
43
44
private constructor(
45
name: string,
46
category: Category,
47
minimumVersion: string,
48
description: NlsString,
49
moduleName: string,
50
enum_: string[],
51
enumDescriptions: NlsString[],
52
) {
53
super(PolicyType.StringEnum, name, category, minimumVersion, description, moduleName);
54
this.enum_ = enum_;
55
this.enumDescriptions = enumDescriptions;
56
}
57
58
protected renderADMXElements(): string[] {
59
return [
60
`<enum id="${this.name}" valueName="${this.name}">`,
61
...this.enum_.map((value, index) => ` <item displayName="$(string.${this.name}_${this.enumDescriptions[index].nlsKey.replace(/\./g, '_')})"><value><string>${value}</string></value></item>`),
62
`</enum>`
63
];
64
}
65
66
renderADMLStrings(translations?: LanguageTranslations) {
67
return [
68
...super.renderADMLStrings(translations),
69
...this.enumDescriptions.map(e => this.renderADMLString(e, translations))
70
];
71
}
72
73
renderADMLPresentationContents() {
74
return `<dropdownList refId="${this.name}" />`;
75
}
76
77
renderJsonValue() {
78
return this.enum_[0];
79
}
80
81
renderProfileValue() {
82
return `<string>${this.enum_[0]}</string>`;
83
}
84
85
renderProfileManifestValue(translations?: LanguageTranslations): string {
86
return `<key>pfm_default</key>
87
<string>${this.enum_[0]}</string>
88
<key>pfm_description</key>
89
<string>${renderProfileString(this.name, this.moduleName, this.description, translations)}</string>
90
<key>pfm_name</key>
91
<string>${this.name}</string>
92
<key>pfm_title</key>
93
<string>${this.name}</string>
94
<key>pfm_type</key>
95
<string>string</string>
96
<key>pfm_range_list</key>
97
<array>
98
${this.enum_.map(e => `<string>${e}</string>`).join('\n ')}
99
</array>`;
100
}
101
}
102
103