Skip to content

Commit bac2240

Browse files
committed
sagas/mpy: Use legacy __main__ on old firmware.
1 parent a18d88d commit bac2240

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

src/hub/reducers.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test('initial state', () => {
5858
"runtime": "hub.runtime.disconnected",
5959
"selectedSlot": 0,
6060
"useLegacyDownload": false,
61+
"useLegacyMainModule": false,
6162
"useLegacyStartUserProgram": false,
6263
"useLegacyStdio": false,
6364
}
@@ -544,3 +545,23 @@ describe('useLegacyStartUserProgram', () => {
544545
).toBeFalsy();
545546
});
546547
});
548+
549+
describe('useLegacyMainModule', () => {
550+
test('Pybricks Profile < v1.5.0', () => {
551+
expect(
552+
reducers(
553+
{ useLegacyMainModule: false } as State,
554+
bleDIServiceDidReceiveSoftwareRevision('1.4.0'),
555+
).useLegacyMainModule,
556+
).toBeTruthy();
557+
});
558+
559+
test('Pybricks Profile >= v1.5.0', () => {
560+
expect(
561+
reducers(
562+
{ useLegacyMainModule: true } as State,
563+
bleDIServiceDidReceiveSoftwareRevision('1.5.0'),
564+
).useLegacyMainModule,
565+
).toBeFalsy();
566+
});
567+
});

src/hub/reducers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ const useLegacyStartUserProgram: Reducer<boolean> = (state = false, action) => {
381381
return state;
382382
};
383383

384+
/**
385+
* When true, use the legacy `__main__` module name instead of the actual file name.
386+
*/
387+
const useLegacyMainModule: Reducer<boolean> = (state = false, action) => {
388+
if (bleDIServiceDidReceiveSoftwareRevision.matches(action)) {
389+
// Behavior changed starting with Pybricks Profile v1.5.0.
390+
return !semver.satisfies(action.version, '^1.5.0');
391+
}
392+
393+
return state;
394+
};
395+
384396
/*
385397
* Returns number of available slots or 0 for slots not supported.
386398
*/
@@ -418,6 +430,7 @@ export default combineReducers({
418430
useLegacyDownload,
419431
useLegacyStdio,
420432
useLegacyStartUserProgram,
433+
useLegacyMainModule,
421434
numOfSlots,
422435
selectedSlot,
423436
});

src/hub/sagas.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2020-2024 The Pybricks Authors
2+
// Copyright (c) 2020-2026 The Pybricks Authors
33

44
import {
55
SagaGenerator,
@@ -202,7 +202,11 @@ function* handleDownloadAndRun(action: ReturnType<typeof downloadAndRun>): Gener
202202
(s: RootState) => s.hub.maxUserProgramSize,
203203
);
204204

205-
yield* put(mpyCompileMulti6());
205+
const useLegacyMainModule = yield* select(
206+
(s: RootState) => s.hub.useLegacyMainModule,
207+
);
208+
209+
yield* put(mpyCompileMulti6(useLegacyMainModule));
206210

207211
const { didCompile, didFailToCompile } = yield* race({
208212
didCompile: take(mpyDidCompileMulti6),

src/mpy/actions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ export const didFailToCompile = createAction((err: string[]) => ({
2323
err,
2424
}));
2525

26-
export const mpyCompileMulti6 = createAction(() => ({
26+
/**
27+
* Request to compile multiple Python files into multi-mpy6 format.
28+
* @param useLegacyMainModule Whether to use the legacy `__main__` module name (changed in protocol v1.5.0).
29+
*/
30+
export const mpyCompileMulti6 = createAction((useLegacyMainModule: boolean) => ({
2731
type: 'mpy.action.compileMulti6',
32+
useLegacyMainModule,
2833
}));
2934

3035
export const mpyDidCompileMulti6 = createAction((file: Blob) => ({

src/mpy/sagas.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ function* handleCompile(action: ReturnType<typeof compile>): Generator {
104104
/**
105105
* Compiles code into the Pybricks multi-mpy6 file format.
106106
*
107-
* This includes a __main__ module which is the file currently open in the
108-
* editor and any imported modules that can be found in the user file system.
107+
* This includes the file currently open in the editor and any imported modules
108+
* that can be found in the user file system.
109109
*/
110-
function* handleCompileMulti6(): Generator {
110+
function* handleCompileMulti6(action: ReturnType<typeof mpyCompileMulti6>): Generator {
111111
// REVISIT: should we be getting the active file here or have it as an
112112
// action parameter?
113113

@@ -129,7 +129,9 @@ function* handleCompileMulti6(): Generator {
129129
}
130130

131131
const mainPy = yield* editorGetValue();
132-
const mainPyPath = metadata.path ?? '__main__.py';
132+
const mainPyPath = action.useLegacyMainModule
133+
? '__main__.py'
134+
: metadata.path ?? '__main__.py';
133135
const mainPyName = mainPyPath.replace(/\.[^.]+$/, '');
134136

135137
const pyFiles = new Map<string, FileContents>([

0 commit comments

Comments
 (0)