-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathextension.ts
More file actions
214 lines (186 loc) · 8.46 KB
/
extension.ts
File metadata and controls
214 lines (186 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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 * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { openSettingWebview } from './webviews/setting';
import { openAboutWebview } from './webviews/about';
import { openCreateProjectWebview } from './webviews/create-project';
import { openAnalyzeWebview } from './webviews/analyze';
import { initOnDidChangeListener } from './listener';
import { executeCommand, initTerminal } from './terminal';
import { getMenuItems, getParallelBuildNumber } from './smart';
import { initDockView } from './dock';
import { setupVEnv } from './venv';
import { initAPI } from './api';
import { openWorkspaceProjectsWebview } from './webviews/project';
import { initProjectTree, setCurrentSelectedBspItem } from './project/tree';
import { DecorationProvider } from './project/fileDecorationProvider';
import { getCurrentProjectInWorkspace } from './webviews/project';
import { initCurrentProject } from './project/cmd';
let _context: vscode.ExtensionContext;
export function postMessageExtensionData(context: vscode.ExtensionContext, panel: vscode.WebviewPanel) {
// 获取插件版本号(从 package.json 中读取)
const extensionVersion = context.extension.packageJSON.version;
const extensionNaeme = context.extension.packageJSON.name;
panel.webview.postMessage({ version: extensionVersion, name: extensionNaeme });
}
// 有两种模式
// isRTThreadWorksapce - workspace模式,会定位.vscode/workspace.json文件是否存在,是否启用
// isRTThread - 项目模式,rtconfig.h文件是否存在
export async function activate(context: vscode.ExtensionContext) {
let isRTThread: boolean = false;
let isRTThreadWorksapce: boolean = false;
_context = context;
// 获取插件版本号(从 package.json 中读取)
const extensionVersion = context.extension.packageJSON.version;
// 例如:version 为 "1.0.0"
console.log('插件版本号:', extensionVersion);
// init context for isRTThread, isRTThreadWorksapce
vscode.commands.executeCommand('setContext', 'isRTThread', isRTThread);
context.workspaceState.update('isRTThread', isRTThread);
vscode.commands.executeCommand('setContext', 'isRTThreadWorksapce', isRTThreadWorksapce);
context.workspaceState.update('isRTThreadWorksapce', isRTThreadWorksapce);
initAPI(context);
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders) {
const workspacePath = workspaceFolders[0].uri.fsPath;
const rtthreadWorkspace = path.join(workspacePath, '.vscode', 'workspace.json');
if (fs.existsSync(rtthreadWorkspace)) {
const json = fs.readFileSync(rtthreadWorkspace, 'utf8');
const jsonNode = JSON.parse(json);
if (jsonNode.hasOwnProperty("bsps")) {
isRTThreadWorksapce = true;
vscode.commands.executeCommand('setContext', 'isRTThreadWorksapce', true);
context.workspaceState.update('isRTThreadWorksapce', isRTThreadWorksapce);
new DecorationProvider(context);
// get current project from workspace.json file
let currentProject = getCurrentProjectInWorkspace();
if (currentProject) {
DecorationProvider.getInstance().markFile(vscode.Uri.file(currentProject));
// 初始化当前项目到 cmd.ts 的 _currentProject 变量
initCurrentProject(currentProject);
// 初始化当前选中的BSP项目,以便后续切换时能正确移除标记
setCurrentSelectedBspItem(currentProject);
}
}
}
else {
// check rtconfig.h exists
const rtconfigPath = path.join(workspacePath, 'rtconfig.h');
if (fs.existsSync(rtconfigPath)) {
/* The workspace is a RT-Thread Project*/
isRTThread = true;
vscode.commands.executeCommand('setContext', 'isRTThread', true);
context.workspaceState.update('isRTThread', isRTThread);
}
}
if (isRTThread || isRTThreadWorksapce) {
// if it's Windows system
if (os.platform() === 'win32') {
await setupVEnv();
}
initTerminal();
setupStatusBarItems(context);
initOnDidChangeListener(context);
// register commands
vscode.commands.registerCommand('extension.executeCommand', (arg1, arg2) => {
if (arg1) {
executeCommand(arg1);
}
if (arg2) {
executeCommand(arg2);
}
});
vscode.commands.registerCommand('extension.clickProject', (arg) => {
if (arg) {
// open file
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(arg.fn));
}
});
}
}
vscode.commands.registerCommand('extension.showSetting', () => {
openSettingWebview(context);
});
vscode.commands.registerCommand('extension.showCreateProject', () => {
openCreateProjectWebview(context);
});
vscode.commands.registerCommand('extension.showAbout', () => {
openAboutWebview(context);
});
vscode.commands.registerCommand('extension.showAnalyze', () => {
openAnalyzeWebview(context);
});
if (isRTThreadWorksapce) {
vscode.commands.registerCommand('extension.showWorkspaceSettings', () => {
openWorkspaceProjectsWebview(context);
});
initProjectTree(context);
}
/* initialize dock view always */
initDockView(context);
initExperimentStatusBarItem(context);
}
function initExperimentStatusBarItem(context: vscode.ExtensionContext) {
if (false) {
const statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 5);
statusItem.text = '$(beaker) 实验性功能';
statusItem.tooltip = 'Experimental features';
statusItem.command = 'extension.Experimental';
statusItem.show();
vscode.commands.registerCommand('extension.Experimental', () => {
console.log('Experimental features are not available yet.');
});
}
}
function setupStatusBarItems(context: vscode.ExtensionContext) {
const buildIcon = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
buildIcon.text = '$(github-action) 构建';
buildIcon.tooltip = 'Build RT-Thread Kernel';
buildIcon.command = 'extension.buildProject';
buildIcon.show();
vscode.commands.registerCommand('extension.buildProject', () => {
const buildNumber = getParallelBuildNumber();
if (buildNumber > 1) {
executeCommand(`scons -j${buildNumber}`);
}
else {
executeCommand('scons');
}
});
let menuItems = getMenuItems();
// Always register the command, regardless of menu items
let disposable = vscode.commands.registerCommand('extension.openMenu', () => {
const items = getMenuItems();
if (!items || items.length === 0) {
// Show message and open settings when no menu items configured
vscode.window.showInformationMessage(
'自定义菜单命令未配置,请在设置中添加自定义命令。',
'打开设置'
).then(selection => {
if (selection === '打开设置') {
vscode.commands.executeCommand('workbench.action.openSettings', 'smart.menuCommands');
}
});
} else {
vscode.window.showQuickPick(items).then(selectedItem => {
if (selectedItem) {
executeCommand(selectedItem);
}
});
}
});
context.subscriptions.push(disposable);
// Only show status bar item if menu items exist
if (menuItems && menuItems.length > 0) {
const statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusItem.text = '$(menu) 自定义构建...';
statusItem.tooltip = 'build with custom commands';
statusItem.command = 'extension.openMenu';
statusItem.show();
context.subscriptions.push(statusItem);
}
}
export function deactivate() { }