Skip to content

Commit

Permalink
Improve the offline version for Python, C/C++, Java
Browse files Browse the repository at this point in the history
  • Loading branch information
lucademenego99 committed Jul 8, 2023
1 parent 954016e commit 1ee4739
Show file tree
Hide file tree
Showing 19 changed files with 639 additions and 170 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Build for a specific language;
npm run build -- --language python
```

**Note**: before building for c++, you need to prepare the webworker:
**Note**: before building for c/c++/python/java, you need to prepare the webworker:

```
npm run prepare-cpp
npm run prepare-[cpp/python/java]
```

The build-all command will generate a `dist/base` folder with the following files:
Expand All @@ -46,6 +46,19 @@ The build-all command will generate a `dist/base` folder with the following file
- various css files: styles to make the slides work.


## Redbean file
The resulting Redbean file contains the dependencies to compile and run all programming languages supported by ICPs, so it is quite heavy.
If you need to prepare a redbean file containing only the dependencies for a specific programming language, you can:
- take the redbean.com file contained in the public folder;
- use the zip command line program available in Linux to insert the dependencies you need and the HTML slides;
- distribute it!

As an example, here are the commands to prepare a Redbean file compatible with Python:
- zip redbean.com custom-style.css reveal.js python-offline.iife.js reveal.css white.css # Common dependencies
- zip redbean.com -r utils # Utils folder available in the public directory, containing only the "python" subfolder
- zip redbean.com index.html # Your HTML slides


## Exported Web Components

The build phase creates the following web components:
Expand Down Expand Up @@ -83,7 +96,7 @@ The available languages that can be used in the editors are the following:
| typescript |||||
| python ||| ||
| java || | ||
| c++ || | ||
| c/c++ || | ||
| sql ||| ||
| p5 || | ||
| processing || | ||
Expand Down
14 changes: 14 additions & 0 deletions build-config/vite.prepare-python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'vite';

export default defineConfig({
build: {
outDir: 'src/modules/workers',
emptyOutDir: false,
lib: {
entry: 'src/modules/workers/pythonOfflineWorker.ts',
formats: ['iife'],
fileName: 'pythonWorkerBundle',
name: 'PythonWorker',
}
},
});
87 changes: 61 additions & 26 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,6 @@ export default (cb) => {

console.log("Build completed!");

if (language == "java-offline") {
// Copy some files for Java to work offline
console.log("Setting up java offline mode.")
child_process.exec("cp src/modules/workers/java/javaWorker.ts dist/base/utils/java/javaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaWorker.ts to javaWorker.js");
cb(err);
}
});
child_process.exec("cp src/modules/workers/java/javaTeaWorkerOffline.ts dist/base/utils/java/javaTeaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaTeaWorkerOffline.ts to javaTeaWorker.js");
cb(err);
}
});
child_process.exec("cp src/modules/workers/java/javaRunWorker.ts dist/base/utils/java/javaRunWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaRunWorker.ts to javaRunWorker.js");
cb(err);
}
});
}

if (language == "full-offline") {
console.log("Setting up redbean.com for full offline mode.")
child_process.exec("cd dist/base/ && chmod +x ../../bin/zip.com && ../../bin/zip.com -r redbean.com utils full-offline.iife.js reveal.js reveal.css blood.css white.css custom-style.css && ../../bin/zip.com -0 redbean.com index.html && chmod +x redbean.com", (err, stdout, stderr) => {
Expand Down Expand Up @@ -120,7 +97,65 @@ export function prepareCpp(cb) {
console.log(`Removing ${file} from src/modules/workers/cpp`);
del([`src/modules/workers/cpp/${file}`]);
});
console.log("Finished preparing C++ files!");
cb();
child_process.exec("cp src/modules/workers/cpp/cppWorkerBundle.iife.js public/utils/cpp/cppWorkerBundle.iife.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] copying worker to public folder");
cb(err);
}
console.log("Finished preparing C++ files!");
cb();
});
});
}

// Create a custom task 'prepare-python'
export function preparePython(cb) {
console.log("Preparing Python files...");
child_process.exec("vite build -c=build-config/vite.prepare-python.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] preparing Python files");
cb(err);
}
// Get the list of files and directories in the public folder
const files = fs.readdirSync("public");
// Remove all these files from src/modules/workers
files.forEach(file => {
console.log(`Removing ${file} from src/modules/workers`);
del([`src/modules/workers/${file}`]);
});
child_process.exec("cp src/modules/workers/pythonWorkerBundle.iife.js public/utils/python/pyodide/pythonWorkerBundle.iife.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] copying worker to public folder");
cb(err);
}
console.log("Finished preparing Python files!");
cb();
});
});
}

// Create a custom task 'prepare-java'
export function prepareJava(cb) {
console.log("Preparing Java files...");
child_process.exec("cp src/modules/workers/java/javaWorker.js public/utils/java/javaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaWorker.js to javaWorker.js");
cb(err);
}
child_process.exec("cp src/modules/workers/java/javaTeaWorkerOffline.js public/utils/java/javaTeaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaTeaWorkerOffline.js to javaTeaWorker.js");
cb(err);
}
child_process.exec("cp src/modules/workers/java/javaRunWorker.js public/utils/java/javaRunWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaRunWorker.js to javaRunWorker.js");
cb(err);
}
console.log("Finished preparing Java files!");
cb();
});
});
});
}
}

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "icp-bundle",
"private": false,
"version": "0.2.4",
"version": "0.2.6",
"author": "Luca De Menego <[email protected]>",
"files": [
"dist/"
Expand All @@ -28,7 +28,9 @@
"dev": "vite",
"build": "gulp",
"prepare-cpp": "gulp prepareCpp",
"build-all": "gulp prepareCpp; gulp --language full; gulp --language java; gulp --language cpp; gulp --language standard-ml; gulp --language javascript; gulp --language p5-and-processing; gulp --language typescript; gulp --language python; gulp --language sql; gulp --language java-offline; gulp --language python-offline; gulp --language cpp-offline; gulp --language full-offline",
"prepare-python": "gulp preparePython",
"prepare-java": "gulp prepareJava",
"build-all": "gulp prepareCpp; gulp preparePython; gulp prepareJava; gulp --language full; gulp --language java; gulp --language cpp; gulp --language standard-ml; gulp --language javascript; gulp --language p5-and-processing; gulp --language typescript; gulp --language python; gulp --language sql; gulp --language java-offline; gulp --language python-offline; gulp --language cpp-offline; gulp --language full-offline",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json",
"github-cdn": "npx gh-pages -d dist"
Expand Down
5 changes: 5 additions & 0 deletions public/utils/cpp/cppWorkerBundle.iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions public/utils/python/pyodide/pythonWorkerBundle.iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/lib/CppOfflineEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
export let downloadable = false;
import BaseEditor from "./BaseEditor.svelte";
import CppWorker from "../modules/workers/cpp/cppWorkerBundle.iife.js?url";
import { onMount } from "svelte";
import { cpp } from "@codemirror/lang-cpp";
let webworker: SharedWorker;
function createWorker(): void {
webworker = new SharedWorker(CppWorker, {
name: "CppWorker",
});
webworker.port.start();
let baseUrl =
document.location.protocol +
"//" +
document.location.host +
document.location.pathname;
baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf("/"));
baseUrl += "/utils/cpp/";
webworker = new SharedWorker(baseUrl + "cppWorkerBundle.iife.js", {
name: "CppWorker",
});
webworker.port.start();
webworker.port.postMessage({ type: "init", baseUrl });
}
Expand Down
Loading

0 comments on commit 1ee4739

Please sign in to comment.