Update SDK constraint in templates of the dart tool to enable
new languge features in 2.14


Change-Id: I731cebcb394ba17cd3a46ac92ae74cc6bf307de0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/209916
Reviewed-by: Ben Konyi <bkonyi@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Commit-Queue: Michael Thomsen <mit@google.com>
diff --git a/pkg/dartdev/lib/src/sdk.dart b/pkg/dartdev/lib/src/sdk.dart
index 426f810..833df09 100644
--- a/pkg/dartdev/lib/src/sdk.dart
+++ b/pkg/dartdev/lib/src/sdk.dart
@@ -39,8 +39,11 @@
 /// A utility class for finding and referencing paths within the Dart SDK.
 class Sdk {
   final String sdkPath;
+  final String version;
 
-  Sdk() : sdkPath = _computeSdkPath;
+  Sdk()
+      : sdkPath = _computeSdkPath,
+        version = Runtime.runtime.version;
 
   // Assume that we want to use the same Dart executable that we used to spawn
   // DartDev. We should be able to run programs with out/ReleaseX64/dart even
@@ -97,25 +100,20 @@
   static Runtime runtime = Runtime._();
 
   // Match "2.10.0-edge.0b2da6e7 (be) ...".
-  static RegExp channelRegex = RegExp(r'.* \(([\d\w]+)\) .*');
+  static final RegExp _channelRegex = RegExp(r'.* \(([\d\w]+)\) .*');
 
-  String _channel;
-
-  Runtime._() {
-    _parseVersion();
-  }
+  /// The SDK's version number (x.y.z-a.b.channel).
+  final String version;
 
   /// The SDK's release channel (`be`, `dev`, `beta`, `stable`).
-  String get channel => _channel;
+  final String channel;
 
-  /// Return whether the SDK is from the stable release channel.
-  bool get stableChannel => channel == 'stable';
+  Runtime._()
+      : version = _computeVersion(Platform.version),
+        channel = _computeChannel(Platform.version);
 
-  void _parseVersion() {
-    final version = Platform.version;
-    final match = channelRegex.firstMatch(version);
-    if (match != null) {
-      _channel = match.group(1);
-    }
-  }
+  static String _computeVersion(String version) =>
+      version.substring(0, version.indexOf(' '));
+  static String _computeChannel(String version) =>
+      _channelRegex.firstMatch(version)?.group(1);
 }
diff --git a/pkg/dartdev/lib/src/templates/common.dart b/pkg/dartdev/lib/src/templates/common.dart
index 2aac572..ac53756 100644
--- a/pkg/dartdev/lib/src/templates/common.dart
+++ b/pkg/dartdev/lib/src/templates/common.dart
@@ -2,7 +2,13 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-final String gitignore = '''
+import '../sdk.dart';
+
+String get sdkConstraint => '''
+  sdk: '>=${sdk.version} <3.0.0'
+''';
+
+const String gitignore = '''
 # Files and directories created by pub.
 .dart_tool/
 .packages
@@ -11,7 +17,7 @@
 build/
 ''';
 
-final String analysisOptions = '''
+const String analysisOptions = '''
 # This file configures the static analysis results for your project (errors,
 # warnings, and lints).
 #
@@ -44,7 +50,7 @@
 # https://dart.dev/guides/language/analysis-options
 ''';
 
-final String changelog = '''
+const String changelog = '''
 ## 1.0.0
 
 - Initial version.
diff --git a/pkg/dartdev/lib/src/templates/console_full.dart b/pkg/dartdev/lib/src/templates/console_full.dart
index 5797c9a..553ceeb 100644
--- a/pkg/dartdev/lib/src/templates/console_full.dart
+++ b/pkg/dartdev/lib/src/templates/console_full.dart
@@ -38,7 +38,7 @@
 # homepage: https://www.example.com
 
 environment:
-  sdk: '>=2.12.0 <3.0.0'
+${common.sdkConstraint}
 
 # dependencies:
 #   path: ^1.8.0
diff --git a/pkg/dartdev/lib/src/templates/console_simple.dart b/pkg/dartdev/lib/src/templates/console_simple.dart
index 376249b..5c6d381 100644
--- a/pkg/dartdev/lib/src/templates/console_simple.dart
+++ b/pkg/dartdev/lib/src/templates/console_simple.dart
@@ -36,7 +36,7 @@
 # homepage: https://www.example.com
 
 environment:
-  sdk: '>=2.12.0 <3.0.0'
+${common.sdkConstraint}
 
 # dependencies:
 #   path: ^1.8.0
diff --git a/pkg/dartdev/lib/src/templates/package_simple.dart b/pkg/dartdev/lib/src/templates/package_simple.dart
index efe98f9..591f110 100644
--- a/pkg/dartdev/lib/src/templates/package_simple.dart
+++ b/pkg/dartdev/lib/src/templates/package_simple.dart
@@ -55,7 +55,7 @@
 # homepage: https://www.example.com
 
 environment:
-  sdk: '>=2.12.0 <3.0.0'
+${common.sdkConstraint}
 
 # dependencies:
 #   path: ^1.8.0
diff --git a/pkg/dartdev/lib/src/templates/server_shelf.dart b/pkg/dartdev/lib/src/templates/server_shelf.dart
index 45873bc..ed6b730 100644
--- a/pkg/dartdev/lib/src/templates/server_shelf.dart
+++ b/pkg/dartdev/lib/src/templates/server_shelf.dart
@@ -42,7 +42,7 @@
 # homepage: https://www.example.com
 
 environment:
-  sdk: '>=2.12.0 <3.0.0'
+${common.sdkConstraint}
 
 dependencies:
   args: ^2.0.0
diff --git a/pkg/dartdev/lib/src/templates/web_simple.dart b/pkg/dartdev/lib/src/templates/web_simple.dart
index 4bceae1..2dde7de 100644
--- a/pkg/dartdev/lib/src/templates/web_simple.dart
+++ b/pkg/dartdev/lib/src/templates/web_simple.dart
@@ -42,7 +42,7 @@
 # homepage: https://www.example.com
 
 environment:
-  sdk: '>=2.12.0 <3.0.0'
+${common.sdkConstraint}
 
 # dependencies:
 #   path: ^1.7.0