Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit c0bc7cc

Browse files
[flutter_plugin_tools] Preserve Dart SDK version in all-plugins-app (#5281)
Fixes `all-plugins-app` to preserve the original application's Dart SDK version to avoid changing language feature opt-ins that the template may rely on.
1 parent d07eaff commit c0bc7cc

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

.ci/flutter_master.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2b8333240d38cf72b8e13580e24d01f5a188e26c
1+
329ceaef666ff8cebd4dc36325ab6bfebdc7c8ef

script/tool/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Adds a new `readme-check` command.
44
- Updates `publish-plugin` command documentation.
5+
- Fixes `all-plugins-app` to preserve the original application's Dart SDK
6+
version to avoid changing language feature opt-ins that the template may
7+
rely on.
58

69
## 0.8.2
710

script/tool/lib/src/create_all_plugins_app_command.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,27 @@ class CreateAllPluginsAppCommand extends PluginCommand {
147147
}
148148

149149
Future<void> _genPubspecWithAllPlugins() async {
150+
final RepositoryPackage buildAllApp = RepositoryPackage(appDirectory);
151+
// Read the old pubspec file's Dart SDK version, in order to preserve it
152+
// in the new file. The template sometimes relies on having opted in to
153+
// specific language features via SDK version, so using a different one
154+
// can cause compilation failures.
155+
final Pubspec originalPubspec = buildAllApp.parsePubspec();
156+
const String dartSdkKey = 'sdk';
157+
final VersionConstraint dartSdkConstraint =
158+
originalPubspec.environment?[dartSdkKey] ??
159+
VersionConstraint.compatibleWith(
160+
Version.parse('2.12.0'),
161+
);
162+
150163
final Map<String, PathDependency> pluginDeps =
151164
await _getValidPathDependencies();
152165
final Pubspec pubspec = Pubspec(
153166
'all_plugins',
154167
description: 'Flutter app containing all 1st party plugins.',
155168
version: Version.parse('1.0.0+1'),
156169
environment: <String, VersionConstraint>{
157-
'sdk': VersionConstraint.compatibleWith(
158-
Version.parse('2.12.0'),
159-
),
170+
dartSdkKey: dartSdkConstraint,
160171
},
161172
dependencies: <String, Dependency>{
162173
'flutter': SdkDependency('flutter'),
@@ -166,8 +177,7 @@ class CreateAllPluginsAppCommand extends PluginCommand {
166177
},
167178
dependencyOverrides: pluginDeps,
168179
);
169-
final File pubspecFile = appDirectory.childFile('pubspec.yaml');
170-
pubspecFile.writeAsStringSync(_pubspecToString(pubspec));
180+
buildAllApp.pubspecFile.writeAsStringSync(_pubspecToString(pubspec));
171181
}
172182

173183
Future<Map<String, PathDependency>> _getValidPathDependencies() async {
@@ -212,7 +222,12 @@ dev_dependencies:${_pubspecMapString(pubspec.devDependencies)}
212222
for (final MapEntry<String, dynamic> entry in values.entries) {
213223
buffer.writeln();
214224
if (entry.value is VersionConstraint) {
215-
buffer.write(' ${entry.key}: ${entry.value}');
225+
String value = entry.value.toString();
226+
// Range constraints require quoting.
227+
if (value.startsWith('>') || value.startsWith('<')) {
228+
value = "'$value'";
229+
}
230+
buffer.write(' ${entry.key}: $value');
216231
} else if (entry.value is SdkDependency) {
217232
final SdkDependency dep = entry.value as SdkDependency;
218233
buffer.write(' ${entry.key}: \n sdk: ${dep.sdk}');

script/tool/test/create_all_plugins_app_command_test.dart

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:io' as io;
6+
57
import 'package:args/command_runner.dart';
68
import 'package:file/file.dart';
79
import 'package:file/local.dart';
10+
import 'package:flutter_plugin_tools/src/common/repository_package.dart';
811
import 'package:flutter_plugin_tools/src/create_all_plugins_app_command.dart';
12+
import 'package:platform/platform.dart';
13+
import 'package:pubspec_parse/pubspec_parse.dart';
914
import 'package:test/test.dart';
1015

1116
import 'util.dart';
@@ -76,14 +81,31 @@ void main() {
7681
]));
7782
});
7883

79-
test('pubspec is compatible with null-safe app code', () async {
84+
test('pubspec preserves existing Dart SDK version', () async {
85+
const String baselineProjectName = 'baseline';
86+
final Directory baselineProjectDirectory =
87+
testRoot.childDirectory(baselineProjectName);
88+
io.Process.runSync(
89+
getFlutterCommand(const LocalPlatform()),
90+
<String>[
91+
'create',
92+
'--template=app',
93+
'--project-name=$baselineProjectName',
94+
baselineProjectDirectory.path,
95+
],
96+
);
97+
final Pubspec baselinePubspec =
98+
RepositoryPackage(baselineProjectDirectory).parsePubspec();
99+
80100
createFakePlugin('plugina', packagesDir);
81101

82102
await runCapturingPrint(runner, <String>['all-plugins-app']);
83-
final String pubspec =
84-
command.appDirectory.childFile('pubspec.yaml').readAsStringSync();
103+
final Pubspec generatedPubspec =
104+
RepositoryPackage(command.appDirectory).parsePubspec();
85105

86-
expect(pubspec, contains(RegExp('sdk:\\s*(?:["\']>=|[^])2\\.12\\.')));
106+
const String dartSdkKey = 'sdk';
107+
expect(generatedPubspec.environment?[dartSdkKey],
108+
baselinePubspec.environment?[dartSdkKey]);
87109
});
88110

89111
test('handles --output-dir', () async {

0 commit comments

Comments
 (0)