aboutsummaryrefslogtreecommitdiff
path: root/rules/package_info.bzl
blob: c79545f135a858ee7e8b6eb7646284433077483d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules for declaring metadata about a package."""

load(
    "@rules_license//rules:providers.bzl",
    "ExperimentalMetadataInfo",
    "PackageInfo",
)

#
# package_info()
#

def _package_info_impl(ctx):
    provider = PackageInfo(
        # Metadata providers must include a type discriminator. We don't need it
        # to collect the providers, but we do need it to write the JSON. We
        # key on the type field to look up the correct block of code to pull
        # data out and format it. We can't to the lookup on the provider class.
        type = "package_info",
        label = ctx.label,
        package_name = ctx.attr.package_name or ctx.build_file_path.rstrip("/BUILD"),
        package_url = ctx.attr.package_url,
        package_version = ctx.attr.package_version,
    )
    # Experimental alternate design, using a generic 'data' back to hold things
    generic_provider = ExperimentalMetadataInfo(
        type = "package_info_alt",
        label = ctx.label,
        data = {
            "package_name": ctx.attr.package_name or ctx.build_file_path.rstrip("/BUILD"),
            "package_url": ctx.attr.package_url,
            "package_version": ctx.attr.package_version
        }
    )
    return [provider, generic_provider]

_package_info = rule(
    implementation = _package_info_impl,
    attrs = {
        "package_name": attr.string(
            doc = "A human readable name identifying this package." +
                  " This may be used to produce an index of OSS packages used by" +
                  " an applicatation.",
        ),
        "package_url": attr.string(
            doc = "The URL this instance of the package was download from." +
                  " This may be used to produce an index of OSS packages used by" +
                  " an applicatation.",
        ),
        "package_version": attr.string(
            doc = "A human readable version string identifying this package." +
                  " This may be used to produce an index of OSS packages used" +
                  " by an applicatation.  It should be a value that" +
                  " increases over time, rather than a commit hash."
        ),
    },
)

# buildifier: disable=function-docstring-args
def package_info(
        name,
        package_name = None,
        package_url = None,
        package_version = None,
        **kwargs):
    """Wrapper for package_info rule.

    @wraps(_package_info)

    Args:
      name: str target name.
      package_name: str A human readable name identifying this package. This
                    may be used to produce an index of OSS packages used by
                    an application.
      package_url: str The canoncial URL this package distribution was retrieved from.
                       Note that, because of local mirroring, that might not be the 
                       physical URL it was retrieved from.
      package_version: str A human readable name identifying version of this package.
      kwargs: other args. Most are ignored.
    """
    visibility = kwargs.get("visibility") or ["//visibility:public"]
    _package_info(
        name = name,
        package_name = package_name,
        package_url = package_url,
        package_version = package_version,
        applicable_licenses = [],
        visibility = visibility,
        tags = [],
        testonly = 0,
    )