resolveUserDefines function

ConfigResult? resolveUserDefines({
  1. required UserDefineReader read,
  2. required UserDefinePathReader readPath,
  3. required UserDefineBaseReader readBase,
  4. required String packageRoot,
  5. required void addDependency(
    1. Uri uri
    ),
  6. required void log(
    1. String message
    ),
  7. String? targetPlatform,
})

Implementation

ConfigResult? resolveUserDefines({
  required UserDefineReader read,
  required UserDefinePathReader readPath,
  required UserDefineBaseReader readBase,
  required String packageRoot,
  required void Function(Uri uri) addDependency,
  required void Function(String message) log,
  String? targetPlatform,
}) {
  final nested = read('config');
  Object? valueFor(String key) =>
      read(key) ?? (nested is Map ? nested[key] : null);
  final targetOverride = targetPlatform == null
      ? null
      : valueFor(targetPlatform) ??
            (targetPlatform == 'web' ? valueFor('wasm') : null);
  final config = <String, Object?>{};
  Uri? configBaseUri;

  for (final key in _userDefineKeys) {
    final directValue = read(key);
    final hasDirectValue = directValue != null;
    final hasNestedValue = nested is Map && nested.containsKey(key);
    if (!hasDirectValue && !hasNestedValue) continue;

    final value = hasDirectValue ? directValue : (nested as Map)[key];
    if (value == null) continue;
    // Bundle selectors do not constrain an explicitly supplied artifact.
    if (targetOverride == null ||
        !const {'type', 'gpl', 'small'}.contains(key)) {
      _validateUserDefine(key, value);
    }
    config[key] = value;
    configBaseUri ??= readBase(key);

    if (_isPlatformOverrideKey(key) &&
        value is String &&
        !isUriLikeOverride(value)) {
      final resolvedPath = readPath(key);
      if (resolvedPath != null) {
        final localPath = _filePathForCurrentHost(resolvedPath);
        if (localPath != null) {
          config[key] = localPath;
          // A shared workspace legitimately carries overrides for other
          // hosts. Only register a dependency when this host can observe the
          // file; otherwise a missing Mac/Windows path can make a Linux hook
          // appear modified forever while the raw value remains available to
          // the host that owns it.
          if (FileSystemEntity.typeSync(localPath, followLinks: false) !=
              FileSystemEntityType.notFound) {
            addDependency(resolvedPath);
          }
        }
      }
    }
  }

  if (config.isEmpty) return null;
  final configBaseDir = configBaseUri == null
      ? p.normalize(packageRoot)
      : p.normalize(Directory.fromUri(configBaseUri).path);
  log('Using configuration from hooks.user_defines');
  return ConfigResult(config, configBaseDir, source: 'hooks.user_defines');
}