<commit_before>#
# PHASE: write executable
#
# DOCUMENT THIS
#
load(
    "@io_bazel_rules_scala//scala/private:rule_impls.bzl",
    "expand_location",
    "first_non_empty",
    "is_windows",
    "java_bin",
    "runfiles_root",
)
load(
    "@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
    _coverage_replacements_provider = "coverage_replacements_provider",
)

def phase_write_executable_scalatest(ctx, p):
    # jvm_flags passed in on the target override scala_test_jvm_flags passed in on the
    # toolchain
    final_jvm_flags = first_non_empty(
        ctx.attr.jvm_flags,
        ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_test_jvm_flags,
    )
    args = struct(
        rjars = p.coverage_runfiles.rjars,
        jvm_flags = [
            "-DRULES_SCALA_MAIN_WS_NAME=%s" % ctx.workspace_name,
            "-DRULES_SCALA_ARGS_FILE=%s" % p.runfiles.args_file.short_path,
        ] + expand_location(ctx, final_jvm_flags),
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_repl(ctx, p):
    args = struct(
        jvm_flags = ["-Dscala.usejavacp=true"] + ctx.attr.jvm_flags,
        main_class = "scala.tools.nsc.MainGenericRunner",
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_junit_test(ctx, p):
    args = struct(
        jvm_flags = p.jvm_flags + ctx.attr.jvm_flags,
        main_class = "com.google.testing.junit.runner.BazelTestRunner",
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_common(ctx, p):
    return _phase_write_executable_default(ctx, p)

def _phase_write_executable_default(ctx, p, _args = struct()):
    return _phase_write_executable(
        ctx,
        p,
        _args.rjars if hasattr(_args, "rjars") else p.compile.rjars,
        _args.jvm_flags if hasattr(_args, "jvm_flags") else ctx.attr.jvm_flags,
        _args.main_class if hasattr(_args, "main_class") else ctx.attr.main_class,
    )

def _phase_write_executable(
        ctx,
        p,
        rjars,
        jvm_flags,
        main_class):
    executable = p.declare_executable.executable
    wrapper = p.java_wrapper

    if (is_windows(ctx)):
        return _write_executable_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper)
    else:
        return _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper)

def _write_executable_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper):
    # TODO: tests coverage support for Windows
    classpath = ";".join(
        [("external/%s" % (j.short_path[3:]) if j.short_path.startswith("../") else j.short_path) for j in rjars.to_list()],
    )
    jvm_flags_str = ";".join(jvm_flags)
    java_for_exe = str(ctx.attr._java_runtime[java_common.JavaRuntimeInfo].java_executable_exec_path)

    cpfile = ctx.actions.declare_file("%s.classpath" % ctx.label.name)
    ctx.actions.write(cpfile, classpath)

    ctx.actions.run(
        outputs = [executable],
        inputs = [cpfile],
        executable = ctx.attr._exe.files_to_run.executable,
        arguments = [executable.path, ctx.workspace_name, java_for_exe, main_class, cpfile.path, jvm_flags_str],
        mnemonic = "ExeLauncher",
        progress_message = "Creating exe launcher",
    )
    return []

def _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper):
    template = ctx.attr._java_stub_template.files.to_list()[0]

    jvm_flags = " ".join(
        [ctx.expand_location(f, ctx.attr.data) for f in jvm_flags],
    )

    javabin = "export REAL_EXTERNAL_JAVA_BIN=${JAVABIN};JAVABIN=%s/%s" % (
        runfiles_root(ctx),
        wrapper.short_path,
    )

    if ctx.configuration.coverage_enabled:
        classpath = ctx.configuration.host_path_separator.join(
            ["${RUNPATH}%s" % (j.short_path) for j in rjars.to_list() + ctx.files._jacocorunner],
        )
        jacoco_metadata_file = ctx.actions.declare_file(
            "%s.jacoco_metadata.txt" % ctx.attr.name,
            sibling = executable,
        )
        ctx.actions.write(jacoco_metadata_file, "\n".join([
            jar.short_path.replace("../", "external/")
            for jar in rjars.to_list()
        ]))
        ctx.actions.expand_template(
            template = template,
            output = executable,
            substitutions = {
                "%classpath%": "\"%s\"" % classpath,
                "%javabin%": javabin,
                "%jarbin%": _jar_path_based_on_java_bin(ctx),
                "%jvm_flags%": jvm_flags,<commit_msg>Rollback usage of `use_jacoco` attribute (#1171)<commit_after>#
# PHASE: write executable
#
# DOCUMENT THIS
#
load(
    "@io_bazel_rules_scala//scala/private:rule_impls.bzl",
    "expand_location",
    "first_non_empty",
    "is_windows",
    "java_bin",
    "runfiles_root",
)
load(
    "@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
    _coverage_replacements_provider = "coverage_replacements_provider",
)

def phase_write_executable_scalatest(ctx, p):
    # jvm_flags passed in on the target override scala_test_jvm_flags passed in on the
    # toolchain
    final_jvm_flags = first_non_empty(
        ctx.attr.jvm_flags,
        ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_test_jvm_flags,
    )
    args = struct(
        rjars = p.coverage_runfiles.rjars,
        jvm_flags = [
            "-DRULES_SCALA_MAIN_WS_NAME=%s" % ctx.workspace_name,
            "-DRULES_SCALA_ARGS_FILE=%s" % p.runfiles.args_file.short_path,
        ] + expand_location(ctx, final_jvm_flags),
        use_jacoco = ctx.configuration.coverage_enabled,
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_repl(ctx, p):
    args = struct(
        jvm_flags = ["-Dscala.usejavacp=true"] + ctx.attr.jvm_flags,
        main_class = "scala.tools.nsc.MainGenericRunner",
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_junit_test(ctx, p):
    args = struct(
        jvm_flags = p.jvm_flags + ctx.attr.jvm_flags,
        main_class = "com.google.testing.junit.runner.BazelTestRunner",
    )
    return _phase_write_executable_default(ctx, p, args)

def phase_write_executable_common(ctx, p):
    return _phase_write_executable_default(ctx, p)

def _phase_write_executable_default(ctx, p, _args = struct()):
    return _phase_write_executable(
        ctx,
        p,
        _args.rjars if hasattr(_args, "rjars") else p.compile.rjars,
        _args.jvm_flags if hasattr(_args, "jvm_flags") else ctx.attr.jvm_flags,
        _args.use_jacoco if hasattr(_args, "use_jacoco") else False,
        _args.main_class if hasattr(_args, "main_class") else ctx.attr.main_class,
    )

def _phase_write_executable(
        ctx,
        p,
        rjars,
        jvm_flags,
        use_jacoco,
        main_class):
    executable = p.declare_executable.executable
    wrapper = p.java_wrapper

    if (is_windows(ctx)):
        return _write_executable_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper, use_jacoco)
    else:
        return _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper, use_jacoco)

def _write_executable_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper, use_jacoco):
    # NOTE: `use_jacoco` is currently ignored on Windows.
    # TODO: tests coverage support for Windows
    classpath = ";".join(
        [("external/%s" % (j.short_path[3:]) if j.short_path.startswith("../") else j.short_path) for j in rjars.to_list()],
    )
    jvm_flags_str = ";".join(jvm_flags)
    java_for_exe = str(ctx.attr._java_runtime[java_common.JavaRuntimeInfo].java_executable_exec_path)

    cpfile = ctx.actions.declare_file("%s.classpath" % ctx.label.name)
    ctx.actions.write(cpfile, classpath)

    ctx.actions.run(
        outputs = [executable],
        inputs = [cpfile],
        executable = ctx.attr._exe.files_to_run.executable,
        arguments = [executable.path, ctx.workspace_name, java_for_exe, main_class, cpfile.path, jvm_flags_str],
        mnemonic = "ExeLauncher",
        progress_message = "Creating exe launcher",
    )
    return []

def _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper, use_jacoco):
    template = ctx.attr._java_stub_template.files.to_list()[0]

    jvm_flags = " ".join(
        [ctx.expand_location(f, ctx.attr.data) for f in jvm_flags],
    )

    javabin = "export REAL_EXTERNAL_JAVA_BIN=${JAVABIN};JAVABIN=%s/%s" % (
        runfiles_root(ctx),
        wrapper.short_path,
    )

    if use_jacoco and ctx.configuration.coverage_enabled:
        classpath = ctx.configuration.host_path_separator.join(
            ["${RUNPATH}%s" % (j.short_path) for j in rjars.to_list() + ctx.files._jacocorunner],
        )
        jacoco_metadata_file = ctx.actions.declare_file(
            "%s.jacoco_metadata.txt" % ctx.attr.name,
            sibling = executable,
        )
        ctx.actions.write(jacoco_metadata_file, "\n".join([
            jar.short_path.replace("../", "external/")
            for jar in rjars.to_list()
        ]))
        ctx.actions.expand_template(
            template = template,
            output = executable,
            substitutions = {
                "%classpath%": "\"%s\"" % classpath,
                "%javabin%": javabin,
                "%jarbin%": _jar_path_based_on_java_bin(ctx),
                "%jvm_flags%": jvm_flags,
{'fraction_non_alphanumeric': 0.11430972893465013, 'fraction_numerical': 0.0008405127127547804, 'mean_word_length': 3.110103626943005, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, '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': '2266430', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3302, 'n_words': 542}