Skip to content

Commit

Permalink
可以直接设置到excel目录;编译完后清理空目录
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jan 10, 2025
1 parent 83223c2 commit 5d5883b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
},
"Y3-Helper.ECAOverridePath": {
"title": "(开发)用于ECA转Lua配置导表的工程路径",
"description": "填编辑器工程路径即可,如 `D:/up1`",
"description": "填编辑器工程路径即可,如 `D:/up1`,也可以填到excel所在的目录",
"type": "string",
"default": "",
"scope": "resource"
Expand Down
19 changes: 17 additions & 2 deletions src/ecaCompiler/excelConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ import * as y3 from 'y3-helper';
import * as vscode from 'vscode';

export class ExcelConfig {
async loadConfig(): Promise<Record<string, string> | undefined> {
async findExcelDir(): Promise<vscode.Uri | undefined> {
const overridePath = vscode.workspace.getConfiguration('Y3-Helper').get('ECAOverridePath');
if (typeof overridePath !== 'string' || overridePath === '') {
return undefined;
}
const excelDirUri = vscode.Uri.file(overridePath + '/excel/编辑器/触发器');
let guessUri = vscode.Uri.file(overridePath + '/excel/编辑器/触发器');
if (await y3.fs.isDirectory(guessUri)) {
return guessUri;
}
return vscode.Uri.file(overridePath);
}

async loadConfig(): Promise<Record<string, string> | undefined> {
const excelDirUri = await this.findExcelDir();
if (!excelDirUri) {
return;
}

if (!await y3.fs.isDirectory(excelDirUri)) {
throw new Error('未找到触发器配置文件夹,请检查配置:' + excelDirUri.fsPath);
}
Expand All @@ -16,6 +28,9 @@ export class ExcelConfig {

for (const fileName of ['02触发器事件条件表.xlsx', '03触发器动作表.xlsx', '04触发器函数表.xlsx']) {
const excelUri = y3.uri(excelDirUri, fileName);
if (!await y3.fs.isFile(excelUri)) {
continue;
}
const sheet = await y3.excel.loadFile(excelUri, '触发器');
const table = sheet.makeTable('B1', 1);
for (const key in table) {
Expand Down
11 changes: 10 additions & 1 deletion src/ecaCompiler/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export class Process {
y3.log.info('【编译ECA】等待文件写入硬盘...');

const baseDir = y3.uri(this.outMap.uri, this.scriptDir, this.outBasseDir);
let removeTask = [];
let scanResult = await y3.fs.scan(baseDir);
for (let file of scanResult) {
if (this.progress?.isCanceled()) {
Expand All @@ -319,7 +320,7 @@ export class Process {
let fullName = `${this,this.outBasseDir}/${file[0]}`;
if (file[1] === vscode.FileType.File && !this.writeCache.has(fullName)) {
y3.log.debug(`【编译ECA】删除多余的文件:${fullName}`);
y3.fs.removeFile(y3.uri(this.outMap.uri, this.scriptDir, fullName));
removeTask.push(y3.fs.removeFile(y3.uri(this.outMap.uri, this.scriptDir, fullName)));
}
}

Expand All @@ -339,5 +340,13 @@ export class Process {
await y3.fs.writeFile(uri, content);
this.progress?.update();
}

if (removeTask.length > 0) {
Promise.all(removeTask).then(async () => {
y3.log.debug('【编译ECA】删除多余文件完成,清理空文件夹');
await y3.fs.deleteEmptyDir(baseDir);
y3.log.debug('【编译ECA】空文件夹清理完成');
});
}
}
}
19 changes: 19 additions & 0 deletions src/tools/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,22 @@ export function isRelativePath(path: string) {
export function isAbsolutePath(path: string) {
return path.startsWith('/') || /^[a-zA-Z]:\\/.test(path);
}

export async function deleteEmptyDir(uri: vscode.Uri | string, recursive = true) {
if (typeof uri === 'string') {
uri = vscode.Uri.file(uri);
}
let files = await vscode.workspace.fs.readDirectory(uri);
if (files.length === 0) {
await vscode.workspace.fs.delete(uri);
return;
}
if (!recursive) {
return;
}
for (const [name, fileType] of files) {
if (fileType === vscode.FileType.Directory) {
await deleteEmptyDir(vscode.Uri.joinPath(uri, name), recursive);
}
}
}

0 comments on commit 5d5883b

Please sign in to comment.