Skip to content

Commit

Permalink
feat: Implement Doctor versions checking models (#1571)
Browse files Browse the repository at this point in the history
* Add doctor version checking required models

* Remove mapper changes

* Minor fix

* Minor improvement

* Remove unused import

* Improve tests descriptions

* feat: Implement Doctor versions checking models

Co-authored-by: Dmytro Tkachuk <[email protected]>
  • Loading branch information
1 parent 7dcfa97 commit c3401c2
Show file tree
Hide file tree
Showing 5 changed files with 652 additions and 0 deletions.
158 changes: 158 additions & 0 deletions metrics/cli/lib/cli/doctor/models/doctor_target_validation_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

import 'dart:io';

import 'package:cli/cli/doctor/doctor.dart';
import 'package:cli/cli/doctor/models/doctor_validation_conclusion.dart';
import 'package:cli/cli/doctor/strings/doctor_strings.dart';
import 'package:metrics_core/metrics_core.dart';

/// A class that represents the [TargetValidationResult] within the [Doctor]
/// versions checking process.
class DoctorTargetValidationResult<T> extends TargetValidationResult<T> {
/// Creates a new instance of the [DoctorTargetValidationResult]
/// with the given parameters.
const DoctorTargetValidationResult._({
ValidationTarget target,
ValidationConclusion conclusion,
String description,
Map<String, dynamic> details,
Map<String, dynamic> context,
T data,
}) : super(
target: target,
conclusion: conclusion,
description: description,
details: details,
context: context,
data: data,
);

/// Creates a new instance of the [DoctorTargetValidationResult]
/// with the given parameters and
/// [DoctorTargetValidationResult.successful] result.
///
/// Represents a successful doctor target validation result.
factory DoctorTargetValidationResult.successful(
ValidationTarget target,
String recommendedVersion,
) {
return DoctorTargetValidationResult._(
target: target,
conclusion: DoctorValidationConclusion.successful(),
details: {DoctorStrings.version: recommendedVersion},
);
}

/// Creates a new instance of the [DoctorTargetValidationResult]
/// with the given parameters and
/// [DoctorTargetValidationResult.failure] result.
///
/// Represents a failure doctor target validation result.
factory DoctorTargetValidationResult.failure(
ValidationTarget target,
String recommendedVersion,
String installUrl,
dynamic error,
) {
final targetName = target?.name;
final installMessage = DoctorStrings.installMessage(
targetName,
installUrl,
);
final context = {
DoctorStrings.processOutput: _createFailureMessage(error),
DoctorStrings.recommendations: installMessage,
};

return DoctorTargetValidationResult._(
target: target,
conclusion: DoctorValidationConclusion.failure(),
description: DoctorStrings.notInstalled,
details: {DoctorStrings.recommendedVersion: recommendedVersion},
context: context,
);
}

/// Creates a new instance of the [DoctorTargetValidationResult]
/// with the given parameters and
/// [DoctorTargetValidationResult.warning] result.
///
/// Represents a warning doctor target validation result.
factory DoctorTargetValidationResult.warning(
ValidationTarget target,
String currentVersion, {
String recommendedVersion,
String installUrl,
dynamic error,
}) {
final details = _createWarningDetails(currentVersion, recommendedVersion);
final context = _createWarningContext(installUrl, error);

return DoctorTargetValidationResult._(
target: target,
conclusion: DoctorValidationConclusion.warning(),
details: details,
context: context,
);
}

/// Creates warning details map using the given [currentVersion]
/// and [recommendedVersion].
///
/// If the given [recommendedVersion] is not `null`,
/// returns a details map with [DoctorStrings.version] key.
static Map<String, dynamic> _createWarningDetails(
String currentVersion,
String recommendedVersion,
) {
final details = {
DoctorStrings.version: currentVersion,
};

if (recommendedVersion != null) {
details[DoctorStrings.recommendedVersion] = recommendedVersion;
}

return details;
}

/// Creates warning context map using the given [installUrl] and [error].
///
/// If the given [installUrl] is not `null`,
/// returns a context map with [DoctorStrings.warning]
/// and [DoctorStrings.recommendations] keys.
///
/// If the given [error] is not `null`, returns a context map
/// with [DoctorStrings.commandError] key.
static Map<String, dynamic> _createWarningContext(
String installUrl,
dynamic error,
) {
final updateMessage = DoctorStrings.updateMessage(installUrl);

final context = <String, dynamic>{};
if (installUrl != null) {
context[DoctorStrings.warning] = DoctorStrings.versionMismatch;
context[DoctorStrings.recommendations] = updateMessage;
}
if (error != null) {
context[DoctorStrings.commandError] = _createFailureMessage(error);
}

return context;
}

/// Creates the failure message using the given [error].
static String _createFailureMessage(dynamic error) {
if (error is ProcessException) {
final executable = error.executable;
final arguments = error.arguments.fold('', (p, e) => '$p $e');

return '\$ $executable $arguments\n${DoctorStrings.commandNotFound} $executable';
}

return error.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

import 'package:cli/cli/doctor/doctor.dart';
import 'package:metrics_core/metrics_core.dart';

/// A class that represents the [ValidationConclusion] within the [Doctor]
/// versions checking process.
class DoctorValidationConclusion extends ValidationConclusion {
/// Creates a new instance of the [DoctorValidationConclusion]
/// with the given [name] and [indicator].
///
/// The given [name] must not be `null`.
const DoctorValidationConclusion._({
String name,
String indicator,
}) : super(
name: name,
indicator: indicator,
);

/// Creates a new instance of the [DoctorValidationConclusion] that represents
/// the successful conclusion of a validation.
factory DoctorValidationConclusion.successful() {
return const DoctorValidationConclusion._(
name: 'successful',
indicator: '+',
);
}

/// Creates a new instance of the [DoctorValidationConclusion] that represents
/// the failure conclusion of a validation.
factory DoctorValidationConclusion.failure() {
return const DoctorValidationConclusion._(
name: 'failure',
indicator: '-',
);
}

/// Creates a new instance of the [DoctorValidationConclusion] that represents
/// the warning conclusion of a validation.
factory DoctorValidationConclusion.warning() {
return const DoctorValidationConclusion._(
name: 'warning',
indicator: '!',
);
}
}
27 changes: 27 additions & 0 deletions metrics/cli/lib/cli/doctor/strings/doctor_strings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

// ignore_for_file: public_member_api_docs

/// A class that holds strings used within the doctor command.
class DoctorStrings {
static const String notInstalled = 'Not installed';
static const String recommendedVersion = 'recommended version';
static const String version = 'version';
static const String processOutput = 'Process output...';
static const String commandNotFound = 'command not found:';
static const String recommendations = 'Recommendations...';
static const String warning = 'Warning message...';
static const String commandError = 'Command Error Message...';
static const String versionMismatch = 'Versions mismatch does not guarantee '
'the successful deployment! We advise to use the recommended version'
' of the tool.';

static String installMessage(String name, String installUrl) {
return 'Install $name ($installUrl)';
}

static String updateMessage(String installUrl) {
return 'Update version ($installUrl)';
}
}
Loading

0 comments on commit c3401c2

Please sign in to comment.