commit 03e0734598d2bbbfb82849838bcf2db8e2ee9ad0 Author: Fenris Wolf Date: Fri Mar 6 08:31:05 2026 +0100 [ini] diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..183ab78 --- /dev/null +++ b/readme.md @@ -0,0 +1,29 @@ +# Laravel Tools + +Sammlung von Skripten und Werkzeugen zur Nutzung von Laravel + + +## Erstellung + +### Voraussetzungen + +(keine) + + +### Anweisungen + +- `tools/build` ausführen (Schalter `-h` verwenden für Erklärung) + + +## Nutzung + +### Voraussetzung + +- Python3 + + +### Anweisungen + +- `tools/install` ausführen +- siehe `laravel-steer -h` + diff --git a/source/__main__.py b/source/__main__.py new file mode 100644 index 0000000..05f9b9b --- /dev/null +++ b/source/__main__.py @@ -0,0 +1,5 @@ +from steer import * + + +if __name__ == '__main__': + main() diff --git a/source/lib/__init__.py b/source/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/source/lib/command.py b/source/lib/command.py new file mode 100644 index 0000000..36375ee --- /dev/null +++ b/source/lib/command.py @@ -0,0 +1,144 @@ +from string import * + + +def command_generic( + head, + args +): + def arg_process(arg): + if (arg.get("name", None) is None): + if (arg.get("value", None) is None): + template = None + else: + template = "{{value}}" + else: + if (arg.get("value", None) is None): + template = "--{{name}}" + else: + template = "--{{name}}={{value}}" + return string_coin( + template, + { + "name": (arg.get("name", None) or ""), + "value": string_wrap( + (arg.get("value", None) or ""), + "'", + "'", + { + "mode": arg.get("quote", False), + } + ), + } + ) + return " ".join( + [head] + + + list( + map( + arg_process, + args + ) + ) + ) + + +def command_directory_create( + path +): + return command_generic( + "mkdir", + [ + {"value": path}, + {"name": "parents"}, + ] + ) + + +def command_remove( + path +): + return command_generic( + "rm", + [ + {"value": path}, + {"name": "recursive"}, + {"name": "force"}, + ] + ) + + +def command_copy( + path_from, + path_to, + options = None +): + options = ( + { + "update": False, + "recursive": True, + } + | + (options or {}) + ) + return command_generic( + "cp", + [ + {"value": path_from}, + {"value": path_to}, + ] + + + ( + [{"name": "update"}] + if options["update"] else + [] + ) + + + ( + [{"name": "recursive"}] + if options["recursive"] else + [] + ) + ) + + +def command_rsync( + path_from, + path_to, + options = None +): + options = ( + { + "verbose": False, + "excludes": [], + } + | + (options or {}) + ) + return command_generic( + "rsync", + ( + [ + {"name": "update"}, + {"name": "recursive"}, + ] + + + ( + [{"name": "verbose"}] + if options["verbose"] else + [] + ) + + + list( + map( + lambda exclude: {"name": "exclude", "value": exclude.replace(".", "\\."), "quote": True}, + options["excludes"] + ) + ) + + + [ + {"value": string_coin("{{path}}/.", {"path": path_from})}, + {"value": string_coin("{{path}}", {"path": path_to})}, + ] + ) + ) + diff --git a/source/lib/execute.py b/source/lib/execute.py new file mode 100644 index 0000000..4a4a9f2 --- /dev/null +++ b/source/lib/execute.py @@ -0,0 +1,32 @@ +import sys as _sys +import os as _os + +from string import * + + +def execute( + command, + options = None +): + options = ( + { + "directory": None, + "show": False, + "run": True, + } + | + (options or {}) + ) + command_complete = string_wrap( + command, + string_coin("cd {{directory}} && ", {"directory": (options["directory"] or "")}), + string_coin(" ; cd - > /dev/null", {}), + { + "mode": (not (options["directory"] is None)) + } + ) + if (options["show"]): + _sys.stderr.write(">> %s\n" % command_complete) + if (options["run"]): + _os.system(command_complete) + diff --git a/source/lib/string.py b/source/lib/string.py new file mode 100644 index 0000000..37d76db --- /dev/null +++ b/source/lib/string.py @@ -0,0 +1,29 @@ +def string_wrap( + value, + character_open, + character_close, + options = None +): + options = ( + { + "mode": True, + } + | + (options or {}) + ) + return ( + (character_open + value + character_close) + if options["mode"] else + value + ) + + +def string_coin( + template, + arguments +): + result = template + for (key, value, ) in arguments.items(): + result = result.replace("{{%s}}" % key, value) + return result + diff --git a/source/steer.py b/source/steer.py new file mode 100644 index 0000000..44f1e23 --- /dev/null +++ b/source/steer.py @@ -0,0 +1,219 @@ +import os as _os +import argparse as _argparse + +from lib.string import * +from lib.command import * +from lib.execute import * + + +def main(): + ## args + argument_parser = _argparse.ArgumentParser( + prog = "laravel-steer", + ) + argument_parser.add_argument( + "action", + type = str, + choices = [ + "clear", + "build", + "run", + "logs", + ], + metavar = "", + ) + argument_parser.add_argument( + "-v", + "--verbose", + action = "store_true", + default = False, + ) + argument_parser.add_argument( + "-q", + "--dry-run", + action = "store_true", + default = False, + ) + argument_parser.add_argument( + "-s", + "--source-directory", + type = str, + default = None, + metavar = "", + ) + argument_parser.add_argument( + "-b", + "--build-directory", + type = str, + default = None, + metavar = "", + ) + argument_parser.add_argument( + "-u", + "--unlock", + action = "store_true", + default = False, + help = "do not copy composer.lock", + ) + argument_parser.add_argument( + "-x", + "--exclude", + type = str, + action = "append", + default = None, + metavar = "", + dest = "excludes", + ) + argument_parser.add_argument( + "-e", + "--env-path", + type = str, + default = None, + metavar = "", + ) + argument_parser.add_argument( + "-p", + "--port", + type = int, + default = None, + metavar = "", + ) + args = argument_parser.parse_args() + + ## exec + if (args.action == "clear"): + if (args.build_directory is None): + raise ValueError("build directory required") + else: + execute( + command_remove(args.build_directory), + { + "show": args.verbose, + "run": (not args.dry_run), + } + ) + elif (args.action == "build"): + if (args.source_directory is None): + raise ValueError("source directory required") + else: + if (args.build_directory is None): + raise ValueError("build directory required") + else: + execute( + command_directory_create(args.build_directory), + { + "show": args.verbose, + "run": (not args.dry_run), + } + ) + if (not args.unnock): + pass + else: + execute( + command_remove( + _os.path.join(args.build_directory, "composer.lock") + ) + ) + execute( + command_rsync( + args.source_directory, + args.build_directory, + { + "verbose": args.verbose, + "excludes": ( + [ + ".editorconfig", + ".env.example", + ".env", + "vendor/", + "_ide_helper.php", + ] + + + ( + ["composer.lock"] + if args.unlock else + [] + ) + + + (args.excludes or []) + ), + } + ), + { + "show": args.verbose, + "run": (not args.dry_run), + } + ) + execute( + command_generic( + "composer", + [ + {"value": "install"}, + ] + ), + { + "directory": args.build_directory, + "show": args.verbose, + "run": (not args.dry_run), + } + ) + if (args.env_path is None): + pass + else: + execute( + command_copy( + args.env_path, + _os.path.join(args.build_directory, ".env"), + ), + { + "show": args.verbose, + "run": (not args.dry_run), + } + ) + elif (args.action == "run"): + if (args.port is None): + raise ValueError("port required") + else: + execute( + command_generic( + "php", + [ + {"value": "artisan"}, + {"value": "serve"}, + {"name": "host", "value": "0.0.0.0"}, + {"name": "port", "value": ("%u" % args.port)}, + ] + ), + { + "directory": args.build_directory, + "show": args.verbose, + "run": (not args.dry_run), + } + ) + elif (args.action == "logs"): + if (args.build_directory is None): + raise ValueError("build directory required") + else: + execute( + command_generic( + "tail", + [ + {"name": "follow"}, + {"value": "storage/logs/laravel.log"}, + ] + ), + { + "directory": args.build_directory, + "show": args.verbose, + "run": (not args.dry_run), + } + ) + else: + raise ValueError( + string_coin( + "invalid action: {{action}}", + { + "action": args.action, + } + ) + ) diff --git a/tools/build b/tools/build new file mode 100755 index 0000000..fe641d6 --- /dev/null +++ b/tools/build @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +## consts + +dir_source=source + + +## args + +if [ $# -ge 1 ] ; then dir_build=$1 && shift ; else dir_build=/tmp/laravel-tools ; fi + + +## vars + +path_steer=${dir_build}/laravel-steer + + +## exec + +mkdir -p ${dir_build} +cd ${dir_source} +rm ${path_steer}.zip --force +zip \ + ${path_steer}.zip \ + lib/string.py \ + lib/command.py \ + lib/execute.py \ + lib/__init__.py \ + steer.py \ + __main__.py +cd - +echo "#!/usr/bin/env python3" | cat - ${path_steer}.zip > ${path_steer} +chmod +x ${path_steer} +rm ${path_steer}.zip diff --git a/tools/install b/tools/install new file mode 100755 index 0000000..6151fec --- /dev/null +++ b/tools/install @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +## consts + +dir_target_scripts=${HOME}/programs/laravel-tools +dir_target_executable=${HOME}/.local/bin + + +## args + +if [ $# -ge 1 ] ; then dir_build=$1 && shift ; else dir_build=/tmp/laravel-tools ; fi + + +## vars + +path_executable_steer=${dir_target_executable}/laravel-steer + + +## exec + +mkdir --parent ${dir_target_scripts} +cp --recursive ${dir_build}/. ${dir_target_scripts}/ +ln --force --symbolic ${dir_target_scripts}/laravel-steer ${path_executable_steer}