createPageCode static method
Future<void>
createPageCode(
{ - required String templateFile,
- required String targetDir,
- required String templateClassName,
- required String className,
- bool? forceFlush = false,
})
Implementation
static Future<void> createPageCode({
required String templateFile,
required String targetDir,
required String templateClassName,
required String className,
bool? forceFlush = false, //慎用
}) async {
if (FFEmpty.stringIsEmpty(targetDir)) {
print("targetDir 参数不能为空");
return;
}
if (!targetDir.endsWith(Platform.pathSeparator)) {
targetDir = "${targetDir}${Platform.pathSeparator}";
}
bool isFileExist = await FFile.isExistFile(templateFile);
if (isFileExist == false) {
print("模板文件不存在 ${templateFile}");
return;
}
String classCode = await FFile.readAsString(templateFile);
if (FFEmpty.stringIsEmpty(classCode)) {
print("模板文件内容不应该为空 ${templateFile}");
return;
}
if (!classCode.contains(templateClassName)) {
print("模板文件不包含类名${templateClassName} ${templateFile}");
return;
}
classCode = classCode.replaceAll(templateClassName, className);
String filePath = "$targetDir$className.dart";
if (forceFlush == true) {
FFile.writeContentByPath(filePath: filePath, content: classCode);
} else {
bool exist = await FFile.isExistFile(filePath);
if (exist) {
print("路径下类已经存在 ${filePath}");
} else {
bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,autoCreate: true);
if (success == true) {
print("路径下类生成成功 ${filePath}");
} else {
print("路径下类生成失败 ${filePath}");
}
}
}
}