支持缓存先前分析的entity

This commit is contained in:
pqcqaq 2024-11-25 14:48:12 +08:00
parent b1e137a7dc
commit 7494d26e15
6 changed files with 127 additions and 32 deletions

View File

@ -23,6 +23,10 @@
"command": "oak-assistant.create-oak-component",
"title": "创建OAK组件"
},
{
"command": "oak-assistant.refreshEntity",
"title": "重新扫描实体信息"
},
{
"command": "oak-entities.jumpToDefinition",
"group": "navigation",
@ -108,6 +112,11 @@
"type": "boolean",
"default": true,
"description": "是否显示新项目提示"
},
"oak-assistant.cacheEntityAnalyze": {
"type": "boolean",
"default": false,
"description": "缓存分析的entity"
}
}
},

View File

@ -4,6 +4,7 @@ import {
pathConfig,
subscribe,
normalizePath,
mkdirsSync,
} from './utils/paths';
import { join } from 'path';
import checkPagesAndNamespace from './plugins/checkPagesAndNamespace';
@ -12,7 +13,10 @@ import {
activateOakComponent,
disposeOakComponent,
} from './plugins/createOakComponent';
import { analyzeOakAppDomain } from './utils/entities';
import {
analyzeOakAppDomain,
registerRefreshEntityCommand,
} from './utils/entities';
import { createOakTreePanel } from './plugins/oakTreePanel';
import { setLoadingEntities } from './utils/status';
import { treePanelCommands } from './plugins/treePanelCommands';
@ -85,11 +89,22 @@ const afterPathSet = async () => {
await waitWorkerReady();
},
},
{
name: '创建缓存目录',
description: '创建缓存目录',
function: async () => {
try {
mkdirsSync(pathConfig.cachePath);
} catch (e) {
console.error('创建缓存目录失败');
}
},
},
{
name: '解析 Entity',
description: '解析项目中的 Entity 结构',
function: async () => {
await analyzeOakAppDomain(pathConfig.oakAppDomainHome);
await analyzeOakAppDomain(pathConfig.oakAppDomainHome, false);
},
},
{
@ -167,7 +182,9 @@ const afterPathSet = async () => {
try {
await step.function();
} catch (error) {
vscode.window.showErrorMessage(`步骤${step.name}出错: ${error}`);
vscode.window.showErrorMessage(
`步骤${step.name}出错: ${error}`
);
}
}
vscode.window.showInformationMessage('分析完成');
@ -223,6 +240,7 @@ export async function activate(context: vscode.ExtensionContext) {
);
createFileWatcher(context);
activateStyleConvert(context);
registerRefreshEntityCommand();
} catch (error) {
console.error('激活插件时出错:', error);
}
@ -247,9 +265,11 @@ export async function activate(context: vscode.ExtensionContext) {
}
// 显示新项目提示
const showTip = vscode.workspace.getConfiguration('oak-assistant').get('showNewProjectTip');
if (!showTip) {
const showTip = vscode.workspace
.getConfiguration('oak-assistant')
.get('showNewProjectTip');
if (!showTip) {
console.log('未找到oak.config.json文件并且未开启新项目提示');
return;
}
@ -259,7 +279,7 @@ export async function activate(context: vscode.ExtensionContext) {
'未找到oak.config.json文件是否以当前工作区根目录为项目主目录创建配置并启用Oak-Assistant插件',
'是',
'否',
"不再提示"
'不再提示'
);
if (value === '是') {
const rootPath = workspaceFolders[0].uri.fsPath;
@ -277,7 +297,13 @@ export async function activate(context: vscode.ExtensionContext) {
loadPlugin(defaultConfig);
});
} else if (value === '不再提示') {
vscode.workspace.getConfiguration('oak-assistant').update('showNewProjectTip', false, vscode.ConfigurationTarget.Global);
vscode.workspace
.getConfiguration('oak-assistant')
.update(
'showNewProjectTip',
false,
vscode.ConfigurationTarget.Global
);
}
return;
}

View File

@ -132,7 +132,7 @@ export function createFileWatcher(context: vscode.ExtensionContext) {
// 监控如果Storage.ts发生变化则重新同步entities
const handleStorageChange = async () => {
analyzeOakAppDomain(pathConfig.oakAppDomainHome);
analyzeOakAppDomain(pathConfig.oakAppDomainHome, true);
};
let disposeStorageWatcher: (() => void) | null = null;

View File

@ -138,13 +138,44 @@ export const getProjectionList = (entityName: string) => {
*/
let isAnalyzing = false;
export const analyzeOakAppDomain = async (oakAppDomainPath: string) => {
export const analyzeOakAppDomain = async (
oakAppDomainPath: string,
forceUpdate: boolean
) => {
if (isAnalyzing) {
return;
}
// 先尝试读取缓存
const cacheFile = join(pathConfig.cachePath, 'oakEntity.json');
const enableCache = vscode.workspace
.getConfiguration('oak-assistant')
.get('cacheEntityAnalyze');
if (enableCache) {
if (fs.existsSync(cacheFile)) {
try {
const context = fs.readFileSync(cacheFile, 'utf-8');
const entityDict = JSON.parse(context);
// 更新 entityDictCache
Object.keys(entityDict).forEach((key) => {
entityDictCache[key] = entityDict[key];
});
isAnalyzing = false;
setLoadingEntities(false);
syncProjectEntityList();
console.log('从缓存加载成功');
return;
} catch (e) {
console.error('尝试从缓存读取失败');
}
} else {
console.log('缓存不存在,开始分析');
}
}
let worker: Worker | null = null;
try {
worker = getWorker();
} catch (error) {
@ -178,6 +209,16 @@ export const analyzeOakAppDomain = async (oakAppDomainPath: string) => {
Object.keys(entityDict).forEach((key) => {
entityDictCache[key] = entityDict[key];
});
if (enableCache) {
// 写入缓存
fs.writeFile(
cacheFile,
JSON.stringify(entityDict),
(err) => {
console.error(err);
}
);
}
}
isAnalyzing = false;
setLoadingEntities(false);
@ -270,3 +311,13 @@ export const genProjections = (name: string): string[] => {
})
.filter((attr) => !!attr);
};
// 注册命令oak-assistant.refreshEntity
export const registerRefreshEntityCommand = () => {
return vscode.commands.registerCommand(
'oak-assistant.refreshEntity',
() => {
analyzeOakAppDomain(pathConfig.oakAppDomainHome, true);
}
);
};

View File

@ -1,5 +1,7 @@
import { debounce, random } from 'lodash';
import os from "os";
import os from 'os';
import fs from 'fs';
import { join } from 'path';
// 如果系统是windows首先采用\\ 否则默认使用/
@ -26,6 +28,8 @@ export const internalPath = {
oakAppDomain: `src${delimiter}oak-app-domain`,
components: `src${delimiter}components`,
locales: `src${delimiter}locales`,
// 插件的缓存文件目录
cachePath: `node_modules${delimiter}aaaaaoakPcache`,
};
export const pathConfig: {
@ -38,6 +42,7 @@ export const pathConfig: {
get oakAppDomainHome(): string;
get componentsHome(): string;
get localesHome(): string;
get cachePath(): string;
} = {
projectHome: '',
get entityHome() {
@ -64,6 +69,9 @@ export const pathConfig: {
get localesHome() {
return `${this.projectHome}${delimiter}${internalPath.locales}`;
},
get cachePath() {
return `${this.projectHome}${delimiter}${internalPath.cachePath}`;
},
};
// 发布订阅模式
@ -73,8 +81,8 @@ const updateDeounced = debounce(() => {
subscribers.forEach((callback) => {
try {
callback();
} catch(e) {
console.log("error", e);
} catch (e) {
console.log('error', e);
}
});
}, 100);
@ -107,9 +115,10 @@ export const isConfigReady = (): boolean => {
};
export const setProjectHome = (projectHome: string) => {
const newHome = (projectHome.endsWith('\\') || projectHome.endsWith('/'))
? projectHome.slice(0, -1)
: projectHome;
const newHome =
projectHome.endsWith('\\') || projectHome.endsWith('/')
? projectHome.slice(0, -1)
: projectHome;
if (newHome !== pathConfig.projectHome) {
pathConfig.projectHome = newHome;
updateDeounced();
@ -166,3 +175,17 @@ export function normalizePath(path: string): string {
export function isRelativePath(path: string): boolean {
return path.startsWith('.') || path.startsWith('..');
}
/**
*
*/
export const mkdirsSync = (dirname: string) => {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(join(dirname, '..'))) {
fs.mkdirSync(dirname);
return true;
}
}
};

View File

@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { join } from 'path';
import { delimiter, pluginPaths } from './paths';
import { delimiter, mkdirsSync, pluginPaths } from './paths';
import fs from 'fs';
import { CreateComponentConfig, CreateOakComponent } from '../types';
import Handlebars from 'handlebars';
@ -39,20 +39,6 @@ export function getTemplateContent(name: TemplateName): string {
return template;
}
/**
*
*/
export const mkdirsSync = (dirname: string) => {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(join(dirname, '..'))) {
fs.mkdirSync(dirname);
return true;
}
}
};
export const outputTemplate = (
name: TemplateName,
data: any,