Update enums.py to generate Kotlin files

This commit is contained in:
Mateo Guzmán
2025-08-09 12:05:19 +02:00
parent 8bf7a34d02
commit 16c82cac8b
3 changed files with 81 additions and 36 deletions

View File

@@ -84,6 +84,12 @@ ENUMS = {
],
}
# Temporary filter enums to not upgrade all enums at once
KOTLIN_ENUM_NAMES = {"Direction"}
ENUMS_KOTLIN = {name: ENUMS[name] for name in KOTLIN_ENUM_NAMES}
ENUMS_JAVA = {name: values for name, values in ENUMS.items() if name not in KOTLIN_ENUM_NAMES}
DO_NOT_STRIP = ["LogLevel"]
BITSET_ENUMS = ["Errata"]
@@ -120,6 +126,9 @@ def to_java_upper(symbol):
return _format_name(symbol, "_", "upper")
def to_kotlin_upper(symbol):
return _format_name(symbol, "_", "upper")
def to_hyphenated_lower(symbol):
return _format_name(symbol, "-", "lower")
@@ -215,7 +224,7 @@ with open(root + "/yoga/YGEnums.cpp", "w") as f:
f.write("\n")
# write out java files
for name, values in sorted(ENUMS.items()):
for name, values in sorted(ENUMS_JAVA.items()):
with open(root + "/java/com/facebook/yoga/Yoga%s.java" % name, "w") as f:
f.write(get_license("java"))
f.write("package com.facebook.yoga;\n\n")
@@ -267,6 +276,48 @@ for name, values in sorted(ENUMS.items()):
f.write(" }\n")
f.write("}\n")
# write out Kotlin files
for name, values in sorted(ENUMS_KOTLIN.items()):
with open(root + "/java/com/facebook/yoga/Yoga%s.kt" % name, "w") as f:
f.write(get_license("kotlin"))
f.write("package com.facebook.yoga\n\n")
f.write("public enum class Yoga%s(public val intValue: Int) {\n" % name)
if len(values) > 0:
for value in values:
if isinstance(value, tuple):
f.write(" %s(%d)" % (to_kotlin_upper(value[0]), value[1]))
else:
f.write(" %s(%d)" % (to_kotlin_upper(value), values.index(value)))
if values.index(value) is len(values) - 1:
f.write(";\n")
else:
f.write(",\n")
else:
f.write("__EMPTY(-1);")
f.write("\n")
f.write(" public fun intValue(): Int = intValue\n")
f.write("\n")
f.write(" public companion object {\n")
f.write(" @JvmStatic\n")
f.write(" public fun fromInt(value: Int): Yoga%s =\n" % name)
f.write(" when (value) {\n")
for value in values:
if isinstance(value, tuple):
f.write(
" %d -> %s\n" % (value[1], to_kotlin_upper(value[0]))
)
else:
f.write(
" %d -> %s\n"
% (values.index(value), to_kotlin_upper(value))
)
f.write(
' else -> throw IllegalArgumentException("Unknown enum value: $value")\n'
)
f.write(" }\n")
f.write(" }\n")
f.write("}\n")
# write out TypeScript file
with open(root + "/javascript/src/generated/YGEnums.ts", "w") as f:
f.write(get_license("js"))

View File

@@ -1,35 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @generated by enums.py
package com.facebook.yoga;
public enum YogaDirection {
INHERIT(0),
LTR(1),
RTL(2);
private final int mIntValue;
YogaDirection(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static YogaDirection fromInt(int value) {
switch (value) {
case 0: return INHERIT;
case 1: return LTR;
case 2: return RTL;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @generated by enums.py
package com.facebook.yoga
public enum class YogaDirection(public val intValue: Int) {
INHERIT(0),
LTR(1),
RTL(2);
public fun intValue(): Int = intValue
public companion object {
@JvmStatic
public fun fromInt(value: Int): YogaDirection =
when (value) {
0 -> INHERIT
1 -> LTR
2 -> RTL
else -> throw IllegalArgumentException("Unknown enum value: $value")
}
}
}