From bad03235f266921e035961b1b82d00e147793583 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 16 Oct 2023 15:10:28 -0400 Subject: [PATCH] [script] Add script to create a new module This adds a helper script to reduce the boilerplate of creating a new module. It creates the module structure and stubs for all necessary files/directories in the module. The script takes a directory path as an argument, verifies that it doesn't already exist and can be written to, deduces the module name from the last part of the path, and then creates all necessary files doing the minimum of what needs to be done. The module contains a root page and single endpoint named "some_api" as an example. If the --tsx option is specified, a stub tsx index is created as well. Note: SQL is contained in the module's directory rather than the LORIS SQL directory because the script doesn't know if it's a project or LORIS module being created (and if project, where SQL is tracked.) --- tools/new_module.php | 359 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100755 tools/new_module.php diff --git a/tools/new_module.php b/tools/new_module.php new file mode 100755 index 00000000000..2964a827e28 --- /dev/null +++ b/tools/new_module.php @@ -0,0 +1,359 @@ +#!/usr/bin/env php + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://www.github.com/aces/Loris/ + */ + +require_once 'generic_includes.php'; + +$flagsEnd = 0; +$flags = getopt("hj", ['jsx', 'help'], $flagsEnd); + + +$jsx = isset($flags['j']) || isset($flags['jsx']); +$help = isset($flags['h']) || isset($flags['help']); + +if ($help) { + usage(); + exit(0); +} +if ($argc !== $flagsEnd+1) { + usage(); + exit(1); +} +$mdir = $argv[$flagsEnd]; + +if (file_exists($mdir)) { + fwrite(STDERR, "$mdir already exists, can not create new module."); + exit(2); +} + +$path = pathinfo($mdir); +if (!empty($path['extension'])) { + fwrite(STDERR, "$mdir should not have a file extension"); + exit(2); +} + +$mname = $path['filename']; +if (!is_writable($path['dirname'])) { + fwrite(STDERR, "Can not create $mname, $path[dirname] is not writeable."); + exit(2); +} + +if (mkdir($mdir) === false) { + fwrite(STDERR, "Can not create directory $mdir"); + exit(2); + +} +if (mkdir($mdir . DIRECTORY_SEPARATOR . "SQL") === false) { + fwrite(STDERR, "Can not create directory $mdir/SQL"); + exit(2); + +} +if (mkdir($mdir . DIRECTORY_SEPARATOR . "php") === false) { + fwrite(STDERR, "Can not create directory $mdir/php"); + exit(2); + +} + +if (mkdir($mdir . DIRECTORY_SEPARATOR . "test") === false) { + fwrite(STDERR, "Can not create directory $mdir/test"); + exit(2); +} + +if (mkdir($mdir . DIRECTORY_SEPARATOR . "help") === false) { + fwrite(STDERR, "Can not create directory $mdir/help"); + exit(2); +} + + +writeFile( + $mdir . DIRECTORY_SEPARATOR . "README.md", +<< "Alright."]); + } +} +EOF +); + +writeFile( + $mdir . DIRECTORY_SEPARATOR. "help" + . DIRECTORY_SEPARATOR . "$mname.md", +<<safeGet(\$this->url . "/$mname/"); + \$bodyText = \$this->safeFindElement( + WebDriverBy::cssSelector("#breadcrumbs") + )->getText(); + \$this->assertStringContainsString("$mname", \$bodyText); + \$this->assertStringNotContainsString( + "You do not have access to this page.", + \$bodyText + ); + \$this->assertStringNotContainsString( + "An error occured while loading the page.", + \$bodyText + ); + } + + function testPageDoesNotLoadWithoutPermissions() + { + // Without permissions + \$this->setupPermissions(array('')); + \$this->safeGet( + \$this->url . "/$mname/" + ); + + \$errorText = \$this->safeFindElement( + WebDriverBy::cssSelector("body") + )->getText(); + \$this->assertStringContainsString( + "You do not have access to this page.", + \$errorText + ); + } +} +EOF +); + +if ($jsx) { + if (mkdir($mdir . DIRECTORY_SEPARATOR . "jsx") === false) { + fwrite(STDERR, "Can not create directory $mdir/help"); + exit(2); + } + writeFile( + $mdir . DIRECTORY_SEPARATOR. "jsx" + . DIRECTORY_SEPARATOR . "index.tsx", +<<Welcome to $mname!; +} +declare const loris: any; +window.addEventListener('load', () => { + const element = document.getElementById('lorisworkspace'); + if (!element) { + throw new Error('Missing lorisworkspace'); + } + const root = createRoot(element); + + root.render( + + ); +}); +EOF + ); +} + + +print <<