From 8425f99ea6f8880a94fa20356faed1b3b071caa0 Mon Sep 17 00:00:00 2001 From: Umang Utkarsh Date: Wed, 4 Oct 2023 14:10:17 +0530 Subject: [PATCH 1/2] script-to-generate-pdez-files --- scripts/updatePdezFiles.js | 144 +++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 scripts/updatePdezFiles.js diff --git a/scripts/updatePdezFiles.js b/scripts/updatePdezFiles.js new file mode 100644 index 00000000..be11ded9 --- /dev/null +++ b/scripts/updatePdezFiles.js @@ -0,0 +1,144 @@ +const fs = require('fs-extra'); +const path = require('path'); +const glob = require('fast-glob'); + +// Define the source and destination directories +const from = path.join(__dirname, '..', '..', 'processing-examples'); +const to = path.join(__dirname, '..', 'content', 'examples'); + +/** + * This script updates the .pdez files for the examples + **/ +const updatePdezFiles = async () => { + if (!examplesRepoExists()) { + console.error( + 'To run this script, you must have the processing-examples repo next to the processing-website repo on your computer.' + ); + return; + } + + // Find examples in both the examples repo and this repo + const fromExamples = findExamples(from); + const toExamples = findExamples(to); + + const missingFrom = diffExamples(toExamples, fromExamples); + const missingTo = diffExamples(fromExamples, toExamples); + const portExamples = fromExamples.filter( + (example) => !missingTo.includes(example) + ); + + if (missingTo.length > 0) { + console.log( + 'Examples from processing-examples that are not in the website:' + ); + console.log('(These need to be manually added to the website)'); + missingTo.map((example) => console.log(`- ${example.path}`)); + console.log(''); + } + + if (missingFrom.length > 0) { + console.log( + 'Examples from the website that are not in processing-examples:' + ); + console.log('(These should probably be deleted from the website)'); + missingFrom.map((example) => console.log(`- ${example.path}`)); + console.log(''); + } + + if (portExamples.length > 0) { + console.log( + 'Examples that are in both repos and will be updated with the script:' + ); + portExamples.map((example) => console.log(`- ${example.path}`)); + console.log(''); + } + + // Perform the update + await performPdezFilesUpdate(portExamples); + + console.log('.pdez files updated successfully!'); +}; + +/** + * Finds all main example files that are of format CATEGORY/SUBCATEGORY/EXAMPLE/EXAMPLE.pde + **/ +const findExamples = (folder) => { + const files = glob.sync('**/*.pde', { cwd: folder }); + const examples = []; + files.forEach((file) => { + const split = file.split(path.sep); + const basename = path.basename(file, '.pde'); + + if (split.length === 4 && split[2] === basename) { + examples.push({ + category: split[0], + subcategory: split[1], + path: file, + name: basename, + dirname: path.dirname(file), + }); + } + }); + return examples; +}; + +/** + * Finds all the examples from examples1 missing from examples2 + **/ +const diffExamples = (examples1, examples2) => { + const missing = []; + loop1: for (let i = 0; i < examples1.length; i++) { + for (let j = 0; j < examples2.length; j++) { + if (examples1[i].path === examples2[j].path) { + continue loop1; + } + } + missing.push(examples1[i]); + } + return missing; +}; + +/** + * Checks whether the processing-examples repo is next to this repo + **/ +const examplesRepoExists = () => fs.existsSync(from); + +/** + * Perform the update of .pdez files + **/ +const performPdezFilesUpdate = async (examples) => { + for (let i = 0; i < examples.length; i++) { + const example = examples[i]; + + // Generate .pdez file content based on the example + const pdezContent = generatePdezContent(example); + + // Write the .pdez file to the example folder + const pdezFilePath = path.join(to, example.dirname, `${example.name}.pdez`); + await fs.writeFile(pdezFilePath, pdezContent); + + console.log(`Updated .pdez file for ${example.path}`); + } +}; + +/** + * Generate .pdez file content for an example + **/ +const generatePdezContent = (example) => { + // Modify this function to generate .pdez content based on your requirements + // You can use example's properties to customize the content + return ` + { + "name": "${example.name}", + "title": "${example.title}", + "author": "${example.author}", + "level": "${example.level}", + "order": "${example.order}", + "description": "${example.description}", + "featured": ${JSON.stringify(example.featured)} + } + `; +}; + +// Run the updatePdezFiles function +updatePdezFiles(); From 33b6494e84d060c0b08272b6019c1cdc0558f48d Mon Sep 17 00:00:00 2001 From: Umang Utkarsh Date: Wed, 4 Oct 2023 14:11:47 +0530 Subject: [PATCH 2/2] script-to-generate-pdez-files2.0 --- scripts/updatePdezFiles.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/updatePdezFiles.js b/scripts/updatePdezFiles.js index be11ded9..8aec0ccf 100644 --- a/scripts/updatePdezFiles.js +++ b/scripts/updatePdezFiles.js @@ -126,7 +126,7 @@ const performPdezFilesUpdate = async (examples) => { **/ const generatePdezContent = (example) => { // Modify this function to generate .pdez content based on your requirements - // You can use example's properties to customize the content + // Use example's properties to customize the content return ` { "name": "${example.name}",