feat: sync all micropython board configurations and scripts
This commit is contained in:
47
mixly/scripts/build-boards.js
Normal file
47
mixly/scripts/build-boards.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const shell = require('shelljs');
|
||||
const fs = require('fs');
|
||||
const fs_plus = require('fs-plus');
|
||||
const path = require('path');
|
||||
const { Command, Option } = require('commander');
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.addOption(new Option('-t, --type <string>', 'boards type', 'micropython').choices([
|
||||
'all', 'arduino', 'micropython', 'python'
|
||||
]));
|
||||
|
||||
program.parse();
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
let ignore = ['arduino', 'micropython', 'python'];
|
||||
|
||||
if (ignore.includes(options.type)) {
|
||||
ignore.splice(ignore.indexOf(options.type), 1);
|
||||
} else if (options.type === 'all') {
|
||||
ignore = [];
|
||||
}
|
||||
|
||||
const ORIGIN_DIR = process.cwd();
|
||||
const DEFAULT_SRC_DIR = path.resolve(ORIGIN_DIR, 'boards/default_src');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_SRC_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_SRC_DIR);
|
||||
for (let name of names) {
|
||||
let splitName = name.split('_');
|
||||
if (ignore.includes(splitName[0])) {
|
||||
continue;
|
||||
}
|
||||
const now = path.resolve(DEFAULT_SRC_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const packagejsonPath = path.resolve(now, 'package.json');
|
||||
if (!fs_plus.isFileSync(packagejsonPath)) {
|
||||
continue;
|
||||
}
|
||||
shell.cd(now);
|
||||
shell.exec('npm run build:prod');
|
||||
}
|
||||
}
|
||||
81
mixly/scripts/build-deps.js
Normal file
81
mixly/scripts/build-deps.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const fs = require('fs');
|
||||
const fs_extra = require('fs-extra');
|
||||
const fs_plus = require('fs-plus');
|
||||
const path = require('path');
|
||||
|
||||
const scanDir = (dirPath, ignore, rootPath) => {
|
||||
let googList = [];
|
||||
const dirName = path.basename(dirPath);
|
||||
const dirIgnore = ignore?.dir ?? [];
|
||||
const fileIgnore = ignore?.file ?? [];
|
||||
if (!fs_plus.isDirectorySync(dirPath) || dirIgnore.includes(dirPath)) {
|
||||
return googList;
|
||||
}
|
||||
|
||||
let nameList = fs.readdirSync(dirPath);
|
||||
for (let i = 0; i < nameList.length; i++) {
|
||||
let childPath = path.join(dirPath, nameList[i]);
|
||||
if (fs_plus.isFileSync(childPath)) {
|
||||
let googObj = scanFile(childPath, ignore, rootPath);
|
||||
if (!googObj) {
|
||||
continue;
|
||||
}
|
||||
googList.push(googObj);
|
||||
} else {
|
||||
googList = [...googList, ...scanDir(childPath, ignore, rootPath)];
|
||||
}
|
||||
}
|
||||
|
||||
return googList;
|
||||
}
|
||||
|
||||
const scanFile = (filePath, ignore, rootPath) => {
|
||||
const fileIgnore = ignore?.file ?? [];
|
||||
if (fileIgnore.includes(filePath)) {
|
||||
return null;
|
||||
}
|
||||
let jsStr = fs.readFileSync(filePath, 'utf8');
|
||||
let googObj = {};
|
||||
googObj.path = '/' + path.relative(rootPath, filePath).replaceAll('\\', '/');
|
||||
googObj.require = match('goog.require', jsStr);
|
||||
googObj.provide = match("goog.provide", jsStr);
|
||||
if (googObj.require || googObj.provide) {
|
||||
if (!googObj.require) {
|
||||
googObj.require = [];
|
||||
}
|
||||
if (!googObj.provide) {
|
||||
googObj.provide = [];
|
||||
}
|
||||
} else {
|
||||
googObj = null;
|
||||
}
|
||||
return googObj;
|
||||
}
|
||||
|
||||
const match = (type, jsStr) => {
|
||||
let list = [];
|
||||
if (type === 'goog.require') {
|
||||
list = jsStr.match(/(?<=goog.require[\s]*\(["|'])[^"|']+(?=["|'][\s]*\))/g);
|
||||
} else {
|
||||
list = jsStr.match(/(?<=goog.provide[\s]*\(["|'])[^"|']+(?=["|'][\s]*\))/g);
|
||||
}
|
||||
if (list) {
|
||||
list = unique(list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 数组去重
|
||||
const unique = (list) => {
|
||||
return Array.from(new Set(list));
|
||||
}
|
||||
|
||||
const generate = (needBuildDir, ignore) => {
|
||||
let outputConfig = [];
|
||||
outputConfig = scanDir(needBuildDir, ignore, needBuildDir);
|
||||
return outputConfig;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generate: generate
|
||||
};
|
||||
132
mixly/scripts/build-examples.js
Normal file
132
mixly/scripts/build-examples.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const fs_extra = require('fs-extra');
|
||||
const fs_plus = require('fs-plus');
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const path = require('path');
|
||||
const { Command, Option } = require('commander');
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.addOption(new Option('-t, --type <string>', 'boards type', 'all').choices([
|
||||
'all', 'special'
|
||||
]))
|
||||
.addOption(new Option('--obfuscate', 'obfuscate file names'));
|
||||
|
||||
program.parse();
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
options.type = options.type ?? 'all';
|
||||
|
||||
if (options.type === 'special') {
|
||||
options.type = path.basename(process.cwd());
|
||||
}
|
||||
|
||||
let targetIndex = 0;
|
||||
|
||||
const generateHash = (inputString) => {
|
||||
// const hash = crypto.createHash('sha256');
|
||||
// hash.update(inputString);
|
||||
// return hash.digest('hex');
|
||||
targetIndex += 1;
|
||||
return String(targetIndex);
|
||||
};
|
||||
|
||||
const getExamples = (dirPath, convertExample = false) => {
|
||||
let examples = {};
|
||||
if (!fs_plus.isDirectorySync(dirPath)) {
|
||||
return examples;
|
||||
}
|
||||
const dataList = fs.readdirSync(dirPath);
|
||||
for (let data of dataList) {
|
||||
let dataPath = path.resolve(dirPath, data);
|
||||
if (fs_plus.isDirectorySync(dataPath)) {
|
||||
let id = data;
|
||||
if (convertExample) {
|
||||
id = generateHash(data);
|
||||
const newDataPath = path.resolve(dirPath, id);
|
||||
fs.renameSync(dataPath, newDataPath);
|
||||
dataPath = newDataPath;
|
||||
}
|
||||
const children = getExamples(dataPath, convertExample);
|
||||
if (!Object.keys(children).length) {
|
||||
continue;
|
||||
}
|
||||
examples[id] = {
|
||||
...children,
|
||||
'__file__': false,
|
||||
'__name__': data
|
||||
};
|
||||
} else {
|
||||
const extname = path.extname(data);
|
||||
if (extname !== '.mix') {
|
||||
continue;
|
||||
}
|
||||
let id = data;
|
||||
if (convertExample) {
|
||||
id = generateHash(data) + extname;
|
||||
const newDataPath = path.resolve(dirPath, id);
|
||||
fs.renameSync(dataPath, newDataPath);
|
||||
dataPath = newDataPath;
|
||||
}
|
||||
examples[id] = {
|
||||
'__file__': true,
|
||||
'__name__': data
|
||||
};
|
||||
}
|
||||
}
|
||||
return examples;
|
||||
}
|
||||
|
||||
const ORIGIN_DIR = path.resolve(__dirname, '../');
|
||||
const DEFAULT_SRC_DIR = path.resolve(ORIGIN_DIR, 'boards/default_src');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_SRC_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_SRC_DIR);
|
||||
for (let name of names) {
|
||||
if (!['all', name].includes(options.type)) {
|
||||
continue;
|
||||
}
|
||||
const now = path.resolve(DEFAULT_SRC_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const examplesPath = path.resolve(now, 'origin/examples');
|
||||
if (!fs_plus.isDirectorySync(examplesPath)) {
|
||||
continue;
|
||||
}
|
||||
let outputPath = path.resolve(examplesPath, 'map.json');
|
||||
let output = getExamples(examplesPath);
|
||||
fs_extra.outputJsonSync(outputPath, output, {
|
||||
spaces: ' '
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!!options.obfuscate) {
|
||||
const DEFAULT_DIR = path.resolve(ORIGIN_DIR, 'boards/default');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_DIR);
|
||||
for (let name of names) {
|
||||
if (!['all', name].includes(options.type)) {
|
||||
continue;
|
||||
}
|
||||
const now = path.resolve(DEFAULT_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const examplesPath = path.resolve(now, 'examples');
|
||||
if (!fs_plus.isDirectorySync(examplesPath)) {
|
||||
continue;
|
||||
}
|
||||
let outputPath = path.resolve(examplesPath, 'map.json');
|
||||
let output = getExamples(examplesPath, !!options.obfuscate);
|
||||
fs_extra.outputJsonSync(outputPath, output, {
|
||||
spaces: ' '
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
55
mixly/scripts/build-libraries.py
Normal file
55
mixly/scripts/build-libraries.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import ast
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
from os import path
|
||||
|
||||
|
||||
def extract_imports(file_path):
|
||||
with open(file_path, "r", encoding="utf-8") as file:
|
||||
tree = ast.parse(file.read(), filename=file_path)
|
||||
|
||||
imports = []
|
||||
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Import):
|
||||
for alias in node.names:
|
||||
imports.append(alias.name)
|
||||
elif isinstance(node, ast.ImportFrom):
|
||||
imports.append(node.module)
|
||||
|
||||
return imports
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
working_path = os.getcwd()
|
||||
target_path = sys.argv[1]
|
||||
if not path.isabs(target_path):
|
||||
target_path = path.abspath(path.join(working_path, target_path))
|
||||
output = None
|
||||
if not path.exists(target_path):
|
||||
os.makedirs(target_path)
|
||||
if path.isfile(target_path):
|
||||
os.remove(target_path)
|
||||
os.makedirs(target_path)
|
||||
output = {}
|
||||
content = os.listdir(target_path)
|
||||
for i in content:
|
||||
if i == "map.json":
|
||||
continue
|
||||
name = i[: i.find(".")]
|
||||
output[name] = {}
|
||||
extname = i[i.find(".") :]
|
||||
file_path = path.join(target_path, i)
|
||||
if path.isfile(file_path) and extname == ".py":
|
||||
output[name]["__require__"] = extract_imports(file_path)
|
||||
output[name]["__file__"] = True
|
||||
output[name]["__size__"] = os.path.getsize(file_path)
|
||||
else:
|
||||
output[name]["__require__"] = []
|
||||
output[name]["__file__"] = False
|
||||
output[name]["__size__"] = 0
|
||||
output[name]["__name__"] = i
|
||||
fw = open(path.join(target_path, "map.json"), "w", encoding="utf-8")
|
||||
json.dump(output, fw, indent=4, ensure_ascii=False)
|
||||
fw.close()
|
||||
48
mixly/scripts/deps-gen.js
Normal file
48
mixly/scripts/deps-gen.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const fs = require('fs');
|
||||
const fs_extra = require('fs-extra');
|
||||
const fs_plus = require('fs-plus');
|
||||
const build_deps = require('./build-deps.js');
|
||||
const path = require('path');
|
||||
|
||||
const config = {
|
||||
"workPath": __dirname,
|
||||
"fileIgnore": [
|
||||
path.resolve(__dirname, '../common/modules/mixly-modules/mixly.min.js')
|
||||
],
|
||||
"dirIgnore": []
|
||||
};
|
||||
|
||||
const needBuildDirList = [
|
||||
path.resolve(__dirname, '../mixly-sw/mixly-modules/'),
|
||||
path.resolve(__dirname, '../common/modules/mixly-modules/')
|
||||
];
|
||||
|
||||
const generateDeps = () => {
|
||||
for (let needBuildDir of needBuildDirList) {
|
||||
let fileIgnore = [];
|
||||
let dirIgnore = [];
|
||||
if (typeof config.fileIgnore === 'object') {
|
||||
for (let data of config.fileIgnore) {
|
||||
fileIgnore.push(path.resolve(needBuildDir, data));
|
||||
}
|
||||
}
|
||||
if (typeof config.dirIgnore === 'object') {
|
||||
for (let data of config.dirIgnore) {
|
||||
dirIgnore.push(path.resolve(needBuildDir, data));
|
||||
}
|
||||
}
|
||||
let outputConfig = [];
|
||||
console.log('deps.json生成中...');
|
||||
const ignore = {
|
||||
dir: dirIgnore,
|
||||
file: fileIgnore
|
||||
};
|
||||
outputConfig = build_deps.generate(needBuildDir, ignore);
|
||||
fs_extra.outputJsonSync(path.join(needBuildDir, 'deps.json'), outputConfig, {
|
||||
spaces: ' '
|
||||
});
|
||||
console.log(path.join(needBuildDir, 'deps.json') + '生成成功');
|
||||
}
|
||||
}
|
||||
|
||||
generateDeps();
|
||||
47
mixly/scripts/publish-boards.js
Normal file
47
mixly/scripts/publish-boards.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const shell = require('shelljs');
|
||||
const fs = require('fs');
|
||||
const fs_plus = require('fs-plus');
|
||||
const path = require('path');
|
||||
const { Command, Option } = require('commander');
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.addOption(new Option('-t, --type <string>', 'boards type', 'micropython').choices([
|
||||
'all', 'arduino', 'micropython', 'python'
|
||||
]));
|
||||
|
||||
program.parse();
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
let ignore = ['arduino', 'micropython', 'python'];
|
||||
|
||||
if (ignore.includes(options.type)) {
|
||||
ignore.splice(ignore.indexOf(options.type), 1);
|
||||
} else if (options.type === 'all') {
|
||||
ignore = [];
|
||||
}
|
||||
|
||||
const ORIGIN_DIR = process.cwd();
|
||||
const DEFAULT_SRC_DIR = path.resolve(ORIGIN_DIR, 'boards/default_src');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_SRC_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_SRC_DIR);
|
||||
for (let name of names) {
|
||||
let splitName = name.split('_');
|
||||
if (ignore.includes(splitName[0])) {
|
||||
continue;
|
||||
}
|
||||
const now = path.resolve(DEFAULT_SRC_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const packagejsonPath = path.resolve(now, 'package.json');
|
||||
if (!fs_plus.isFileSync(packagejsonPath)) {
|
||||
continue;
|
||||
}
|
||||
shell.cd(now);
|
||||
shell.exec('npm run publish:board');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user