Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 11:f33d9ff8b6e8 | 12:de54585a40f1 |
|---|---|
| 1 def _foo_impl(ctx): | 1 def _foo_impl(ctx): |
| 2 pass | 2 out = ctx.actions.declare_file(ctx.label.name) |
| 3 ctx.actions.write( | |
| 4 output = out, | |
| 5 content = "Hello!\n", | |
| 6 ) | |
| 7 return [DefaultInfo(files = depset([out]))] | |
| 3 | 8 |
| 4 foo_binary = rule( | 9 foo_binary = rule( |
| 5 implementation = _foo_impl, | 10 implementation = _foo_impl, |
| 6 ) | 11 ) |
| 7 | 12 |
| 13 | |
| 14 def _bundle_impl(ctx): | |
| 15 """ | |
| 16 bundle binary target into a folder which can later be used to make a post to github easily. | |
| 17 """ | |
| 18 binary_target = ctx.attr.binary | |
| 19 binary = binary_target[DefaultInfo].files.to_list()[0] # First files are binary | |
| 20 runfiles_files = binary_target[DefaultInfo].default_runfiles.files.to_list() | |
| 21 | |
| 22 # Name as output directory | |
| 23 out_dir = ctx.actions.declare_directory(ctx.label.name) | |
| 24 | |
| 25 copy_cmd = [] | |
| 26 copy_cmd.append("mkdir -p {}".format(out_dir.path)) | |
| 27 | |
| 28 for f in runfiles_files: | |
| 29 if f.path == binary.path: | |
| 30 continue | |
| 31 # Remove the first folder (output) and last file (actaul files that needed to be copied) | |
| 32 paths = "/".join(f.path.split("/")[:-1]) | |
| 33 full_path = "{}/{}".format(out_dir.path, paths) | |
| 34 copy_cmd.append("mkdir -p {}".format(full_path)) | |
| 35 copy_cmd.append("cp {} {}".format(f.path, full_path)) | |
| 36 | |
| 37 copy_cmd.append("cp {} {}".format(binary.path, out_dir.path)) | |
| 38 | |
| 39 ctx.actions.run_shell( | |
| 40 inputs = runfiles_files, | |
| 41 outputs = [out_dir], | |
| 42 command = " && ".join(copy_cmd), | |
| 43 progress_message = "Bundling {}".format(ctx.label.name), | |
| 44 ) | |
| 45 | |
| 46 print("[INFO] See {}".format(out_dir.path)) | |
| 47 | |
| 48 return [DefaultInfo(files = depset([out_dir]))] | |
| 49 | |
| 50 bundle = rule( | |
| 51 implementation = _bundle_impl, | |
| 52 attrs = { | |
| 53 "binary": attr.label( | |
| 54 doc = "The cc_binary target to bundle", | |
| 55 providers = [DefaultInfo], | |
| 56 ), | |
| 57 }, | |
| 58 ) | |
| 59 | |
| 60 | |
| 61 def _bun_binary_impl(ctx): | |
| 62 out = ctx.actions.declare_file("bun") | |
| 63 ctx.actions.run_shell( | |
| 64 inputs = ctx.files.srcs, | |
| 65 outputs = [out], | |
| 66 command = """ | |
| 67 mkdir -p {outdir} | |
| 68 unzip -j {src} {inner} -d {outdir} | |
| 69 chmod +x {outdir}/bun | |
| 70 mv {outdir}/bun {out} | |
| 71 """.format( | |
| 72 outdir = out.dirname, | |
| 73 src = ctx.files.srcs[0].path, | |
| 74 inner = "bun-darwin-aarch64/bun", | |
| 75 out = out.path, | |
| 76 ), | |
| 77 ) | |
| 78 return DefaultInfo( | |
| 79 files = depset([out]), | |
| 80 executable = out, | |
| 81 ) | |
| 82 | |
| 83 bun_binary = rule( | |
| 84 implementation = _bun_binary_impl, | |
| 85 attrs = { | |
| 86 "srcs": attr.label_list(allow_files=True), | |
| 87 }, | |
| 88 executable = True, | |
| 89 ) | |
| 90 | |
| 91 def _bun_build_impl(ctx): | |
| 92 out = ctx.actions.declare_file(ctx.label.name + ".js") | |
| 93 inputs = [ctx.file.src] + ctx.files.data | |
| 94 | |
| 95 ctx.actions.run_shell( | |
| 96 inputs = inputs, | |
| 97 outputs = [out], | |
| 98 tools = [ctx.executable._bun] + inputs, | |
| 99 command = """ | |
| 100 cp -r third_party/bun/** . \ | |
| 101 && cp -r playground/** . \ | |
| 102 && {} build {} --outfile {} | |
| 103 """.format( | |
| 104 ctx.executable._bun.path, | |
| 105 ctx.file.src.path.split("/")[-1], | |
| 106 out.path, | |
| 107 ), | |
| 108 progress_message = "Bundling {} with Bun!\n\n".format(ctx.file.src.path), | |
| 109 ) | |
| 110 | |
| 111 return [DefaultInfo(files=depset([out]))] | |
| 112 | |
| 113 bun_build = rule( | |
| 114 implementation = _bun_build_impl, | |
| 115 attrs = { | |
| 116 "src": attr.label(allow_single_file = [".ts", ".tsx", ".js", ".jsx"]), | |
| 117 "_bun": attr.label( | |
| 118 default = Label("//third_party/bun:bun"), | |
| 119 executable = True, | |
| 120 cfg = "exec", | |
| 121 ), | |
| 122 "data": attr.label_list(allow_files=True), | |
| 123 }, | |
| 124 ) | |
| 125 | |
| 126 def _move_files_into_dir_impl(ctx): | |
| 127 srcs = ctx.files.srcs | |
| 128 for src in srcs: | |
| 129 out = ctx.actions.declare_file(ctx.attr.dest + "/" + src.basename) | |
| 130 ctx.actions.symlink( | |
| 131 output = out, | |
| 132 target_file = src, | |
| 133 ) | |
| 134 return [DefaultInfo(files = depset([out]))] | |
| 135 | |
| 136 move_files_into_dir = rule( | |
| 137 implementation = _move_files_into_dir_impl, | |
| 138 attrs = { | |
| 139 "srcs": attr.label_list(allow_files=True), | |
| 140 "dest": attr.string(), | |
| 141 }, | |
| 142 ) | |
| 143 | |
| 144 def _move_to_directory_impl(ctx): | |
| 145 srcs = ctx.files.data | |
| 146 res = [] | |
| 147 for src in srcs: | |
| 148 true = "/".join(src.path.split("/")[2:]) | |
| 149 path = ctx.attr.dest + "/" + true if ctx.attr.dest != "" else true | |
| 150 out = ctx.actions.declare_file( | |
| 151 path | |
| 152 ); | |
| 153 ctx.actions.symlink( | |
| 154 output = out, | |
| 155 target_file = src, | |
| 156 ) | |
| 157 res.append(out) | |
| 158 return [DefaultInfo(files = depset(res))] | |
| 159 | |
| 160 move_to_directory = rule( | |
| 161 implementation = _move_to_directory_impl, | |
| 162 attrs = { | |
| 163 "data": attr.label_list(allow_files=True), | |
| 164 "dest": attr.string(), | |
| 165 }, | |
| 166 ) |