fix: recursively search for package_config.json to support workspace resolution
This commit is contained in:
parent
a4e7a7453e
commit
ceb91a0ea3
|
|
@ -87,9 +87,27 @@ class TemplateLoader {
|
||||||
final dirs = <String>[];
|
final dirs = <String>[];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 尝试从 .dart_tool/package_config.json 解析包路径
|
// 递归向上查找 .dart_tool/package_config.json
|
||||||
final packageConfigFile = File('.dart_tool/package_config.json');
|
File? packageConfigFile;
|
||||||
if (!packageConfigFile.existsSync()) {
|
var currentDir = Directory.current;
|
||||||
|
const maxDepth = 6; // 增加搜索深度以支持 monorepo
|
||||||
|
var depth = 0;
|
||||||
|
|
||||||
|
while (depth < maxDepth) {
|
||||||
|
final checkFile =
|
||||||
|
File(p.join(currentDir.path, '.dart_tool', 'package_config.json'));
|
||||||
|
if (checkFile.existsSync()) {
|
||||||
|
packageConfigFile = checkFile;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
final parent = currentDir.parent;
|
||||||
|
if (parent.path == currentDir.path) break;
|
||||||
|
currentDir = parent;
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packageConfigFile == null) {
|
||||||
return dirs;
|
return dirs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +121,7 @@ class TemplateLoader {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (package != null) {
|
if (package != null) {
|
||||||
final rootUri = package['rootUri'] as String;
|
var rootUri = package['rootUri'] as String;
|
||||||
String packagePath;
|
String packagePath;
|
||||||
|
|
||||||
// 处理 file:// 协议
|
// 处理 file:// 协议
|
||||||
|
|
@ -117,18 +135,27 @@ class TemplateLoader {
|
||||||
|
|
||||||
// 如果不是绝对路径,需要相对于 .dart_tool/package_config.json 所在目录解析
|
// 如果不是绝对路径,需要相对于 .dart_tool/package_config.json 所在目录解析
|
||||||
if (!p.isAbsolute(packagePath)) {
|
if (!p.isAbsolute(packagePath)) {
|
||||||
// package_config.json 在 .dart_tool 目录下
|
// package_config.json 在 packageConfigFile.parent 目录下 (.dart_tool)
|
||||||
packagePath = p.normalize(p.join('.dart_tool', packagePath));
|
packagePath =
|
||||||
|
p.normalize(p.join(packageConfigFile.parent.path, packagePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加模板目录路径
|
// 添加模板目录路径
|
||||||
final templateDir = p.join(packagePath, 'lib', 'templates');
|
// 1. 尝试 lib/templates (标准包结构)
|
||||||
|
var templateDir = p.join(packagePath, 'lib', 'templates');
|
||||||
if (Directory(templateDir).existsSync()) {
|
if (Directory(templateDir).existsSync()) {
|
||||||
dirs.add(templateDir);
|
dirs.add(templateDir);
|
||||||
|
} else {
|
||||||
|
// 2. 尝试 templates (本地调试或非标准结构)
|
||||||
|
templateDir = p.join(packagePath, 'templates');
|
||||||
|
if (Directory(templateDir).existsSync()) {
|
||||||
|
dirs.add(templateDir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (e) {
|
||||||
// 忽略错误,返回空列表
|
// 忽略错误,返回空列表
|
||||||
|
print('Warning: Failed to load package templates: $e');
|
||||||
}
|
}
|
||||||
|
|
||||||
return dirs;
|
return dirs;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue