Skip to content
Merged
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
21 changes: 21 additions & 0 deletions src/hub/reducers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test('initial state', () => {
"runtime": "hub.runtime.disconnected",
"selectedSlot": 0,
"useLegacyDownload": false,
"useLegacyMainModule": false,
"useLegacyStartUserProgram": false,
"useLegacyStdio": false,
}
Expand Down Expand Up @@ -544,3 +545,23 @@ describe('useLegacyStartUserProgram', () => {
).toBeFalsy();
});
});

describe('useLegacyMainModule', () => {
test('Pybricks Profile < v1.5.0', () => {
expect(
reducers(
{ useLegacyMainModule: false } as State,
bleDIServiceDidReceiveSoftwareRevision('1.4.0'),
).useLegacyMainModule,
).toBeTruthy();
});

test('Pybricks Profile >= v1.5.0', () => {
expect(
reducers(
{ useLegacyMainModule: true } as State,
bleDIServiceDidReceiveSoftwareRevision('1.5.0'),
).useLegacyMainModule,
).toBeFalsy();
});
});
29 changes: 26 additions & 3 deletions src/hub/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2025 The Pybricks Authors
// Copyright (c) 2020-2026 The Pybricks Authors

import { Reducer, combineReducers } from 'redux';
import * as semver from 'semver';
Expand Down Expand Up @@ -31,6 +31,7 @@ import {
usbDidDisconnectPybricks,
usbDidReceiveDeviceName,
usbDidReceiveFirmwareRevision,
usbDidReceiveSoftwareRevision,
usbDisconnectPybricks,
} from '../usb/actions';
import { pythonVersionToSemver } from '../utils/version';
Expand Down Expand Up @@ -361,7 +362,10 @@ const useLegacyDownload: Reducer<boolean> = (state = false, action) => {
* When true, use NUS for stdio instead of Pybricks control characteristic.
*/
const useLegacyStdio: Reducer<boolean> = (state = false, action) => {
if (bleDIServiceDidReceiveSoftwareRevision.matches(action)) {
if (
bleDIServiceDidReceiveSoftwareRevision.matches(action) ||
usbDidReceiveSoftwareRevision.matches(action)
) {
// Behavior changed starting with Pybricks Profile v1.3.0.
return !semver.satisfies(action.version, '^1.3.0');
}
Expand All @@ -373,14 +377,32 @@ const useLegacyStdio: Reducer<boolean> = (state = false, action) => {
* When true, use Legacy StartUserProgram.
*/
const useLegacyStartUserProgram: Reducer<boolean> = (state = false, action) => {
if (bleDIServiceDidReceiveSoftwareRevision.matches(action)) {
if (
bleDIServiceDidReceiveSoftwareRevision.matches(action) ||
usbDidReceiveSoftwareRevision.matches(action)
) {
// Behavior changed starting with Pybricks Profile v1.4.0.
return !semver.satisfies(action.version, '^1.4.0');
}

return state;
};

/**
* When true, use the legacy `__main__` module name instead of the actual file name.
*/
const useLegacyMainModule: Reducer<boolean> = (state = false, action) => {
if (
bleDIServiceDidReceiveSoftwareRevision.matches(action) ||
usbDidReceiveSoftwareRevision.matches(action)
) {
// Behavior changed starting with Pybricks Profile v1.5.0.
return !semver.satisfies(action.version, '^1.5.0');
}

return state;
};

/*
* Returns number of available slots or 0 for slots not supported.
*/
Expand Down Expand Up @@ -418,6 +440,7 @@ export default combineReducers({
useLegacyDownload,
useLegacyStdio,
useLegacyStartUserProgram,
useLegacyMainModule,
numOfSlots,
selectedSlot,
});
22 changes: 15 additions & 7 deletions src/mpy/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2023 The Pybricks Authors
// Copyright (c) 2020-2026 The Pybricks Authors

import { compile as mpyCrossCompileV5 } from '@pybricks/mpy-cross-v5';
import { compile as mpyCrossCompileV6 } from '@pybricks/mpy-cross-v6';
Expand Down Expand Up @@ -104,8 +104,8 @@ function* handleCompile(action: ReturnType<typeof compile>): Generator {
/**
* Compiles code into the Pybricks multi-mpy6 file format.
*
* This includes a __main__ module which is the file currently open in the
* editor and any imported modules that can be found in the user file system.
* This includes the file currently open in the editor and any imported modules
* that can be found in the user file system.
*/
function* handleCompileMulti6(): Generator {
// REVISIT: should we be getting the active file here or have it as an
Expand All @@ -128,14 +128,22 @@ function* handleCompileMulti6(): Generator {
return;
}

const mainPy = yield* editorGetValue();
const useLegacyMainModule = yield* select(
(s: RootState) => s.hub.useLegacyMainModule,
);

const mainPyContents = yield* editorGetValue();
const mainPyPath = metadata.path ?? '__main__.py';
const mainPyName = useLegacyMainModule
? '__main__'
: mainPyPath.replace(/\.[^.]+$/, '');

const pyFiles = new Map<string, FileContents>([
['__main__', { path: metadata.path ?? '__main__.py', contents: mainPy }],
[mainPyName, { path: mainPyPath, contents: mainPyContents }],
]);

const checkedModules = new Set<string>(['__main__']);
const uncheckedScripts = new Array<string>(mainPy);
const checkedModules = new Set<string>([mainPyName]);
const uncheckedScripts = new Array<string>(mainPyContents);

for (;;) {
// parse all unchecked scripts to find imported modules that haven't
Expand Down
Loading