[flutter_tools] General info project validator (#103653)
This commit is contained in:
parent
680bc17cde
commit
336aa267f9
@ -69,6 +69,8 @@ class FlutterManifest {
|
||||
/// A map representation of the `flutter` section in the `pubspec.yaml` file.
|
||||
Map<String, Object?> _flutterDescriptor = <String, Object?>{};
|
||||
|
||||
Map<String, Object?> get flutterDescriptor => _flutterDescriptor;
|
||||
|
||||
/// True if the `pubspec.yaml` file does not exist.
|
||||
bool get isEmpty => _descriptor.isEmpty;
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'flutter_manifest.dart';
|
||||
import 'project.dart';
|
||||
import 'project_validator_result.dart';
|
||||
|
||||
@ -11,7 +12,100 @@ abstract class ProjectValidator {
|
||||
/// Can return more than one result in case a file/command have a lot of info to share to the user
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project);
|
||||
/// new ProjectValidators should be added here for the ValidateProjectCommand to run
|
||||
static const List <ProjectValidator> allProjectValidators = <ProjectValidator>[
|
||||
// TODO(jasguerrero): add validators
|
||||
static List <ProjectValidator> allProjectValidators = <ProjectValidator>[
|
||||
GeneralInfoProjectValidator(),
|
||||
];
|
||||
}
|
||||
|
||||
/// Validator run for all platforms that extract information from the pubspec.yaml.
|
||||
///
|
||||
/// Specific info from different platforms should be written in their own ProjectValidator.
|
||||
class GeneralInfoProjectValidator extends ProjectValidator{
|
||||
@override
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project) async {
|
||||
final FlutterManifest flutterManifest = project.manifest;
|
||||
final List<ProjectValidatorResult> result = <ProjectValidatorResult>[];
|
||||
final ProjectValidatorResult appNameValidatorResult = _getAppNameResult(flutterManifest);
|
||||
result.add(appNameValidatorResult);
|
||||
final String supportedPlatforms = _getSupportedPlatforms(project);
|
||||
if (supportedPlatforms.isEmpty) {
|
||||
return result;
|
||||
}
|
||||
final ProjectValidatorResult supportedPlatformsResult = ProjectValidatorResult(
|
||||
name: 'Supported Platforms',
|
||||
value: supportedPlatforms,
|
||||
status: StatusProjectValidator.success
|
||||
);
|
||||
final ProjectValidatorResult isFlutterPackage = _isFlutterPackageValidatorResult(flutterManifest);
|
||||
result.addAll(<ProjectValidatorResult>[supportedPlatformsResult, isFlutterPackage]);
|
||||
if (flutterManifest.flutterDescriptor.isNotEmpty) {
|
||||
result.add(_materialDesignResult(flutterManifest));
|
||||
result.add(_pluginValidatorResult(flutterManifest));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ProjectValidatorResult _getAppNameResult(FlutterManifest flutterManifest) {
|
||||
final String appName = flutterManifest.appName;
|
||||
const String name = 'App Name';
|
||||
if (appName.isEmpty) {
|
||||
return const ProjectValidatorResult(
|
||||
name: name,
|
||||
value: 'name not found',
|
||||
status: StatusProjectValidator.error
|
||||
);
|
||||
}
|
||||
return ProjectValidatorResult(
|
||||
name: name,
|
||||
value: appName,
|
||||
status: StatusProjectValidator.success
|
||||
);
|
||||
}
|
||||
|
||||
ProjectValidatorResult _isFlutterPackageValidatorResult(FlutterManifest flutterManifest) {
|
||||
final String value;
|
||||
final StatusProjectValidator status;
|
||||
if (flutterManifest.flutterDescriptor.isNotEmpty) {
|
||||
value = 'yes';
|
||||
status = StatusProjectValidator.success;
|
||||
} else {
|
||||
value = 'no';
|
||||
status = StatusProjectValidator.warning;
|
||||
}
|
||||
|
||||
return ProjectValidatorResult(
|
||||
name: 'Is Flutter Package',
|
||||
value: value,
|
||||
status: status
|
||||
);
|
||||
}
|
||||
|
||||
ProjectValidatorResult _materialDesignResult(FlutterManifest flutterManifest) {
|
||||
return ProjectValidatorResult(
|
||||
name: 'Uses Material Design',
|
||||
value: flutterManifest.usesMaterialDesign? 'yes' : 'no',
|
||||
status: StatusProjectValidator.success
|
||||
);
|
||||
}
|
||||
|
||||
String _getSupportedPlatforms(FlutterProject project) {
|
||||
return project.getSupportedPlatforms().map((SupportedPlatform platform) => platform.name).join(', ');
|
||||
}
|
||||
|
||||
ProjectValidatorResult _pluginValidatorResult(FlutterManifest flutterManifest) {
|
||||
return ProjectValidatorResult(
|
||||
name: 'Is Plugin',
|
||||
value: flutterManifest.isPlugin? 'yes' : 'no',
|
||||
status: StatusProjectValidator.success
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool supportsProject(FlutterProject project) {
|
||||
// this validator will run for any type of project
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
String get title => 'General Info';
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import '../../src/test_flutter_command_runner.dart';
|
||||
|
||||
class ProjectValidatorDummy extends ProjectValidator {
|
||||
@override
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project) async{
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
|
||||
return <ProjectValidatorResult>[
|
||||
const ProjectValidatorResult(name: 'pass', value: 'value', status: StatusProjectValidator.success),
|
||||
const ProjectValidatorResult(name: 'fail', value: 'my error', status: StatusProjectValidator.error),
|
||||
@ -37,7 +37,7 @@ class ProjectValidatorDummy extends ProjectValidator {
|
||||
|
||||
class ProjectValidatorSecondDummy extends ProjectValidator {
|
||||
@override
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project) async{
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
|
||||
return <ProjectValidatorResult>[
|
||||
const ProjectValidatorResult(name: 'second', value: 'pass', status: StatusProjectValidator.success),
|
||||
const ProjectValidatorResult(name: 'other fail', value: 'second fail', status: StatusProjectValidator.error),
|
||||
@ -55,7 +55,7 @@ class ProjectValidatorSecondDummy extends ProjectValidator {
|
||||
|
||||
class ProjectValidatorCrash extends ProjectValidator {
|
||||
@override
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project) async{
|
||||
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
|
||||
throw Exception('my exception');
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
// 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.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/commands/validate_project.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/project_validator.dart';
|
||||
|
||||
import '../src/context.dart';
|
||||
import '../src/test_flutter_command_runner.dart';
|
||||
|
||||
void main() {
|
||||
FileSystem fileSystem;
|
||||
|
||||
group('analyze project command', () {
|
||||
|
||||
setUp(() {
|
||||
fileSystem = globals.localFileSystem;
|
||||
});
|
||||
|
||||
testUsingContext('General Info Project Validator', () async {
|
||||
final BufferLogger loggerTest = BufferLogger.test();
|
||||
final ValidateProjectCommand command = ValidateProjectCommand(
|
||||
fileSystem: fileSystem,
|
||||
logger: loggerTest,
|
||||
allProjectValidators: <ProjectValidator>[GeneralInfoProjectValidator()]
|
||||
);
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
|
||||
await runner.run(<String>['validate-project', '../../dev/integration_tests/flutter_gallery']);
|
||||
|
||||
const String expected = '\n'
|
||||
'┌────────────────────────────────────────────────────────────────────────────┐\n'
|
||||
'│ General Info │\n'
|
||||
'│ [✓] App Name: flutter_gallery │\n'
|
||||
'│ [✓] Supported Platforms: android, ios, web, macos, linux, windows, fuchsia │\n'
|
||||
'│ [✓] Is Flutter Package: yes │\n'
|
||||
'│ [✓] Uses Material Design: yes │\n'
|
||||
'│ [✓] Is Plugin: no │\n'
|
||||
'└────────────────────────────────────────────────────────────────────────────┘\n';
|
||||
|
||||
expect(loggerTest.statusText, contains(expected));
|
||||
});
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user