Skip to content

Remove main file if empty during hex creation #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/fs/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ export class FileSystem extends TypedEventTarget<EventMap> {

async statistics(): Promise<Statistics> {
const fs = await this.initialize();
const currentMainFile = fs.readBytes(MAIN_FILE);
let currentMainFile;
if (fs.exists(MAIN_FILE)) {
currentMainFile = fs.readBytes(MAIN_FILE);
}
const files = fs.ls();
let numMagicModules = 0;
for (const file of files) {
Expand All @@ -418,12 +421,13 @@ export class FileSystem extends TypedEventTarget<EventMap> {
return {
files: files.length,
storageUsed: fs.getStorageUsed(),
lines:
this.cachedInitialProject &&
this.cachedInitialProject.files[MAIN_FILE] ===
fromByteArray(currentMainFile)
? undefined
: lineNumFromUint8Array(currentMainFile),
lines: !currentMainFile
? 0
: this.cachedInitialProject &&
this.cachedInitialProject.files[MAIN_FILE] ===
fromByteArray(currentMainFile)
? undefined
: lineNumFromUint8Array(currentMainFile),
magicModules: numMagicModules,
};
}
Expand All @@ -450,8 +454,18 @@ export class FileSystem extends TypedEventTarget<EventMap> {
);
}

private async removeMainFileIfEmpty(fs: MicropythonFsHex): Promise<void> {
if (fs.exists(MAIN_FILE)) {
const currentMainFile = await fs.read(MAIN_FILE);
if (!currentMainFile) {
fs.remove(MAIN_FILE);
}
}
}

async toHexForSave(): Promise<string> {
const fs = await this.initialize();
await this.removeMainFileIfEmpty(fs);
return fs.getUniversalHex();
}

Expand All @@ -465,6 +479,7 @@ export class FileSystem extends TypedEventTarget<EventMap> {
try {
const fs = await this.initialize();
const boardId = BoardId.forVersion(boardVersion).id;
await this.removeMainFileIfEmpty(fs);
return fs.getIntelHex(boardId);
} catch (e: any) {
throw new FlashDataError(e.message);
Expand Down
23 changes: 13 additions & 10 deletions src/project/project-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,6 @@ export class ProjectActions {
throw new Error("Device connection doesn't support flash");
}

this.logging.event({
type: "flash",
detail: await this.projectStats(),
});

if (this.device.status === ConnectionStatus.NOT_SUPPORTED) {
this.webusbNotSupportedError(finalFocusRef);
return;
Expand Down Expand Up @@ -552,6 +547,12 @@ export class ProjectActions {
this.handleWebUSBError(e, ConnectionAction.FLASH, finalFocusRef);
}
}

// Get the project stats after flashing as this will remove the main file if empty.
this.logging.event({
type: "flash",
detail: await this.projectStats(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs moving, otherwise the first save will include an empty main.py (which is then removed), and subsequent saves will not (unless code is added by the user).

});
};

/**
Expand All @@ -561,11 +562,6 @@ export class ProjectActions {
finalFocusRef: FinalFocusRef,
saveViaWebUsbNotSupported?: boolean
) => {
this.logging.event({
type: "save",
detail: await this.projectStats(),
});

if (!(await this.ensureProjectName(finalFocusRef))) {
return;
}
Expand All @@ -581,6 +577,13 @@ export class ProjectActions {
});
return;
}

// Get the project stats after hex creation as this will remove the main file if empty.
this.logging.event({
type: "save",
detail: await this.projectStats(),
});

const blob = new Blob([download], {
type: "application/octet-stream",
});
Expand Down