autosnippet 1.2.4 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/injection.js +82 -24
- package/package.json +1 -1
package/bin/injection.js
CHANGED
|
@@ -136,41 +136,99 @@ async function handleHeaderLine(specFile, updateFile, headerLine, importArray, i
|
|
|
136
136
|
// ✅ 优先检查:如果头文件路径和当前文件在同一个目录下,直接使用文件名
|
|
137
137
|
const headRelativePathFromInfo = headerInfo.headRelativePath;
|
|
138
138
|
let isSameDirectory = false;
|
|
139
|
+
let headFullPath = null;
|
|
140
|
+
let headDir = null;
|
|
139
141
|
|
|
140
142
|
if (headRelativePathFromInfo) {
|
|
141
143
|
// 从头文件相对路径构造完整路径
|
|
142
|
-
|
|
143
|
-
const headDir = path.dirname(headFullPath);
|
|
144
|
+
// headRelativePathFromInfo 是相对于模块根目录的路径(如 Code/BDGlobalVideoPlayerManager.h)
|
|
144
145
|
|
|
145
|
-
//
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
// 方法:从当前文件路径向上查找,直到找到包含 headRelativePathFromInfo 的路径
|
|
147
|
+
const headName = path.basename(headRelativePathFromInfo); // BDGlobalVideoPlayerManager.h
|
|
148
|
+
|
|
149
|
+
// 方案1:尝试在当前文件所在目录直接查找
|
|
150
|
+
const headInCurrentDir = path.join(updateFileDir, headName);
|
|
151
|
+
if (fs.existsSync(headInCurrentDir)) {
|
|
152
|
+
headFullPath = headInCurrentDir;
|
|
153
|
+
headDir = updateFileDir;
|
|
154
|
+
} else {
|
|
155
|
+
// 方案2:从当前文件路径向上查找,尝试找到头文件
|
|
156
|
+
let searchDir = updateFileDir;
|
|
157
|
+
for (let i = 0; i < 5 && searchDir !== path.dirname(searchDir); i++) {
|
|
158
|
+
const possiblePath = path.join(searchDir, headRelativePathFromInfo);
|
|
159
|
+
if (fs.existsSync(possiblePath)) {
|
|
160
|
+
headFullPath = possiblePath;
|
|
161
|
+
headDir = path.dirname(possiblePath);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
searchDir = path.dirname(searchDir);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 方案3:尝试使用模块路径拼接
|
|
168
|
+
if (!headFullPath) {
|
|
169
|
+
const possiblePaths = [
|
|
170
|
+
path.join(currentPackageInfo.path, headRelativePathFromInfo), // 当前模块根目录
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
// 如果当前文件在 Code/ 目录下,尝试向上查找模块根目录
|
|
174
|
+
if (updateFileDir.endsWith('Code')) {
|
|
175
|
+
const possibleModuleRoot = path.dirname(updateFileDir);
|
|
176
|
+
possiblePaths.push(path.join(possibleModuleRoot, headRelativePathFromInfo));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for (const possiblePath of possiblePaths) {
|
|
180
|
+
if (fs.existsSync(possiblePath)) {
|
|
181
|
+
headFullPath = possiblePath;
|
|
182
|
+
headDir = path.dirname(possiblePath);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (headDir) {
|
|
190
|
+
console.log(`🔍 目录比较: 当前目录=${updateFileDir}, 头文件目录=${headDir}, headRelativePath=${headRelativePathFromInfo}`);
|
|
191
|
+
|
|
192
|
+
// 检查是否在同一个目录(使用规范化路径比较)
|
|
193
|
+
const normalizedUpdateDir = path.normalize(updateFileDir);
|
|
194
|
+
const normalizedHeadDir = path.normalize(headDir);
|
|
195
|
+
|
|
196
|
+
if (normalizedUpdateDir === normalizedHeadDir) {
|
|
197
|
+
isSameDirectory = true;
|
|
198
|
+
// 如果同目录,直接使用文件名
|
|
199
|
+
header.relativePathToCurrentFile = header.headerName;
|
|
200
|
+
console.log(`✅ 识别为同目录,使用文件名: ${header.headerName}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ✅ 如果能找到头文件完整路径且不同目录,计算相对路径(即使模块名不同)
|
|
205
|
+
if (headFullPath && fs.existsSync(headFullPath) && !isSameDirectory) {
|
|
206
|
+
// 计算从当前文件到头文件的相对路径
|
|
207
|
+
const relativePathToHeader = path.relative(updateFileDir, headDir);
|
|
208
|
+
|
|
209
|
+
// 如果相对路径为空或为当前目录,使用文件名
|
|
210
|
+
if (!relativePathToHeader || relativePathToHeader === '.') {
|
|
211
|
+
header.relativePathToCurrentFile = header.headerName;
|
|
212
|
+
} else {
|
|
213
|
+
// 拼接相对路径和文件名
|
|
214
|
+
header.relativePathToCurrentFile = path.join(relativePathToHeader, header.headerName).replace(/\\/g, '/');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
console.log(`✅ 计算相对路径: ${updateFileDir} -> ${headDir} = ${header.relativePathToCurrentFile}`);
|
|
150
218
|
}
|
|
151
219
|
}
|
|
152
220
|
|
|
153
221
|
// ✅ 判断是否为同一模块
|
|
154
222
|
const isSameModule = currentModuleName === headerInfo.moduleName;
|
|
155
223
|
|
|
156
|
-
//
|
|
157
|
-
|
|
224
|
+
// ✅ 决定是否使用相对路径
|
|
225
|
+
// 1. 同目录 -> 使用文件名
|
|
226
|
+
// 2. 同一模块 -> 使用相对路径
|
|
227
|
+
// 3. 能找到完整路径 -> 使用相对路径
|
|
228
|
+
// 4. 其他 -> 使用 <> 格式
|
|
229
|
+
const shouldUseRelativePath = isSameDirectory || isSameModule || (headFullPath && fs.existsSync(headFullPath));
|
|
158
230
|
|
|
159
|
-
console.log(`🔍 模块识别: 当前模块=${currentModuleName}, 头文件模块=${headerInfo.moduleName}, 同一模块=${isSameModule}, 同目录=${isSameDirectory}`);
|
|
160
|
-
|
|
161
|
-
// ✅ 如果是同一模块且不同目录,计算相对于当前文件的相对路径
|
|
162
|
-
if (isSameModule && headRelativePathFromInfo && !isSameDirectory) {
|
|
163
|
-
// 当前文件相对于模块根目录的路径
|
|
164
|
-
const currentFileRelativeToModuleRoot = path.relative(currentPackageInfo.path, updateFile);
|
|
165
|
-
// 头文件相对于模块根目录的路径
|
|
166
|
-
const headRelativeToModuleRoot = headRelativePathFromInfo;
|
|
167
|
-
|
|
168
|
-
// 计算从当前文件到头文件的相对路径
|
|
169
|
-
const currentFileDir = path.dirname(currentFileRelativeToModuleRoot);
|
|
170
|
-
const relativePathToHeader = path.relative(currentFileDir, headRelativeToModuleRoot).replace(/\\/g, '/');
|
|
171
|
-
|
|
172
|
-
header.relativePathToCurrentFile = relativePathToHeader;
|
|
173
|
-
}
|
|
231
|
+
console.log(`🔍 模块识别: 当前模块=${currentModuleName}, 头文件模块=${headerInfo.moduleName}, 同一模块=${isSameModule}, 同目录=${isSameDirectory}, 找到头文件路径=${headFullPath ? '是' : '否'}`);
|
|
174
232
|
|
|
175
233
|
// 如果同目录或同一模块,使用相对路径;否则使用 <> 格式
|
|
176
234
|
handleModuleHeader(specFile, updateFile, header, importArray, !shouldUseRelativePath);
|