selectDartSdkVersion static method

String selectDartSdkVersion({
  1. required List<String> supportedSdkMinorVersions,
  2. String? commandLineVersion,
  3. String? scloudVersion,
  4. String? toolVersionsVersion,
  5. String? pubspecVersionConstraint,
  6. String? lockVersionConstraint,
})

Selects the Dart SDK minor version to build a deployment with, such as 3.13.

supportedSdkMinorVersions are the minor versions Serverpod Cloud supports, such as ['3.11', '3.12', '3.13'].

The requested version is the first of commandLineVersion, scloudVersion, toolVersionsVersion and pubspecVersionConstraint that holds a value. The rest are ignored. lockVersionConstraint is not a request: whichever version is selected must satisfy it too, since a build resolves against the lockfile.

Every value is a version or a pub-style constraint, such as 3.13, 3.13.2 or >=3.11.0 <4.0.0. A supported version satisfies a value when its minor line overlaps it, which is how a version maps to a build image.

Returns the highest version of supportedSdkMinorVersions that satisfies both the requested version and lockVersionConstraint.

Throws FailureException if a value cannot be parsed, if supportedSdkMinorVersions is empty, or if no supported version satisfies both.

Implementation

static String selectDartSdkVersion({
  required final List<String> supportedSdkMinorVersions,
  final String? commandLineVersion,
  final String? scloudVersion,
  final String? toolVersionsVersion,
  final String? pubspecVersionConstraint,
  final String? lockVersionConstraint,
}) {
  final constraints = [
    ?_firstConstraint([
      (commandLineVersion, '--dart-version flag'),
      (scloudVersion, 'scloud.yaml'),
      (toolVersionsVersion, '.tool-versions'),
      (pubspecVersionConstraint, 'pubspec.yaml'),
    ]),
    ?_constraint(lockVersionConstraint, 'pubspec.lock'),
  ];

  final minorVersions = _minorVersionsOf(supportedSdkMinorVersions);

  for (final minorVersion in minorVersions.reversed) {
    final minorLine = VersionRange(
      min: minorVersion,
      includeMin: true,
      max: Version(minorVersion.major, minorVersion.minor + 1, 0),
    );
    final satisfiesAll = constraints.every(
      (final constraint) => constraint.constraint.allowsAny(minorLine),
    );
    if (satisfiesAll) {
      return '${minorVersion.major}.${minorVersion.minor}';
    }
  }

  throw FailureException(
    error:
        'No Dart SDK version supported by Serverpod Cloud satisfies the '
        'Dart SDK version constraints of the project:\n'
        '${constraints.map((final c) => '  ${c.value} (from ${c.source})').join('\n')}\n'
        'Available Dart SDK versions: ${supportedSdkMinorVersions.join(', ')}.',
    hint:
        'Change the requested Dart SDK version, or the Dart SDK constraints '
        'of the project, so that they agree.',
  );
}