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>[];
|
||||
|
||||
try {
|
||||
// 尝试从 .dart_tool/package_config.json 解析包路径
|
||||
final packageConfigFile = File('.dart_tool/package_config.json');
|
||||
if (!packageConfigFile.existsSync()) {
|
||||
// 递归向上查找 .dart_tool/package_config.json
|
||||
File? packageConfigFile;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +121,7 @@ class TemplateLoader {
|
|||
);
|
||||
|
||||
if (package != null) {
|
||||
final rootUri = package['rootUri'] as String;
|
||||
var rootUri = package['rootUri'] as String;
|
||||
String packagePath;
|
||||
|
||||
// 处理 file:// 协议
|
||||
|
|
@ -117,18 +135,27 @@ class TemplateLoader {
|
|||
|
||||
// 如果不是绝对路径,需要相对于 .dart_tool/package_config.json 所在目录解析
|
||||
if (!p.isAbsolute(packagePath)) {
|
||||
// package_config.json 在 .dart_tool 目录下
|
||||
packagePath = p.normalize(p.join('.dart_tool', packagePath));
|
||||
// package_config.json 在 packageConfigFile.parent 目录下 (.dart_tool)
|
||||
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()) {
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue