Unverified Commit f851e7fa authored by chunhtai's avatar chunhtai Committed by GitHub

Add ios analyzer command for universal links (#134155)

ios version of https://github.com/flutter/flutter/pull/131009/files
parent 2f66f25f
......@@ -11,12 +11,14 @@ import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/terminal.dart';
import '../project.dart';
import '../project_validator.dart';
import '../runner/flutter_command.dart';
import 'analyze_base.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
import 'android_analyze.dart';
import 'ios_analyze.dart';
import 'validate_project.dart';
class AnalyzeCommand extends FlutterCommand {
......@@ -107,6 +109,12 @@ class AnalyzeCommand extends FlutterCommand {
hide: !verboseHelp,
);
argParser.addFlag('ios',
negatable: false,
help: 'Analyze iOS Xcode sub-project. Used by internal tools only.',
hide: !verboseHelp,
);
if (verboseHelp) {
argParser.addSeparator('Usage: flutter analyze --android [arguments]');
}
......@@ -127,8 +135,42 @@ class AnalyzeCommand extends FlutterCommand {
argParser.addOption('build-variant',
help: 'Sets the Android build variant to be analyzed.',
valueHelp: 'use "flutter analyze --android --list-build-variants" to get '
'all available build variants',
valueHelp: 'build variant',
hide: !verboseHelp,
);
if (verboseHelp) {
argParser.addSeparator('Usage: flutter analyze --ios [arguments]');
}
argParser.addFlag('list-build-options',
help: 'Print out a list of available build options for the '
'iOS Xcode sub-project.',
hide: !verboseHelp,
);
argParser.addFlag('output-universal-link-settings',
negatable: false,
help: 'Output a JSON with iOS Xcode universal link settings into a file. '
'The "--configuration", "--scheme", and "--target" must also be set.',
hide: !verboseHelp,
);
argParser.addOption('configuration',
help: 'Sets the iOS build configuration to be analyzed.',
valueHelp: 'configuration',
hide: !verboseHelp,
);
argParser.addOption('scheme',
help: 'Sets the iOS build scheme to be analyzed.',
valueHelp: 'scheme',
hide: !verboseHelp,
);
argParser.addOption('target',
help: 'Sets the iOS build target to be analyzed.',
valueHelp: 'target',
hide: !verboseHelp,
);
}
......@@ -218,6 +260,53 @@ class AnalyzeCommand extends FlutterCommand {
buildVariant: buildVariant,
logger: _logger,
).analyze();
} else if (boolArg('ios')) {
final IOSAnalyzeOption option;
final String? configuration;
final String? target;
final String? scheme;
if (argResults!['list-build-options'] as bool && argResults!['output-universal-link-settings'] as bool) {
throwToolExit('Only one of "--list-build-options" or "--output-universal-link-settings" can be provided');
}
if (argResults!['list-build-options'] as bool) {
option = IOSAnalyzeOption.listBuildOptions;
configuration = null;
target = null;
scheme = null;
} else if (argResults!['output-universal-link-settings'] as bool) {
option = IOSAnalyzeOption.outputUniversalLinkSettings;
configuration = argResults!['configuration'] as String?;
if (configuration == null) {
throwToolExit('"--configuration" must be provided');
}
target = argResults!['target'] as String?;
if (target == null) {
throwToolExit('"--target" must be provided');
}
scheme = argResults!['scheme'] as String?;
if (scheme == null) {
throwToolExit('"--scheme" must be provided');
}
} else {
throwToolExit('No argument is provided to analyze. Use -h to see available commands.');
}
final Set<String> items = findDirectories(argResults!, _fileSystem);
final String directoryPath;
if (items.isEmpty) { // user did not specify any path
directoryPath = _fileSystem.currentDirectory.path;
} else if (items.length > 1) { // if the user sends more than one path
throwToolExit('The iOS analyze can process only one directory path');
} else {
directoryPath = items.first;
}
await IOSAnalyze(
project: FlutterProject.fromDirectory(_fileSystem.directory(directoryPath)),
option: option,
configuration: configuration,
target: target,
scheme: scheme,
logger: _logger,
).analyze();
} else if (boolArg('suggestions')) {
final String directoryPath;
if (boolArg('watch')) {
......
// 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');
}
}
}
......@@ -215,11 +215,11 @@ class IosProject extends XcodeBasedProject {
return parent.isModule || _editableDirectory.existsSync();
}
/// Output universal link related project settings of the iOS sub-project into
/// Outputs universal link related project settings of the iOS sub-project into
/// a json file.
///
/// The return future will resolve to string path to the output file.
Future<String> outputUniversalLinkSettings({
Future<String> outputsUniversalLinkSettings({
required String configuration,
required String scheme,
required String target,
......
// 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;
}
......@@ -751,7 +751,7 @@ apply plugin: 'kotlin-android'
'applinks:example2.com',
],
);
final String outputFilePath = await project.ios.outputUniversalLinkSettings(
final String outputFilePath = await project.ios.outputsUniversalLinkSettings(
target: 'Runner',
scheme: 'Debug',
configuration: 'config',
......@@ -800,7 +800,7 @@ apply plugin: 'kotlin-android'
],
);
final String outputFilePath = await project.ios.outputUniversalLinkSettings(
final String outputFilePath = await project.ios.outputsUniversalLinkSettings(
target: 'Runner',
scheme: 'Debug',
configuration: 'config',
......@@ -836,7 +836,7 @@ apply plugin: 'kotlin-android'
};
xcodeProjectInterpreter.xcodeProjectInfo = XcodeProjectInfo(<String>[], <String>[], <String>['Runner'], logger);
testPlistUtils.setProperty(PlistParser.kCFBundleIdentifierKey, r'$(PRODUCT_BUNDLE_IDENTIFIER)');
final String outputFilePath = await project.ios.outputUniversalLinkSettings(
final String outputFilePath = await project.ios.outputsUniversalLinkSettings(
target: 'Runner',
scheme: 'Debug',
configuration: 'config',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment