Skip to content

Commit

Permalink
fix: upload didn't work without project
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Apr 19, 2022
1 parent 219e743 commit 493aaf8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
19 changes: 13 additions & 6 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { writeFile } = require("fs").promises;
const vscode = require("vscode");
const { msgs } = require("../utils/msgs");
const { mapEnumsToQuickPick } = require("../utils/misc");
const { relative, join } = require("path");
const { relative } = require("path");

/**
* Commands contains all commands that can be accessed through VSCode.
Expand All @@ -30,7 +30,8 @@ class Commands {
await value.bind(this)(...params);
} catch (err) {
vscode.window.showErrorMessage(
`[Pymakr] Failed to run command: ${key}. Reason: ${err.message || err.name || err
`[Pymakr] Failed to run command: ${key}. Reason: ${
err.message || err.name || err
}. Please see logs for info.`
);
this.log.error(`Failed to run command: ${key} with params:`, params);
Expand Down Expand Up @@ -391,7 +392,7 @@ class Commands {
},

// todo remove
newDeviceRecover: async () => { },
newDeviceRecover: async () => {},

/**
* Uploads parent project to the device. Can only be accessed from devices in the projects view.
Expand All @@ -408,9 +409,13 @@ class Commands {
*/
uploadPrompt: async (uri) => {
const project = this.pymakr.vscodeHelpers.coerceProject(uri);
const devices = await this.pymakr.vscodeHelpers.devicePickerByProject(project);
const devices = await this.pymakr.vscodeHelpers.devicePicker(project?.devices);

const getRelativeFromProject = () => relative(project.folder, uri.fsPath).replace(/\\+/, "/");
const getBasename = () => `/${uri.fsPath.replace(/.*[/\\]/g, "")}`;

const relativePathFromProject = project ? getRelativeFromProject() : getBasename();

const relativePathFromProject = relative(project.folder, uri.fsPath).replace(/\\+/, "/");
const destination = await vscode.window.showInputBox({
title: "destination",
value: relativePathFromProject,
Expand All @@ -426,9 +431,11 @@ class Commands {
* @param {string} destination not including the device.rootPath ( /flash or / )
*/
upload: async ({ fsPath }, device, destination) => {
const friendlySource = fsPath.replace(/.*[/\\]/g, "");
if (!device.connected) await device.connect();
try {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (progress) => {
progress.report({ message: `copying...}` });
progress.report({ message: `Uploading "${friendlySource}" to "${device.name}"...` });
await device.upload(fsPath, destination);
});
} catch (err) {
Expand Down
15 changes: 15 additions & 0 deletions src/utils/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class MissingProjectError extends Error {
/**
* @param {string=} message
*/
constructor(message) {
super(message || 'Project not found');
this.name = 'MissingProjectError'
}
}

const errors = {
MissingProjectError
}

module.exports = errors
24 changes: 19 additions & 5 deletions src/utils/vscodeHelpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const vscode = require("vscode");
const { Project } = require("../Project.js");
const { ProjectDeviceTreeItem, ProjectTreeItem } = require("../providers/ProjectsProvider.js");
const errors = require("./errors.js");
const { getNearestPymakrProjectDir } = require("./misc.js");

/**
Expand Down Expand Up @@ -34,14 +35,27 @@ const createVSCodeHelpers = (pymakr) => {
/**
* @param {vscode.Uri | import('../Project.js').Project} projectOrUri
*/
devicePickerByProject: async (projectOrUri) => {
if (!projectOrUri) throw new Error("projectOrUri can't be undefined");
devicesByProject: (projectOrUri) => {
if (!projectOrUri) throw new errors.MissingProjectError();
const project = helpers.coerceProject(projectOrUri);
return project.devices;
},

/**
* @param {vscode.Uri | import('../Project.js').Project} projectOrUri
*/
devicePickerByProject: (projectOrUri) => {
return helpers.devicePicker(helpers.devicesByProject(projectOrUri));
},

/**
* @param {Device[]} preselectedDevices
*/
devicePicker: async (preselectedDevices = []) => {
const answers = await vscode.window.showQuickPick(
pymakr.devicesStore.get().map((device) => ({
label: device.name,
picked: project.devices.includes(device),
picked: preselectedDevices.includes(device),
_device: device,
})),
{ canPickMany: true }
Expand All @@ -60,12 +74,12 @@ const createVSCodeHelpers = (pymakr) => {
}
}
},
showAddDeviceToFileExplorerProgressBar: ()=>{
showAddDeviceToFileExplorerProgressBar: () => {
vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (token) => {
token.report({ message: "Adding device to explorer..." });
});
return new Promise((resolve) => vscode.workspace.onDidChangeWorkspaceFolders(resolve));
}
},
};
return helpers;
};
Expand Down

0 comments on commit 493aaf8

Please sign in to comment.