forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ios analyzer command for universal links (#134155)
ios version of https://github.com/flutter/flutter/pull/131009/files
- Loading branch information
Showing
5 changed files
with
327 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import '../base/logger.dart'; | ||
import '../convert.dart'; | ||
import '../ios/xcodeproj.dart'; | ||
import '../project.dart'; | ||
|
||
/// The type of analysis to perform. | ||
enum IOSAnalyzeOption { | ||
/// Prints out available build variants of the iOS Xcode sub-project. | ||
/// | ||
/// An example output: | ||
/// | ||
/// {"configurations":["Debug","Release","Profile"],"schemes":["Runner"],"targets":["Runner","RunnerTests"]} | ||
listBuildOptions, | ||
|
||
/// Outputs universal link settings of the iOS Xcode sub-project into a file. | ||
/// | ||
/// The file path will be printed after the command is run successfully. | ||
outputUniversalLinkSettings, | ||
} | ||
|
||
/// Analyze the iOS Xcode sub-project of a Flutter project. | ||
/// | ||
/// The [userPath] must be point to a flutter project. | ||
class IOSAnalyze { | ||
IOSAnalyze({ | ||
required this.project, | ||
required this.option, | ||
this.configuration, | ||
this.scheme, | ||
this.target, | ||
required this.logger, | ||
}) : assert(option == IOSAnalyzeOption.listBuildOptions || | ||
(configuration != null && scheme != null && target != null)); | ||
|
||
final FlutterProject project; | ||
final IOSAnalyzeOption option; | ||
final String? configuration; | ||
final String? scheme; | ||
final String? target; | ||
final Logger logger; | ||
|
||
Future<void> analyze() async { | ||
switch (option) { | ||
case IOSAnalyzeOption.listBuildOptions: | ||
final XcodeProjectInfo? info = await project.ios.projectInfo(); | ||
final Map<String, List<String>> result; | ||
if (info == null) { | ||
result = const <String, List<String>>{}; | ||
} else { | ||
result = <String, List<String>>{ | ||
'configurations': info.buildConfigurations, | ||
'schemes': info.schemes, | ||
'targets': info.targets, | ||
}; | ||
} | ||
logger.printStatus(jsonEncode(result)); | ||
case IOSAnalyzeOption.outputUniversalLinkSettings: | ||
await project.ios.outputsUniversalLinkSettings(configuration: configuration!, scheme: scheme!, target: target!); | ||
final String filePath = await project.ios.outputsUniversalLinkSettings(configuration: configuration!, scheme: scheme!, target: target!); | ||
logger.printStatus('result saved in $filePath'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
packages/flutter_tools/test/commands.shard/hermetic/ios_analyze_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:args/command_runner.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:flutter_tools/src/artifacts.dart'; | ||
import 'package:flutter_tools/src/base/file_system.dart'; | ||
import 'package:flutter_tools/src/base/logger.dart'; | ||
import 'package:flutter_tools/src/base/platform.dart'; | ||
import 'package:flutter_tools/src/base/terminal.dart'; | ||
import 'package:flutter_tools/src/cache.dart'; | ||
import 'package:flutter_tools/src/commands/analyze.dart'; | ||
import 'package:flutter_tools/src/commands/ios_analyze.dart'; | ||
import 'package:flutter_tools/src/ios/xcodeproj.dart'; | ||
import 'package:flutter_tools/src/project.dart'; | ||
import 'package:flutter_tools/src/project_validator.dart'; | ||
import 'package:test/fake.dart'; | ||
|
||
import '../../src/common.dart'; | ||
import '../../src/context.dart'; | ||
import '../../src/test_flutter_command_runner.dart'; | ||
|
||
void main() { | ||
group('ios analyze command', () { | ||
late BufferLogger logger; | ||
late FileSystem fileSystem; | ||
late Platform platform; | ||
late FakeProcessManager processManager; | ||
late Terminal terminal; | ||
late AnalyzeCommand command; | ||
late CommandRunner<void> runner; | ||
|
||
setUpAll(() { | ||
Cache.disableLocking(); | ||
}); | ||
|
||
setUp(() { | ||
logger = BufferLogger.test(); | ||
fileSystem = MemoryFileSystem.test(); | ||
platform = FakePlatform(); | ||
processManager = FakeProcessManager.empty(); | ||
terminal = Terminal.test(); | ||
command = AnalyzeCommand( | ||
artifacts: Artifacts.test(), | ||
fileSystem: fileSystem, | ||
logger: logger, | ||
platform: platform, | ||
processManager: processManager, | ||
terminal: terminal, | ||
allProjectValidators: <ProjectValidator>[], | ||
suppressAnalytics: true, | ||
); | ||
runner = createTestCommandRunner(command); | ||
|
||
// Setup repo roots | ||
const String homePath = '/home/user/flutter'; | ||
Cache.flutterRoot = homePath; | ||
for (final String dir in <String>['dev', 'examples', 'packages']) { | ||
fileSystem.directory(homePath).childDirectory(dir).createSync(recursive: true); | ||
} | ||
}); | ||
|
||
testWithoutContext('can output json file', () async { | ||
final MockIosProject ios = MockIosProject(); | ||
final MockFlutterProject project = MockFlutterProject(ios); | ||
const String expectedConfig = 'someConfig'; | ||
const String expectedScheme = 'someScheme'; | ||
const String expectedTarget = 'someConfig'; | ||
const String expectedOutputFile = '/someFile'; | ||
ios.outputFileLocation = expectedOutputFile; | ||
await IOSAnalyze( | ||
project: project, | ||
option: IOSAnalyzeOption.outputUniversalLinkSettings, | ||
configuration: expectedConfig, | ||
scheme: expectedScheme, | ||
target: expectedTarget, | ||
logger: logger, | ||
).analyze(); | ||
expect(logger.statusText, contains(expectedOutputFile)); | ||
expect(ios.outputConfiguration, expectedConfig); | ||
expect(ios.outputScheme, expectedScheme); | ||
expect(ios.outputTarget, expectedTarget); | ||
}); | ||
|
||
testWithoutContext('can list build options', () async { | ||
final MockIosProject ios = MockIosProject(); | ||
final MockFlutterProject project = MockFlutterProject(ios); | ||
const List<String> targets = <String>['target1', 'target2']; | ||
const List<String> configs = <String>['config1', 'config2']; | ||
const List<String> schemes = <String>['scheme1', 'scheme2']; | ||
ios.expectedProjectInfo = XcodeProjectInfo(targets, configs, schemes, logger); | ||
await IOSAnalyze( | ||
project: project, | ||
option: IOSAnalyzeOption.listBuildOptions, | ||
logger: logger, | ||
).analyze(); | ||
final Map<String, Object?> jsonOutput = jsonDecode(logger.statusText) as Map<String, Object?>; | ||
expect(jsonOutput['targets'], unorderedEquals(targets)); | ||
expect(jsonOutput['configurations'], unorderedEquals(configs)); | ||
expect(jsonOutput['schemes'], unorderedEquals(schemes)); | ||
}); | ||
|
||
testUsingContext('throws if provide multiple path', () async { | ||
final Directory tempDir = fileSystem.systemTempDirectory.createTempSync('someTemp'); | ||
final Directory anotherTempDir = fileSystem.systemTempDirectory.createTempSync('another'); | ||
await expectLater( | ||
runner.run(<String>['analyze', '--ios', '--list-build-options', tempDir.path, anotherTempDir.path]), | ||
throwsA( | ||
isA<Exception>().having( | ||
(Exception e) => e.toString(), | ||
'description', | ||
contains('The iOS analyze can process only one directory path'), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
testUsingContext('throws if not enough parameters', () async { | ||
final Directory tempDir = fileSystem.systemTempDirectory.createTempSync('someTemp'); | ||
await expectLater( | ||
runner.run(<String>['analyze', '--ios', '--output-universal-link-settings', tempDir.path]), | ||
throwsA( | ||
isA<Exception>().having( | ||
(Exception e) => e.toString(), | ||
'description', | ||
contains('"--configuration" must be provided'), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
class MockFlutterProject extends Fake implements FlutterProject { | ||
MockFlutterProject(this.ios); | ||
|
||
@override | ||
final IosProject ios; | ||
} | ||
|
||
class MockIosProject extends Fake implements IosProject { | ||
String? outputConfiguration; | ||
String? outputScheme; | ||
String? outputTarget; | ||
late String outputFileLocation; | ||
late XcodeProjectInfo expectedProjectInfo; | ||
|
||
@override | ||
Future<String> outputsUniversalLinkSettings({required String configuration, required String scheme, required String target}) async { | ||
outputConfiguration = configuration; | ||
outputScheme = scheme; | ||
outputTarget = target; | ||
return outputFileLocation; | ||
} | ||
@override | ||
Future<XcodeProjectInfo> projectInfo() async => expectedProjectInfo; | ||
|
||
} |
Oops, something went wrong.