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

View File

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

View File

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

View File

@ -138,11 +138,42 @@ export const getProjectionList = (entityName: string) => {
*/ */
let isAnalyzing = false; let isAnalyzing = false;
export const analyzeOakAppDomain = async (oakAppDomainPath: string) => { export const analyzeOakAppDomain = async (
oakAppDomainPath: string,
forceUpdate: boolean
) => {
if (isAnalyzing) { if (isAnalyzing) {
return; 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; let worker: Worker | null = null;
try { try {
@ -178,6 +209,16 @@ export const analyzeOakAppDomain = async (oakAppDomainPath: string) => {
Object.keys(entityDict).forEach((key) => { Object.keys(entityDict).forEach((key) => {
entityDictCache[key] = entityDict[key]; entityDictCache[key] = entityDict[key];
}); });
if (enableCache) {
// 写入缓存
fs.writeFile(
cacheFile,
JSON.stringify(entityDict),
(err) => {
console.error(err);
}
);
}
} }
isAnalyzing = false; isAnalyzing = false;
setLoadingEntities(false); setLoadingEntities(false);
@ -270,3 +311,13 @@ export const genProjections = (name: string): string[] => {
}) })
.filter((attr) => !!attr); .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 { debounce, random } from 'lodash';
import os from "os"; import os from 'os';
import fs from 'fs';
import { join } from 'path';
// 如果系统是windows首先采用\\ 否则默认使用/ // 如果系统是windows首先采用\\ 否则默认使用/
@ -26,6 +28,8 @@ export const internalPath = {
oakAppDomain: `src${delimiter}oak-app-domain`, oakAppDomain: `src${delimiter}oak-app-domain`,
components: `src${delimiter}components`, components: `src${delimiter}components`,
locales: `src${delimiter}locales`, locales: `src${delimiter}locales`,
// 插件的缓存文件目录
cachePath: `node_modules${delimiter}aaaaaoakPcache`,
}; };
export const pathConfig: { export const pathConfig: {
@ -38,6 +42,7 @@ export const pathConfig: {
get oakAppDomainHome(): string; get oakAppDomainHome(): string;
get componentsHome(): string; get componentsHome(): string;
get localesHome(): string; get localesHome(): string;
get cachePath(): string;
} = { } = {
projectHome: '', projectHome: '',
get entityHome() { get entityHome() {
@ -64,6 +69,9 @@ export const pathConfig: {
get localesHome() { get localesHome() {
return `${this.projectHome}${delimiter}${internalPath.locales}`; return `${this.projectHome}${delimiter}${internalPath.locales}`;
}, },
get cachePath() {
return `${this.projectHome}${delimiter}${internalPath.cachePath}`;
},
}; };
// 发布订阅模式 // 发布订阅模式
@ -73,8 +81,8 @@ const updateDeounced = debounce(() => {
subscribers.forEach((callback) => { subscribers.forEach((callback) => {
try { try {
callback(); callback();
} catch(e) { } catch (e) {
console.log("error", e); console.log('error', e);
} }
}); });
}, 100); }, 100);
@ -107,9 +115,10 @@ export const isConfigReady = (): boolean => {
}; };
export const setProjectHome = (projectHome: string) => { export const setProjectHome = (projectHome: string) => {
const newHome = (projectHome.endsWith('\\') || projectHome.endsWith('/')) const newHome =
? projectHome.slice(0, -1) projectHome.endsWith('\\') || projectHome.endsWith('/')
: projectHome; ? projectHome.slice(0, -1)
: projectHome;
if (newHome !== pathConfig.projectHome) { if (newHome !== pathConfig.projectHome) {
pathConfig.projectHome = newHome; pathConfig.projectHome = newHome;
updateDeounced(); updateDeounced();
@ -166,3 +175,17 @@ export function normalizePath(path: string): string {
export function isRelativePath(path: string): boolean { export function isRelativePath(path: string): boolean {
return path.startsWith('.') || path.startsWith('..'); 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 * as vscode from 'vscode';
import { join } from 'path'; import { join } from 'path';
import { delimiter, pluginPaths } from './paths'; import { delimiter, mkdirsSync, pluginPaths } from './paths';
import fs from 'fs'; import fs from 'fs';
import { CreateComponentConfig, CreateOakComponent } from '../types'; import { CreateComponentConfig, CreateOakComponent } from '../types';
import Handlebars from 'handlebars'; import Handlebars from 'handlebars';
@ -39,20 +39,6 @@ export function getTemplateContent(name: TemplateName): string {
return template; 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 = ( export const outputTemplate = (
name: TemplateName, name: TemplateName,
data: any, data: any,