Mercurial
diff gui_ze/gui_ze.bzl @ 12:de54585a40f1
Adding bun and node modules.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 02 Oct 2025 14:39:48 -0700 |
| parents | 98f96b8032e5 |
| children | 2b9e75756825 |
line wrap: on
line diff
--- a/gui_ze/gui_ze.bzl Tue Sep 30 05:01:34 2025 -0700 +++ b/gui_ze/gui_ze.bzl Thu Oct 02 14:39:48 2025 -0700 @@ -1,7 +1,166 @@ def _foo_impl(ctx): - pass + out = ctx.actions.declare_file(ctx.label.name) + ctx.actions.write( + output = out, + content = "Hello!\n", + ) + return [DefaultInfo(files = depset([out]))] foo_binary = rule( implementation = _foo_impl, ) + +def _bundle_impl(ctx): + """ + bundle binary target into a folder which can later be used to make a post to github easily. + """ + binary_target = ctx.attr.binary + binary = binary_target[DefaultInfo].files.to_list()[0] # First files are binary + runfiles_files = binary_target[DefaultInfo].default_runfiles.files.to_list() + + # Name as output directory + out_dir = ctx.actions.declare_directory(ctx.label.name) + + copy_cmd = [] + copy_cmd.append("mkdir -p {}".format(out_dir.path)) + + for f in runfiles_files: + if f.path == binary.path: + continue + # Remove the first folder (output) and last file (actaul files that needed to be copied) + paths = "/".join(f.path.split("/")[:-1]) + full_path = "{}/{}".format(out_dir.path, paths) + copy_cmd.append("mkdir -p {}".format(full_path)) + copy_cmd.append("cp {} {}".format(f.path, full_path)) + + copy_cmd.append("cp {} {}".format(binary.path, out_dir.path)) + + ctx.actions.run_shell( + inputs = runfiles_files, + outputs = [out_dir], + command = " && ".join(copy_cmd), + progress_message = "Bundling {}".format(ctx.label.name), + ) + + print("[INFO] See {}".format(out_dir.path)) + + return [DefaultInfo(files = depset([out_dir]))] + +bundle = rule( + implementation = _bundle_impl, + attrs = { + "binary": attr.label( + doc = "The cc_binary target to bundle", + providers = [DefaultInfo], + ), + }, +) + + +def _bun_binary_impl(ctx): + out = ctx.actions.declare_file("bun") + ctx.actions.run_shell( + inputs = ctx.files.srcs, + outputs = [out], + command = """ + mkdir -p {outdir} + unzip -j {src} {inner} -d {outdir} + chmod +x {outdir}/bun + mv {outdir}/bun {out} + """.format( + outdir = out.dirname, + src = ctx.files.srcs[0].path, + inner = "bun-darwin-aarch64/bun", + out = out.path, + ), + ) + return DefaultInfo( + files = depset([out]), + executable = out, + ) + +bun_binary = rule( + implementation = _bun_binary_impl, + attrs = { + "srcs": attr.label_list(allow_files=True), + }, + executable = True, +) + +def _bun_build_impl(ctx): + out = ctx.actions.declare_file(ctx.label.name + ".js") + inputs = [ctx.file.src] + ctx.files.data + + ctx.actions.run_shell( + inputs = inputs, + outputs = [out], + tools = [ctx.executable._bun] + inputs, + command = """ + cp -r third_party/bun/** . \ + && cp -r playground/** . \ + && {} build {} --outfile {} + """.format( + ctx.executable._bun.path, + ctx.file.src.path.split("/")[-1], + out.path, + ), + progress_message = "Bundling {} with Bun!\n\n".format(ctx.file.src.path), + ) + + return [DefaultInfo(files=depset([out]))] + +bun_build = rule( + implementation = _bun_build_impl, + attrs = { + "src": attr.label(allow_single_file = [".ts", ".tsx", ".js", ".jsx"]), + "_bun": attr.label( + default = Label("//third_party/bun:bun"), + executable = True, + cfg = "exec", + ), + "data": attr.label_list(allow_files=True), + }, +) + +def _move_files_into_dir_impl(ctx): + srcs = ctx.files.srcs + for src in srcs: + out = ctx.actions.declare_file(ctx.attr.dest + "/" + src.basename) + ctx.actions.symlink( + output = out, + target_file = src, + ) + return [DefaultInfo(files = depset([out]))] + +move_files_into_dir = rule( + implementation = _move_files_into_dir_impl, + attrs = { + "srcs": attr.label_list(allow_files=True), + "dest": attr.string(), + }, +) + +def _move_to_directory_impl(ctx): + srcs = ctx.files.data + res = [] + for src in srcs: + true = "/".join(src.path.split("/")[2:]) + path = ctx.attr.dest + "/" + true if ctx.attr.dest != "" else true + out = ctx.actions.declare_file( + path + ); + ctx.actions.symlink( + output = out, + target_file = src, + ) + res.append(out) + return [DefaultInfo(files = depset(res))] + +move_to_directory = rule( + implementation = _move_to_directory_impl, + attrs = { + "data": attr.label_list(allow_files=True), + "dest": attr.string(), + }, +)