Skip to content

Commit 8f026a8

Browse files
feat: open file on device
Opens the corresponding file on a connected device
1 parent 51c8748 commit 8f026a8

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@
358358
}
359359
],
360360
"commands": [
361+
{
362+
"command": "pymakr.openOnDevice",
363+
"title": "Open file on device.",
364+
"category": "PyMakr"
365+
},
361366
{
362367
"command": "pymakr.logState",
363368
"title": "[DEBUG] Logs current state to the terminal",
@@ -776,6 +781,11 @@
776781
{
777782
"command": "pymakr.uploadPrompt",
778783
"group": "pymakr"
784+
},
785+
{
786+
"command": "pymakr.openOnDevice",
787+
"group": "pymakr",
788+
"when": "resourceScheme == file"
779789
}
780790
],
781791
"pymakr.explorerContextMenu": [
@@ -792,6 +802,11 @@
792802
{
793803
"command": "pymakr.uploadPrompt",
794804
"group": "pymakr"
805+
},
806+
{
807+
"command": "pymakr.openOnDevice",
808+
"group": "pymakr",
809+
"when": "resourceScheme == file"
795810
}
796811
],
797812
"view/item/context": [

src/commands/index.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
const { mkdirSync, readFileSync, writeFileSync } = require("fs");
2-
const { writeFile } = require("fs").promises;
32
const vscode = require("vscode");
43
const { msgs } = require("../utils/msgs");
5-
const {
6-
mapEnumsToQuickPick,
7-
getTemplates,
8-
copyTemplateByName,
9-
serializedEntriesToObj,
10-
objToSerializedEntries,
11-
waitFor,
12-
} = require("../utils/misc");
4+
const { mapEnumsToQuickPick, getTemplates, copyTemplateByName, waitFor } = require("../utils/misc");
135
const { relative } = require("path");
146
const { Project } = require("../Project");
157
const { DeviceManager } = require("../Watcher/DeviceManager");
@@ -46,6 +38,40 @@ class Commands {
4638
}
4739

4840
commands = {
41+
/**
42+
*
43+
* @param {vscode.Uri} file
44+
*/
45+
openOnDevice: async (file) => {
46+
this.pymakr.notifier.notifications.openOnDeviceFile();
47+
48+
const project = this.pymakr.vscodeHelpers.coerceProject(file);
49+
50+
if (!project) return this.pymakr.notifier.notifications.openOnDeviceHasNoProject(file);
51+
52+
const devices = project.devices
53+
.filter((device) => device.adapter.__proxyMeta.target.isConnected())
54+
.filter((device) => !device.busy.get());
55+
56+
devices.forEach(async (device) => {
57+
const path = relative(project.folder, file.fsPath).replace(/\\/g, "/");
58+
const uri = vscode.Uri.parse(`${device.protocol}://${device.address}${device.config.rootPath}/${path}`);
59+
try {
60+
await vscode.window.showTextDocument(uri);
61+
} catch (err) {
62+
if (err.message.match(/Unable to resolve nonexistent file/)) {
63+
const result = await this.pymakr.notifier.notifications.openOnDeviceFileDoesntExist(path, device);
64+
if (result === "create") {
65+
await this.commands.upload(file, device, path);
66+
await vscode.window.showTextDocument(uri);
67+
}
68+
}
69+
}
70+
});
71+
72+
if (!devices.length) this.pymakr.notifier.notifications.openOnDeviceNoAvailableDevice(project);
73+
},
74+
4975
// todo link to this command from configuration's "Devices: Include" section
5076
listDevices: async () => {
5177
let uri = vscode.Uri.parse("pymakrDocument:" + "Pymakr: available devices");

src/utils/Notifier.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ class Notifier {
201201
}
202202
),
203203

204+
openOnDeviceFile: () =>
205+
this.createNotification(
206+
"info",
207+
'"Open file on device." opens the corresponding file on all idle devices that are connected to the file\'s project.',
208+
{
209+
"": [null, this.DONT_SHOW_AGAIN],
210+
}
211+
),
212+
213+
openOnDeviceFileDoesntExist: (file, device) =>
214+
this.createNotification("info", `"${file}" does not exist on "${device.name}"`, { create: "Upload it" }),
215+
216+
openOnDeviceHasNoProject: (file) =>
217+
this.createNotification("info", `"${file.path.split("/").pop()}" does not belong to any project.`),
218+
219+
openOnDeviceNoAvailableDevice: (project) =>
220+
this.createNotification(
221+
"info",
222+
`No available devices found for "${project.name}". Please make sure that at least one idle device is connected to the project.`
223+
),
224+
204225
/**
205226
*
206227
* @param {import('../Watcher/DeviceManager').DeviceManager} deviceManager

0 commit comments

Comments
 (0)