Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 3c7d18b

Browse files
Sneezrylirenhe
authored andcommitted
support multi-workspace (#507)
support multi-workspace
1 parent 4ec65f2 commit 3c7d18b

File tree

7 files changed

+62
-32
lines changed

7 files changed

+62
-32
lines changed

src/arduino/arduino.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { LibraryManager } from "./libraryManager";
1919
import { VscodeSettings } from "./vscodeSettings";
2020

2121
import { arduinoChannel } from "../common/outputChannel";
22+
import { ArduinoWorkspace } from "../common/workspace";
2223
import { SerialMonitor } from "../serialmonitor/serialMonitor";
2324
import { UsbDetector } from "../serialmonitor/usbDetector";
2425

@@ -96,12 +97,12 @@ export class ArduinoApp {
9697
return;
9798
}
9899

99-
if (!vscode.workspace.rootPath) {
100+
if (!ArduinoWorkspace.rootPath) {
100101
vscode.window.showWarningMessage("Cannot find the sketch file.");
101102
return;
102103
}
103104

104-
if (!dc.sketch || !util.fileExistsSync(path.join(vscode.workspace.rootPath, dc.sketch))) {
105+
if (!dc.sketch || !util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
105106
await this.getMainSketch(dc);
106107
}
107108
if (!dc.port) {
@@ -118,13 +119,13 @@ export class ArduinoApp {
118119
UsbDetector.getInstance().pauseListening();
119120
await vscode.workspace.saveAll(false);
120121

121-
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
122+
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
122123
const args = ["--upload", "--board", boardDescriptor, "--port", dc.port, appPath];
123124
if (VscodeSettings.getInstance().logLevel === "verbose") {
124125
args.push("--verbose");
125126
}
126127
if (dc.output) {
127-
const outputPath = path.resolve(vscode.workspace.rootPath, dc.output);
128+
const outputPath = path.resolve(ArduinoWorkspace.rootPath, dc.output);
128129
args.push("--pref", `build.path=${outputPath}`);
129130
} else {
130131
const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README.";
@@ -148,25 +149,25 @@ export class ArduinoApp {
148149
return;
149150
}
150151

151-
if (!vscode.workspace.rootPath) {
152+
if (!ArduinoWorkspace.rootPath) {
152153
vscode.window.showWarningMessage("Cannot find the sketch file.");
153154
return;
154155
}
155156

156-
if (!dc.sketch || !util.fileExistsSync(path.join(vscode.workspace.rootPath, dc.sketch))) {
157+
if (!dc.sketch || !util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
157158
await this.getMainSketch(dc);
158159
}
159160

160161
await vscode.workspace.saveAll(false);
161162

162163
arduinoChannel.start(`Verify sketch - ${dc.sketch}`);
163-
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
164+
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
164165
const args = ["--verify", "--board", boardDescriptor, appPath];
165166
if (VscodeSettings.getInstance().logLevel === "verbose") {
166167
args.push("--verbose");
167168
}
168169
if (output || dc.output) {
169-
const outputPath = path.resolve(vscode.workspace.rootPath, output || dc.output);
170+
const outputPath = path.resolve(ArduinoWorkspace.rootPath, output || dc.output);
170171
args.push("--pref", `build.path=${outputPath}`);
171172
} else {
172173
const msg = "Output path is not specified. Unable to reuse previously compiled files. Verify could be slow. See README.";
@@ -194,10 +195,10 @@ export class ArduinoApp {
194195
} else {
195196
libPaths = this.getDefaultPackageLibPaths();
196197
}
197-
if (!vscode.workspace.rootPath) {
198+
if (!ArduinoWorkspace.rootPath) {
198199
return;
199200
}
200-
const configFilePath = path.join(vscode.workspace.rootPath, constants.CPP_CONFIG_FILE);
201+
const configFilePath = path.join(ArduinoWorkspace.rootPath, constants.CPP_CONFIG_FILE);
201202
let deviceContext = null;
202203
if (!util.fileExistsSync(configFilePath)) {
203204
util.mkdirRecursivelySync(path.dirname(configFilePath));
@@ -261,11 +262,11 @@ export class ArduinoApp {
261262

262263
// Include the *.h header files from selected library to the arduino sketch.
263264
public async includeLibrary(libraryPath: string) {
264-
if (!vscode.workspace.rootPath) {
265+
if (!ArduinoWorkspace.rootPath) {
265266
return;
266267
}
267268
const dc = DeviceContext.getInstance();
268-
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
269+
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
269270
if (util.fileExistsSync(appPath)) {
270271
const hFiles = glob.sync(`${libraryPath}/*.h`, {
271272
nodir: true,

src/common/workspace.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import * as fs from "fs";
5+
import * as path from "path";
6+
import * as vscode from "vscode";
7+
8+
export class ArduinoWorkspace {
9+
static get rootPath(): string|undefined {
10+
const workspaceFolders = vscode.workspace.workspaceFolders;
11+
if (!workspaceFolders || workspaceFolders.length === 0) {
12+
return undefined;
13+
}
14+
15+
for (const workspaceFolder of workspaceFolders) {
16+
const workspaceFolderPath = workspaceFolder.uri.fsPath;
17+
const arduinoConfigPath = path.join(workspaceFolderPath, ".vscode", "arduino.json");
18+
if (fs.existsSync(arduinoConfigPath)) {
19+
return workspaceFolderPath;
20+
}
21+
}
22+
23+
return workspaceFolders[0].uri.fsPath;
24+
}
25+
}

src/debug/configurationProvider.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ArduinoContext from "../arduinoContext";
1111
import { VscodeSettings } from "../arduino/vscodeSettings";
1212
import * as platform from "../common/platform";
1313
import * as util from "../common/util";
14+
import { ArduinoWorkspace } from "../common/workspace";
1415
import { DeviceContext } from "../deviceContext";
1516
import * as Logger from "../logger/logger";
1617

@@ -118,8 +119,8 @@ export class ArduinoDebugConfigurationProvider implements vscode.DebugConfigurat
118119
if (!config.program || config.program === "${file}") {
119120
// make a unique temp folder because keeping same temp folder will corrupt the build when board is changed
120121
const outputFolder = path.join(dc.output || `.build`, ArduinoContext.boardManager.currentBoard.board);
121-
util.mkdirRecursivelySync(path.join(vscode.workspace.rootPath, outputFolder));
122-
if (!dc.sketch || !util.fileExistsSync(path.join(vscode.workspace.rootPath, dc.sketch))) {
122+
util.mkdirRecursivelySync(path.join(ArduinoWorkspace.rootPath, outputFolder));
123+
if (!dc.sketch || !util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
123124
await dc.resolveMainSketch();
124125
}
125126

@@ -128,11 +129,11 @@ export class ArduinoDebugConfigurationProvider implements vscode.DebugConfigurat
128129
return false;
129130
}
130131

131-
if (!util.fileExistsSync(path.join(vscode.workspace.rootPath, dc.sketch))) {
132+
if (!util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
132133
vscode.window.showErrorMessage(`Cannot find ${dc.sketch}, Please specify the sketch in the arduino.json file`);
133134
return false;
134135
}
135-
config.program = path.join(vscode.workspace.rootPath, outputFolder, `${path.basename(dc.sketch)}.elf`);
136+
config.program = path.join(ArduinoWorkspace.rootPath, outputFolder, `${path.basename(dc.sketch)}.elf`);
136137

137138
// always compile elf to make sure debug the right elf
138139
if (!await ArduinoContext.arduinoApp.verify(outputFolder)) {

src/deviceContext.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as util from "./common/util";
99
import * as Logger from "./logger/logger";
1010

1111
import { ARDUINO_CONFIG_FILE } from "./common/constants";
12+
import { ArduinoWorkspace } from "./common/workspace";
1213

1314
/**
1415
* Interface that represents the arduino context information.
@@ -87,10 +88,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
8788
* @constructor
8889
*/
8990
private constructor() {
90-
if (vscode.workspace && vscode.workspace.rootPath) {
91-
this._watcher = vscode.workspace.createFileSystemWatcher(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE));
91+
if (vscode.workspace && ArduinoWorkspace.rootPath) {
92+
this._watcher = vscode.workspace.createFileSystemWatcher(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE));
9293
// We only care about the deletion arduino.json in the .vscode folder:
93-
this._vscodeWatcher = vscode.workspace.createFileSystemWatcher(path.join(vscode.workspace.rootPath, ".vscode"), true, true, false);
94+
this._vscodeWatcher = vscode.workspace.createFileSystemWatcher(path.join(ArduinoWorkspace.rootPath, ".vscode"), true, true, false);
9495

9596
this._watcher.onDidCreate(() => this.loadContext());
9697
this._watcher.onDidChange(() => this.loadContext());
@@ -169,10 +170,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
169170
}
170171

171172
public saveContext() {
172-
if (!vscode.workspace.rootPath) {
173+
if (!ArduinoWorkspace.rootPath) {
173174
return;
174175
}
175-
const deviceConfigFile = path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE);
176+
const deviceConfigFile = path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE);
176177
let deviceConfigJson: any = {};
177178
if (util.fileExistsSync(deviceConfigFile)) {
178179
deviceConfigJson = util.tryParseJSON(fs.readFileSync(deviceConfigFile, "utf8"));
@@ -256,11 +257,11 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
256257
}
257258

258259
public async initialize() {
259-
if (vscode.workspace.rootPath && util.fileExistsSync(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE))) {
260+
if (ArduinoWorkspace.rootPath && util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE))) {
260261
vscode.window.showInformationMessage("Arduino.json is already generated.");
261262
return;
262263
} else {
263-
if (!vscode.workspace.rootPath) {
264+
if (!ArduinoWorkspace.rootPath) {
264265
vscode.window.showInformationMessage("Please open an folder first.");
265266
return;
266267
}
@@ -293,20 +294,20 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
293294
newSketchFileName = (newSketchFileName && newSketchFileName.trim()) || "";
294295
if (newSketchFileName) {
295296
const snippets = fs.readFileSync(path.join(this.extensionPath, "snippets", "sample.ino"));
296-
fs.writeFileSync(path.join(vscode.workspace.rootPath, newSketchFileName), snippets);
297+
fs.writeFileSync(path.join(ArduinoWorkspace.rootPath, newSketchFileName), snippets);
297298
this.sketch = newSketchFileName;
298299
// Open the new sketch file.
299-
const textDocument = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, newSketchFileName));
300+
const textDocument = await vscode.workspace.openTextDocument(path.join(ArduinoWorkspace.rootPath, newSketchFileName));
300301
vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One, true);
301302
} else {
302303
this._sketch = undefined;
303304
}
304305
} else if (fileUris.length === 1) {
305-
this.sketch = path.relative(vscode.workspace.rootPath, fileUris[0].fsPath);
306+
this.sketch = path.relative(ArduinoWorkspace.rootPath, fileUris[0].fsPath);
306307
} else if (fileUris.length > 1) {
307308
const chosen = await vscode.window.showQuickPick(<vscode.QuickPickItem[]>fileUris.map((fileUri): vscode.QuickPickItem => {
308309
return <vscode.QuickPickItem>{
309-
label: path.relative(vscode.workspace.rootPath, fileUri.fsPath),
310+
label: path.relative(ArduinoWorkspace.rootPath, fileUri.fsPath),
310311
description: fileUri.fsPath,
311312
};
312313
}), { placeHolder: "Select the main sketch file" });

src/extension.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
LIBRARY_MANAGER_URI,
1414
} from "./common/constants";
1515
import * as util from "./common/util";
16+
import { ArduinoWorkspace } from "./common/workspace";
1617
import { ArduinoDebugConfigurationProvider } from "./debug/configurationProvider";
1718
import { DeviceContext } from "./deviceContext";
1819
import { CompletionProvider } from "./langService/completionProvider";
@@ -31,7 +32,7 @@ export async function activate(context: vscode.ExtensionContext) {
3132
const openEditor = vscode.window.activeTextEditor;
3233
if (openEditor && openEditor.document.fileName.endsWith(".ino")) {
3334
const workingFile = path.normalize(openEditor.document.fileName);
34-
const workspaceFolder = (vscode.workspace && vscode.workspace.rootPath) || "";
35+
const workspaceFolder = (vscode.workspace && ArduinoWorkspace.rootPath) || "";
3536
if (!workspaceFolder || workingFile.indexOf(path.normalize(workspaceFolder)) < 0) {
3637
vscode.window.showWarningMessage(`The working file "${workingFile}" is not under the workspace folder, ` +
3738
"the arduino extension might not work appropriately.");
@@ -181,8 +182,8 @@ export async function activate(context: vscode.ExtensionContext) {
181182
UsbDetector.getInstance().initialize(context.extensionPath);
182183
UsbDetector.getInstance().startListening();
183184

184-
if (vscode.workspace.rootPath && (
185-
util.fileExistsSync(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE))
185+
if (ArduinoWorkspace.rootPath && (
186+
util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE))
186187
|| (openEditor && openEditor.document.fileName.endsWith(".ino")))) {
187188
(async () => {
188189
if (!ArduinoContext.initialized) {

src/langService/completionProvider.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as util from "../common/util";
99

1010
import ArduinoActivator from "../arduinoActivator";
1111
import ArduinoContext from "../arduinoContext";
12+
import { ArduinoWorkspace } from "../common/workspace";
1213

1314
export class CompletionProvider implements vscode.CompletionItemProvider {
1415

@@ -23,8 +24,8 @@ export class CompletionProvider implements vscode.CompletionItemProvider {
2324
private _activated: boolean = false;
2425

2526
constructor() {
26-
if (vscode.workspace && vscode.workspace.rootPath) {
27-
this._cppConfigFile = path.join(vscode.workspace.rootPath, constants.CPP_CONFIG_FILE);
27+
if (vscode.workspace && ArduinoWorkspace.rootPath) {
28+
this._cppConfigFile = path.join(ArduinoWorkspace.rootPath, constants.CPP_CONFIG_FILE);
2829
this._watcher = vscode.workspace.createFileSystemWatcher(this._cppConfigFile);
2930
this._watcher.onDidCreate(() => this.updateLibList());
3031
this._watcher.onDidChange(() => this.updateLibList());

vscode-arduino-0.2.11.vsix

3.11 MB
Binary file not shown.

0 commit comments

Comments
 (0)