scrow-monorepo/dev-server/minecraft.bzl
2026-08-07 00:37:40 +02:00

72 lines
2 KiB
Python

def _dev_server_impl(ctx):
script = ctx.actions.declare_file(ctx.label.name + "_runner")
content = """
#!/bin/sh
set -eu
cp -f '%s' server.jar
printf eula=true > eula.txt
mkdir -p plugins
rm -f plugins/*.jar
for plugin in %s
do
cp -v \"$plugin\" plugins/
done
mkdir -p postgres-data
podman run \
--rm \
--name scrow-postgres-dev \
-p 5432:5432 \
-v ./postgres-data/:/data \
-e POSTGRES_USER=postgres \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-d \
docker.io/postgres:alpine || true
echo "Starting paper server in $PWD"
exec env HOST_POSTGRES='jdbc:postgresql://127.0.0.1:5432/postgres' java -jar server.jar --nogui %s
""" % (
ctx.file.server_jar.short_path,
" ".join([p.short_path for p in ctx.files.plugins]),
" ".join(["--add-plugin %s" % p.short_path for p in ctx.files.local_plugins]),
)
ctx.actions.write(
output = script,
content = content,
is_executable = True,
)
return DefaultInfo(
executable = script,
runfiles = ctx.runfiles(files = [ctx.file.server_jar] + ctx.files.plugins + ctx.files.local_plugins)
)
dev_server = rule(
implementation = _dev_server_impl,
executable = True,
attrs = {
"server_jar": attr.label(allow_single_file = True, mandatory = True),
"plugins": attr.label_list(),
"local_plugins": attr.label_list(),
},
)
def _fetch_plugin_impl(ctx):
out_file = ctx.actions.declare_file(ctx.attr.filename)
ctx.actions.run_shell(
outputs = [out_file],
command = "curl -Lso \"$1\" \"$2\"",
arguments = [out_file.path, ctx.attr.url],
execution_requirements = {
"requires-network": "1",
},
progress_message = "Downloading plugin %s" % ctx.attr.filename,
)
return [DefaultInfo(files = depset([out_file]))]
fetch_file = rule(
implementation = _fetch_plugin_impl,
attrs = {
"url": attr.string(mandatory = True),
"filename": attr.string(mandatory = True),
},
)