autosnippet 1.1.15 → 1.1.16
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/cache.js +4 -3
- package/bin/create.js +57 -20
- package/bin/init.js +3 -8
- package/bin/injection.js +130 -28
- package/package.json +1 -1
package/bin/cache.js
CHANGED
|
@@ -41,11 +41,12 @@ async function updateCache(specFile, content) {
|
|
|
41
41
|
linkCache[key] = element['{readme}'];
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
// ✅ SPM 模块:headName 已经包含相对路径,不需要 specHeadPath
|
|
44
45
|
if (element['{headName}']
|
|
45
|
-
&& element['{specHeadPath}']
|
|
46
46
|
&& element['{language}'] !== 'Xcode.SourceCodeLanguage.Swift') {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
// headName 本身已经是相对路径,直接使用
|
|
48
|
+
const headerFileName = path.basename(element['{headName}']);
|
|
49
|
+
headCache[headerFileName] = element['{headName}'];
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
});
|
package/bin/create.js
CHANGED
|
@@ -13,23 +13,32 @@ const CMD_PATH = process.cwd();
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* 根据文件路径确定模块名(SPM)
|
|
16
|
+
* 返回路径中最近的(最深的)target 名称
|
|
16
17
|
*/
|
|
17
18
|
function determineModuleName(filePath, packageInfo) {
|
|
18
19
|
// 从路径中提取模块名
|
|
19
|
-
// 例如:
|
|
20
|
+
// 例如:Business/BDNetworkAPI/Code/... -> BDNetworkAPI(不是 Business)
|
|
20
21
|
const packagePath = packageInfo.path;
|
|
21
22
|
const relativePath = path.relative(packagePath, filePath);
|
|
22
23
|
const segments = relativePath.split(path.sep);
|
|
23
24
|
|
|
24
|
-
//
|
|
25
|
-
|
|
25
|
+
// ✅ 从后向前查找匹配的 target(优先匹配路径中更深的 target)
|
|
26
|
+
// 这样可以避免匹配到聚合 target(如 Business),而匹配到实际的 target(如 BDNetworkAPI)
|
|
27
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
28
|
+
const segment = segments[i];
|
|
26
29
|
if (packageInfo.targets.includes(segment)) {
|
|
27
30
|
return segment;
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
// 如果找不到,使用第一个 target
|
|
32
|
-
|
|
34
|
+
// 如果找不到,使用第一个 target(排除包名,如果包名也是 target)
|
|
35
|
+
const firstTarget = packageInfo.targets[0];
|
|
36
|
+
if (firstTarget && firstTarget !== packageInfo.name) {
|
|
37
|
+
return firstTarget;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 最后回退到包名
|
|
41
|
+
return packageInfo.name;
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
function updateCodeSnippets(specFile, word, key, value) {
|
|
@@ -234,7 +243,9 @@ function readStream(specFile, filePathArr, snippet, isHaveHeader) {
|
|
|
234
243
|
if (!packagePath) {
|
|
235
244
|
console.log('未找到 Package.swift 文件,请检查路径。');
|
|
236
245
|
snippet['{content}'] = codeList;
|
|
237
|
-
|
|
246
|
+
// ✅ SPM 模块:.boxspec 文件位置在模块根目录(与 Package.swift 同级)
|
|
247
|
+
const moduleSpecFile = path.join(path.dirname(packagePath), 'AutoSnippet.boxspec.json');
|
|
248
|
+
saveFromFile(moduleSpecFile, snippet);
|
|
238
249
|
return;
|
|
239
250
|
}
|
|
240
251
|
|
|
@@ -242,41 +253,51 @@ function readStream(specFile, filePathArr, snippet, isHaveHeader) {
|
|
|
242
253
|
const packageInfo = await findPath.parsePackageSwift(packagePath);
|
|
243
254
|
if (!packageInfo) {
|
|
244
255
|
snippet['{content}'] = codeList;
|
|
245
|
-
|
|
256
|
+
const moduleSpecFile = path.join(path.dirname(packagePath), 'AutoSnippet.boxspec.json');
|
|
257
|
+
saveFromFile(moduleSpecFile, snippet);
|
|
246
258
|
return;
|
|
247
259
|
}
|
|
248
260
|
|
|
249
261
|
// 根据当前文件路径确定模块名
|
|
250
262
|
const moduleName = determineModuleName(filePath, packageInfo);
|
|
251
|
-
const
|
|
263
|
+
const headerNameWithoutExt = fileName.substring(0, fileName.length - 2); // 移除 .h
|
|
264
|
+
|
|
265
|
+
// ✅ 模块根目录就是 Package.swift 所在目录(packageInfo.path)
|
|
266
|
+
// .boxspec.json 文件将创建在这个目录下(与 Package.swift 同级)
|
|
267
|
+
const moduleRoot = packageInfo.path;
|
|
252
268
|
|
|
253
269
|
// ✅ 查找头文件(适配 SPM 的 include/ModuleName/ 结构)
|
|
254
|
-
|
|
270
|
+
// 在 Package.swift 所在目录下查找(可能包含 Services/Services/... 这样的结构)
|
|
271
|
+
const headerPath = await findPath.findSubHeaderPath(moduleRoot, headerNameWithoutExt, moduleName);
|
|
255
272
|
|
|
256
273
|
snippet['{content}'] = codeList;
|
|
257
|
-
snippet['{specName}'] = moduleName;
|
|
258
|
-
snippet['{headName}'] = fileName;
|
|
274
|
+
snippet['{specName}'] = moduleName; // ✅ specName 是 target 名称(如 BDNetworkAPI),不是包名(如 Business)
|
|
259
275
|
|
|
260
276
|
if (headerPath) {
|
|
261
|
-
// ✅
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
277
|
+
// ✅ headName 存储相对于 Package.swift 所在目录的相对路径
|
|
278
|
+
// 例如:如果 Package.swift 在 Business/,头文件在 Business/Business/BDNetworkAPI/Code/xxx.h
|
|
279
|
+
// 则 headName = "Business/BDNetworkAPI/Code/xxx.h"
|
|
280
|
+
const headerRelativePath = path.relative(moduleRoot, headerPath);
|
|
281
|
+
snippet['{headName}'] = headerRelativePath;
|
|
282
|
+
} else {
|
|
283
|
+
// 如果找不到头文件,使用文件名
|
|
284
|
+
snippet['{headName}'] = fileName;
|
|
265
285
|
}
|
|
266
286
|
|
|
267
287
|
// 查找 README.md
|
|
268
288
|
try {
|
|
269
289
|
const readmePath = path.join(packageInfo.path, README_NAME);
|
|
270
290
|
await fs.promises.access(readmePath);
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
const readmeRelativePath = path.relative(specFileDir, readmePath);
|
|
291
|
+
const moduleRoot = packageInfo.path;
|
|
292
|
+
const readmeRelativePath = path.relative(moduleRoot, readmePath);
|
|
274
293
|
snippet['{readme}'] = encodeURI(readmeRelativePath);
|
|
275
294
|
} catch {
|
|
276
295
|
// README.md 不存在,跳过
|
|
277
296
|
}
|
|
278
297
|
|
|
279
|
-
|
|
298
|
+
// ✅ SPM 模块:.boxspec 文件位置在模块根目录(与 Package.swift 同级)
|
|
299
|
+
const moduleSpecFile = path.join(packageInfo.path, 'AutoSnippet.boxspec.json');
|
|
300
|
+
saveFromFile(moduleSpecFile, snippet);
|
|
280
301
|
}).catch(function (err) {
|
|
281
302
|
console.error('Error finding Package.swift:', err);
|
|
282
303
|
snippet['{content}'] = codeList;
|
|
@@ -304,7 +325,23 @@ function saveFromFile(specFile, snippet) {
|
|
|
304
325
|
placeholder = JSON.parse(data);
|
|
305
326
|
}
|
|
306
327
|
} catch (err) {
|
|
307
|
-
|
|
328
|
+
// ✅ 文件不存在或读取失败,创建新的配置文件
|
|
329
|
+
if (err.code === 'ENOENT') {
|
|
330
|
+
// 确保目录存在
|
|
331
|
+
const specFileDir = path.dirname(specFile);
|
|
332
|
+
try {
|
|
333
|
+
fs.mkdirSync(specFileDir, { recursive: true });
|
|
334
|
+
} catch (mkdirErr) {
|
|
335
|
+
// 目录可能已存在,忽略错误
|
|
336
|
+
}
|
|
337
|
+
// 创建空的配置文件结构
|
|
338
|
+
placeholder = {
|
|
339
|
+
list: []
|
|
340
|
+
};
|
|
341
|
+
} else {
|
|
342
|
+
console.error(err);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
308
345
|
}
|
|
309
346
|
|
|
310
347
|
if (placeholder != null) {
|
package/bin/init.js
CHANGED
|
@@ -48,14 +48,9 @@ async function initSpec() {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
idsArray.push(item['{identifier}']);
|
|
51
|
-
// ✅
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const decodedPath = decodeURI(item['{specHeadPath}']);
|
|
55
|
-
const combinedPath = thePath ? path.join(thePath, decodedPath) : decodedPath;
|
|
56
|
-
// 规范化路径(处理 .. 和 .)
|
|
57
|
-
item['{specHeadPath}'] = encodeURI(path.normalize(combinedPath));
|
|
58
|
-
}
|
|
51
|
+
// ✅ SPM 模块:headName 已经是相对于模块根目录的相对路径,如果子模块与主配置不在同一目录,需要调整
|
|
52
|
+
// headName 保持相对于各自模块根目录的路径(不需要转换)
|
|
53
|
+
// 去掉 specHeadPath 的处理逻辑
|
|
59
54
|
return true;
|
|
60
55
|
});
|
|
61
56
|
specObj.list = specObj.list.concat(arr);
|
package/bin/injection.js
CHANGED
|
@@ -58,33 +58,117 @@ function handleHeaderLineSwift(specFile, updateFile, headerLine, importArray) {
|
|
|
58
58
|
|
|
59
59
|
// specFile实际上是获取缓存的key,用来获取Snippet的模块空间信息,没有路径意义
|
|
60
60
|
// updateFile是当前修改文件路径,用来获取当前模块空间信息
|
|
61
|
-
function handleHeaderLine(specFile, updateFile, headerLine, importArray, isSwift) {
|
|
61
|
+
async function handleHeaderLine(specFile, updateFile, headerLine, importArray, isSwift) {
|
|
62
62
|
if (isSwift) {
|
|
63
63
|
handleHeaderLineSwift(specFile, updateFile, headerLine, importArray);
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
const header = createHeader(headerLine);
|
|
67
67
|
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
// ✅ 重新设计:区分当前模块内部和外部
|
|
69
|
+
// 1. 获取当前文件所在的模块(通过 Package.swift)
|
|
70
|
+
// 2. 获取头文件所在的模块(从头文件路径查找 Package.swift,确定最近的 target 模块名)
|
|
71
|
+
// 3. 比较模块名:
|
|
72
|
+
// - 相同模块 -> 使用相对路径 #import "Header.h"
|
|
73
|
+
// - 不同模块 -> 使用 #import <ModuleName/Header.h>
|
|
74
|
+
|
|
75
|
+
// 获取当前文件所在的模块
|
|
76
|
+
const updateFileDir = path.dirname(updateFile);
|
|
77
|
+
const currentPackagePath = await findPath.findPackageSwiftPath(updateFileDir);
|
|
78
|
+
|
|
79
|
+
if (!currentPackagePath) {
|
|
80
|
+
// 找不到当前模块的 Package.swift,默认使用 <> 格式
|
|
81
|
+
handleModuleHeader(specFile, updateFile, header, importArray, true);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
72
84
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
85
|
+
const currentPackageInfo = await findPath.parsePackageSwift(currentPackagePath);
|
|
86
|
+
if (!currentPackageInfo) {
|
|
87
|
+
handleModuleHeader(specFile, updateFile, header, importArray, true);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 获取当前文件所在的模块名(通过路径判断)
|
|
92
|
+
const currentModuleName = determineCurrentModuleName(updateFile, currentPackageInfo);
|
|
93
|
+
|
|
94
|
+
// ✅ 获取头文件缓存,然后确定头文件所在的模块名
|
|
95
|
+
const headCache = await cache.getHeadCache(specFile);
|
|
96
|
+
const headerModuleName = await determineHeaderModuleName(specFile, header, headCache);
|
|
97
|
+
|
|
98
|
+
// ✅ 更新 header 中的模块名和 specName,确保使用正确的模块名(最近的 target 模块名)
|
|
99
|
+
header.moduleName = headerModuleName;
|
|
100
|
+
header.specName = '<' + headerModuleName + '/' + header.headerName + '>';
|
|
101
|
+
|
|
102
|
+
// ✅ 判断是否为同一模块
|
|
103
|
+
const isSameModule = currentModuleName === headerModuleName;
|
|
104
|
+
|
|
105
|
+
// 如果是同一模块,使用相对路径;否则使用 <> 格式
|
|
106
|
+
handleModuleHeader(specFile, updateFile, header, importArray, !isSameModule);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 根据文件路径确定当前模块名(SPM)
|
|
111
|
+
*/
|
|
112
|
+
function determineCurrentModuleName(filePath, packageInfo) {
|
|
113
|
+
const relativePath = path.relative(packageInfo.path, filePath);
|
|
114
|
+
const segments = relativePath.split(path.sep);
|
|
115
|
+
|
|
116
|
+
// 查找匹配的 target(最近的 target)
|
|
117
|
+
for (const segment of segments) {
|
|
118
|
+
if (packageInfo.targets.includes(segment)) {
|
|
119
|
+
return segment;
|
|
81
120
|
}
|
|
82
|
-
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 如果找不到,使用第一个 target
|
|
124
|
+
return packageInfo.targets[0] || packageInfo.name;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 确定头文件所在的模块名(SPM)
|
|
129
|
+
* 从头文件路径查找 Package.swift,确定最近的 target 模块名
|
|
130
|
+
*/
|
|
131
|
+
async function determineHeaderModuleName(specFile, header, headCache) {
|
|
132
|
+
if (!headCache) {
|
|
133
|
+
// 没有缓存,使用从 headerLine 解析的模块名(可能不准确)
|
|
134
|
+
return header.moduleName;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 从头缓存中获取头文件的相对路径
|
|
138
|
+
const headRelativePath = headCache[header.headerName];
|
|
139
|
+
if (!headRelativePath) {
|
|
140
|
+
// 找不到头文件路径,使用从 headerLine 解析的模块名
|
|
141
|
+
return header.moduleName;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ✅ specFile 位于模块根目录(如 ModuleRoot/AutoSnippet.boxspec.json)
|
|
145
|
+
const moduleRootDir = path.dirname(specFile);
|
|
146
|
+
const headPath = path.join(moduleRootDir, headRelativePath);
|
|
147
|
+
|
|
148
|
+
// 从头文件路径向上查找 Package.swift
|
|
149
|
+
const headerPackagePath = await findPath.findPackageSwiftPath(headPath);
|
|
150
|
+
if (!headerPackagePath) {
|
|
151
|
+
// 找不到 Package.swift,使用从 headerLine 解析的模块名
|
|
152
|
+
return header.moduleName;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 解析 Package.swift 获取模块信息
|
|
156
|
+
const headerPackageInfo = await findPath.parsePackageSwift(headerPackagePath);
|
|
157
|
+
if (!headerPackageInfo) {
|
|
158
|
+
return header.moduleName;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ✅ 根据头文件路径确定最近的 target 模块名
|
|
162
|
+
return determineCurrentModuleName(headPath, headerPackageInfo);
|
|
83
163
|
}
|
|
84
164
|
|
|
85
165
|
// isOuter区分模块内部引用""格式和模块外部引用<>格式
|
|
166
|
+
// ✅ SPM 模块:
|
|
167
|
+
// - isOuter = false:同一模块内部,使用相对路径 #import "Header.h"
|
|
168
|
+
// - isOuter = true:不同模块之间,使用 #import <ModuleName/Header.h>
|
|
86
169
|
function handleModuleHeader(specFile, updateFile, header, importArray, isOuter) {
|
|
87
|
-
|
|
170
|
+
// ✅ 根据 isOuter 选择不同的引入格式
|
|
171
|
+
const headName = isOuter ? header.name : header.headerStrName; // isOuter: <ModuleName/Header.h>, 否则: "Header.h"
|
|
88
172
|
const moduleName = isOuter ? header.specName : header.moduleStrName;
|
|
89
173
|
|
|
90
174
|
// 检查是否已经引入头文件
|
|
@@ -93,20 +177,21 @@ function handleModuleHeader(specFile, updateFile, header, importArray, isOuter)
|
|
|
93
177
|
|
|
94
178
|
if (importHeader === headName) {
|
|
95
179
|
// 已经引入头文件
|
|
96
|
-
handelAddHeaderStatus(specFile, updateFile, header, true, false);
|
|
180
|
+
handelAddHeaderStatus(specFile, updateFile, header, true, false, isOuter);
|
|
97
181
|
return;
|
|
98
182
|
} else if (importHeader === moduleName) {
|
|
99
|
-
//
|
|
100
|
-
handelAddHeaderStatus(specFile, updateFile, header, false, true);
|
|
183
|
+
// 已经引入模块头文件(如 <ModuleName/ModuleName.h>)
|
|
184
|
+
handelAddHeaderStatus(specFile, updateFile, header, false, true, isOuter);
|
|
101
185
|
return;
|
|
102
186
|
}
|
|
103
187
|
}
|
|
104
188
|
|
|
105
189
|
// 没有找到已引入的头文件,直接添加
|
|
106
|
-
addHeaderToFile
|
|
190
|
+
// addHeaderToFile 会根据 isOuter 使用不同的格式
|
|
191
|
+
addHeaderToFile(updateFile, header, isOuter);
|
|
107
192
|
}
|
|
108
193
|
|
|
109
|
-
function handelAddHeaderStatus(specFile, updateFile, header, isAddedHeader, isAddedSpecHeader) {
|
|
194
|
+
function handelAddHeaderStatus(specFile, updateFile, header, isAddedHeader, isAddedSpecHeader, isOuter) {
|
|
110
195
|
if (isAddedHeader) {
|
|
111
196
|
// 已经引入头文件
|
|
112
197
|
removeMarkFromFile(updateFile, header, '依赖头文件已存在,不需要额外引入。');
|
|
@@ -117,22 +202,32 @@ function handelAddHeaderStatus(specFile, updateFile, header, isAddedHeader, isAd
|
|
|
117
202
|
// spec header足够了,不需要添加头文件
|
|
118
203
|
removeMarkFromFile(updateFile, header, '依赖模块头文件已存在,不需要额外引入。');
|
|
119
204
|
} else {
|
|
120
|
-
|
|
205
|
+
// 使用传入的 isOuter 参数
|
|
206
|
+
addHeaderToFile(updateFile, header, isOuter);
|
|
121
207
|
}
|
|
122
208
|
});
|
|
123
209
|
} else {
|
|
124
210
|
// 都没找到,添加头文件
|
|
125
|
-
|
|
211
|
+
// 使用传入的 isOuter 参数
|
|
212
|
+
addHeaderToFile(updateFile, header, isOuter);
|
|
126
213
|
}
|
|
127
214
|
}
|
|
128
215
|
|
|
129
216
|
function isAddedToSpecHeader(specFile, header, callback) {
|
|
130
217
|
cache.getHeadCache(specFile).then(function (headCache) {
|
|
131
218
|
if (headCache) {
|
|
132
|
-
// ✅
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
const
|
|
219
|
+
// ✅ SPM 模块:headName 已经是相对于模块根目录的相对路径
|
|
220
|
+
// specFile 位于模块根目录(如 ModuleRoot/AutoSnippet.boxspec.json)
|
|
221
|
+
const moduleRootDir = path.dirname(specFile);
|
|
222
|
+
const headRelativePath = headCache[header.headerName];
|
|
223
|
+
|
|
224
|
+
if (!headRelativePath) {
|
|
225
|
+
callback(false);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 拼接头文件的完整路径
|
|
230
|
+
const headPath = path.join(moduleRootDir, headRelativePath);
|
|
136
231
|
|
|
137
232
|
try {
|
|
138
233
|
// 读取当前头文件所在工作空间里默认暴露的头文件
|
|
@@ -169,8 +264,15 @@ function removeMarkFromFile(updateFile, header, string) {
|
|
|
169
264
|
});
|
|
170
265
|
}
|
|
171
266
|
|
|
172
|
-
function addHeaderToFile(updateFile, header) {
|
|
173
|
-
|
|
267
|
+
function addHeaderToFile(updateFile, header, isOuter) {
|
|
268
|
+
// ✅ 根据 isOuter 选择不同的引入格式
|
|
269
|
+
// isOuter = true: 使用 <> 格式(#import <ModuleName/Header.h>)
|
|
270
|
+
// isOuter = false: 使用相对路径(#import "Header.h")
|
|
271
|
+
const importLine = isOuter
|
|
272
|
+
? importMark + ' ' + header.name // <ModuleName/Header.h>
|
|
273
|
+
: importMark + ' ' + header.headerStrName; // "Header.h"
|
|
274
|
+
|
|
275
|
+
readStream(updateFile, importLine, importMark);
|
|
174
276
|
checkDependency(updateFile, header.moduleName, '自动注入头文件完成。').catch(err => {
|
|
175
277
|
console.error('Error checking dependency:', err);
|
|
176
278
|
});
|