blob: eb8f046056b5d5fd506ca2bdea0dda1233b7663b [file] [log] [blame]
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. 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:io' show File, Platform;
import 'package:analysis_server_client/protocol.dart';
import 'package:path/path.dart' as path;
/// Read pubspec.yaml and return the version in that file.
String get pubspecVersion {
String dir = path.dirname(Platform.script.toFilePath());
File pubspec = new File(path.join(dir, '..', 'pubspec.yaml'));
List<String> lines = pubspec.readAsLinesSync();
if (lines[0] != 'name: dartfix') {
throw 'Expected dartfix pubspec in: ${pubspec.path}';
}
String version;
if (lines[1].startsWith('version:')) {
version = lines[1].substring(8).trim();
}
if (version == null || version.isEmpty) {
throw 'Failed to find dartfix pubspec version in ${pubspec.path}';
}
return version;
}
int compareSuggestions(DartFixSuggestion s1, DartFixSuggestion s2) {
int result = s1.description.compareTo(s2.description);
if (result != 0) {
return result;
}
return (s2.location?.offset ?? 0) - (s1.location?.offset ?? 0);
}
/// Return the analysis_server executable by proceeding upward
/// until finding the Dart SDK repository root then returning
/// the analysis_server executable within the repository.
/// Return `null` if it cannot be found.
String findServerPath() {
String pathname = Platform.script.toFilePath();
while (true) {
String parent = path.dirname(pathname);
if (parent.length >= pathname.length) {
return null;
}
String serverPath =
path.join(parent, 'pkg', 'analysis_server', 'bin', 'server.dart');
if (new File(serverPath).existsSync()) {
return serverPath;
}
pathname = parent;
}
}
bool shouldShowError(AnalysisError error) {
// Only show diagnostics that will affect the fixes.
return error.type.name != 'HINT' &&
error.type.name != 'LINT' &&
error.type.name != 'TODO' &&
// TODO(danrubel): Rather than checking the error.code with
// specific strings, add something to the error indicating that
// it will be automatically fixed by edit.dartfix.
error.code != 'wrong_number_of_type_arguments_constructor';
}
String toSentenceFragment(String message) {
return message.endsWith('.')
? message.substring(0, message.length - 1)
: message;
}