packages feed

cabal-buck2-0.6.0.0: buck2/prelude/user/write_file.bzl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

load("@prelude//decls:common.bzl", "buck")
load("@prelude//decls:core_rules.bzl", "core_args")
load(":rule_spec.bzl", "RuleRegistrationSpec")

_NL = {
    "unix": "\n",
    "windows": "\r\n",
}

def _impl(ctx: AnalysisContext):
    # Figure out the type of newlines we're using.
    newline_type = ctx.attrs.newline
    if newline_type == "auto":
        newline_type = ctx.attrs._auto_newline

    # Join lines with newline.
    newline = _NL[newline_type]
    content = "".join([line + newline for line in ctx.attrs.content])

    output = ctx.actions.write(
        ctx.attrs.out,
        content,
        is_executable = ctx.attrs.is_executable,
        has_content_based_path = ctx.attrs.has_content_based_path,
    )

    providers = [DefaultInfo(default_output = output)]

    if ctx.attrs.is_executable:
        providers.append(RunInfo(cmd_args(output)))

    return providers

registration_spec = RuleRegistrationSpec(
    name = "write_file",
    impl = _impl,
    attrs = buck.labels_arg()
    | buck.contacts_arg()
    | core_args.has_content_based_path_attr()
    | {
        # API based on https://github.com/bazelbuild/bazel-skylib/blob/main/docs/write_file_doc.md.
        "content": attrs.list(attrs.string(), default = []),
        "is_executable": attrs.bool(default = False),
        "newline": attrs.enum(["auto", "unix", "windows"], default = "auto"),
        "out": attrs.string(),
        "_auto_newline": attrs.default_only(
            attrs.string(
                default = select({
                    "DEFAULT": "unix",
                    "config//os:windows": "windows",
                }),
            ),
        ),
    },
)