laravel-tools/source/steer.py

278 lines
8.8 KiB
Python
Raw Normal View History

2026-03-06 08:31:05 +01:00
import os as _os
import argparse as _argparse
2026-09-17 17:01:20 +02:00
import lib.string as _string
import lib.command as _command
import lib.execute as _execute
2026-03-06 08:31:05 +01:00
def main():
## args
argument_parser = _argparse.ArgumentParser(
prog = "laravel-steer",
)
argument_parser.add_argument(
"action",
type = str,
choices = [
2026-09-17 17:01:20 +02:00
"selftest",
2026-03-06 08:31:05 +01:00
"clear",
"build",
2026-09-17 17:01:20 +02:00
"init",
2026-03-06 08:31:05 +01:00
"run",
"logs",
],
metavar = "<action>",
)
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 = "<source-directory>",
)
argument_parser.add_argument(
"-b",
"--build-directory",
type = str,
default = None,
metavar = "<build-directory>",
)
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 = "<exclude>",
dest = "excludes",
)
argument_parser.add_argument(
"-e",
"--env-path",
type = str,
default = None,
metavar = "<env-path>",
)
argument_parser.add_argument(
"-p",
"--port",
type = int,
2026-09-17 17:01:20 +02:00
default = 8000,
2026-03-06 08:31:05 +01:00
metavar = "<port>",
)
args = argument_parser.parse_args()
## exec
2026-09-17 17:01:20 +02:00
if (args.action == "selftest"):
print(
_command.directory_create(
"/tmp/foo"
)
)
elif (args.action == "clear"):
2026-03-06 08:31:05 +01:00
if (args.build_directory is None):
raise ValueError("build directory required")
else:
2026-09-17 17:01:20 +02:00
_execute.execute(
_command.remove(args.build_directory),
2026-03-06 08:31:05 +01:00
{
"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:
2026-09-17 17:01:20 +02:00
## create build directory
if True:
_execute.execute(
_command.directory_create(args.build_directory),
{
"show": args.verbose,
"run": (not args.dry_run),
}
2026-03-06 08:31:05 +01:00
)
2026-09-17 17:01:20 +02:00
## unlock
if True:
if (not args.unlock):
pass
else:
_execute.execute(
_command.remove(
_os.path.join(args.build_directory, "composer.lock")
)
)
## copy files
if True:
_execute.execute(
_command.rsync(
args.source_directory,
args.build_directory,
{
"verbose": args.verbose,
"excludes": (
[
".editorconfig",
".env.example",
".env",
"vendor/",
"storage/",
"_ide_helper.php",
]
+
(
["composer.lock"]
if args.unlock else
[]
)
+
(args.excludes or [])
),
}
),
2026-03-06 08:31:05 +01:00
{
2026-09-17 17:01:20 +02:00
"show": args.verbose,
"run": (not args.dry_run),
}
)
## set up storage
if True:
for name in ["sessions", "cache", "views"]:
_execute.execute(
# TODO: {"name": "group", "value": "www-data"},
_command.generic(
"install",
2026-03-06 08:31:05 +01:00
[
2026-09-17 17:01:20 +02:00
{"name": "directory"},
{"name": "mode", "value": "777"},
{"value": _os.path.join(args.build_directory, "storage", "framework", name)},
2026-03-06 08:31:05 +01:00
]
),
2026-09-17 17:01:20 +02:00
{
"directory": args.build_directory,
"show": args.verbose,
"run": (not args.dry_run),
}
)
## place env file
if True:
if (args.env_path is None):
pass
else:
_execute.execute(
_command.copy(
args.env_path,
_os.path.join(args.build_directory, ".env"),
),
{
"show": args.verbose,
"run": (not args.dry_run),
}
)
elif (args.action == "init"):
if (args.build_directory is None):
raise ValueError("build directory required")
else:
## install composer packages
if True:
_execute.execute(
_command.generic(
"composer",
[
{"value": "install"},
]
2026-03-06 08:31:05 +01:00
),
{
2026-09-17 17:01:20 +02:00
"directory": args.build_directory,
2026-03-06 08:31:05 +01:00
"show": args.verbose,
"run": (not args.dry_run),
}
)
2026-09-17 17:01:20 +02:00
## run migrations
if True:
_execute.execute(
_command.generic(
"php",
2026-03-06 08:31:05 +01:00
[
2026-09-17 17:01:20 +02:00
{"value": "artisan"},
{"value": "migrate"},
2026-03-06 08:31:05 +01:00
]
),
{
"directory": args.build_directory,
"show": args.verbose,
"run": (not args.dry_run),
}
)
elif (args.action == "run"):
if (args.port is None):
raise ValueError("port required")
else:
2026-09-17 17:01:20 +02:00
_execute.execute(
_command.generic(
2026-03-06 08:31:05 +01:00
"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:
2026-09-17 17:01:20 +02:00
_execute.execute(
_command.generic(
2026-03-06 08:31:05 +01:00
"tail",
[
{"name": "follow"},
{"value": "storage/logs/laravel.log"},
]
),
{
"directory": args.build_directory,
"show": args.verbose,
"run": (not args.dry_run),
}
)
else:
raise ValueError(
2026-09-17 17:01:20 +02:00
_string.coin(
2026-03-06 08:31:05 +01:00
"invalid action: {{action}}",
{
"action": args.action,
}
)
)