<commit_before>"""Common utilities for creating and manipulating dart context providers."""

load(
    "//dart/build_rules/common:constants.bzl",
    "analysis_extension",
    "api_summary_extension",
    "dart_filetypes",
    "ALL_PLATFORMS",
)
load(
    "//dart/build_rules/common:ddc.bzl",
    "compute_ddc_output_dir",
)
load(
    "//dart/build_rules/common:label.bzl",
    "label_to_dart_package_name",
)
load(
    "//dart/build_rules/common:path.bzl",
    "filter_files",
)
load(
    "//dart/build_rules/common:_archive.bzl",
    "create_archive",
)

def collect_dart_context(dart_ctx, transitive = True, include_self = True):
  """Collect direct or transitive deps in a map, merging contexts as needed."""
  dart_ctxs = [dart_ctx]
  if transitive:
    dart_ctxs += [d.dart for d in dart_ctx.transitive_deps.values()]
  else:
    dart_ctxs += [d.dart for d in dart_ctx.deps]

  # Optionally, exclude all self-packages.
  if not include_self:
    dart_ctxs = [c for c in dart_ctxs if c.package != dart_ctx.package]

  # Merge Dart context by package.
  ctx_map = {}
  for dc in dart_ctxs:
    if dc.package in ctx_map:
      dc = _merge_dart_context(ctx_map[dc.package], dc)
    ctx_map[dc.package] = dc
  return ctx_map

def make_dart_context(
    ctx,
    package = None,
    lib_root = None,
    enable_summaries = False,
    enable_analysis = False,
    checks = None,
    srcs = None,
    outline_srcs = None,
    data = None,
    deps = None,
    platforms = ALL_PLATFORMS,
    license_files = []):
  """Creates a dart context for a target.

  Args:
    ctx: The target context.
    package: The dart package name.
    lib_root: The lib folder for the package.
    enable_summaries: Whether to generate analyzer summaries.
    enable_analysis: Whether to generate analyzer output.
    checks: A file for post-processed analyzer output.
    srcs: Source files.
    outline_srcs: Source files with possibly incomplete implementations but full
      api outlines. These are intended for use when generating summaries only.
      Defaults to `srcs` if not provided.
    data: Data files.
    deps: Dart library dependencies.
    platforms: List of platforms this dart context supports. Defaults to the intersection of
      all dependency platforms or ["web", "flutter", "vm"] if no dependencies are provided.
    license_files: License files associated with the target.

  Returns:
    The dart context; a struct.
  """
  label = ctx.label
  if not package:
    package = label_to_dart_package_name(label)

  if not lib_root:
    lib_root = ""
    if label.workspace_root.startswith("external/"):
      lib_root += label.workspace_root[len("external/"):] + "/"
    if label.package:
      lib_root += label.package + "/"
    lib_root += "lib/"

  srcs = depset(srcs or [])
  outline_srcs = depset(outline_srcs or srcs)
  dart_srcs = filter_files(dart_filetypes, srcs)
  data = depset(data or [])
  deps = deps or []
  archive_srcs = list(srcs)
  platforms = list(platforms)

  for platform in platforms:
    if platform not in ALL_PLATFORMS:
      fail(("Invalid platforms selection for %s. " % label) +
           ("%s is not an available platform. " % platform) +
           ("Should be one of %s" % ALL_PLATFORMS))


  archive = None
  if archive_srcs:
    archive = create_archive(ctx, archive_srcs, ctx.label.name)

  transitive_srcs, transitive_dart_srcs, transitive_data, transitive_deps, transitive_archives, platforms_intersection = (
      _collect_files(srcs, dart_srcs, data, deps, archive, platforms))

  if len(platforms_intersection) == 0:
    dep_platforms = ""
    for dep in deps:<commit_msg>Error if provided incompatible platforms<commit_after>"""Common utilities for creating and manipulating dart context providers."""

load(
    "//dart/build_rules/common:constants.bzl",
    "analysis_extension",
    "api_summary_extension",
    "dart_filetypes",
    "ALL_PLATFORMS",
)
load(
    "//dart/build_rules/common:ddc.bzl",
    "compute_ddc_output_dir",
)
load(
    "//dart/build_rules/common:label.bzl",
    "label_to_dart_package_name",
)
load(
    "//dart/build_rules/common:path.bzl",
    "filter_files",
)
load(
    "//dart/build_rules/common:_archive.bzl",
    "create_archive",
)

def collect_dart_context(dart_ctx, transitive = True, include_self = True):
  """Collect direct or transitive deps in a map, merging contexts as needed."""
  dart_ctxs = [dart_ctx]
  if transitive:
    dart_ctxs += [d.dart for d in dart_ctx.transitive_deps.values()]
  else:
    dart_ctxs += [d.dart for d in dart_ctx.deps]

  # Optionally, exclude all self-packages.
  if not include_self:
    dart_ctxs = [c for c in dart_ctxs if c.package != dart_ctx.package]

  # Merge Dart context by package.
  ctx_map = {}
  for dc in dart_ctxs:
    if dc.package in ctx_map:
      dc = _merge_dart_context(ctx_map[dc.package], dc)
    ctx_map[dc.package] = dc
  return ctx_map

def make_dart_context(
    ctx,
    package = None,
    lib_root = None,
    enable_summaries = False,
    enable_analysis = False,
    checks = None,
    srcs = None,
    outline_srcs = None,
    data = None,
    deps = None,
    platforms = None,
    license_files = []):
  """Creates a dart context for a target.

  Args:
    ctx: The target context.
    package: The dart package name.
    lib_root: The lib folder for the package.
    enable_summaries: Whether to generate analyzer summaries.
    enable_analysis: Whether to generate analyzer output.
    checks: A file for post-processed analyzer output.
    srcs: Source files.
    outline_srcs: Source files with possibly incomplete implementations but full
      api outlines. These are intended for use when generating summaries only.
      Defaults to `srcs` if not provided.
    data: Data files.
    deps: Dart library dependencies.
    platforms: List of platforms this dart context supports. Defaults to the intersection of
      all dependency platforms or ["web", "flutter", "vm"] if no dependencies are provided.
    license_files: License files associated with the target.

  Returns:
    The dart context; a struct.
  """
  label = ctx.label
  if not package:
    package = label_to_dart_package_name(label)

  if not lib_root:
    lib_root = ""
    if label.workspace_root.startswith("external/"):
      lib_root += label.workspace_root[len("external/"):] + "/"
    if label.package:
      lib_root += label.package + "/"
    lib_root += "lib/"

  srcs = depset(srcs or [])
  outline_srcs = depset(outline_srcs or srcs)
  dart_srcs = filter_files(dart_filetypes, srcs)
  data = depset(data or [])
  deps = deps or []
  archive_srcs = list(srcs)

  archive = None
  if archive_srcs:
    archive = create_archive(ctx, archive_srcs, ctx.label.name)

  explicit_platforms = False
  if not platforms:
    platforms = list(ALL_PLATFORMS)
  else:
    explicit_platforms = True
    for platform in platforms:
      if platform not in ALL_PLATFORMS:
        fail(("Invalid platforms selection for %s. " % label) +
             ("%s is not an available platform. " % platform) +
             ("Should be one of %s" % ALL_PLATFORMS))

  transitive_srcs, transitive_dart_srcs, transitive_data, transitive_deps, transitive_archives, platforms_intersection = (
      _collect_files(srcs, dart_srcs, data, deps, archive, platforms))

  if len(platforms_intersection) == 0 or (explicit_platforms and platforms != platforms_intersection):
    dep_platforms = ""
    for dep in deps:
{'fraction_non_alphanumeric': 0.09946091644204852, 'fraction_numerical': 0.00026954177897574127, 'mean_word_length': 3.3859338061465722, 'pattern_counts': {'":': 0, '<': 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': '2345991', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2433, 'n_words': 690}