Skip to content

Commit

Permalink
Detect if firmware is in subdirectory by locating .micropico file as …
Browse files Browse the repository at this point in the history
…default root

Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Nov 17, 2024
1 parent 9bc9b2e commit 6ff83aa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
"micropico.syncFolder": {
"type": "string",
"scope": "machine-overridable",
"default": "",
"default": null,
"title": "Sync Folder",
"description": "This folder will be uploaded to the pyboard when using the sync button. Leave empty to sync the complete project. (only allows folders within the project). Use a path relative to the project you opened in vscode, without leading or trailing slash",
"order": 4
Expand Down
1 change: 1 addition & 0 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ export default class Activator {

return;
}
this.settings.reload();

const syncDir = await this.settings.requestSyncFolder("Download");

Expand Down
24 changes: 23 additions & 1 deletion src/osHelper.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { lstat } from "fs";
import { lstat, readdirSync } from "fs";
import { readFile, stat, writeFile } from "fs/promises";
import { basename, join } from "path";
import { rimrafSync } from "rimraf";

export async function pathExists(path: string): Promise<boolean> {
Expand Down Expand Up @@ -61,3 +62,24 @@ export function removeJunction(junctionPath: string): Promise<boolean> {
});
});
}

/**
* Searches for a file in a directory and its subdirectories.
*
* @param directory The directory to search in.
* @param fileName The name of the file to search for.
* @returns The path to the file if found, otherwise undefined.
*/
export function searchFile(
directory: string,
fileName: string
): string | undefined {
const contents = readdirSync(directory, {
encoding: "utf8",
recursive: true,
});

const file = contents.find(c => basename(c) === fileName);

return file ? join(directory, file) : undefined;
}
48 changes: 40 additions & 8 deletions src/settings.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Memento, Uri, WorkspaceConfiguration } from "vscode";
import { window, workspace as vsWorkspace } from "vscode";
import { extName, getProjectPath, settingsStubsBasePath } from "./api.mjs";
import { join, relative } from "path";
import { dirname, join, relative } from "path";
import { PicoMpyCom } from "@paulober/pico-mpy-com";
import { searchFile } from "./osHelper.mjs";

export enum SettingsKey {
autoConnect = "autoConnect",
Expand Down Expand Up @@ -75,6 +76,10 @@ export default class Settings {
return this.config.update(key, value, true);
}

public updateWorkspaceFolder<T>(key: string, value: T): Thenable<void> {
return this.config.update(key, value, null);
}

public updatePython<T>(key: string, value: T): Thenable<void> {
return this.pythonConfig.update(key, value, null);
}
Expand Down Expand Up @@ -126,22 +131,22 @@ export default class Settings {
* Returns the absolute path to the sync folder. If the sync folder is undefined,
* the project path is returned.
*
* @returns the absolute path to the sync folder
* @returns The absolute path to the sync folder and if the setting is undefined
*/
public getSyncFolderAbsPath(): string | undefined {
public getSyncFolderAbsPath(): [string | undefined, boolean] {
const syncDir = this.getString(SettingsKey.syncFolder);
const projectDir = getProjectPath();

if (syncDir === undefined) {
return projectDir;
if (syncDir === undefined || syncDir.length === 0) {
return [projectDir, true];
}

if (projectDir === undefined) {
// How can this ever happen??!
return undefined;
return [undefined, false];
}

return join(projectDir, syncDir);
return [join(projectDir, syncDir), false];
}

/**
Expand All @@ -158,14 +163,41 @@ export default class Settings {
public async requestSyncFolder(
actionTitle: string
): Promise<[string, string] | undefined> {
const syncFolder = this.getSyncFolderAbsPath();
let [syncFolder, syncSettingNotSet] = this.getSyncFolderAbsPath();
const projectDir = getProjectPath();

if (projectDir === undefined) {
// How can this ever happen??!
return;
}

// sync folder setting not set
if (syncSettingNotSet) {
const activationFile = searchFile(projectDir, ".micropico");
const actParent = activationFile ? dirname(activationFile) : undefined;

// check if activation file is not in project root
if (activationFile && actParent && actParent !== projectDir) {
syncFolder = actParent;

// update transparent to the user
await this.updateWorkspaceFolder(
SettingsKey.syncFolder,
relative(projectDir, actParent)
);

void window.showWarningMessage(
`Sync folder has been set to \`${relative(
projectDir,
actParent
)}\` ` +
"because the `.micropico` file was found in a subdirectory " +
"and no sync folder was set. To disable this behavior, " +
"set a sync folder in the settings to `.` for the project root."
);
}
}

let additionalSyncFolders = this.getArray(
SettingsKey.additionalSyncFolders
)?.map(sf => join(projectDir, sf));
Expand Down
1 change: 0 additions & 1 deletion src/stubs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export default class Stubs {
};

if (!justUpdate) {
defaultSettings["micropico.syncFolder"] = "";
defaultSettings["micropico.openOnStart"] = true;
}

Expand Down

0 comments on commit 6ff83aa

Please sign in to comment.