-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlayers.js
156 lines (131 loc) · 5.06 KB
/
layers.js
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 fs from 'fs';
import * as path from 'path';
import * as helper from './helpers/index.js';
const swift2CsTypeMapping = {
'Double': 'double',
'Bool': 'bool',
'String': 'string',
'StyleColor': 'Color'
};
await generateLayers() ;
async function generateLayers() {
const info = JSON.parse(fs.readFileSync('layers.json', 'utf8'));
const output = path.join(info.mauiRepo, info.output)
if (!fs.existsSync(output)) {
fs.mkdirSync(output)
}
const input = path.join(info.mapboxRepo, info.input);
await fs.readdirSync(input)
// .filter(x => !info.ignored.some(y => x.endsWith(y + '.swift')))
// .map(x => /(\w+)\.swift/.exec(x)[1])
.forEach(x => generateCs(path.join(input, x), output));
}
async function generateCs(swiftFile, outputDir) {
const content = fs.readFileSync(swiftFile, 'utf8');
if (content.indexOf('@_spi(Experimental) public struct') > -1) {
return;
}
const fileName = /(\w+).swift/.exec(swiftFile)[1];
const layerName = /(\w+)Layer/.exec(fileName)[1];
const hasSource = content.indexOf('public init(id: String, source: String)') > -1;
let shouldSkip = false;
const lines = content.split('\n')
.filter(x =>
!(/^@/.test(x.trim())) // Ignore swift directive
&& !(/^import /.test(x.trim())) // Ignore import directives
&& x != '// This file is generated.'
);
const layoutKeys = getKeys(lines, (x) => { return /enum LayoutCodingKeys:/.test(x) });
const paintKeys = getKeys(lines, (x) => { return /enum PaintCodingKeys:/.test(x) });
const result = lines.map(x => {
// public struct BackgroundLayer: Layer, Equatable
if (/public struct (\w+):/.test(x)) {
const structName = /public struct (\w+):/.exec(x)[1];
shouldSkip = true;
return `public class ${structName} : MapboxLayer
{
public ${structName}(string id${hasSource ? ', string source' : ''})
: base(id)
{
Type = LayerType.${layerName};${hasSource ? '\n Source = source;' : ''}
Visibility = new PropertyValue<Visibility>(MapboxMaui.Visibility.Visible);
}`;
}
// public var visibility: Value<Visibility>
if (/public var visibility/.test(x)) {
shouldSkip = false;
return null;
}
if (/public init/.test(x)) {
shouldSkip = true;
return null;
}
if (shouldSkip) {
// Ignore properties declared in base class
return null;
}
// public var backgroundColor: Value<StyleColor>?
if (/public var (\w+)/.test(x)) {
const propertyParts = (/public var (\w+): (.+)/.exec(x)).slice(1, 3);
const propertyName = helper.pascalCase(propertyParts[0]);
let propertyType = propertyParts[1]
.replace('Value<', '')
.replace('>', '')
.replace('?', '');
if (/\[/.test(propertyType)) {
const innerType = /\[(\w+)/.exec(propertyType)[1];
propertyType = `${swift2CsTypeMapping[innerType] || innerType}[]`;
} else {
propertyType = swift2CsTypeMapping[propertyType] || propertyType;
}
const isPaint = paintKeys.filter(k => { return k[0] == propertyName }).length > 0;
return ` public PropertyValue<${propertyType}> ${propertyName}
{
get => GetProperty<PropertyValue<${propertyType}>>(
${isPaint ? 'PaintCodingKeys' : 'LayoutCodingKeys' }.${propertyName},
default,
MapboxLayerKey.${isPaint ? 'paint' : 'layout' }
);
set => SetProperty(
${isPaint ? 'PaintCodingKeys' : 'LayoutCodingKeys' }.${propertyName},
value,
MapboxLayerKey.${isPaint ? 'paint' : 'layout' }
);
}`;
}
return x;
}).filter(x => { return !!x });
fs.writeFileSync(
path.join(outputDir, fileName + '.cs'),
`namespace MapboxMaui.Styles;
${result.join('\n')}
public static class PaintCodingKeys {
${paintKeys
.map(x => ` public const string ${x[0]} = ${x[1]};`).join('\n')}
}
public static class LayoutCodingKeys {
${layoutKeys
.filter(x => x[0] != 'Visibility')
.map(x => ` public const string ${x[0]} = ${x[1]};`).join('\n')}
}
}`
)
}
function getKeys(lines, filter) {
let shouldSkip = true;
return lines.map(x => {
if (filter(x)) {
shouldSkip = false;
return null;
}
if (shouldSkip) {
return null;
}
if (x.trim() == '}') {
shouldSkip = true;
return null;
}
const parts = (/case (\w+) = ("[^"]+")/.exec(x)).slice(1, 3);
return [helper.pascalCase(parts[0]), parts[1]];
}).filter(x => { return !!x });
}