|
| 1 | +## Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. |
| 2 | +## |
| 3 | +## Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +## you may not use this file except in compliance with the License. |
| 5 | +## You may obtain a copy of the License at |
| 6 | +## |
| 7 | +## https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +## |
| 9 | +## Unless required by applicable law or agreed to in writing, software |
| 10 | +## distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +## See the License for the specific language governing permissions and |
| 13 | +## limitations under the License. |
| 14 | +""" |
| 15 | +Implementation of the pkl project package command as a Bazel rule. |
| 16 | +""" |
| 17 | + |
| 18 | +load("@rules_pkl//pkl/private:providers.bzl", "PklMetadataInfo", "PklPackageInfo") |
| 19 | + |
| 20 | +def _pkl_package_impl(ctx): |
| 21 | + pkl_toolchain = ctx.toolchains["@rules_pkl//pkl:toolchain_type"] |
| 22 | + executable = pkl_toolchain.cli[DefaultInfo].files_to_run.executable |
| 23 | + |
| 24 | + project_metadata_info = ctx.attr.project[PklMetadataInfo] |
| 25 | + pkl_project_file = project_metadata_info.pkl_project_file |
| 26 | + pkl_project_deps = project_metadata_info.pkl_project_deps |
| 27 | + pkl_project_name = project_metadata_info.pkl_project_name |
| 28 | + pkl_project_version = project_metadata_info.pkl_project_version |
| 29 | + |
| 30 | + artifact_prefix = "{name}@{version}".format(name = pkl_project_name, version = pkl_project_version) |
| 31 | + |
| 32 | + metadata_file = ctx.actions.declare_file("{prefix}".format(prefix = artifact_prefix)) |
| 33 | + metadata_file_checksum = ctx.actions.declare_file("{prefix}.sha256".format(prefix = artifact_prefix)) |
| 34 | + package_archive = ctx.actions.declare_file("{prefix}.zip".format(prefix = artifact_prefix)) |
| 35 | + package_archive_checksum = ctx.actions.declare_file("{prefix}.zip.sha256".format(prefix = artifact_prefix)) |
| 36 | + |
| 37 | + outputs = [metadata_file, metadata_file_checksum, package_archive, package_archive_checksum] |
| 38 | + |
| 39 | + parts = [ctx.var["BINDIR"]] |
| 40 | + if ctx.label.package: |
| 41 | + parts.append(ctx.label.package) |
| 42 | + output_dir = "/".join(parts) |
| 43 | + |
| 44 | + working_dir = "%s/work" % ctx.label.name |
| 45 | + |
| 46 | + src_symlinks = [] |
| 47 | + |
| 48 | + pkl_project_symlink = ctx.actions.declare_file("{}/{}".format(working_dir, "PklProject")) |
| 49 | + ctx.actions.symlink( |
| 50 | + target_file = pkl_project_file, |
| 51 | + output = pkl_project_symlink, |
| 52 | + ) |
| 53 | + src_symlinks.append(pkl_project_symlink) |
| 54 | + |
| 55 | + pkl_project_deps_symlink = ctx.actions.declare_file("{}/{}".format(working_dir, "PklProject.deps.json")) |
| 56 | + ctx.actions.symlink( |
| 57 | + target_file = pkl_project_deps, |
| 58 | + output = pkl_project_deps_symlink, |
| 59 | + ) |
| 60 | + src_symlinks.append(pkl_project_deps_symlink) |
| 61 | + |
| 62 | + for f in ctx.files.srcs: |
| 63 | + f_path = f.short_path |
| 64 | + bin_path = ctx.bin_dir.path + "/" |
| 65 | + if f_path.startswith(bin_path): |
| 66 | + f_path = f_path.removeprefix(bin_path) |
| 67 | + if ctx.attr.strip_prefix: |
| 68 | + strip_prefix = ctx.attr.strip_prefix + "/" |
| 69 | + if not f_path.startswith(strip_prefix): |
| 70 | + fail("User asked to strip '{}' prefix from srcs, but source file {} does not start with the prefix".format( |
| 71 | + strip_prefix, |
| 72 | + f_path, |
| 73 | + )) |
| 74 | + f_path = f_path.removeprefix(strip_prefix) |
| 75 | + src_symlink = ctx.actions.declare_file("{}/{}".format(working_dir, f_path)) |
| 76 | + ctx.actions.symlink( |
| 77 | + target_file = f, |
| 78 | + output = src_symlink, |
| 79 | + ) |
| 80 | + src_symlinks.append(src_symlink) |
| 81 | + |
| 82 | + args = ctx.actions.args() |
| 83 | + args.add_all(["project", "package", pkl_project_symlink.dirname]) |
| 84 | + args.add_all(["--output-path", "{output_dir}".format(output_dir = output_dir)]) |
| 85 | + args.add_all(ctx.attr.extra_flags) |
| 86 | + |
| 87 | + ctx.actions.run( |
| 88 | + executable = executable, |
| 89 | + outputs = outputs, |
| 90 | + inputs = [pkl_project_file, pkl_project_deps] + src_symlinks, |
| 91 | + arguments = [args], |
| 92 | + ) |
| 93 | + |
| 94 | + return [ |
| 95 | + DefaultInfo(files = depset(outputs)), |
| 96 | + PklPackageInfo( |
| 97 | + metadata_file = metadata_file, |
| 98 | + metadata_file_checksum = metadata_file_checksum, |
| 99 | + package_archive = package_archive, |
| 100 | + package_archive_checksum = package_archive_checksum, |
| 101 | + project_metadata_info = project_metadata_info, |
| 102 | + ), |
| 103 | + ] |
| 104 | + |
| 105 | +pkl_package = rule( |
| 106 | + _pkl_package_impl, |
| 107 | + doc = """ |
| 108 | + Prepares a Pkl project to be published as a package as per the `pkl project package` command, using Bazel. |
| 109 | + You should have at most one `pkl_package` rule per `pkl_project` repo rule. |
| 110 | + """, |
| 111 | + attrs = { |
| 112 | + "project": attr.label( |
| 113 | + providers = [ |
| 114 | + PklMetadataInfo, |
| 115 | + ], |
| 116 | + mandatory = True, |
| 117 | + ), |
| 118 | + "srcs": attr.label_list(allow_files = [".pkl"]), |
| 119 | + "strip_prefix": attr.string(doc = "Strip a directory prefix from the srcs."), |
| 120 | + "extra_flags": attr.string_list(default = []), |
| 121 | + }, |
| 122 | + toolchains = [ |
| 123 | + "@rules_pkl//pkl:toolchain_type", |
| 124 | + ], |
| 125 | +) |
0 commit comments