Fixup Enum Generator

Summary:
https://github.com/facebook/yoga/pull/1116 adds a new enum. The enum generator is out of date with copyright header, and some codemods, but it also looks like there were manual changes, types added, etc since generation. I fixed up the script to incorporate generating the changes folks made manually, and also added an enum that was previously only added manually to the C ABI.

Changelog:
[General][Fixed] - Fixup Yoga Enum Generator

Reviewed By: yungsters

Differential Revision: D39922252

fbshipit-source-id: b678fa9a43a896873d8c434745bdaf3f16fd991f
This commit is contained in:
Nick Gerleman
2022-09-29 22:25:24 -07:00
committed by Facebook GitHub Bot
parent fd180de774
commit 5a18ccdbe2
41 changed files with 320 additions and 162 deletions

109
enums.py
View File

@@ -1,4 +1,4 @@
# Copyright (c) Facebook, Inc. and its affiliates.
# 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.
@@ -31,7 +31,7 @@ ENUMS = {
"SpaceBetween",
"SpaceAround",
],
"PositionType": ["Relative", "Absolute"],
"PositionType": ["Static", "Relative", "Absolute"],
"Display": ["Flex", "None"],
"Wrap": ["NoWrap", "Wrap", "WrapReverse"],
"MeasureMode": ["Undefined", "Exactly", "AtMost"],
@@ -56,14 +56,26 @@ ENUMS = {
"PrintOptions": [("Layout", 1), ("Style", 2), ("Children", 4)],
}
LICENSE = """/**
* Copyright (c) Facebook, Inc. and its affiliates.
# Generated Java enums used to emit @DoNotStrip, but D17519844 removed them
# manually from all but YogaLogLevel. TODO: Is it safe to remove from it as
# well?
DO_NOT_STRIP = ["LogLevel"]
def get_license(ext):
prologue = "/**" if ext == "js" else "/*"
return """{}
* 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
""".format(
prologue
)
def to_java_upper(symbol):
@@ -92,32 +104,40 @@ root = os.path.dirname(os.path.abspath(__file__))
# write out C & Objective-C headers
with open(root + "/yoga/YGEnums.h", "w") as f:
f.write(LICENSE)
f.write("#pragma once\n\n")
f.write(get_license("cpp"))
f.write("#pragma once\n")
f.write("// clang-format: off\n\n")
f.write('#include "YGMacros.h"\n\n')
f.write("YG_EXTERN_C_BEGIN\n\n")
for name, values in sorted(ENUMS.items()):
f.write("#define YG%sCount %s\n" % (name, len(values)))
f.write("typedef YG_ENUM_BEGIN(YG%s) {\n" % name)
f.write('YG_EXTERN_C_BEGIN\n\n')
items = sorted(ENUMS.items())
for name, values in items:
if (isinstance(values[0], tuple)):
f.write("YG_ENUM_DECL(\n")
else:
f.write("YG_ENUM_SEQ_DECL(\n")
f.write(" YG%s,\n" % name)
for value in values:
if isinstance(value, tuple):
f.write(" YG%s%s = %d,\n" % (name, value[0], value[1]))
f.write(" YG%s%s = %d" % (name, value[0], value[1]))
else:
f.write(" YG%s%s,\n" % (name, value))
f.write("} YG_ENUM_END(YG%s);\n" % name)
f.write(
"WIN_EXPORT const char *YG%sToString(const YG%s value);\n" % (name, name)
)
f.write(" YG%s%s" % (name, value))
if value == values[-1]:
f.write(")\n")
else:
f.write(",\n")
f.write("\n")
f.write("YG_EXTERN_C_END\n")
# write out C body for printing
with open(root + "/yoga/YGEnums.cpp", "w") as f:
f.write(LICENSE)
f.write(get_license("cpp"))
f.write('#include "YGEnums.h"\n\n')
for name, values in sorted(ENUMS.items()):
f.write("const char *YG%sToString(const YG%s value){\n" % (name, name))
f.write(" switch(value){\n")
items = sorted(ENUMS.items())
for name, values in items:
f.write("const char* YG%sToString(const YG%s value) {\n" % (name, name))
f.write(" switch (value) {\n")
for value in values:
if isinstance(value, tuple):
f.write(" case YG%s%s:\n" % (name, value[0]))
@@ -127,15 +147,18 @@ with open(root + "/yoga/YGEnums.cpp", "w") as f:
f.write(' return "%s";\n' % to_log_lower(value))
f.write(" }\n")
f.write(' return "unknown";\n')
f.write("}\n\n")
f.write("}\n")
if name != items[-1][0]:
f.write("\n")
# write out java files
for name, values in sorted(ENUMS.items()):
with open(root + "/java/com/facebook/yoga/Yoga%s.java" % name, "w") as f:
f.write(LICENSE.replace("/**", "/*", 1))
f.write(get_license("java"))
f.write("package com.facebook.yoga;\n\n")
f.write("import com.facebook.proguard.annotations.DoNotStrip;\n\n")
f.write("@DoNotStrip\n")
if name in DO_NOT_STRIP:
f.write("import com.facebook.proguard.annotations.DoNotStrip;\n\n")
f.write("@DoNotStrip\n")
f.write("public enum Yoga%s {\n" % name)
if len(values) > 0:
for value in values:
@@ -150,7 +173,7 @@ for name, values in sorted(ENUMS.items()):
else:
f.write("__EMPTY(-1);")
f.write("\n")
f.write(" private int mIntValue;\n")
f.write(" private final int mIntValue;\n")
f.write("\n")
f.write(" Yoga%s(int intValue) {\n" % name)
f.write(" mIntValue = intValue;\n")
@@ -160,6 +183,8 @@ for name, values in sorted(ENUMS.items()):
f.write(" return mIntValue;\n")
f.write(" }\n")
f.write("\n")
if name in DO_NOT_STRIP:
f.write(" @DoNotStrip\n")
f.write(" public static Yoga%s fromInt(int value) {\n" % name)
f.write(" switch (value) {\n")
for value in values:
@@ -182,7 +207,7 @@ for name, values in sorted(ENUMS.items()):
# write out csharp files
for name, values in sorted(ENUMS.items()):
with open(root + "/csharp/Facebook.Yoga/Yoga%s.cs" % name, "w") as f:
f.write(LICENSE)
f.write(get_license("cs"))
f.write("namespace Facebook.Yoga\n{\n")
if isinstance(next(iter(values or []), None), tuple):
f.write(" [System.Flags]\n")
@@ -197,9 +222,12 @@ for name, values in sorted(ENUMS.items()):
# write out javascript file
with open(root + "/javascript/sources/YGEnums.js", "w") as f:
f.write(LICENSE)
f.write("module.exports = {\n\n")
for name, values in sorted(ENUMS.items()):
f.write(get_license("js"))
f.write("// @flow\n")
f.write("// @format\n")
f.write("const CONSTANTS = {\n")
items = sorted(ENUMS.items())
for name, values in items:
f.write(" %s_COUNT: %s,\n" % (to_java_upper(name), len(values)))
base = 0
for value in values:
@@ -214,5 +242,24 @@ with open(root + "/javascript/sources/YGEnums.js", "w") as f:
" %s_%s: %d,\n" % (to_java_upper(name), to_java_upper(value), base)
)
base += 1
f.write("\n")
if name != items[-1][0]:
f.write("\n")
f.write("};\n")
for name, values in sorted(ENUMS.items()):
f.write("export type Yoga${} =\n".format(name))
for value in values:
unpackedValue = value[0] if isinstance(value, tuple) else value
f.write(
" | typeof CONSTANTS.{}_{}".format(
to_java_upper(name), to_java_upper(unpackedValue)
)
)
if values[-1] == value:
f.write(";\n")
else:
f.write("\n")
f.write("\n")
f.write("module.exports = CONSTANTS;\n")