1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16// The module 'vscode' contains the VS Code extensibility API 17// Import the module and reference it with the alias vscode in your code below 18const vscode = require('vscode'); 19const fs = require('fs'); 20const path = require('path'); 21const { readFile } = require('./util/VsPluginTool'); 22 23var extensionIds = []; 24var importCheck = false; 25 26// This method is called when your extension is activated 27// Your extension is activated the very first time the command is executed 28 29/** 30 * @param {vscode.ExtensionContext} context 31 */ 32function activate(context) { 33 34 // Use the console to output diagnostic information (console.log) and errors (console.error) 35 // This line of code will only be executed once when your extension is activated 36 console.log('Congratulations, your extension "Kaihong Assist Tools" is now active!'); 37 38 // The command has been defined in the package.json file 39 // Now provide the implementation of the command with registerCommand 40 // The commandId parameter must match the command field in package.json 41 let disposable = vscode.commands.registerCommand('assist_tools', function () { 42 // The code you place here will be executed every time your command is executed 43 44 globalPanel = vscode.window.createWebviewPanel( 45 'assist tools', // Identifies the type of WebView 46 'Kaihong Assist Tools', // Title of the panel displayed to the user 47 vscode.ViewColumn.Two, // Display the WebView panel in the form of new columns in the editor 48 { 49 enableScripts: true, // Enable or disable JS, default is Enable 50 retainContextWhenHidden: true, // Keep the WebView state when it is hidden to avoid being reset 51 } 52 ); 53 globalPanel.webview.html = getWebviewContent(context); 54 let msg; 55 globalPanel.webview.onDidReceiveMessage(message => { 56 msg = message.msg; 57 if (msg == "cancel") { 58 globalPanel.dispose(); 59 } else if (msg == "startApi") { 60 const extensionId = 'kaihong.ApiScan'; 61 installStartExtension(extensionId); 62 } else if (msg == "startGn") { 63 const extensionId = 'kaihong.gn-gen'; 64 installStartExtension(extensionId); 65 } else if (msg == "startService") { 66 const extensionId = 'kaihong.service-gen'; 67 installStartExtension(extensionId); 68 } else if (msg == "startTs") { 69 const extensionId = 'kaihong.ts-gen'; 70 installStartExtension(extensionId); 71 } else if (msg == "startNapi") { 72 const extensionId = 'kaihong.napi-gen'; 73 installStartExtension(extensionId); 74 } else if (msg == "param") { 75 let isSelectToolChain = installExtensions(message); 76 startExtensions(isSelectToolChain); 77 } 78 }, undefined, context.subscriptions); 79 // Display a message box to the user 80 vscode.window.showInformationMessage('Welcome to use Kaihong Assist Tools!'); 81 }); 82 83 context.subscriptions.push(disposable); 84} 85 86function installExtensions(message) { 87 importCheck = message.importIsCheck; 88 let checkApi = message.checkApi; 89 let checkGn = message.checkGn; 90 let checkService = message.checkService; 91 let checkTs = message.checkTs; 92 let checkNapi = message.checkNapi; 93 if (importCheck) { 94 if (extensionIds.length != 0) { 95 extensionIds.length = 0; 96 } 97 if (checkApi == 'true') { 98 extensionIds.push('kaihong.ApiScan') 99 } 100 if (checkGn == 'true') { 101 extensionIds.push('kaihong.gn-gen') 102 } 103 if (checkService == 'true') { 104 extensionIds.push('kaihong.service-gen') 105 } 106 if (checkTs == 'true') { 107 extensionIds.push('kaihong.ts-gen') 108 } 109 if (checkNapi == 'true') { 110 extensionIds.push('kaihong.napi-gen') 111 } 112 } 113 startInstallExtensions(extensionIds).catch((error) => { 114 console.error(error); 115 }); 116 return importCheck; 117} 118 119async function startInstallExtensions(extensionIds) { 120 const promises = extensionIds.map(async (extensionId) => { 121 const extension = vscode.extensions.getExtension(extensionId); 122 if (!extension) { 123 await vscode.commands.executeCommand('workbench.extensions.installExtension', extensionId); 124 console.log(`扩展插件 ${extensionId} 下载完成`); 125 } else { 126 console.log(`扩展插件 ${extensionId} 已经下载`); 127 } 128 }); 129 await Promise.all(promises); 130 console.log('所有扩展插件下载完成'); 131} 132 133/** 134* 执行完毕后启动工具链中下一个插件 135*/ 136function nextPluginExeCommand(nextPluginId) { 137 if (nextPluginId == "kaihong.ApiScan") { 138 return 'api_scan'; 139 } else if (nextPluginId == "kaihong.gn-gen") { 140 return 'generate_gn'; 141 } else if (nextPluginId == "kaihong.service-gen") { 142 return 'generate_service'; 143 } else if (nextPluginId == "kaihong.ts-gen") { 144 return 'generate_ts'; 145 } else if (nextPluginId == "kaihong.napi-gen") { 146 return 'generate_napi'; 147 } else { 148 return null; 149 } 150} 151 152 153function startExtensions(isSelectToolChain) { 154 //启动工具链,根据需求启用插件 155 let extensionId0 = extensionIds[0]; 156 //将isSelectToolChain和extensionIds数组传入其他插件 157 //启动第一个插件 158 let nextStartPlugin = nextPluginExeCommand(extensionId0); 159 try { 160 vscode.commands.executeCommand(nextStartPlugin, '', isSelectToolChain, extensionIds); 161 } catch (error) { 162 console.error(error); 163 } 164} 165 166// 参数单位 毫秒 167function wait(ms) { 168 return new Promise(resolve => setTimeout(() => resolve(), ms)); 169}; 170 171async function installStartExtension(extensionId) { 172 const extension = vscode.extensions.getExtension(extensionId); 173 if (!extension) { 174 try { 175 // 下载插件 176 vscode.window.showInformationMessage(`Extension ${extensionId} installing...`); 177 setTimeout(() => { 178 const active = vscode.window.activeInformationMessage; 179 if (active && active.message === `Extension ${extensionId} installing...`) { 180 active.dispose(); 181 } 182 }, 8000); 183 await vscode.commands.executeCommand('workbench.extensions.installExtension', extensionId); 184 vscode.window.showInformationMessage(`Extension ${extensionId} installed successfully.`); 185 vscode.window.showInformationMessage(`Extension ${extensionId} activating...`); 186 console.log(`Extension ${extensionId} activating...`); 187 await wait(1000); // 等待下载插件初始化 188 const extensionDone = vscode.extensions.getExtension(extensionId); 189 if (extensionDone && extensionDone.isActive) { 190 vscode.window.showInformationMessage(`Extension ${extensionId} activated successfully.`); 191 console.log(`Extension ${extensionId} activated successfully.`); 192 } else { 193 console.log('请等待插件初始化完成') 194 await wait(1000); 195 } 196 } catch (error) { 197 console.log(`Failed to install extension ${extensionId}: ${error.message}`); 198 } 199 } 200 201 // 启动扩展 202 if (extensionId == "kaihong.ApiScan") { 203 vscode.commands.executeCommand('api_scan', '', false, ''); 204 } else if (extensionId == "kaihong.gn-gen") { 205 vscode.commands.executeCommand('generate_gn', '', false, ''); 206 } else if (extensionId == "kaihong.service-gen") { 207 vscode.commands.executeCommand('generate_service', '', false, ''); 208 } else if (extensionId == "kaihong.ts-gen") { 209 vscode.commands.executeCommand('generate_ts', '', false, ''); 210 } else if (extensionId == "kaihong.napi-gen") { 211 vscode.commands.executeCommand('generate_napi', '', false, ''); 212 } 213 else { 214 console.error('the toolChain does not include this extension!') 215 } 216} 217 218function getWebviewContent(context) { 219 let data = readFile(__dirname + '/vs_plugin_view.html'); 220 data = getWebViewContent(context, '/vs_plugin_view.html'); 221 return data.toString(); 222} 223 224function getWebViewContent(context, templatePath) { 225 const resourcePath = path.join(context.extensionPath, templatePath); 226 const dirPath = path.dirname(resourcePath); 227 let html = fs.readFileSync(resourcePath, 'utf-8'); 228 html = html.replace(/(<link.+?href="|<script.+?src="|<iframe.+?src="|<img.+?src=")(.+?)"/g, (m, $1, $2) => { 229 if ($2.indexOf("https://") < 0) { 230 return $1 + globalPanel.webview.asWebviewUri(vscode.Uri.file(path.resolve(dirPath, $2))) + '"'; 231 } else { 232 return $1 + $2+'"'; 233 } 234 }); 235 return html; 236} 237 238// This method is called when your extension is deactivated 239function deactivate() {} 240 241module.exports = { 242 activate, 243 deactivate 244} 245