Skip to content

Commit

Permalink
env 改为单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Mar 28, 2024
1 parent 8de73cc commit 1e12b74
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 125 deletions.
8 changes: 3 additions & 5 deletions src/ECAInfo/customEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env } from '../env';
import { env } from '../env';
import * as vscode from 'vscode';

type Event = {
Expand All @@ -13,18 +13,16 @@ type EventArg = {
};

export class Loader extends vscode.Disposable {
private env: Env;
private uri: vscode.Uri;
private onUpdate: (customEvent: Loader) => void;
private data?: Object;
private _events?: Event[];

constructor(env: Env, onUpdated: (customEvent: Loader) => void) {
constructor(onUpdated: (customEvent: Loader) => void) {
let watcher: vscode.FileSystemWatcher;
super(() => watcher?.dispose());
this.env = env;
this.onUpdate = onUpdated;
this.uri = vscode.Uri.joinPath(this.env.scriptUri!, '../customevent.json');
this.uri = vscode.Uri.joinPath(env.scriptUri!, '../customevent.json');
watcher = vscode.workspace.createFileSystemWatcher(this.uri.fsPath);
watcher.onDidCreate(this.update, this);
watcher.onDidChange(this.update, this);
Expand Down
17 changes: 7 additions & 10 deletions src/editorTable/CSVeditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env } from "../env";
import { env } from "../env";
import * as csv from 'fast-csv';
import { EditorTableType, editorTableTypeToFolderName, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName } from '../constants';
import * as vscode from 'vscode';
Expand All @@ -9,9 +9,6 @@ import { hash, isJson, getFileNameByVscodeUri, isCSV, isPathValid } from '../uti
* 物编数据CSV表格的编辑器
*/
export class CSVeditor {
constructor(private env: Env) {

}

public modifyUID(oldUID: number, newUID: number) {

Expand All @@ -21,13 +18,13 @@ export class CSVeditor {
* 从工程文件添加
*/
public addEditorTableItemFromProject(editorTableItem: vscode.QuickPickItem) {
if (!editorTableItem.description || !this.env.scriptUri) {
if (!editorTableItem.description || !env.scriptUri) {
vscode.window.showErrorMessage("未初始化Y3项目");
return;
}
let englishEditorTableType = chineseTypeNameToEnglishTypeName[editorTableItem.description];
let csvRelativePath = this.env.tableTypeToCSVfolderPath[englishEditorTableType];
let csvPath = vscode.Uri.joinPath(this.env.scriptUri, csvRelativePath);
let csvRelativePath = env.tableTypeToCSVfolderPath[englishEditorTableType];
let csvPath = vscode.Uri.joinPath(env.scriptUri, csvRelativePath);
if (!isPathValid(csvPath.fsPath)) {
vscode.window.showErrorMessage("未找到CSV文件,请先生成");
return;
Expand Down Expand Up @@ -91,7 +88,7 @@ export class CSVeditor {
for (let type in EditorTableType) {
let typeStr = EditorTableType[type as keyof typeof EditorTableType];
let folderName: string = editorTableTypeToFolderName[typeStr];
res = res.concat(this.searchEditorTableItemsInFolder(type,path.join(this.env.editorTablePath, folderName), query));
res = res.concat(this.searchEditorTableItemsInFolder(type,path.join(env.editorTablePath, folderName), query));
}
return res;
}
Expand Down Expand Up @@ -125,7 +122,7 @@ export class CSVeditor {
let name;
if (editorTableJson.hasOwnProperty('name')) {
let nameKey: any = editorTableJson['name'];
name = this.env.zhlanguageJson[nameKey];
name = env.zhlanguageJson[nameKey];
}
let uid = editorTableJson['uid'];
if (!uid || typeof uid !=='number') {
Expand All @@ -149,4 +146,4 @@ export class CSVeditor {
});
return res;
}
}
}
32 changes: 14 additions & 18 deletions src/editorTable/CSVimporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as vscode from 'vscode';
import csvParser from 'csv-parser';
import * as fs from 'fs';

import { Env } from "../env";
import { env } from "../env";
import { isInDirectory, isPathValid, toUnicodeIgnoreASCII, hash } from '../utility';
import { csvTypeToPath } from "../constants";
import * as path from 'path';
Expand All @@ -15,9 +15,7 @@ import { v4 as uuidv4 } from 'uuid';
export class CSVimporter
{
private readonly csvTypeToPath: Readonly<{ [key: string]: string }>;
private env: Env;
public constructor(env: Env) {
this.env = env;
public constructor() {
this.csvTypeToPath = csvTypeToPath;
}

Expand All @@ -26,13 +24,13 @@ export class CSVimporter
* @returns true or false 成功或失败
*/
public async importCSVFromOrderFolder():Promise<boolean> {
if (!this.env.scriptUri) {
if (!env.scriptUri) {
vscode.window.showErrorMessage("scriptUri不存在,请检查项目是否初始化");
return false;
}
for (const key in this.env.tableTypeToCSVfolderPath) {
let csvRelativeFolderPath = this.env.tableTypeToCSVfolderPath[key];
let csvFolderUri: vscode.Uri = vscode.Uri.joinPath(this.env.scriptUri, csvRelativeFolderPath);
for (const key in env.tableTypeToCSVfolderPath) {
let csvRelativeFolderPath = env.tableTypeToCSVfolderPath[key];
let csvFolderUri: vscode.Uri = vscode.Uri.joinPath(env.scriptUri, csvRelativeFolderPath);
if (!isPathValid(csvFolderUri.fsPath)) {
vscode.window.showErrorMessage("未找到CSV表格的路径,请从模板中生成");
return false;
Expand All @@ -52,12 +50,12 @@ export class CSVimporter
* @returns
*/
private async importAllCSVinFolder(folder: vscode.Uri, tableType:string): Promise<boolean> {
if (!this.env.editorTableUri) {
if (!env.editorTableUri) {
vscode.window.showErrorMessage("物编数据表路径为空");
return false;
}
let targetTableFolder = this.csvTypeToPath[tableType];
let targetEditorTablePath: vscode.Uri = vscode.Uri.joinPath(this.env.editorTableUri, targetTableFolder);
let targetEditorTablePath: vscode.Uri = vscode.Uri.joinPath(env.editorTableUri, targetTableFolder);
let files = await vscode.workspace.fs.readDirectory(folder);
for (const file of files) {
console.log(file);
Expand Down Expand Up @@ -148,8 +146,8 @@ export class CSVimporter
let attrJson: any;
// 自定义属性的描述要加到工程文件的attr.json中
try {
if (this.env.mapUri) {
attrJson = await fs.readFileSync(vscode.Uri.joinPath(this.env.mapUri, "attr.json").fsPath);
if (env.mapUri) {
attrJson = await fs.readFileSync(vscode.Uri.joinPath(env.mapUri, "attr.json").fsPath);
attrJson = JSON.parse(attrJson);
}
else {
Expand All @@ -176,7 +174,7 @@ export class CSVimporter
attrJson['c'].push(customAttrData);
}
try {
await fs.writeFileSync(vscode.Uri.joinPath(this.env.mapUri, "attr.json").fsPath, toUnicodeIgnoreASCII(JSON.stringify(attrJson, null, 2)), 'utf8');
await fs.writeFileSync(vscode.Uri.joinPath(env.mapUri, "attr.json").fsPath, toUnicodeIgnoreASCII(JSON.stringify(attrJson, null, 2)), 'utf8');
}
catch (error)
{
Expand All @@ -188,8 +186,8 @@ export class CSVimporter
else if (key === 'name') {
let zhlanguageJson: any;
try {
if (this.env.mapUri) {
zhlanguageJson = await fs.readFileSync(vscode.Uri.joinPath(this.env.mapUri, "zhlanguage.json").fsPath, 'utf8');
if (env.mapUri) {
zhlanguageJson = await fs.readFileSync(vscode.Uri.joinPath(env.mapUri, "zhlanguage.json").fsPath, 'utf8');
zhlanguageJson = JSON.parse(zhlanguageJson);
}
else {
Expand All @@ -206,7 +204,7 @@ export class CSVimporter
zhlanguageJson[hashOfName] = value;
jsonData[key] = hashOfName;
try {
await fs.writeFileSync(vscode.Uri.joinPath(this.env.mapUri, "zhlanguage.json").fsPath, JSON.stringify(zhlanguageJson, null, 2), 'utf8');
await fs.writeFileSync(vscode.Uri.joinPath(env.mapUri, "zhlanguage.json").fsPath, JSON.stringify(zhlanguageJson, null, 2), 'utf8');
}
catch (error) {
vscode.window.showErrorMessage("保存zhlanguage.json失败");
Expand Down Expand Up @@ -343,5 +341,3 @@ export class CSVimporter
return true;
}
}


10 changes: 5 additions & 5 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { Env } from '../env';
import { env } from '../env';
import { isPathValid, isJson, getFileNameByVscodeUri } from '../utility';
import { encode } from 'punycode';
import { englishPathToChinese } from '../constants';
Expand All @@ -14,23 +14,23 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode
private editorTablePath: string = "";
private zhlanguageJson: any = undefined;

constructor(private env: Env) {
constructor() {
this.englishPathToChinese = englishPathToChinese;
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage("当前未打开工作目录");
return;
}

this.editorTablePath = this.env.editorTablePath;
this.editorTablePath = env.editorTablePath;

// 载入中文名称
this.zhlanguageJson = this.env.zhlanguageJson;
this.zhlanguageJson = env.zhlanguageJson;
}

refresh(): void {

// 重新读取zhlanguage.json以刷新
this.env.refreshZhlanguageJson();
env.refreshZhlanguageJson();
this._onDidChangeTreeData.fire(undefined);
}

Expand Down
8 changes: 3 additions & 5 deletions src/editorTable/templateGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import { Env } from '../env';
import { env } from '../env';
import * as path from 'path';
import { isPathValid } from '../utility';
import { chineseTypeNameToEnglishTypeName,englishTypeNameToChineseTypeName } from '../constants';
export class TemplateGenerator{
private env: Env;
private readonly englishToChinese;
private readonly chineseToEnglish;
public constructor(env: Env) {
this.env = env;
public constructor() {
this.chineseToEnglish = chineseTypeNameToEnglishTypeName;
this.englishToChinese = englishTypeNameToChineseTypeName;
}
Expand Down Expand Up @@ -75,4 +73,4 @@ export class TemplateGenerator{
fs.renameSync(oldFileName, newFileName);
}
}
}
}
4 changes: 3 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EditorTableType } from './constants';

type EditorVersion = '1.0' | '2.0' | 'unknown';

export class Env {
class Env {

constructor() {
this.initTableTypeToCSVfolderPath();// 初始化时从插件配置更新物编数据对应存放文件夹路径的关系
Expand Down Expand Up @@ -369,3 +369,5 @@ export class Env {


}

export let env = new Env();
Loading

0 comments on commit 1e12b74

Please sign in to comment.