Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/flutter_tools/lib/src/build_system/targets/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import 'package:package_config/package_config.dart';

import '../../artifacts.dart';
import '../../base/build.dart';
import '../../base/common.dart';
import '../../base/file_system.dart';
import '../../base/io.dart';
import '../../build_info.dart';
import '../../compile.dart';
import '../../dart/package_map.dart';
import '../../devfs.dart';
import '../../globals.dart' as globals show platform, xcode;
import '../../globals.dart' as globals show xcode;
import '../../project.dart';
import '../../runner/flutter_command.dart';
import '../build_system.dart';
import '../depfile.dart';
import '../exceptions.dart';
Expand Down Expand Up @@ -310,15 +308,17 @@ class KernelSnapshot extends Target {
if (flavor == null) {
return;
}
if (globals.platform.environment[kAppFlavor] != null) {
throwToolExit('$kAppFlavor is used by the framework and cannot be set in the environment.');
}
if (dartDefines.any((String define) => define.startsWith(kAppFlavor))) {
throwToolExit(
'$kAppFlavor is used by the framework and cannot be '
'set using --${FlutterOptions.kDartDefinesOption} or --${FlutterOptions.kDartDefineFromFileOption}',
);
}

// It is possible there is a flavor already in dartDefines, from another
// part of the build process, but this should take precedence as it happens
// last (xcodebuild execution).
//
// See https://github.com/flutter/flutter/issues/169598.

// If the flavor is already in the dart defines, remove it.
dartDefines.removeWhere((String define) => define.startsWith(kAppFlavor));

// Then, add it to the end.
dartDefines.add('$kAppFlavor=$flavor');
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,18 @@ abstract class FlutterCommand extends Command<void> {
final String? cliFlavor = argParser.options.containsKey('flavor') ? stringArg('flavor') : null;
final String? flavor = cliFlavor ?? defaultFlavor;

if (globals.platform.environment[kAppFlavor] != null) {
throwToolExit('$kAppFlavor is used by the framework and cannot be set in the environment.');
}
if (dartDefines.any((String define) => define.startsWith(kAppFlavor))) {
throwToolExit(
'$kAppFlavor is used by the framework and cannot be '
'set using --${FlutterOptions.kDartDefinesOption} or --${FlutterOptions.kDartDefineFromFileOption}',
);
}
if (flavor != null) {
dartDefines.add('$kAppFlavor=$flavor');
}
_addFlutterVersionToDartDefines(globals.flutterVersion, dartDefines);

return BuildInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:flutter_tools/src/build_system/exceptions.dart';
import 'package:flutter_tools/src/build_system/targets/common.dart';
import 'package:flutter_tools/src/build_system/targets/ios.dart';
import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:test/fake.dart';

Expand Down Expand Up @@ -440,69 +441,76 @@ void main() {
);

testUsingContext(
"tool exits when $kAppFlavor is already set in user's environment",
'KernelSnapshot sets flavor in dartDefines from Xcode build configuration if ios app',
() async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
final Future<void> buildResult = const KernelSnapshot().build(
androidEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.android)
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kFlavor] = 'strawberry'
..defines[kTrackWidgetCreation] = 'false',
final String build = iosEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
platform: TargetPlatform.ios,
mode: BuildMode.debug,
);

expect(
buildResult,
throwsToolExit(
message: '$kAppFlavor is used by the framework and cannot be set in the environment.',
fileSystem.directory('/ios/Runner.xcodeproj').createSync(recursive: true);
processManager.addCommands(<FakeCommand>[
FakeCommand(
command: <String>[
artifacts.getArtifactPath(Artifact.engineDartAotRuntime),
artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
'--sdk-root',
'$flutterPatchedSdkPath/',
'--target=flutter',
'--no-print-incremental-dependencies',
'-D$kAppFlavor=chocolate',
...buildModeOptions(BuildMode.debug, <String>[]),
'--no-link-platform',
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'--depfile',
'$build/kernel_snapshot_program.d',
'--incremental',
'--initialize-from-dill',
'$build/app.dill',
'--verbosity=error',
'file:///lib/main.dart',
],
stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n',
),
);
},
overrides: <Type, Generator>{
Platform: () => FakePlatform(environment: <String, String>{kAppFlavor: 'I was already set'}),
},
);
]);

testUsingContext(
'tool exits when $kAppFlavor is set in --dart-define or --dart-define-from-file',
() async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
final Future<void> buildResult = const KernelSnapshot().build(
androidEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.android)
await const KernelSnapshot().build(
iosEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.ios)
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kFlavor] = 'strawberry'
..defines[kDartDefines] = encodeDartDefines(<String>[kAppFlavor, 'strawberry'])
..defines[kXcodeConfiguration] = 'Debug-chocolate'
..defines[kTrackWidgetCreation] = 'false',
);

expect(
buildResult,
throwsToolExit(
message:
'$kAppFlavor is used by the framework and cannot be set using --dart-define or --dart-define-from-file',
),
);
expect(processManager, hasNoRemainingExpectations);
},
overrides: <Type, Generator>{
XcodeProjectInterpreter:
() => FakeXcodeProjectInterpreter(schemes: <String>['Runner', 'chocolate']),
},
);

testUsingContext(
'KernelSnapshot sets flavor in dartDefines from Xcode build configuration if ios app',
'KernelSnapshot sets flavor in dartDefines from Xcode build configuration if macos app',
() async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
final String build = iosEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
platform: TargetPlatform.ios,
platform: TargetPlatform.darwin,
mode: BuildMode.debug,
);
fileSystem.directory('/ios/Runner.xcodeproj').createSync(recursive: true);
fileSystem.directory('/macos/Runner.xcodeproj').createSync(recursive: true);
processManager.addCommands(<FakeCommand>[
FakeCommand(
command: <String>[
Expand All @@ -514,7 +522,6 @@ void main() {
'--no-print-incremental-dependencies',
'-D$kAppFlavor=chocolate',
...buildModeOptions(BuildMode.debug, <String>[]),
'--no-link-platform',
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
Expand All @@ -533,7 +540,7 @@ void main() {

await const KernelSnapshot().build(
iosEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.ios)
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.darwin)
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kFlavor] = 'strawberry'
..defines[kXcodeConfiguration] = 'Debug-chocolate'
Expand All @@ -549,7 +556,7 @@ void main() {
);

testUsingContext(
'KernelSnapshot sets flavor in dartDefines from Xcode build configuration if macos app',
'KernelSnapshot does not add kAppFlavor twice to Dart defines',
() async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
Expand All @@ -560,7 +567,6 @@ void main() {
platform: TargetPlatform.darwin,
mode: BuildMode.debug,
);
fileSystem.directory('/macos/Runner.xcodeproj').createSync(recursive: true);
processManager.addCommands(<FakeCommand>[
FakeCommand(
command: <String>[
Expand All @@ -570,7 +576,7 @@ void main() {
'$flutterPatchedSdkPath/',
'--target=flutter',
'--no-print-incremental-dependencies',
'-D$kAppFlavor=chocolate',
'-D$kAppFlavor=strawberry',
...buildModeOptions(BuildMode.debug, <String>[]),
'--packages',
'/.dart_tool/package_config.json',
Expand All @@ -592,16 +598,17 @@ void main() {
iosEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.darwin)
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kDartDefines] = base64Encode(utf8.encode('FLUTTER_APP_FLAVOR=vanilla'))
..defines[kFlavor] = 'strawberry'
..defines[kXcodeConfiguration] = 'Debug-chocolate'
..defines[kTrackWidgetCreation] = 'false',
);

expect(processManager, hasNoRemainingExpectations);
},
overrides: <Type, Generator>{
XcodeProjectInterpreter:
() => FakeXcodeProjectInterpreter(schemes: <String>['Runner', 'chocolate']),
Platform: () => macPlatform,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,98 @@ flutter:
);
});

testUsingContext(
"tool exits when $kAppFlavor is already set in user's environemnt",
() async {
final CommandRunner<void> runner = createTestCommandRunner(
_TestRunCommandThatOnlyValidates(),
);
expect(
runner.run(<String>['run', '--no-pub', '--no-hot']),
throwsToolExit(
message: '$kAppFlavor is used by the framework and cannot be set in the environment.',
),
);
},
overrides: <Type, Generator>{
DeviceManager:
() => FakeDeviceManager()..attachedDevices = <Device>[FakeDevice('name', 'id')],
FileSystem: () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('lib/main.dart').createSync(recursive: true);
fileSystem.file('pubspec.yaml').createSync();
return fileSystem;
},
ProcessManager: FakeProcessManager.empty,
Platform: () => FakePlatform()..environment = <String, String>{kAppFlavor: 'AlreadySet'},
},
);

testUsingContext(
'tool exits when $kAppFlavor is set in --dart-define',
() async {
final CommandRunner<void> runner = createTestCommandRunner(
_TestRunCommandThatOnlyValidates(),
);
expect(
runner.run(<String>[
'run',
'--dart-define=$kAppFlavor=AlreadySet',
'--no-pub',
'--no-hot',
]),
throwsToolExit(
message: '$kAppFlavor is used by the framework and cannot be set using --dart-define',
),
);
},
overrides: <Type, Generator>{
DeviceManager:
() => FakeDeviceManager()..attachedDevices = <Device>[FakeDevice('name', 'id')],
FileSystem: () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('lib/main.dart').createSync(recursive: true);
fileSystem.file('pubspec.yaml').createSync();
return fileSystem;
},
ProcessManager: FakeProcessManager.empty,
},
);

testUsingContext(
'tool exits when $kAppFlavor is set in --dart-define-from-file',
() async {
final CommandRunner<void> runner = createTestCommandRunner(
_TestRunCommandThatOnlyValidates(),
);
expect(
runner.run(<String>[
'run',
'--dart-define-from-file=config.json',
'--no-pub',
'--no-hot',
]),
throwsToolExit(
message: '$kAppFlavor is used by the framework and cannot be set using --dart-define',
),
);
},
overrides: <Type, Generator>{
DeviceManager:
() => FakeDeviceManager()..attachedDevices = <Device>[FakeDevice('name', 'id')],
FileSystem: () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('lib/main.dart').createSync(recursive: true);
fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('config.json')
..createSync()
..writeAsStringSync('{"$kAppFlavor": "AlreadySet"}');
return fileSystem;
},
ProcessManager: FakeProcessManager.empty,
},
);

group('Flutter version', () {
for (final String dartDefine in FlutterCommand.flutterVersionDartDefines) {
testUsingContext(
Expand Down
Loading