Skip to content

Commit

Permalink
1. Add support for device firmware and arduinoData attributes.
Browse files Browse the repository at this point in the history
2. Change to read external resources under a single-level directory structure.
  • Loading branch information
zhengyangliu committed Aug 2, 2023
1 parent 3c8095e commit e57f3bc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 110 deletions.
16 changes: 1 addition & 15 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ const THIRD_PARTY_TRANSLATIONS_FILE = 'third-party-locales.json';
*/
const RECHECK_INTERVAL = 1000 * 1;

/**
* Extenions class.
* @readonly
*/
const EXTENSION_CLASS = ['shield', 'actuator', 'sensor', 'communication', 'display', 'kit', 'other'];

/**
* Device type.
* @readonly
*/
const DEVICE_TYPE = ['arduino', 'microbit', 'microPython'];


module.exports = {
DIRECTORY_NAME,
Expand All @@ -78,7 +66,5 @@ module.exports = {
REOPEN_INTERVAL,
OFFICIAL_TRANSLATIONS_FILE,
THIRD_PARTY_TRANSLATIONS_FILE,
RECHECK_INTERVAL,
EXTENSION_CLASS,
DEVICE_TYPE
RECHECK_INTERVAL
};
98 changes: 51 additions & 47 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,60 +37,64 @@ class OpenBlockDevice {

parseDeviceList.forEach(deviceId => {
let matched = false;
Object.entries(devices).forEach(catlog => {
Object.entries(catlog[1]).forEach(dev => {
const content = dev[1]['index.js'](formatMessage);
Object.entries(devices).forEach(dev => {
const content = dev[1]['index.js'](formatMessage);

const processDeviceData = deviceData => {
if (deviceData.deviceId === deviceId) {
const processDeviceData = deviceData => {
if (deviceData.deviceId === deviceId) {

const basePath = path.join(this.type, catlog[0], dev[0]);
const basePath = path.join(this.type, dev[0]);

// Convert a local file path to a network address
if (deviceData.iconURL) {
deviceData.iconURL = path.join(basePath, deviceData.iconURL);
}
if (deviceData.connectionIconURL) {
deviceData.connectionIconURL = path.join(basePath, deviceData.connectionIconURL);
}
if (deviceData.connectionSmallIconURL) {
deviceData.connectionSmallIconURL = path.join(basePath,
deviceData.connectionSmallIconURL);
}
if (deviceData.register) {
deviceData.register = path.join(basePath, deviceData.register);
}
if (deviceData.translations) {
deviceData.translations = path.join(basePath, deviceData.translations);
}
// Convert a local file path to a network address
if (deviceData.iconURL) {
deviceData.iconURL = path.join(basePath, deviceData.iconURL);
}
if (deviceData.connectionIconURL) {
deviceData.connectionIconURL = path.join(basePath, deviceData.connectionIconURL);
}
if (deviceData.connectionSmallIconURL) {
deviceData.connectionSmallIconURL = path.join(basePath,
deviceData.connectionSmallIconURL);
}
if (deviceData.register) {
deviceData.register = path.join(basePath, deviceData.register);
}
if (deviceData.translations) {
deviceData.translations = path.join(basePath, deviceData.translations);
}
if (deviceData.firmware) {
deviceData.firmware = path.join(basePath, deviceData.firmware);
}
if (deviceData.arduinoData) {
deviceData.arduinoData = path.join(basePath, deviceData.arduinoData);
}

// filter data based on the edition accessed
if (edition === 'cmtye') {
// when accessing data in the community version, the multi-programming
// framework device only reads the arduino content
if (deviceData.typeList > 1 && deviceData.deviceId.indexOf('arduino') === -1) {
return;
}
// if the device is not inherited from build-in board, filter it out
if (deviceData.deviceId.indexOf('_') === -1) {
return;
}
// filter data based on the edition accessed
if (edition === 'cmtye') {
// when accessing data in the community version, the multi-programming
// framework device only reads the arduino content
if (deviceData.typeList > 1 && deviceData.deviceId.indexOf('arduino') === -1) {
return;
}
// if the device is not inherited from build-in board, filter it out
if (deviceData.deviceId.indexOf('_') === -1) {
return;
}

matched = true;
devicesThumbnailData.push(deviceData);
}
};

// Support for multiple frameworks device data
if (content.length > 1) {
content.forEach(deviceData => {
processDeviceData(deviceData);
});
} else {
processDeviceData(content);

matched = true;
devicesThumbnailData.push(deviceData);
}
});
};

// Support for multiple frameworks device data
if (content.length > 1) {
content.forEach(deviceData => {
processDeviceData(deviceData);
});
} else {
processDeviceData(content);
}
});
if (!matched) {
devicesThumbnailData.push({deviceId: deviceId});
Expand Down
90 changes: 42 additions & 48 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const requireAll = require('require-all');
const path = require('path');
const fs = require('fs');

const {EXTENSION_CLASS, DEVICE_TYPE} = require('./config');

const TYPE = 'extensions';

/**
Expand All @@ -18,56 +16,52 @@ class OpenBlockExtension {
assembleData (userDataPath, edition, formatMessage) {
const extensionsThumbnailData = [];

DEVICE_TYPE.forEach(deviceType => {
EXTENSION_CLASS.forEach(extClass => {
const extPath = path.join(userDataPath, this.type, deviceType, extClass);
if (fs.existsSync(extPath)) {
const data = requireAll({dirname: extPath, filter: /index.js$/, recursive: true});
Object.entries(data).forEach(ext => {
// Modify the attribute to point to the real address.
const content = ext[1]['index.js'](formatMessage);
const basePath = path.join(this.type, deviceType, extClass, ext[0]);

// Convert a local file path to a network address
if (content.iconURL) {
content.iconURL = path.join(basePath, content.iconURL);
}
if (content.blocks) {
content.blocks = path.join(basePath, content.blocks);
}
if (content.generator) {
content.generator = path.join(basePath, content.generator);
}
if (content.toolbox) {
content.toolbox = path.join(basePath, content.toolbox);
}
if (content.msg) {
content.msg = path.join(basePath, content.msg);
}
if (content.library) {
content.library = path.join(extPath, ext[0], content.library);
}
if (content.main) {
content.main = path.join(basePath, content.main);
}
if (content.translations) {
content.translations = path.join(basePath, content.translations);
}
const extPath = path.join(userDataPath, this.type);
if (fs.existsSync(extPath)) {
const data = requireAll({dirname: extPath, filter: /index.js$/, recursive: true});
Object.entries(data).forEach(ext => {
// Modify the attribute to point to the real address.
const content = ext[1]['index.js'](formatMessage);
const basePath = path.join(this.type, ext[0]);

// filter data based on the edition accessed
if (edition === 'cmtye') {
// if the extension only has main.js but no blocks.js,
// the plugin should be blocked
if (!!content.main && !content.blocks) {
return;
}
}
// Convert a local file path to a network address
if (content.iconURL) {
content.iconURL = path.join(basePath, content.iconURL);
}
if (content.blocks) {
content.blocks = path.join(basePath, content.blocks);
}
if (content.generator) {
content.generator = path.join(basePath, content.generator);
}
if (content.toolbox) {
content.toolbox = path.join(basePath, content.toolbox);
}
if (content.msg) {
content.msg = path.join(basePath, content.msg);
}
if (content.library) {
content.library = path.join(userDataPath, basePath, content.library);
}
if (content.main) {
content.main = path.join(basePath, content.main);
}
if (content.translations) {
content.translations = path.join(basePath, content.translations);
}

extensionsThumbnailData.push(content);
});
// filter data based on the edition accessed
if (edition === 'cmtye') {
// if the extension only has main.js but no blocks.js,
// the plugin should be blocked
if (!!content.main && !content.blocks) {
return;
}
}

extensionsThumbnailData.push(content);
});
});
}

return extensionsThumbnailData;
}
Expand Down

0 comments on commit e57f3bc

Please sign in to comment.