Skip to content

Commit 66b7500

Browse files
authored
feat(scratch): Add scaffolding ArduinoScratch extension (#1)
* Add initial configuration and tasks for Scratch Arduino integration * Remove redundant cleanup command from scratch:init task and comment out formatMessage import in Arduino extension * Refactor task names in Taskfile.yaml for consistency and update Scratch3Arduino class to include example block and clean up imports * Update Scratch3Arduino block opcode and remove commented-out code in patch-gui.js
1 parent faaacdd commit 66b7500

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scratch-editor

Taskfile.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3"
2+
vars:
3+
SCRATCH_EDITOR_VERSION: v12.0.1
4+
5+
tasks:
6+
scratch:init:
7+
- git clone --depth 1 --branch {{ .SCRATCH_EDITOR_VERSION }} https://github.com/scratchfoundation/scratch-editor.git
8+
- cd scratch-editor && npm install && npm run build
9+
- cd scratch-editor/packages/scratch-gui && npm install
10+
- cd scratch-editor/packages/scratch-vm && npm install
11+
12+
scratch:patch:
13+
- cd scratch-editor/packages/scratch-gui && node ../../../scratch-arduino-extensions/scripts/patch-gui.js
14+
15+
scratch:start:
16+
- cd scratch-editor/packages/scratch-gui && npm start
17+
18+
scratch:clean:
19+
- rm -rf scratch-editor
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Option 1: Relative path to cousin folder
2+
// const formatMessage = require('../../../../../../scratch-editor/node_modules/format-message');
3+
const BlockType = require('../../../../../../scratch-editor/packages/scratch-vm/src/extension-support/block-type');
4+
5+
/**
6+
* Url of icon to be displayed at the left edge of each extension block.
7+
* @type {string}
8+
*/
9+
// eslint-disable-next-line max-len
10+
const iconURI = '';
11+
12+
/**
13+
* Url of icon to be displayed in the toolbox menu for the extension category.
14+
* @type {string}
15+
*/
16+
// eslint-disable-next-line max-len
17+
const menuIconURI = ''
18+
19+
class Scratch3Arduino {
20+
constructor (runtime) {
21+
this.runtime = runtime;
22+
}
23+
};
24+
25+
Scratch3Arduino.prototype.getInfo = function () {
26+
return {
27+
id: 'arduino',
28+
name: "Arduino",
29+
menuIconURI: menuIconURI,
30+
blockIconURI: iconURI,
31+
blocks: [
32+
{
33+
opcode: 'noop',
34+
blockType: BlockType.COMMAND,
35+
text: 'do nothing',
36+
func: 'noop'
37+
},
38+
],
39+
40+
};
41+
}
42+
43+
Scratch3Arduino.prototype.noop = function () {
44+
};
45+
46+
module.exports = Scratch3Arduino;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const path = require("path");
2+
const fs = require("fs");
3+
4+
const BaseDir = path.resolve(__dirname, "../"); // base dir is the 'scratch-arduino-extensions'
5+
6+
const ScratchVmExtensionsManagerFile = path.resolve(
7+
BaseDir,
8+
"../scratch-editor/packages/scratch-vm/src/extension-support/extension-manager.js"
9+
);
10+
11+
const ScratchVmVirtualMachineFile = path.resolve(
12+
BaseDir,
13+
"../scratch-editor/packages/scratch-vm/src/virtual-machine.js",
14+
);
15+
16+
const ExtIds = ["Scratch3Arduino"];
17+
const ExtDirName = "scratch3_arduino";
18+
19+
const PatchedExtensionDir = path.resolve(
20+
BaseDir,
21+
"./packages/scratch-vm/src/extensions/",
22+
ExtDirName,
23+
);
24+
const ScratchVmExtensionsDir = path.resolve(
25+
BaseDir,
26+
"../scratch-editor/packages/scratch-vm/src/extensions",
27+
ExtDirName,
28+
);
29+
30+
if (!fs.existsSync(ScratchVmExtensionsDir)) {
31+
fs.symlinkSync(PatchedExtensionDir, ScratchVmExtensionsDir, "dir");
32+
console.log("Set symbolic link to", ScratchVmExtensionsDir);
33+
} else {
34+
console.log("Symbolic link already set to", ScratchVmExtensionsDir);
35+
}
36+
37+
let managerCode = fs.readFileSync(ScratchVmExtensionsManagerFile, "utf-8");
38+
for (const ExtId of ExtIds) {
39+
if (managerCode.includes(ExtId)) {
40+
console.log(`Already registered in manager: ${ExtId}`);
41+
} else {
42+
fs.copyFileSync(ScratchVmExtensionsManagerFile, `${ScratchVmExtensionsManagerFile}.orig`);
43+
managerCode = managerCode.replace(
44+
/builtinExtensions = {[\s\S]*?};/,
45+
`$&\n\nbuiltinExtensions.${ExtId} = () => require('../extensions/${ExtDirName}');`,
46+
);
47+
fs.writeFileSync(ScratchVmExtensionsManagerFile, managerCode);
48+
console.log(`Registered in manager: ${ExtId}`);
49+
}
50+
}
51+
52+
53+
// Add the extension as a core extension.
54+
let vmCode = fs.readFileSync(ScratchVmVirtualMachineFile, "utf-8");
55+
for (const ExtId of ExtIds) {
56+
if (vmCode.includes(ExtId)) {
57+
console.log(`Already added as a core extension: ${ExtId}`);
58+
} else {
59+
fs.copyFileSync(ScratchVmVirtualMachineFile, `${ScratchVmVirtualMachineFile}.orig`);
60+
vmCode = vmCode.replace(
61+
/CORE_EXTENSIONS = \[[\s\S]*?\];/,
62+
`$&\n\nCORE_EXTENSIONS.push('${ExtId}');`,
63+
);
64+
fs.writeFileSync(ScratchVmVirtualMachineFile, vmCode);
65+
console.log(`Add as a core extension: ${ExtId}`);
66+
}
67+
}

0 commit comments

Comments
 (0)