resolvePerClient static method

Map<String, String> resolvePerClient(
  1. List<String> specs,
  2. List<String> clients
)

Resolves --build-version values into a version per client. A plain value (1.4.0+12) applies to every client without its own entry; client=1.4.0+12 targets one client. Clients without either keep the pubspec version (absent from the map).

Implementation

static Map<String, String> resolvePerClient(
    List<String> specs, List<String> clients) {
  String? fallback;
  final perClient = <String, String>{};
  for (final raw in specs.map((s) => s.trim()).where((s) => s.isNotEmpty)) {
    final eq = raw.indexOf('=');
    final client = eq < 0 ? null : raw.substring(0, eq).trim();
    final version = (eq < 0 ? raw : raw.substring(eq + 1)).trim();

    if (!pattern.hasMatch(version)) {
      throw BuildException(
        'Invalid --build-version "$raw".',
        fix: 'Use 1.4.0, 1.4.0+12, or client=1.4.0+12.',
      );
    }
    if (client == null) {
      if (fallback != null) {
        throw BuildException(
          'More than one --build-version without a client name.',
          fix: 'Give one default version, and client=version for the others.',
        );
      }
      fallback = version;
    } else {
      if (!clients.contains(client)) {
        throw BuildException(
          '--build-version names "$client", which is not being built.',
          fix: 'Clients in this build: ${clients.join(', ')}.',
        );
      }
      if (perClient.containsKey(client)) {
        throw BuildException('--build-version given twice for "$client".');
      }
      perClient[client] = version;
    }
  }
  return {
    for (final client in clients)
      if ((perClient[client] ?? fallback) != null)
        client: (perClient[client] ?? fallback)!,
  };
}