Skip to content

Commit

Permalink
Merge pull request #310 from alibaba/feat/vscode
Browse files Browse the repository at this point in the history
Feat/vscode
  • Loading branch information
z979054461 authored Jun 28, 2022
2 parents 5fb6b7f + 3cf35f0 commit 768a12e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 12 deletions.
24 changes: 22 additions & 2 deletions packages/vscode-pont/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
{
"view": "pontOrigins",
"contents": "请参阅[官方文档](https://github.com/alibaba/pont) 使用 pont start 命令 创建 pont-config.json 文件",
"when": "!pontContext.noConfigFile"
"when": "pontContext.noConfigFile"
},
{
"view": "pontOrigins",
"contents": "请安装或升级pont-engine \n [yarn add pont-engine](command:pont.installPontEngine?%7B%22type%22%3A%22yarn%22%7D) \n [npm i -D pont-engine](command:pont.installPontEngine?%7B%22type%22%3A%22npm%22%7D)",
"when": "!pontContext.versionError",
"when": "pontContext.versionError",
"enablement": "pontConfig.isInit"
}
],
Expand Down Expand Up @@ -93,11 +93,21 @@
"title": "更新模块",
"icon": "$(code)"
},
{
"command": "pont.qickPickMod",
"title": "搜索模块",
"icon": "$(search)"
},
{
"command": "pont.updateBo",
"title": "更新基类",
"icon": "$(code)"
},
{
"command": "pont.qickPickBo",
"title": "搜索基类",
"icon": "$(search)"
},
{
"command": "pont.jumpToMocks",
"title": "jump to mock position"
Expand Down Expand Up @@ -172,10 +182,20 @@
"when": "viewItem == MOD",
"group": "inline"
},
{
"command": "pont.qickPickMod",
"when": "viewItem == MODParent",
"group": "inline"
},
{
"command": "pont.updateBo",
"when": "viewItem == BO",
"group": "inline"
},
{
"command": "pont.qickPickBo",
"when": "viewItem == BOParent",
"group": "inline"
}
]
}
Expand Down
38 changes: 38 additions & 0 deletions packages/vscode-pont/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export const commandMap = {
visitMocks: 'pont.visitMocks',
/** 更新本地接口 */
updateMod: 'pont.updateMod',
/** 搜索并更新本地接口 */
qickPickMod: 'pont.updateMod',
/** 更新本地基类 */
updateBo: 'pont.updateBo',
/** 搜索并更新本地基类 */
qickPickBo: 'pont.updateBo',
/** 显示pont侧边栏 */
showPontBar: 'pont.showPontBar',
/** 重新创建 Manager */
Expand Down Expand Up @@ -236,6 +240,22 @@ export class CommandCenter {
);
}

@command('pont.qickPickMod')
async qickPickMod() {
const modDiffs = this.manager.diffs.modDiffs;
const items = modDiffs.map((item) => {
return {
label: item.name,
description: `${item.details[0]}${item.details.length} 条更新`
} as QuickPickItem;
});
const pickItem = await window.showQuickPick(items);

if (!pickItem) return;

this.updateMod(getPontOriginsProvider().modList.find((item) => item.label === pickItem.label));
}

@command('pont.updateBo')
async updateBo(item: OriginTreeItem) {
if (!item) return;
Expand All @@ -262,6 +282,24 @@ export class CommandCenter {
);
}

@command('pont.qickPickBo')
async qickPicBo() {
const boDiffs = this.manager.diffs.boDiffs;

const items = boDiffs.map((item) => {
return {
label: item.name,
description: item.details.join(', ')
} as QuickPickItem;
});

const pickItem = await window.showQuickPick(items);

if (!pickItem) return;

this.updateMod(getPontOriginsProvider().boList.find((item) => item.label === pickItem.label));
}

@command('pont.showPontBar')
async showPontBar() {
commands.executeCommand('pontOrigins.focus');
Expand Down
21 changes: 11 additions & 10 deletions packages/vscode-pont/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { Manager, Config, lookForFiles } from 'pont-engine';
import * as path from 'path';
import { showProgress, verifyPontEngineVersion } from './utils';
import { MocksServer } from './mocks';
import { lookForFiles } from 'pont-engine';
import { syncNpm, verifyPontEngineVersion } from './utils';
import { CommandCenter } from './commands';
import { setContext } from './utils/setContext';
import { initViews } from './views';
import { getPontOriginsProvider } from './views/pontOrigins';

const managerCleanUps: vscode.Disposable[] = [];

Expand All @@ -20,8 +17,8 @@ function doCleanUp() {
export async function activate(context: vscode.ExtensionContext) {
console.log('extension "Pont" is now active!');

setContext('versionError', true);
setContext('noConfigFile', true);
setContext('versionError', false);
setContext('noConfigFile', false);

const disposables: vscode.Disposable[] = [];
context.subscriptions.push(new vscode.Disposable(() => vscode.Disposable.from(...disposables).dispose()));
Expand All @@ -40,12 +37,16 @@ export async function activate(context: vscode.ExtensionContext) {

commandCenter.setConfigPath(configPath);

await syncNpm();

const versionError = !verifyPontEngineVersion();
const noConfigFile = !configPath;
// 没有安装 pont-engine 或 版本不一致。安装当前vscode插件对应版本的pont-engine
setContext('versionError', verifyPontEngineVersion());
setContext('versionError', versionError);

setContext('noConfigFile', !!configPath);
setContext('noConfigFile', noConfigFile);

if (configPath) {
if (!noConfigFile && !versionError) {
commandCenter.createManager();
}

Expand Down
32 changes: 32 additions & 0 deletions packages/vscode-pont/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import * as child_process from 'child_process';
import { Manager, Interface } from 'pont-engine';
import * as path from 'path';
import * as fs from 'fs';
Expand Down Expand Up @@ -119,3 +120,34 @@ export async function findInterface(editor: vscode.TextEditor, manager: Manager)

return { foundInterface };
}

export async function syncNpm() {
try {
const currVersion = require(path.join(__dirname, '../node_modules/pont-engine/package.json')).version;
const projectVersionPath = path.join(vscode.workspace.rootPath, 'node_modules/pont-engine/package.json');
const yarnPath = path.join(vscode.workspace.rootPath, 'yarn.lock');

const hasProjectVersion = fs.existsSync(projectVersionPath);
const useYarn = fs.existsSync(yarnPath);

const cmd = useYarn ? 'yarn add -D pont-engine@' + currVersion : 'npm i -D pont-engine@' + currVersion;

if (!hasProjectVersion) {
console.log(cmd);
child_process.execSync(cmd, {
cwd: vscode.workspace.rootPath
});
} else {
const projectVersion = require(projectVersionPath).version;

if (projectVersion !== currVersion) {
console.log(cmd);
child_process.execSync(cmd, {
cwd: vscode.workspace.rootPath
});
}
}
} catch (e) {
vscode.window.showErrorMessage('npm 同步错误' + e.toString());
}
}
8 changes: 8 additions & 0 deletions packages/vscode-pont/src/views/pontOrigins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { CONFIG_FILE, getFileName } from 'pont-engine/lib/utils';
class PontOriginsProvider implements vscode.TreeDataProvider<OriginTreeItem> {
manager: Manager;

modList: OriginTreeItem[] = [];

boList: OriginTreeItem[] = [];

constructor() {}

getTreeItem(element: OriginTreeItem): vscode.TreeItem {
Expand Down Expand Up @@ -69,6 +73,7 @@ class PontOriginsProvider implements vscode.TreeDataProvider<OriginTreeItem> {
'MOD'
);
modTreeItem.tooltip = originUrl;
modTreeItem.contextValue = 'MODParent'
items.push(modTreeItem);
}

Expand All @@ -79,6 +84,7 @@ class PontOriginsProvider implements vscode.TreeDataProvider<OriginTreeItem> {
'BO'
);
boTreeItem.tooltip = originUrl;
boTreeItem.contextValue = 'BOParent'
items.push(boTreeItem);
}

Expand All @@ -103,6 +109,7 @@ class PontOriginsProvider implements vscode.TreeDataProvider<OriginTreeItem> {
return modItem;
});

this.modList = modList;
return Promise.resolve(modList);
}

Expand All @@ -116,6 +123,7 @@ class PontOriginsProvider implements vscode.TreeDataProvider<OriginTreeItem> {
return boItem;
});

this.boList = boList;
return Promise.resolve(boList);
}

Expand Down

0 comments on commit 768a12e

Please sign in to comment.