Inital commit
This commit is contained in:
198
wails_bun/private/dev_session.bzl
Normal file
198
wails_bun/private/dev_session.bzl
Normal file
@@ -0,0 +1,198 @@
|
||||
"""Dev-session rules for Bun-backed Wails apps."""
|
||||
|
||||
load("//wails:private/common.bzl", "bash_launcher")
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
def _absolute_label(label_package, name):
|
||||
if label_package:
|
||||
return "//%s:%s" % (label_package, name)
|
||||
return "//:%s" % name
|
||||
|
||||
def _wails_bun_watch_run_impl(ctx):
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
|
||||
resolve_lines = """
|
||||
tool="$(resolve_runfile "{tool_short_path}")"
|
||||
binary="$(resolve_runfile "{binary_short_path}")"
|
||||
build_assets="$(resolve_runfile "{build_assets_short_path}")"
|
||||
bindings=""
|
||||
if [[ -n "{bindings_short_path}" ]]; then
|
||||
bindings="$(resolve_runfile "{bindings_short_path}")"
|
||||
fi
|
||||
icon=""
|
||||
if [[ -n "{icon_short_path}" ]]; then
|
||||
icon="$(resolve_runfile "{icon_short_path}")"
|
||||
fi
|
||||
""".format(
|
||||
tool_short_path = ctx.executable._tool.short_path,
|
||||
binary_short_path = ctx.executable.binary.short_path,
|
||||
build_assets_short_path = ctx.files.build_assets[0].short_path,
|
||||
bindings_short_path = ctx.executable.bindings.short_path if ctx.executable.bindings else "",
|
||||
icon_short_path = ctx.file.icon.short_path if ctx.file.icon else "",
|
||||
)
|
||||
|
||||
binding_command = ""
|
||||
if ctx.executable.bindings:
|
||||
binding_command = "\"$bindings\"\n"
|
||||
|
||||
command_lines = """
|
||||
{binding_command}exec "$tool" \
|
||||
--binary "$binary" \
|
||||
--build-assets "$build_assets" \
|
||||
--frontend-url {frontend_url} \
|
||||
--icon "$icon" \
|
||||
--mode 'dev'
|
||||
""".format(
|
||||
binding_command = binding_command,
|
||||
frontend_url = _shell_quote(ctx.attr.frontend_url),
|
||||
)
|
||||
|
||||
ctx.actions.write(
|
||||
output = launcher,
|
||||
is_executable = True,
|
||||
content = bash_launcher(resolve_lines, command_lines),
|
||||
)
|
||||
|
||||
transitive_files = [
|
||||
ctx.attr._tool[DefaultInfo].default_runfiles.files,
|
||||
ctx.attr.binary[DefaultInfo].default_runfiles.files,
|
||||
]
|
||||
files = [
|
||||
ctx.executable._tool,
|
||||
ctx.executable.binary,
|
||||
] + ctx.files.build_assets + ([ctx.file.icon] if ctx.file.icon else [])
|
||||
|
||||
if ctx.attr.bindings:
|
||||
transitive_files.append(ctx.attr.bindings[DefaultInfo].default_runfiles.files)
|
||||
files.append(ctx.executable.bindings)
|
||||
|
||||
runfiles = ctx.runfiles(
|
||||
files = files,
|
||||
transitive_files = depset(transitive = transitive_files),
|
||||
)
|
||||
|
||||
return [DefaultInfo(executable = launcher, runfiles = runfiles)]
|
||||
|
||||
_wails_bun_watch_run = rule(
|
||||
implementation = _wails_bun_watch_run_impl,
|
||||
attrs = {
|
||||
"binary": attr.label(
|
||||
mandatory = True,
|
||||
executable = True,
|
||||
cfg = "target",
|
||||
),
|
||||
"bindings": attr.label(
|
||||
executable = True,
|
||||
cfg = "target",
|
||||
),
|
||||
"build_assets": attr.label(
|
||||
mandatory = True,
|
||||
allow_files = True,
|
||||
),
|
||||
"frontend_url": attr.string(default = "http://127.0.0.1:9245"),
|
||||
"icon": attr.label(allow_single_file = True),
|
||||
"_tool": attr.label(
|
||||
default = "//wails/tools:launch_app",
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
),
|
||||
},
|
||||
executable = True,
|
||||
)
|
||||
|
||||
def _wails_bun_dev_session_impl(ctx):
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
resolve_lines = """
|
||||
tool="$(resolve_runfile "{tool_short_path}")"
|
||||
frontend_dev="$(resolve_runfile "{frontend_dev_short_path}")"
|
||||
""".format(
|
||||
tool_short_path = ctx.executable._tool.short_path,
|
||||
frontend_dev_short_path = ctx.executable.frontend_dev.short_path,
|
||||
)
|
||||
|
||||
command_lines = """
|
||||
exec "$tool" \
|
||||
--frontend-dev "$frontend_dev" \
|
||||
--frontend-url {frontend_url} \
|
||||
--watch-target {watch_target} \
|
||||
--workspace-dir {workspace_dir}
|
||||
""".format(
|
||||
frontend_url = _shell_quote(ctx.attr.frontend_url),
|
||||
watch_target = _shell_quote(ctx.attr.watch_target),
|
||||
workspace_dir = _shell_quote(ctx.attr.workspace_dir),
|
||||
)
|
||||
|
||||
ctx.actions.write(
|
||||
output = launcher,
|
||||
is_executable = True,
|
||||
content = bash_launcher(resolve_lines, command_lines),
|
||||
)
|
||||
|
||||
runfiles = ctx.runfiles(
|
||||
files = [
|
||||
ctx.executable._tool,
|
||||
ctx.executable.frontend_dev,
|
||||
],
|
||||
transitive_files = depset(transitive = [
|
||||
ctx.attr._tool[DefaultInfo].default_runfiles.files,
|
||||
ctx.attr.frontend_dev[DefaultInfo].default_runfiles.files,
|
||||
]),
|
||||
)
|
||||
|
||||
return [DefaultInfo(executable = launcher, runfiles = runfiles)]
|
||||
|
||||
_wails_bun_dev_session_rule = rule(
|
||||
implementation = _wails_bun_dev_session_impl,
|
||||
attrs = {
|
||||
"frontend_dev": attr.label(
|
||||
mandatory = True,
|
||||
executable = True,
|
||||
cfg = "target",
|
||||
),
|
||||
"frontend_url": attr.string(default = "http://127.0.0.1:9245"),
|
||||
"watch_target": attr.string(mandatory = True),
|
||||
"workspace_dir": attr.string(default = "."),
|
||||
"_tool": attr.label(
|
||||
default = "//wails_bun/tools:bun_dev_session",
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
),
|
||||
},
|
||||
executable = True,
|
||||
)
|
||||
|
||||
def wails_bun_dev_session(
|
||||
name,
|
||||
workspace_dir,
|
||||
frontend_dev,
|
||||
app_binary,
|
||||
build_assets,
|
||||
bindings_target = None,
|
||||
icon = None,
|
||||
frontend_url = "http://127.0.0.1:9245",
|
||||
tags = None,
|
||||
visibility = None):
|
||||
watch_name = name + "_watch"
|
||||
|
||||
_wails_bun_watch_run(
|
||||
name = watch_name,
|
||||
binary = app_binary,
|
||||
bindings = bindings_target,
|
||||
build_assets = build_assets,
|
||||
frontend_url = frontend_url,
|
||||
icon = icon,
|
||||
tags = ["manual"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
_wails_bun_dev_session_rule(
|
||||
name = name,
|
||||
frontend_dev = frontend_dev,
|
||||
frontend_url = frontend_url,
|
||||
tags = tags,
|
||||
visibility = visibility,
|
||||
watch_target = _absolute_label(native.package_name(), watch_name),
|
||||
workspace_dir = workspace_dir,
|
||||
)
|
||||
274
wails_bun/private/frontend_dist.bzl
Normal file
274
wails_bun/private/frontend_dist.bzl
Normal file
@@ -0,0 +1,274 @@
|
||||
"""Bun frontend build helpers for Wails."""
|
||||
|
||||
load("//wails:private/common.bzl", "bash_launcher", "write_manifest")
|
||||
|
||||
def _node_modules_root_from_path(path):
|
||||
marker = "/node_modules/"
|
||||
marker_index = path.find(marker)
|
||||
if marker_index < 0:
|
||||
return None
|
||||
return path[:marker_index + len("/node_modules")]
|
||||
|
||||
def _dirname(path):
|
||||
index = path.rfind("/")
|
||||
if index < 0:
|
||||
return "."
|
||||
return path[:index] or "."
|
||||
|
||||
def _wails_bun_frontend_dist_impl(ctx):
|
||||
out_dir = ctx.actions.declare_directory(ctx.label.name)
|
||||
|
||||
manifest_srcs = list(ctx.files.srcs)
|
||||
if ctx.file.package_json.path not in [src.path for src in manifest_srcs]:
|
||||
manifest_srcs.append(ctx.file.package_json)
|
||||
|
||||
manifest = write_manifest(
|
||||
ctx,
|
||||
ctx.label.name + ".manifest",
|
||||
manifest_srcs,
|
||||
ctx.attr.strip_prefix,
|
||||
)
|
||||
|
||||
if not ctx.files.node_modules:
|
||||
fail("wails_bun_frontend_dist requires a non-empty node_modules tree")
|
||||
|
||||
workspace_marker = ""
|
||||
if ctx.attr.workspace_dir:
|
||||
workspace_marker = "/%s/node_modules/" % ctx.attr.workspace_dir.strip("/")
|
||||
|
||||
shortest_path = None
|
||||
shared_node_modules_root = None
|
||||
workspace_node_modules_root = None
|
||||
|
||||
for src in ctx.files.node_modules:
|
||||
if shortest_path == None or len(src.path) < len(shortest_path):
|
||||
shortest_path = src.path
|
||||
if workspace_marker and workspace_marker in src.path and workspace_node_modules_root == None:
|
||||
workspace_node_modules_root = _node_modules_root_from_path(src.path)
|
||||
|
||||
if shortest_path:
|
||||
shared_node_modules_root = _node_modules_root_from_path(shortest_path)
|
||||
|
||||
node_modules_root = workspace_node_modules_root or shared_node_modules_root
|
||||
if node_modules_root == None or shared_node_modules_root == None:
|
||||
fail("unable to determine node_modules roots from node_modules inputs")
|
||||
|
||||
package_json_rel = ctx.file.package_json.short_path
|
||||
if ctx.attr.strip_prefix and package_json_rel.startswith(ctx.attr.strip_prefix):
|
||||
package_json_rel = package_json_rel[len(ctx.attr.strip_prefix):]
|
||||
package_dir = _dirname(package_json_rel)
|
||||
|
||||
args = ctx.actions.args()
|
||||
args.add("--build-script", ctx.attr.build_script)
|
||||
args.add("--manifest", manifest.path)
|
||||
args.add("--node-modules-root", node_modules_root)
|
||||
args.add("--out", out_dir.path)
|
||||
args.add("--package-dir", package_dir)
|
||||
args.add("--shared-node-modules-root", shared_node_modules_root)
|
||||
args.add("--bun-darwin-aarch64", ctx.file._bun_darwin_aarch64.path)
|
||||
args.add("--bun-darwin-x64", ctx.file._bun_darwin_x64.path)
|
||||
args.add("--bun-linux-aarch64", ctx.file._bun_linux_aarch64.path)
|
||||
args.add("--bun-linux-x64", ctx.file._bun_linux_x64.path)
|
||||
args.add("--bun-windows-x64", ctx.file._bun_windows_x64.path)
|
||||
if ctx.attr.workspace_dir:
|
||||
args.add("--workspace-dir", ctx.attr.workspace_dir)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = ctx.executable._tool,
|
||||
arguments = [args],
|
||||
inputs = depset(
|
||||
manifest_srcs +
|
||||
ctx.files.node_modules + [
|
||||
manifest,
|
||||
ctx.file._bun_darwin_aarch64,
|
||||
ctx.file._bun_darwin_x64,
|
||||
ctx.file._bun_linux_aarch64,
|
||||
ctx.file._bun_linux_x64,
|
||||
ctx.file._bun_windows_x64,
|
||||
],
|
||||
),
|
||||
outputs = [out_dir],
|
||||
tools = [ctx.attr._tool[DefaultInfo].files_to_run],
|
||||
mnemonic = "WailsBunFrontendDist",
|
||||
progress_message = "Building Bun frontend dist for %s" % ctx.label,
|
||||
)
|
||||
|
||||
return [DefaultInfo(files = depset([out_dir]))]
|
||||
|
||||
def _wails_bun_frontend_dev_impl(ctx):
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
package_dir = _dirname(ctx.file.package_json.short_path)
|
||||
|
||||
resolve_lines = """
|
||||
tool="$(resolve_runfile "{tool_short_path}")"
|
||||
bun_darwin_aarch64="$(resolve_runfile "{bun_darwin_aarch64_short_path}")"
|
||||
bun_darwin_x64="$(resolve_runfile "{bun_darwin_x64_short_path}")"
|
||||
bun_linux_aarch64="$(resolve_runfile "{bun_linux_aarch64_short_path}")"
|
||||
bun_linux_x64="$(resolve_runfile "{bun_linux_x64_short_path}")"
|
||||
bun_windows_x64="$(resolve_runfile "{bun_windows_x64_short_path}")"
|
||||
""".format(
|
||||
tool_short_path = ctx.executable._tool.short_path,
|
||||
bun_darwin_aarch64_short_path = ctx.file._bun_darwin_aarch64.short_path,
|
||||
bun_darwin_x64_short_path = ctx.file._bun_darwin_x64.short_path,
|
||||
bun_linux_aarch64_short_path = ctx.file._bun_linux_aarch64.short_path,
|
||||
bun_linux_x64_short_path = ctx.file._bun_linux_x64.short_path,
|
||||
bun_windows_x64_short_path = ctx.file._bun_windows_x64.short_path,
|
||||
)
|
||||
|
||||
command_lines = """
|
||||
exec "$tool" \
|
||||
--bun-darwin-aarch64 "$bun_darwin_aarch64" \
|
||||
--bun-darwin-x64 "$bun_darwin_x64" \
|
||||
--bun-linux-aarch64 "$bun_linux_aarch64" \
|
||||
--bun-linux-x64 "$bun_linux_x64" \
|
||||
--bun-windows-x64 "$bun_windows_x64" \
|
||||
--package-dir {package_dir} \
|
||||
--script {script}
|
||||
""".format(
|
||||
package_dir = _shell_quote(ctx.attr.workspace_dir or package_dir),
|
||||
script = _shell_quote(ctx.attr.script),
|
||||
)
|
||||
|
||||
ctx.actions.write(
|
||||
output = launcher,
|
||||
is_executable = True,
|
||||
content = bash_launcher(resolve_lines, command_lines),
|
||||
)
|
||||
|
||||
runfiles = ctx.runfiles(
|
||||
files = [
|
||||
ctx.executable._tool,
|
||||
ctx.file._bun_darwin_aarch64,
|
||||
ctx.file._bun_darwin_x64,
|
||||
ctx.file._bun_linux_aarch64,
|
||||
ctx.file._bun_linux_x64,
|
||||
ctx.file._bun_windows_x64,
|
||||
] + ([ctx.file.package_json] if ctx.file.package_json else []),
|
||||
transitive_files = depset(transitive = [
|
||||
ctx.attr._tool[DefaultInfo].default_runfiles.files,
|
||||
]),
|
||||
)
|
||||
|
||||
return [DefaultInfo(executable = launcher, runfiles = runfiles)]
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
wails_bun_frontend_dist = rule(
|
||||
implementation = _wails_bun_frontend_dist_impl,
|
||||
attrs = {
|
||||
"build_script": attr.string(default = "build"),
|
||||
"node_modules": attr.label(
|
||||
mandatory = True,
|
||||
allow_files = True,
|
||||
),
|
||||
"package_json": attr.label(
|
||||
mandatory = True,
|
||||
allow_single_file = True,
|
||||
),
|
||||
"srcs": attr.label_list(
|
||||
mandatory = True,
|
||||
allow_files = True,
|
||||
),
|
||||
"strip_prefix": attr.string(default = ""),
|
||||
"workspace_dir": attr.string(default = ""),
|
||||
"_tool": attr.label(
|
||||
default = "//wails_bun/tools:frontend_dist_action",
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
),
|
||||
"_bun_darwin_aarch64": attr.label(
|
||||
default = "@bun_darwin_aarch64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_darwin_x64": attr.label(
|
||||
default = "@bun_darwin_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_linux_aarch64": attr.label(
|
||||
default = "@bun_linux_aarch64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_linux_x64": attr.label(
|
||||
default = "@bun_linux_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_windows_x64": attr.label(
|
||||
default = "@bun_windows_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
_wails_bun_frontend_dev = rule(
|
||||
implementation = _wails_bun_frontend_dev_impl,
|
||||
attrs = {
|
||||
"data": attr.label_list(allow_files = True),
|
||||
"node_modules": attr.label(
|
||||
mandatory = True,
|
||||
allow_files = True,
|
||||
),
|
||||
"package_json": attr.label(
|
||||
mandatory = True,
|
||||
allow_single_file = True,
|
||||
),
|
||||
"script": attr.string(default = "dev"),
|
||||
"workspace_dir": attr.string(default = ""),
|
||||
"_tool": attr.label(
|
||||
default = "//wails_bun/tools:frontend_dev_server",
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
),
|
||||
"_bun_darwin_aarch64": attr.label(
|
||||
default = "@bun_darwin_aarch64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_darwin_x64": attr.label(
|
||||
default = "@bun_darwin_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_linux_aarch64": attr.label(
|
||||
default = "@bun_linux_aarch64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_linux_x64": attr.label(
|
||||
default = "@bun_linux_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bun_windows_x64": attr.label(
|
||||
default = "@bun_windows_x64//:bun",
|
||||
cfg = "exec",
|
||||
allow_single_file = True,
|
||||
),
|
||||
},
|
||||
executable = True,
|
||||
)
|
||||
|
||||
def wails_bun_frontend_dev(
|
||||
name,
|
||||
data,
|
||||
node_modules,
|
||||
package_json,
|
||||
script = "dev",
|
||||
workspace_dir = "",
|
||||
tags = None,
|
||||
visibility = None):
|
||||
_wails_bun_frontend_dev(
|
||||
name = name,
|
||||
data = data,
|
||||
node_modules = node_modules,
|
||||
package_json = package_json,
|
||||
script = script,
|
||||
tags = tags,
|
||||
visibility = visibility,
|
||||
workspace_dir = workspace_dir,
|
||||
)
|
||||
71
wails_bun/private/macros.bzl
Normal file
71
wails_bun/private/macros.bzl
Normal file
@@ -0,0 +1,71 @@
|
||||
"""Macros for Bun-backed Wails applications."""
|
||||
|
||||
load("//wails:defs.bzl", "wails_build_assets", "wails_generate_bindings", "wails_run")
|
||||
load(":private/dev_session.bzl", "wails_bun_dev_session")
|
||||
load(":private/frontend_dist.bzl", "wails_bun_frontend_dev", "wails_bun_frontend_dist")
|
||||
|
||||
def wails_bun_app(
|
||||
name,
|
||||
app_binary,
|
||||
frontend_srcs,
|
||||
package_json,
|
||||
node_modules,
|
||||
build_asset_srcs,
|
||||
app_name,
|
||||
binary_name,
|
||||
icon = None,
|
||||
bindings_package_dir = ".",
|
||||
frontend_strip_prefix = "",
|
||||
build_strip_prefix = "",
|
||||
visibility = None):
|
||||
wails_bun_frontend_dist(
|
||||
name = name + "_frontend_dist",
|
||||
srcs = frontend_srcs,
|
||||
node_modules = node_modules,
|
||||
package_json = package_json,
|
||||
strip_prefix = frontend_strip_prefix,
|
||||
visibility = visibility,
|
||||
)
|
||||
|
||||
wails_build_assets(
|
||||
name = name + "_build_assets",
|
||||
srcs = build_asset_srcs,
|
||||
app_name = app_name,
|
||||
binary_name = binary_name,
|
||||
strip_prefix = build_strip_prefix,
|
||||
visibility = visibility,
|
||||
)
|
||||
|
||||
wails_generate_bindings(
|
||||
name = name + "_bindings",
|
||||
out_dir = "frontend/src/lib/bindings",
|
||||
package_dir = bindings_package_dir,
|
||||
visibility = visibility,
|
||||
)
|
||||
|
||||
wails_bun_frontend_dev(
|
||||
name = name + "_frontend_dev",
|
||||
data = frontend_srcs,
|
||||
node_modules = node_modules,
|
||||
package_json = package_json,
|
||||
visibility = visibility,
|
||||
)
|
||||
|
||||
wails_run(
|
||||
name = name + "_run",
|
||||
binary = app_binary,
|
||||
build_assets = name + "_build_assets",
|
||||
icon = icon,
|
||||
visibility = visibility,
|
||||
)
|
||||
|
||||
wails_bun_dev_session(
|
||||
name = name + "_dev",
|
||||
workspace_dir = native.package_name() or ".",
|
||||
frontend_dev = name + "_frontend_dev",
|
||||
app_binary = app_binary,
|
||||
build_assets = name + "_build_assets",
|
||||
bindings_target = name + "_bindings",
|
||||
icon = icon,
|
||||
visibility = visibility,
|
||||
)
|
||||
Reference in New Issue
Block a user