<commit_before>"""Rules for generating code from Arcs schemas.

Rules are re-exported in build_defs.bzl -- use those instead.
"""

load("//third_party/java/arcs/build_defs:sigh.bzl", "sigh_command")
load(":kotlin.bzl", "arcs_kt_library")

def _output_name(src, suffix = ""):
    """Cleans up the given file name, and replaces the .arcs extension."""

    # For references to files in other build targets, extract the filename:
    #   //src/wasm/tests:manifest.arcs -> manifest.arcs
    if src.startswith("//"):
        src = src.split(":")[1]
    return src.replace(".arcs", "").replace("_", "-").replace(".", "-") + suffix

def _run_schema2wasm(name, src, deps, out, language_name, language_flag, package):
    """Generates source code for the given .arcs schema file.

    Runs sigh schema2wasm to generate the output.
    """

    if not src.endswith(".arcs"):
        fail("src must be a .arcs file")

    if type(deps) == str:
        deps = [deps]

    sigh_command(
        name = name,
        srcs = [src],
        outs = [out],
        deps = deps,
        progress_message = "Generating {} entity schemas".format(language_name),

        # TODO: generated header guard should contain whole workspace-relative
        # path to file.
        sigh_cmd = "schema2wasm " +
                   language_flag + " " +
                   "--outdir $(dirname {OUT}) " +
                   "--outfile $(basename {OUT}) " +
                   "--package " + package + " " +
                   "{SRC}",
    )

# TODO: Specify the appropriate c++ package name, given the new repo structure
def arcs_cc_schema(name, src, deps = [], out = None, package = "arcs"):
    """Generates a C++ header file for the given .arcs schema file."""
    _run_schema2wasm(
        name = name + "_genrule",
        src = src,
        deps = deps,
        out = out or _output_name(src, ".h"),
        language_flag = "--cpp",
        language_name = "C++",
        package = package,
    )

def arcs_kt_schema(name, srcs, deps = [], package = "arcs.sdk"):
    """Generates a Kotlin file for the given .arcs schema file.

    Args:
      name: name of the target to create
      srcs: list of Arcs manifest files to include
      deps: list of imported manifests
      package: package name to use for the generated source code
    """
    outs = []
    for src in srcs:
        out = _output_name(src, "_GeneratedSchemas.kt")
        outs.append(out)
        _run_schema2wasm(
            name = _output_name(src, "_genrule"),
            src = src,
            out = out,
            deps = deps,
            language_flag = "--kotlin",
            language_name = "Kotlin",
            package = package,
        )

    arcs_kt_library(
        name = name,
        srcs = outs,
        deps = [],
    )
<commit_msg>Deps be correct, not robust (#4413)<commit_after>"""Rules for generating code from Arcs schemas.

Rules are re-exported in build_defs.bzl -- use those instead.
"""

load("//third_party/java/arcs/build_defs:sigh.bzl", "sigh_command")
load(":kotlin.bzl", "arcs_kt_library")

def _output_name(src, suffix = ""):
    """Cleans up the given file name, and replaces the .arcs extension."""

    # For references to files in other build targets, extract the filename:
    #   //src/wasm/tests:manifest.arcs -> manifest.arcs
    if src.startswith("//"):
        src = src.split(":")[1]
    return src.replace(".arcs", "").replace("_", "-").replace(".", "-") + suffix

def _run_schema2wasm(name, src, deps, out, language_name, language_flag, package):
    """Generates source code for the given .arcs schema file.

    Runs sigh schema2wasm to generate the output.
    """

    if not src.endswith(".arcs"):
        fail("src must be a .arcs file")

    if type(deps) == str:
        fail("deps must be a list")

    sigh_command(
        name = name,
        srcs = [src],
        outs = [out],
        deps = deps,
        progress_message = "Generating {} entity schemas".format(language_name),

        # TODO: generated header guard should contain whole workspace-relative
        # path to file.
        sigh_cmd = "schema2wasm " +
                   language_flag + " " +
                   "--outdir $(dirname {OUT}) " +
                   "--outfile $(basename {OUT}) " +
                   "--package " + package + " " +
                   "{SRC}",
    )

# TODO: Specify the appropriate c++ package name, given the new repo structure
def arcs_cc_schema(name, src, deps = [], out = None, package = "arcs"):
    """Generates a C++ header file for the given .arcs schema file."""
    _run_schema2wasm(
        name = name + "_genrule",
        src = src,
        deps = deps,
        out = out or _output_name(src, ".h"),
        language_flag = "--cpp",
        language_name = "C++",
        package = package,
    )

def arcs_kt_schema(name, srcs, deps = [], package = "arcs.sdk"):
    """Generates a Kotlin file for the given .arcs schema file.

    Args:
      name: name of the target to create
      srcs: list of Arcs manifest files to include
      deps: list of imported manifests
      package: package name to use for the generated source code
    """
    outs = []
    for src in srcs:
        out = _output_name(src, "_GeneratedSchemas.kt")
        outs.append(out)
        _run_schema2wasm(
            name = _output_name(src, "_genrule"),
            src = src,
            out = out,
            deps = deps,
            language_flag = "--kotlin",
            language_name = "Kotlin",
            package = package,
        )

    arcs_kt_library(
        name = name,
        srcs = outs,
        deps = [],
    )

{'fraction_non_alphanumeric': 0.12872869318181818, 'fraction_numerical': 0.002840909090909091, 'mean_word_length': 2.125971143174251, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7483011', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1713, 'n_words': 522}