technical_debt__cost.dart 5.68 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 12
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

13
// the numbers below are prime, so that the totals don't seem round. :-)
14 15
const double todoCost = 1009.0; // about two average SWE days, in dollars
const double ignoreCost = 2003.0; // four average SWE days, in dollars
16 17
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
18
const double ignoreForFileCost = 2477.0; // similar thinking as skipCost
19 20
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.
21
const double legacyDeprecationCost = 9973.0; // a couple of weeks.
22

23 24 25
final RegExp todoPattern = RegExp(r'(?://|#) *TODO');
final RegExp ignorePattern = RegExp(r'// *ignore:');
final RegExp ignoreForFilePattern = RegExp(r'// *ignore_for_file:');
26 27
final RegExp asDynamicPattern = RegExp(r'\bas dynamic\b');
final RegExp deprecationPattern = RegExp(r'^ *@[dD]eprecated');
28
const Pattern globalsPattern = 'globals.';
29
const String legacyDeprecationPattern = '// ignore: flutter_deprecation_syntax, https';
30

31
Future<double> findCostsForFile(File file) async {
32
  if (path.extension(file.path) == '.py')
33
    return pythonCost;
34 35 36
  if (path.extension(file.path) != '.dart' &&
      path.extension(file.path) != '.yaml' &&
      path.extension(file.path) != '.sh')
37
    return 0.0;
38
  final bool isTest = file.path.endsWith('_test.dart');
39
  double total = 0.0;
40
  for (final String line in await file.readAsLines()) {
41
    if (line.contains(todoPattern))
42
      total += todoCost;
43
    if (line.contains(ignorePattern))
44
      total += ignoreCost;
45 46
    if (line.contains(ignoreForFilePattern))
      total += ignoreForFileCost;
47
    if (!isTest && line.contains(asDynamicPattern))
48
      total += asDynamicCost;
49 50
    if (line.contains(deprecationPattern))
      total += deprecationCost;
51 52
    if (line.contains(legacyDeprecationPattern))
      total += legacyDeprecationCost;
53 54
    if (isTest && line.contains('skip:'))
      total += skipCost;
55
  }
56
  return total;
57 58
}

59 60 61 62
Future<int> findGlobalsForFile(File file) async {
  if (path.extension(file.path) != '.dart')
    return 0;
  int total = 0;
63
  for (final String line in await file.readAsLines()) {
64 65 66 67 68 69
    if (line.contains(globalsPattern))
      total += 1;
  }
  return total;
}

70 71 72 73 74 75 76
Future<double> findCostsForRepo() async {
  final Process git = await startProcess(
    'git',
    <String>['ls-files', '--full-name', flutterDirectory.path],
    workingDirectory: flutterDirectory.path,
  );
  double total = 0.0;
77
  await for (final String entry in git.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()))
78
    total += await findCostsForFile(File(path.join(flutterDirectory.path, entry)));
79 80
  final int gitExitCode = await git.exitCode;
  if (gitExitCode != 0)
81
    throw Exception('git exit with unexpected error code $gitExitCode');
82 83 84
  return total;
}

85 86 87 88 89 90 91
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;
92
  await for (final String entry in git.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()))
93 94 95 96 97 98 99
    total += await findGlobalsForFile(File(path.join(flutterDirectory.path, entry)));
  final int gitExitCode = await git.exitCode;
  if (gitExitCode != 0)
    throw Exception('git exit with unexpected error code $gitExitCode');
  return total;
}

100 101 102 103 104 105 106
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;
  if (count < 2) // we'll always have flutter and flutter_test, at least...
107
    throw Exception('"flutter update-packages --transitive-closure" returned bogus output:\n${lines.join("\n")}');
108 109 110
  return count;
}

111 112 113 114 115 116 117 118 119 120 121
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;
  if (count < 2) // we'll always have flutter and flutter_test, at least...
    throw Exception('"flutter update-packages --transitive-closure" returned bogus output:\n${lines.join("\n")}');
  return count;
}

122 123
const String _kCostBenchmarkKey = 'technical_debt_in_dollars';
const String _kNumberOfDependenciesKey = 'dependencies_count';
124
const String _kNumberOfConsumerDependenciesKey = 'consumer_dependencies_count';
125
const String _kNumberOfFlutterToolGlobals = 'flutter_tool_globals_count';
126

127
Future<void> main() async {
128
  await task(() async {
129
    return TaskResult.success(
130 131 132
      <String, dynamic>{
        _kCostBenchmarkKey: await findCostsForRepo(),
        _kNumberOfDependenciesKey: await countDependencies(),
133
        _kNumberOfConsumerDependenciesKey: await countConsumerDependencies(),
134
        _kNumberOfFlutterToolGlobals: await findGlobalsForTool(),
135 136 137 138
      },
      benchmarkScoreKeys: <String>[
        _kCostBenchmarkKey,
        _kNumberOfDependenciesKey,
139
        _kNumberOfConsumerDependenciesKey,
140
        _kNumberOfFlutterToolGlobals,
141
      ],
142 143 144
    );
  });
}