generated from tokens-studio/plugin-example-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-xaml.ts
152 lines (137 loc) · 5.48 KB
/
build-xaml.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
import { readFileSync, appendFileSync, unlinkSync, existsSync } from "fs";
import StyleDictionaryPackage, { Dictionary, Platform } from 'style-dictionary';
const resDictStartTag = '<ResourceDictionary\r\n xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\r\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\r\n>';
const resDictBrushesStartTag = '<ResourceDictionary\r\n xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\r\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\r\n xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"\r\n>\r\n <ResourceDictionary.MergedDictionaries>\r\n <ResourceDictionary Source="Colors.xaml" />\r\n </ResourceDictionary.MergedDictionaries>\r\n';
const resDictGlobalStartTag = '<ResourceDictionary\r\n xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\r\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\r\n xmlns:system="clr-namespace:System;assembly=mscorlib"\r\n>'
const resDictEndTag = '</ResourceDictionary>';
const getXamlColorTokens = (dictionary: Dictionary, prefix: string): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'color')
.map(t => `<Color x:Key="${prefix}${t.name}">${t.value}</Color>`)
.join('\r\n ');
};
const getXamlBrushTokens = (dictionary: Dictionary, prefix: string): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'color')
.map(t => `<SolidColorBrush\r\n x:Key="${t.name}"\r\n Color="{StaticResource ${prefix}${t.name}}"\r\n p:Freeze="True"\r\n />`)
.join('\r\n ');
};
const getXamlFontSizeTokens = (dictionary: Dictionary): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'fontSize')
.map(t => `<system:Double x:Key="${t.name}">${t.value}</system:Double>`)
.join('\r\n ');
};
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.fontweights
const xamlFontWeightValues: Record<string, string> = {
'100': 'Thin',
'200': 'ExtraLight',
'300': 'Light',
'400': 'Regular',
'500': 'Medium',
'600': 'SemiBold',
'700': 'Bold',
'800': 'ExtraBold',
'900': 'Heavy',
'950': 'ExtraBlack'
}
const getXamlFontWeightTokens = (dictionary: Dictionary): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'fontWeight')
.map(t => `<FontWeight x:Key="${t.name}">${xamlFontWeightValues[t.value as string]}</FontWeight>`)
.join('\r\n ');
};
const getXamlStrokeWidthTokens = (dictionary: Dictionary): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'strokeWidth')
.map(t => `<Thickness x:Key="${t.name}">${t.value}</Thickness>`)
.join('\r\n ');
};
const getXamlBorderRadiusTokens = (dictionary: Dictionary): string | undefined => {
return dictionary.allTokens
.filter(t => t.attributes?.category === 'borderRadius')
.map(t => `<CornerRadius x:Key="${t.name}">${t.value}</CornerRadius>`)
.join('\r\n ');
};
const getXamlFileHeader = () => `<!--\r\n Do not edit directly\r\n Generated on ${new Date().toUTCString()}\r\n-->`;
StyleDictionaryPackage.registerFormat({
name: 'xaml/fluentui/color',
formatter: ({ dictionary, options }) => {
const tokens = getXamlColorTokens(dictionary, options.prefix);
return ` ${tokens}\r\n`
}
});
StyleDictionaryPackage.registerFormat({
name: 'xaml/fluentui/brush',
formatter: ({ dictionary, options }) => {
const tokens = getXamlBrushTokens(dictionary, options.prefix);
return `${getXamlFileHeader()}\r\n${resDictBrushesStartTag}\r\n ${tokens}\r\n${resDictEndTag}`
}
});
StyleDictionaryPackage.registerFormat({
name: 'xaml/fluentui/global',
formatter: ({ dictionary }) => {
const tokens = [
getXamlFontSizeTokens(dictionary),
getXamlFontWeightTokens(dictionary),
getXamlStrokeWidthTokens(dictionary),
getXamlBorderRadiusTokens(dictionary),
].join('\r\n\r\n ');
return `${getXamlFileHeader()}\r\n${resDictGlobalStartTag}\r\n ${tokens}\r\n${resDictEndTag}`
}
});
export const getXamlPlatform: (theme: string) => Platform = (theme) => ({
transforms: [
'attribute/cti',
'name/cti/pascal',
],
buildPath: 'generated/xaml/',
files: theme === 'global' ? [
{
format: 'xaml/fluentui/global',
destination: 'ConstantResources.xaml',
}
] : [
{
format: 'xaml/fluentui/color',
destination: `${theme}.Colors.xaml`,
options: {
prefix: `${theme[0].toUpperCase()}${theme.substring(1).toLowerCase()}.`,
}
},
{
format: 'xaml/fluentui/brush',
destination: `${theme[0].toUpperCase()}${theme.substring(1).toLowerCase()}.xaml`,
options: {
prefix: `${theme[0].toUpperCase()}${theme.substring(1).toLowerCase()}.`,
}
}
]
});
const concatFiles = (sources: string[], destination: string, header?: string, footer?: string) => {
if (existsSync(destination)) {
unlinkSync(destination);
}
if (header) {
appendFileSync(destination, header);
}
for (const src of sources) {
const content = readFileSync(src);
appendFileSync(destination, content);
unlinkSync(src);
}
if (footer) {
appendFileSync(destination, footer);
}
}
export const concatXamlFiles = () => {
concatFiles(
[
'generated/xaml/dark.Colors.xaml',
'generated/xaml/light.Colors.xaml'
],
'generated/xaml/Colors.xaml',
`${getXamlFileHeader()}\r\n${resDictStartTag}\r\n`,
resDictEndTag
);
}