-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathenums.js
153 lines (129 loc) · 4.45 KB
/
enums.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
import fs from 'fs';
import * as path from 'path';
import * as helper from './helpers/index.js';
const nameMapping = {
'ToString': 'ToStringX'
};
const javaNameNamping = {
'StyleProjectionName': 'ProjectionName'
}
generateEnums() ;
function generateEnums() {
var info = JSON.parse(fs.readFileSync('enums.json', 'utf8'));
info.enums.map(x => generateEnum(info, x));
}
async function generateEnum(repoInfo, enumInfo) {
var output = path.join(repoInfo.mauiRepo, enumInfo.output);
if (!fs.existsSync(output)) {
fs.mkdirSync(output);
}
var input = path.join(repoInfo.mapboxRepo, enumInfo.input, enumInfo.name);
var content = fs.readFileSync(input, 'utf8');
var objcName = '';
var csName = '';
var javaName = '';
var cases = {};
var skipped = false;
var scopedName = '';
var lines = content.split('\n')
.map(line => {
if (/public extension (\w+)/.test(line.trim())) {
scopedName = /public extension (\w+)/.exec(line.trim())[1];
return '__NA__';
}
if (/^import/.test(line)) {
return "__NA__";
}
if (/^public struct/.test(line.trim())) {
skipped = true;
let matches = /struct (\w+)/.exec(line.trim())
objcName = matches[1];
csName = scopedName + objcName;
javaName = javaNameNamping[objcName] ?? objcName;
objcName =!!scopedName
? `TMB${scopedName}${objcName}`
: `TMB${objcName}`;
scopedName = '';
return `public readonly struct ${csName} : INamedString
{
public string Value { get; }
private ${csName}(string value) => Value = value;
public override string ToString() => Value;
public static implicit operator string(${csName} value) => value.Value;
public static implicit operator ${csName}(string value) => new (value);
`
}
if (/public init/.test(line)
|| /rawValue/.test(line)) {
skipped = true;
}
if (/^public static let/.test(line.trim())) {
skipped = false;
let matches = /public static let (\w+).+("[^"]+")/.exec(line.trim())
let caseKey = helper.pascalCase(matches[1]);
let caseValue = matches[2] ?? `"${matches[1]}"`;
cases[caseKey] = caseValue;
let staticFieldName = nameMapping[caseKey] || caseKey;
return ` public static readonly ${csName} ${staticFieldName} = new (${caseValue});`;
}
if (skipped) {
return '__NA__';
}
if (/^@/.test(line.trim())) {
return line.replace('@', '// @')
}
return line;
}).filter(x => x != '__NA__');
fs.writeFileSync(
path.join(output, enumInfo.name.replace('.swift', '.cs')),
[
`namespace MapboxMaui;
`
]
.concat(lines)
.join('\n') + '\n}');
}
function generateIOSMapping(csName, objcName, cases) {
var objcQualifiedName = `MapboxMapsObjC.${objcName}`;
return `#if IOS
partial class ${csName}Extensions
{
public static ${objcQualifiedName} ToPlatform(this ${csName} value)
{
return value.Value switch
{
${Object.entries(cases).map(x => ` ${x[1]} => ${objcQualifiedName}.${x[0]}`).join(',\n')}
};
}
public static ${csName} ToPlatform(this ${objcQualifiedName} value)
{
return value switch
{
${Object.entries(cases).map(x => ` ${objcQualifiedName}.${x[0]} => ${x[1]}`).join(',\n')}
};
}
public static ${csName} ${csName}X(this Foundation.NSNumber value)
{
return value.${csName}().ToPlatform();
}
public static Foundation.NSNumber AsNumber(this ${csName} value)
{
return Foundation.NSNumber.FromInt32((int)ToPlatform(value));
}
}
#endif`;
}
function generateAndroidMapping(csName, javaName, cases, enumInfo) {
var javaQualifiedName = `${(enumInfo.javaNs ?? '')}${javaName}`;
return `#if ANDROID
partial class ${csName}Extensions
{
public static ${javaQualifiedName} ToPlatform(this ${csName} value)
{
return ${javaQualifiedName}.ValueOf(
value.Value.ToUpper(new System.Globalization.CultureInfo("en-US"))
);
}
}
#endif`;
}