technical_debt__cost.dart 7.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:flutter_devicelab/framework/framework.dart';
9
import 'package:flutter_devicelab/framework/task_result.dart';
10 11
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;
12 13
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
14

15
// the numbers below are prime, so that the totals don't seem round. :-)
16 17
const double todoCost = 1009.0; // about two average SWE days, in dollars
const double ignoreCost = 2003.0; // four average SWE days, in dollars
18 19
const double pythonCost = 3001.0; // six average SWE days, in dollars
const double skipCost = 2473.0; // 20 hours: 5 to fix the issue we're ignoring, 15 to fix the bugs we missed because the test was off
20
const double ignoreForFileCost = 2477.0; // similar thinking as skipCost
21 22
const double asDynamicCost = 2011.0; // a few days to refactor the code.
const double deprecationCost = 233.0; // a few hours to remove the old code.
23
const double legacyDeprecationCost = 9973.0; // a couple of weeks.
24 25
const double packageNullSafetyMigrationCost = 2017.0; // a few days to migrate package.
const double fileNullSafetyMigrationCost = 257.0; // a few hours to migrate file.
26

27 28 29
final RegExp todoPattern = RegExp(r'(?://|#) *TODO');
final RegExp ignorePattern = RegExp(r'// *ignore:');
final RegExp ignoreForFilePattern = RegExp(r'// *ignore_for_file:');
30 31
final RegExp asDynamicPattern = RegExp(r'\bas dynamic\b');
final RegExp deprecationPattern = RegExp(r'^ *@[dD]eprecated');
32
const Pattern globalsPattern = 'globals.';
33
const String legacyDeprecationPattern = '// flutter_ignore: deprecation_syntax, https';
34 35 36
final RegExp dartVersionPattern = RegExp(r'// *@dart *= *(\d+).(\d+)');

final Version firstNullSafeDartVersion = Version(2, 12, 0);
37

38
Future<double> findCostsForFile(File file) async {
39
  if (path.extension(file.path) == '.py') {
40
    return pythonCost;
41
  }
42 43
  if (path.extension(file.path) != '.dart' &&
      path.extension(file.path) != '.yaml' &&
44
      path.extension(file.path) != '.sh') {
45
    return 0.0;
46
  }
47
  final bool isTest = file.path.endsWith('_test.dart');
48
  final bool isDart = file.path.endsWith('.dart');
49
  double total = 0.0;
50
  for (final String line in await file.readAsLines()) {
51
    if (line.contains(todoPattern)) {
52
      total += todoCost;
53 54
    }
    if (line.contains(ignorePattern)) {
55
      total += ignoreCost;
56 57
    }
    if (line.contains(ignoreForFilePattern)) {
58
      total += ignoreForFileCost;
59 60
    }
    if (!isTest && line.contains(asDynamicPattern)) {
61
      total += asDynamicCost;
62 63
    }
    if (line.contains(deprecationPattern)) {
64
      total += deprecationCost;
65 66
    }
    if (line.contains(legacyDeprecationPattern)) {
67
      total += legacyDeprecationCost;
68 69
    }
    if (isTest && line.contains('skip:') && !line.contains('[intended]')) {
70
      total += skipCost;
71 72
    }
    if (isDart && isOptingOutOfNullSafety(line)) {
73
      total += fileNullSafetyMigrationCost;
74
    }
75
  }
76
  if (path.basename(file.path) == 'pubspec.yaml' && !packageIsNullSafe(file)) {
77
    total += packageNullSafetyMigrationCost;
78
  }
79
  return total;
80 81
}

82
bool isOptingOutOfNullSafety(String line) {
83
  final RegExpMatch? match = dartVersionPattern.firstMatch(line);
84 85 86 87
  if (match == null) {
    return false;
  }
  assert(match.groupCount == 2);
88
  return Version(int.parse(match.group(1)!), int.parse(match.group(2)!), 0) < firstNullSafeDartVersion;
89 90 91 92 93
}

bool packageIsNullSafe(File file) {
  assert(path.basename(file.path) == 'pubspec.yaml');
  final Pubspec pubspec = Pubspec.parse(file.readAsStringSync());
94
  final VersionConstraint? constraint = pubspec.environment == null ? null : pubspec.environment!['sdk'];
95 96 97 98
  final bool hasConstraint = constraint != null && !constraint.isAny && !constraint.isEmpty;
  return hasConstraint &&
      constraint is VersionRange &&
      constraint.min != null &&
99
      Version(constraint.min!.major, constraint.min!.minor, 0) >= firstNullSafeDartVersion;
100 101
}

102
Future<int> findGlobalsForFile(File file) async {
103
  if (path.extension(file.path) != '.dart') {
104
    return 0;
105
  }
106
  int total = 0;
107
  for (final String line in await file.readAsLines()) {
108
    if (line.contains(globalsPattern)) {
109
      total += 1;
110
    }
111 112 113 114
  }
  return total;
}

115 116 117 118 119 120 121
Future<double> findCostsForRepo() async {
  final Process git = await startProcess(
    'git',
    <String>['ls-files', '--full-name', flutterDirectory.path],
    workingDirectory: flutterDirectory.path,
  );
  double total = 0.0;
122
  await for (final String entry in git.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter())) {
123
    total += await findCostsForFile(File(path.join(flutterDirectory.path, entry)));
124
  }
125
  final int gitExitCode = await git.exitCode;
126
  if (gitExitCode != 0) {
127
    throw Exception('git exit with unexpected error code $gitExitCode');
128
  }
129 130 131
  return total;
}

132 133 134 135 136 137 138
Future<int> findGlobalsForTool() async {
  final Process git = await startProcess(
    'git',
    <String>['ls-files', '--full-name', path.join(flutterDirectory.path, 'packages', 'flutter_tools')],
    workingDirectory: flutterDirectory.path,
  );
  int total = 0;
139
  await for (final String entry in git.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter())) {
140
    total += await findGlobalsForFile(File(path.join(flutterDirectory.path, entry)));
141
  }
142
  final int gitExitCode = await git.exitCode;
143
  if (gitExitCode != 0) {
144
    throw Exception('git exit with unexpected error code $gitExitCode');
145
  }
146 147 148 149 150 151 152 153 154
  return total;
}

Future<int> countDependencies() async {
  final List<String> lines = (await evalFlutter(
    'update-packages',
    options: <String>['--transitive-closure'],
  )).split('\n');
  final int count = lines.where((String line) => line.contains('->')).length;
155
  if (count < 2) {
156
    throw Exception('"flutter update-packages --transitive-closure" returned bogus output:\n${lines.join("\n")}');
157
  }
158 159 160 161 162 163 164 165 166
  return count;
}

Future<int> countConsumerDependencies() async {
  final List<String> lines = (await evalFlutter(
    'update-packages',
    options: <String>['--transitive-closure', '--consumer-only'],
  )).split('\n');
  final int count = lines.where((String line) => line.contains('->')).length;
167
  if (count < 2) {
168
    throw Exception('"flutter update-packages --transitive-closure" returned bogus output:\n${lines.join("\n")}');
169
  }
170 171 172
  return count;
}

173 174
const String _kCostBenchmarkKey = 'technical_debt_in_dollars';
const String _kNumberOfDependenciesKey = 'dependencies_count';
175
const String _kNumberOfConsumerDependenciesKey = 'consumer_dependencies_count';
176
const String _kNumberOfFlutterToolGlobals = 'flutter_tool_globals_count';
177

178
Future<void> main() async {
179
  await task(() async {
180
    return TaskResult.success(
181 182
      <String, dynamic>{
        _kCostBenchmarkKey: await findCostsForRepo(),
183 184 185
        _kNumberOfDependenciesKey: await countDependencies(),
        _kNumberOfConsumerDependenciesKey: await countConsumerDependencies(),
        _kNumberOfFlutterToolGlobals: await findGlobalsForTool(),
186 187 188 189
      },
      benchmarkScoreKeys: <String>[
        _kCostBenchmarkKey,
        _kNumberOfDependenciesKey,
190
        _kNumberOfConsumerDependenciesKey,
191
        _kNumberOfFlutterToolGlobals,
192
      ],
193 194 195
    );
  });
}