fix: template bugs
This commit is contained in:
parent
95166f25e6
commit
15463bfc74
|
|
@ -122,10 +122,11 @@ mixin RetrofitApiSchema {
|
||||||
return 'unnamedMethod';
|
return 'unnamedMethod';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 直接使用路径最后部分,转换为 camelCase
|
||||||
|
// 例如: GetTaskTypeInfo -> getTaskTypeInfo
|
||||||
|
// 不添加 HTTP 方法前缀,保持与 Swagger 文档一致
|
||||||
final lastPart = pathParts.last.replaceAll(RegExp(r'\W+'), '');
|
final lastPart = pathParts.last.replaceAll(RegExp(r'\W+'), '');
|
||||||
final method = path.method.value.toLowerCase();
|
final methodName = StringHelper.toCamelCase(lastPart);
|
||||||
final pascalPart = StringHelper.toPascalCase(lastPart);
|
|
||||||
final methodName = '$method$pascalPart';
|
|
||||||
|
|
||||||
return methodName;
|
return methodName;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ mixin RetrofitApiTemplateData {
|
||||||
List<String> _getImportsForPaths(List<ApiPath> paths) {
|
List<String> _getImportsForPaths(List<ApiPath> paths) {
|
||||||
final imports = <String>{};
|
final imports = <String>{};
|
||||||
final config = ConfigRepository.loadSync();
|
final config = ConfigRepository.loadSync();
|
||||||
|
|
||||||
// 添加基础包导入
|
// 添加基础包导入
|
||||||
imports
|
imports
|
||||||
..add('package:dio/dio.dart')
|
..add('package:dio/dio.dart')
|
||||||
|
|
@ -74,36 +74,26 @@ mixin RetrofitApiTemplateData {
|
||||||
/// 获取 models index.dart 的导入路径
|
/// 获取 models index.dart 的导入路径
|
||||||
String _getModelsIndexImport() {
|
String _getModelsIndexImport() {
|
||||||
final config = ConfigRepository.loadSync();
|
final config = ConfigRepository.loadSync();
|
||||||
final apiDir = config.apiDir;
|
|
||||||
final modelsDir = config.modelsDir;
|
final modelsDir = config.modelsDir;
|
||||||
|
|
||||||
// 如果配置为空,返回空字符串
|
// 如果配置为空,返回空字符串
|
||||||
if (apiDir.isEmpty || modelsDir.isEmpty) {
|
if (modelsDir.isEmpty) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取包名(从 base_result_import 中提取)
|
// 从 pubspec.yaml 获取包名
|
||||||
final baseResultImport = config.baseResultImport;
|
final packageName = PathResolver.getPackageName();
|
||||||
if (baseResultImport.isEmpty) {
|
if (packageName == null || packageName.isEmpty) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从 base_result_import 中提取包名
|
|
||||||
// 例如: "package:example_app/common/base_result.dart" -> "example_app"
|
|
||||||
final packageMatch = RegExp(r'^package:([^/]+)/').firstMatch(baseResultImport);
|
|
||||||
if (packageMatch == null) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
final packageName = packageMatch.group(1)!;
|
|
||||||
|
|
||||||
// 将 models_dir 转换为包导入路径
|
// 将 models_dir 转换为包导入路径
|
||||||
// 例如: "./lib/src/api_models" -> "package:example_app/src/api_models/index.dart"
|
// 例如: "./lib/src/api_models" -> "package:oa_api_hs/src/api_models/index.dart"
|
||||||
var modelsPath = modelsDir
|
final modelsPath = modelsDir
|
||||||
.replaceAll(r'\', '/')
|
.replaceAll(r'\', '/')
|
||||||
.replaceAll('./', '')
|
.replaceAll('./', '')
|
||||||
.replaceAll('lib/', '');
|
.replaceAll('lib/', '');
|
||||||
|
|
||||||
return 'package:$packageName/$modelsPath/index.dart';
|
return 'package:$packageName/$modelsPath/index.dart';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,17 +102,24 @@ mixin RetrofitApiTemplateData {
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> _buildMethodData(ApiPath path) {
|
Map<String, dynamic> _buildMethodData(ApiPath path) {
|
||||||
|
final params = _buildParametersData(path);
|
||||||
return {
|
return {
|
||||||
'docLines': _buildDocLines(path),
|
'docLines': _buildDocLines(path),
|
||||||
'annotations': _buildAnnotations(path),
|
'annotations': _buildAnnotations(path),
|
||||||
'returnType': _g._generateReturnType(path),
|
'returnType': _g._generateReturnType(path),
|
||||||
'methodName': _g._generateSimpleMethodName(path),
|
'methodName': _g._generateSimpleMethodName(path),
|
||||||
'params': _buildParametersData(path),
|
'params': params,
|
||||||
|
'hasParams': params.isNotEmpty,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> _buildDocLines(ApiPath path) {
|
List<String> _buildDocLines(ApiPath path) {
|
||||||
final docLines = <String>[];
|
final docLines = <String>[];
|
||||||
|
|
||||||
|
// 添加 HTTP 方法信息
|
||||||
|
final httpMethod = path.method.value.toUpperCase();
|
||||||
|
docLines.add('[$httpMethod]');
|
||||||
|
|
||||||
if (path.summary.isNotEmpty) {
|
if (path.summary.isNotEmpty) {
|
||||||
// Clean summary to remove newlines and other problematic characters
|
// Clean summary to remove newlines and other problematic characters
|
||||||
docLines.add(TextCleaner.cleanDescription(path.summary));
|
docLines.add(TextCleaner.cleanDescription(path.summary));
|
||||||
|
|
@ -137,7 +134,7 @@ mixin RetrofitApiTemplateData {
|
||||||
List<String> _buildAnnotations(ApiPath path) {
|
List<String> _buildAnnotations(ApiPath path) {
|
||||||
final annotations = <String>[];
|
final annotations = <String>[];
|
||||||
final method = path.method.value.toUpperCase();
|
final method = path.method.value.toUpperCase();
|
||||||
annotations.add('@$method(\'${path.path}\')');
|
annotations.add("@$method('${path.path}')");
|
||||||
|
|
||||||
if (path.isMultipart) {
|
if (path.isMultipart) {
|
||||||
annotations.add('@MultiPart()');
|
annotations.add('@MultiPart()');
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import 'package:swagger_generator_flutter/core/config_repository.dart';
|
||||||
import 'package:swagger_generator_flutter/core/models.dart';
|
import 'package:swagger_generator_flutter/core/models.dart';
|
||||||
import 'package:swagger_generator_flutter/core/template_renderer.dart';
|
import 'package:swagger_generator_flutter/core/template_renderer.dart';
|
||||||
import 'package:swagger_generator_flutter/pipeline/generate/impl/base_generator.dart';
|
import 'package:swagger_generator_flutter/pipeline/generate/impl/base_generator.dart';
|
||||||
|
import 'package:swagger_generator_flutter/utils/path_resolver.dart';
|
||||||
import 'package:swagger_generator_flutter/utils/string_helper.dart';
|
import 'package:swagger_generator_flutter/utils/string_helper.dart';
|
||||||
import 'package:swagger_generator_flutter/utils/string_utils/index.dart';
|
import 'package:swagger_generator_flutter/utils/string_utils/index.dart';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,10 @@ class TemplateLoader {
|
||||||
// 从当前目录向上查找 templates 与 lib/templates
|
// 从当前目录向上查找 templates 与 lib/templates
|
||||||
_collectUpwardTemplateDirs().forEach(addDir);
|
_collectUpwardTemplateDirs().forEach(addDir);
|
||||||
|
|
||||||
|
// 尝试从包的 lib/templates 目录加载(用于包依赖场景)
|
||||||
|
final packageTemplateDirs = _findPackageTemplateDirs();
|
||||||
|
packageTemplateDirs.forEach(addDir);
|
||||||
|
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,6 +82,48 @@ class TemplateLoader {
|
||||||
return dirs;
|
return dirs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 查找包的模板目录(用于作为依赖使用时)
|
||||||
|
List<String> _findPackageTemplateDirs() {
|
||||||
|
final dirs = <String>[];
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 尝试从 .dart_tool/package_config.json 解析包路径
|
||||||
|
final packageConfigFile = File('.dart_tool/package_config.json');
|
||||||
|
if (!packageConfigFile.existsSync()) {
|
||||||
|
return dirs;
|
||||||
|
}
|
||||||
|
|
||||||
|
final configContent = packageConfigFile.readAsStringSync();
|
||||||
|
|
||||||
|
// 简单的正则匹配提取 swagger_generator_flutter 的 rootUri
|
||||||
|
// 格式示例: "name": "swagger_generator_flutter", ... "rootUri": "file:///path/to/package"
|
||||||
|
final pattern = RegExp(
|
||||||
|
r'"name"\s*:\s*"swagger_generator_flutter"[^}]*"rootUri"\s*:\s*"([^"]+)"',
|
||||||
|
multiLine: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
final match = pattern.firstMatch(configContent);
|
||||||
|
if (match != null) {
|
||||||
|
var rootUri = match.group(1)!;
|
||||||
|
|
||||||
|
// 移除 file:// 前缀
|
||||||
|
if (rootUri.startsWith('file://')) {
|
||||||
|
rootUri = rootUri.substring(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加模板目录路径
|
||||||
|
final templateDir = p.join(rootUri, 'lib', 'templates');
|
||||||
|
if (Directory(templateDir).existsSync()) {
|
||||||
|
dirs.add(templateDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// 忽略错误,返回空列表
|
||||||
|
}
|
||||||
|
|
||||||
|
return dirs;
|
||||||
|
}
|
||||||
|
|
||||||
void clearCache() {
|
void clearCache() {
|
||||||
_cache.clear();
|
_cache.clear();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,6 @@
|
||||||
{{#annotations}}
|
{{#annotations}}
|
||||||
{{.}}
|
{{.}}
|
||||||
{{/annotations}}
|
{{/annotations}}
|
||||||
Future<{{returnType}}> {{methodName}}(
|
Future<{{returnType}}> {{methodName}}({{#hasParams}}{
|
||||||
{{#params}} {{#annotation}}{{.}} {{/annotation}}{{type}} {{name}},
|
{{#params}} {{#annotation}}{{.}} {{/annotation}}{{type}} {{name}},
|
||||||
{{/params}} );
|
{{/params}} }{{/hasParams}});
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,30 @@ class PathResolver {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 获取项目包名(从 pubspec.yaml 中读取)
|
||||||
|
static String? getPackageName() {
|
||||||
|
final configDir = getConfigDirectory();
|
||||||
|
if (configDir == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final pubspecFile = File(path.join(configDir, 'pubspec.yaml'));
|
||||||
|
if (!pubspecFile.existsSync()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final content = pubspecFile.readAsStringSync();
|
||||||
|
// 简单的正则匹配提取包名
|
||||||
|
// 格式: name: package_name
|
||||||
|
final match =
|
||||||
|
RegExp(r'^name:\s*(\S+)', multiLine: true).firstMatch(content);
|
||||||
|
return match?.group(1);
|
||||||
|
} catch (_) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 解析路径(支持相对路径和绝对路径)
|
/// 解析路径(支持相对路径和绝对路径)
|
||||||
/// 如果是相对路径,相对于项目根目录(配置文件所在目录)
|
/// 如果是相对路径,相对于项目根目录(配置文件所在目录)
|
||||||
static String resolvePath(String filePath) {
|
static String resolvePath(String filePath) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue