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

Commit 6f91122

Browse files
authored
[tool] Update tool to set macOS deployment target to 10.15. (#6605)
1 parent 2eb9a1b commit 6f91122

File tree

2 files changed

+175
-2
lines changed

2 files changed

+175
-2
lines changed

script/tool/lib/src/create_all_plugins_app_command.dart

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@ import 'dart:io' as io;
66

77
import 'package:file/file.dart';
88
import 'package:path/path.dart' as p;
9+
import 'package:platform/platform.dart';
910
import 'package:pub_semver/pub_semver.dart';
1011
import 'package:pubspec_parse/pubspec_parse.dart';
1112

1213
import 'common/core.dart';
1314
import 'common/package_command.dart';
15+
import 'common/process_runner.dart';
1416
import 'common/repository_package.dart';
1517

1618
const String _outputDirectoryFlag = 'output-dir';
1719

20+
const int _exitUpdateMacosPodfileFailed = 3;
21+
const int _exitUpdateMacosPbxprojFailed = 4;
22+
const int _exitGenNativeBuildFilesFailed = 5;
23+
1824
/// A command to create an application that builds all in a single application.
1925
class CreateAllPluginsAppCommand extends PackageCommand {
2026
/// Creates an instance of the builder command.
2127
CreateAllPluginsAppCommand(
2228
Directory packagesDir, {
29+
ProcessRunner processRunner = const ProcessRunner(),
2330
Directory? pluginsRoot,
24-
}) : super(packagesDir) {
31+
Platform platform = const LocalPlatform(),
32+
}) : super(packagesDir, processRunner: processRunner, platform: platform) {
2533
final Directory defaultDir =
2634
pluginsRoot ?? packagesDir.fileSystem.currentDirectory;
2735
argParser.addOption(_outputDirectoryFlag,
@@ -61,10 +69,28 @@ class CreateAllPluginsAppCommand extends PackageCommand {
6169
print('');
6270
}
6371

72+
await _genPubspecWithAllPlugins();
73+
74+
// Run `flutter pub get` to generate all native build files.
75+
// TODO(stuartmorgan): This hangs on Windows for some reason. Since it's
76+
// currently not needed on Windows, skip it there, but we should investigate
77+
// further and/or implement https://github.com/flutter/flutter/issues/93407,
78+
// and remove the need for this conditional.
79+
if (!platform.isWindows) {
80+
if (!await _genNativeBuildFiles()) {
81+
printError(
82+
"Failed to generate native build files via 'flutter pub get'");
83+
throw ToolExit(_exitGenNativeBuildFilesFailed);
84+
}
85+
}
86+
6487
await Future.wait(<Future<void>>[
65-
_genPubspecWithAllPlugins(),
6688
_updateAppGradle(),
6789
_updateManifest(),
90+
_updateMacosPbxproj(),
91+
// This step requires the native file generation triggered by
92+
// flutter pub get above, so can't currently be run on Windows.
93+
if (!platform.isWindows) _updateMacosPodfile(),
6894
]);
6995
}
7096

@@ -259,4 +285,61 @@ dev_dependencies:${_pubspecMapString(pubspec.devDependencies)}
259285

260286
return buffer.toString();
261287
}
288+
289+
Future<bool> _genNativeBuildFiles() async {
290+
final int exitCode = await processRunner.runAndStream(
291+
flutterCommand,
292+
<String>['pub', 'get'],
293+
workingDir: _appDirectory,
294+
);
295+
return exitCode == 0;
296+
}
297+
298+
Future<void> _updateMacosPodfile() async {
299+
/// Only change the macOS deployment target if the host platform is macOS.
300+
/// The Podfile is not generated on other platforms.
301+
if (!platform.isMacOS) {
302+
return;
303+
}
304+
305+
final File podfileFile =
306+
app.platformDirectory(FlutterPlatform.macos).childFile('Podfile');
307+
if (!podfileFile.existsSync()) {
308+
printError("Can't find Podfile for macOS");
309+
throw ToolExit(_exitUpdateMacosPodfileFailed);
310+
}
311+
312+
final StringBuffer newPodfile = StringBuffer();
313+
for (final String line in podfileFile.readAsLinesSync()) {
314+
if (line.contains('platform :osx')) {
315+
// macOS 10.15 is required by in_app_purchase.
316+
newPodfile.writeln("platform :osx, '10.15'");
317+
} else {
318+
newPodfile.writeln(line);
319+
}
320+
}
321+
podfileFile.writeAsStringSync(newPodfile.toString());
322+
}
323+
324+
Future<void> _updateMacosPbxproj() async {
325+
final File pbxprojFile = app
326+
.platformDirectory(FlutterPlatform.macos)
327+
.childDirectory('Runner.xcodeproj')
328+
.childFile('project.pbxproj');
329+
if (!pbxprojFile.existsSync()) {
330+
printError("Can't find project.pbxproj for macOS");
331+
throw ToolExit(_exitUpdateMacosPbxprojFailed);
332+
}
333+
334+
final StringBuffer newPbxproj = StringBuffer();
335+
for (final String line in pbxprojFile.readAsLinesSync()) {
336+
if (line.contains('MACOSX_DEPLOYMENT_TARGET')) {
337+
// macOS 10.15 is required by in_app_purchase.
338+
newPbxproj.writeln(' MACOSX_DEPLOYMENT_TARGET = 10.15;');
339+
} else {
340+
newPbxproj.writeln(line);
341+
}
342+
}
343+
pbxprojFile.writeAsStringSync(newPbxproj.toString());
344+
}
262345
}

script/tool/test/create_all_plugins_app_command_test.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import 'dart:io' as io;
77
import 'package:args/command_runner.dart';
88
import 'package:file/file.dart';
99
import 'package:file/local.dart';
10+
import 'package:flutter_plugin_tools/src/common/core.dart';
1011
import 'package:flutter_plugin_tools/src/create_all_plugins_app_command.dart';
1112
import 'package:platform/platform.dart';
1213
import 'package:test/test.dart';
1314

15+
import 'mocks.dart';
1416
import 'util.dart';
1517

1618
void main() {
@@ -20,6 +22,7 @@ void main() {
2022
late FileSystem fileSystem;
2123
late Directory testRoot;
2224
late Directory packagesDir;
25+
late RecordingProcessRunner processRunner;
2326

2427
setUp(() {
2528
// Since the core of this command is a call to 'flutter create', the test
@@ -28,9 +31,11 @@ void main() {
2831
fileSystem = const LocalFileSystem();
2932
testRoot = fileSystem.systemTempDirectory.createTempSync();
3033
packagesDir = testRoot.childDirectory('packages');
34+
processRunner = RecordingProcessRunner();
3135

3236
command = CreateAllPluginsAppCommand(
3337
packagesDir,
38+
processRunner: processRunner,
3439
pluginsRoot: testRoot,
3540
);
3641
runner = CommandRunner<void>(
@@ -103,6 +108,91 @@ void main() {
103108
baselinePubspec.environment?[dartSdkKey]);
104109
});
105110

111+
test('macOS deployment target is modified in Podfile', () async {
112+
createFakePlugin('plugina', packagesDir);
113+
114+
final File podfileFile = command.packagesDir.parent
115+
.childDirectory('all_plugins')
116+
.childDirectory('macos')
117+
.childFile('Podfile');
118+
podfileFile.createSync(recursive: true);
119+
podfileFile.writeAsStringSync("""
120+
platform :osx, '10.11'
121+
# some other line
122+
""");
123+
124+
await runCapturingPrint(runner, <String>['all-plugins-app']);
125+
final List<String> podfile = command.app
126+
.platformDirectory(FlutterPlatform.macos)
127+
.childFile('Podfile')
128+
.readAsLinesSync();
129+
130+
expect(
131+
podfile,
132+
everyElement((String line) =>
133+
!line.contains('platform :osx') || line.contains("'10.15'")));
134+
},
135+
// Podfile is only generated (and thus only edited) on macOS.
136+
skip: !io.Platform.isMacOS);
137+
138+
test('macOS deployment target is modified in pbxproj', () async {
139+
createFakePlugin('plugina', packagesDir);
140+
141+
await runCapturingPrint(runner, <String>['all-plugins-app']);
142+
final List<String> pbxproj = command.app
143+
.platformDirectory(FlutterPlatform.macos)
144+
.childDirectory('Runner.xcodeproj')
145+
.childFile('project.pbxproj')
146+
.readAsLinesSync();
147+
148+
expect(
149+
pbxproj,
150+
everyElement((String line) =>
151+
!line.contains('MACOSX_DEPLOYMENT_TARGET') ||
152+
line.contains('10.15')));
153+
});
154+
155+
test('calls flutter pub get', () async {
156+
createFakePlugin('plugina', packagesDir);
157+
158+
await runCapturingPrint(runner, <String>['all-plugins-app']);
159+
160+
expect(
161+
processRunner.recordedCalls,
162+
orderedEquals(<ProcessCall>[
163+
ProcessCall(
164+
getFlutterCommand(const LocalPlatform()),
165+
const <String>['pub', 'get'],
166+
testRoot.childDirectory('all_plugins').path),
167+
]));
168+
},
169+
// See comment about Windows in create_all_plugins_app_command.dart
170+
skip: io.Platform.isWindows);
171+
172+
test('fails if flutter pub get fails', () async {
173+
createFakePlugin('plugina', packagesDir);
174+
175+
processRunner.mockProcessesForExecutable[
176+
getFlutterCommand(const LocalPlatform())] = <io.Process>[
177+
MockProcess(exitCode: 1)
178+
];
179+
Error? commandError;
180+
final List<String> output = await runCapturingPrint(
181+
runner, <String>['all-plugins-app'], errorHandler: (Error e) {
182+
commandError = e;
183+
});
184+
185+
expect(commandError, isA<ToolExit>());
186+
expect(
187+
output,
188+
containsAllInOrder(<Matcher>[
189+
contains(
190+
"Failed to generate native build files via 'flutter pub get'"),
191+
]));
192+
},
193+
// See comment about Windows in create_all_plugins_app_command.dart
194+
skip: io.Platform.isWindows);
195+
106196
test('handles --output-dir', () async {
107197
createFakePlugin('plugina', packagesDir);
108198

0 commit comments

Comments
 (0)