mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-08 02:15:57 +00:00
ref[TypeScript]: refactor generate TypeScript protocol
This commit is contained in:
@@ -44,7 +44,7 @@ public abstract class GenerateProtocolPath {
|
||||
}
|
||||
|
||||
public static String protocolAbsolutePath(short protocolId, CodeLanguage language) {
|
||||
var path = getProtocolPath(protocolId);
|
||||
var path = protocolPathSlash(protocolId);
|
||||
var name = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
if (StringUtils.isBlank(path)) {
|
||||
path = name;
|
||||
@@ -72,7 +72,7 @@ public abstract class GenerateProtocolPath {
|
||||
case GdScript:
|
||||
break;
|
||||
case Python:
|
||||
if (StringUtils.isBlank(getProtocolPath(protocolId))) {
|
||||
if (StringUtils.isBlank(protocolPathSlash(protocolId))) {
|
||||
path = StringUtils.PERIOD;
|
||||
} else {
|
||||
path = StringUtils.substringBeforeLast(path, StringUtils.SLASH);
|
||||
@@ -88,28 +88,21 @@ public abstract class GenerateProtocolPath {
|
||||
/**
|
||||
* 获取协议生成的路径
|
||||
*/
|
||||
public static String protocolPath(short protocolId) {
|
||||
public static String protocolPathPeriod(short protocolId) {
|
||||
return protocolPathMap.get(protocolId);
|
||||
}
|
||||
|
||||
public static String getProtocolPath(short protocolId) {
|
||||
AssertionUtils.notNull(protocolPathMap
|
||||
, "[{}]The initialization has been completed. Get Protocol Path cannot be called after the initialization is completed."
|
||||
, GenerateProtocolPath.class.getSimpleName());
|
||||
|
||||
public static String protocolPathSlash(short protocolId) {
|
||||
var protocolPath = protocolPathMap.get(protocolId);
|
||||
if (StringUtils.isBlank(protocolPath)) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
return protocolPath.replaceAll(StringUtils.PERIOD_REGEX, StringUtils.SLASH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取协议生成的首字母大写的路径
|
||||
*/
|
||||
public static String getCapitalizeProtocolPath(short protocolId) {
|
||||
return StringUtils.joinWith(StringUtils.SLASH, Arrays.stream(getProtocolPath(protocolId).split(StringUtils.SLASH)).map(it -> StringUtils.capitalize(it)).toArray());
|
||||
public static String capitalizeProtocolPathFold(short protocolId) {
|
||||
return StringUtils.joinWith(StringUtils.SLASH, Arrays.stream(protocolPathSlash(protocolId).split(StringUtils.SLASH)).map(it -> StringUtils.capitalize(it)).toArray());
|
||||
}
|
||||
|
||||
public static String getRelativePath(short protocolId, short relativeProtocolId) {
|
||||
|
||||
@@ -158,7 +158,7 @@ public class CodeGenerateCpp implements ICodeGenerate {
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
|
||||
var outputPath = StringUtils.format("{}/{}/{}.h", protocolOutputPath, GenerateProtocolPath.protocolPath(protocol_id), protocol_name);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.h", protocolOutputPath, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated C++ protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
@@ -250,7 +250,7 @@ public class CodeGenerateCpp implements ICodeGenerate {
|
||||
var cppBuilder = new StringBuilder();
|
||||
for (var subProtocolId : subProtocols) {
|
||||
var protocolClassName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
|
||||
var subProtocolPath = StringUtils.format("#include \"{}/{}/{}.h\"", protocolOutputRootPath, GenerateProtocolPath.getCapitalizeProtocolPath(subProtocolId), protocolClassName);
|
||||
var subProtocolPath = StringUtils.format("#include \"{}/{}/{}.h\"", protocolOutputRootPath, GenerateProtocolPath.capitalizeProtocolPathFold(subProtocolId), protocolClassName);
|
||||
cppBuilder.append(subProtocolPath).append(LS);
|
||||
}
|
||||
return cppBuilder.toString();
|
||||
|
||||
@@ -116,7 +116,7 @@ public class CodeGenerateCsharp implements ICodeGenerate {
|
||||
var protocolId = registration.protocolId();
|
||||
var protocolClazzName = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var formatProtocolTemplate = formatProtocolTemplate(registration);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.cs", protocolOutputPath, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId), protocolClazzName);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.cs", protocolOutputPath, GenerateProtocolPath.capitalizeProtocolPathFold(protocolId), protocolClazzName);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated C# protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
+1
-1
@@ -142,7 +142,7 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var formatProtocolTemplate = formatProtocolTemplate(registration);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocol_id), protocol_name);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated ES protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
@@ -124,7 +124,7 @@ public abstract class GenerateGdUtils {
|
||||
protocolTemplate = StringUtils.format(protocolTemplate, protocolId, protocolClazzName, includeSubProtocol, classNote, fieldDefinition.trim(),
|
||||
toStringJsonTemplate, toStringParams, StringUtils.EMPTY_JSON, writeObject.trim(), readObject.trim());
|
||||
|
||||
var outputPath = StringUtils.format("{}/{}/{}.gd", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.gd", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocolId), protocolClazzName);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, protocolTemplate, true);
|
||||
logger.info("Generated GdScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
+1
-1
@@ -157,7 +157,7 @@ public class CodeGenerateJavaScript implements ICodeGenerate {
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
|
||||
var outputPath = StringUtils.format("{}/{}/{}.js", protocolOutputPath, GenerateProtocolPath.protocolPath(protocol_id), protocol_name);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.js", protocolOutputPath, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated Javascript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
@@ -136,7 +136,7 @@ public class CodeGenerateLua implements ICodeGenerate {
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var path = GenerateProtocolPath.protocolPath(protocol_id);
|
||||
var path = GenerateProtocolPath.protocolPathPeriod(protocol_id);
|
||||
protocol_imports.append(StringUtils.format("local {} = require(\"{}.{}.{}\")", protocol_name, protocolOutputRootPath, path.replace(StringUtils.SLASH, StringUtils.PERIOD), protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}", protocol_id, protocol_name)).append(LS);
|
||||
}
|
||||
@@ -156,7 +156,7 @@ public class CodeGenerateLua implements ICodeGenerate {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var formatProtocolTemplate = formatProtocolTemplate(registration);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.lua", protocolOutputPath, GenerateProtocolPath.protocolPath(protocol_id), protocol_name);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.lua", protocolOutputPath, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated Lua protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
@@ -123,7 +123,7 @@ public abstract class GeneratePyUtils {
|
||||
|
||||
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName
|
||||
, fieldDefinition.trim(), protocolId, writeObject.trim(), protocolClazzName, readObject.trim());
|
||||
var outputPath = StringUtils.format("{}/{}/{}.py", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.py", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocolId), protocolClazzName);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, protocolTemplate, true);
|
||||
logger.info("Generated Python protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
|
||||
+2
-2
@@ -169,7 +169,7 @@ public class CodeGenerateTypeScript implements ICodeGenerate {
|
||||
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
|
||||
));
|
||||
|
||||
var outputPath = StringUtils.format("{}/{}/{}.ts", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocol_id), protocol_name);
|
||||
var outputPath = StringUtils.format("{}/{}/{}.ts", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated TypeScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
@@ -234,7 +234,7 @@ public class CodeGenerateTypeScript implements ICodeGenerate {
|
||||
// import IByteBuffer first
|
||||
var protocolId = registration.getId();
|
||||
var importBuilder = new StringBuilder();
|
||||
var protocolPath = GenerateProtocolPath.protocolPath(protocolId);
|
||||
var protocolPath = GenerateProtocolPath.protocolPathPeriod(protocolId);
|
||||
var splits = protocolPath.split(StringUtils.PERIOD_REGEX);
|
||||
importBuilder.append(StringUtils.format("import IByteBuffer from '{}IByteBuffer';", "../".repeat(splits.length))).append(LS);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user