technical_debt__cost.dart 4.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2017 The Chromium Authors. 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:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

// the numbers below are odd, so that the totals don't seem round. :-)
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
const double asDynamicCost = 2003.0; // same as ignoring analyzer warning
20

21 22 23 24
final RegExp todoPattern = RegExp(r'(?://|#) *TODO');
final RegExp ignorePattern = RegExp(r'// *ignore:');
final RegExp ignoreForFilePattern = RegExp(r'// *ignore_for_file:');
final RegExp asDynamicPattern = RegExp(r'as dynamic');
25

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

50 51 52 53 54 55 56
Future<double> findCostsForRepo() async {
  final Process git = await startProcess(
    'git',
    <String>['ls-files', '--full-name', flutterDirectory.path],
    workingDirectory: flutterDirectory.path,
  );
  double total = 0.0;
57
  await for (String entry in git.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()))
58
    total += await findCostsForFile(File(path.join(flutterDirectory.path, entry)));
59 60
  final int gitExitCode = await git.exitCode;
  if (gitExitCode != 0)
61
    throw Exception('git exit with unexpected error code $gitExitCode');
62 63 64 65 66 67 68 69 70 71
  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;
  if (count < 2) // we'll always have flutter and flutter_test, at least...
72
    throw Exception('"flutter update-packages --transitive-closure" returned bogus output:\n${lines.join("\n")}');
73 74 75
  return count;
}

76 77 78 79 80 81 82 83 84 85 86
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;
}

87 88
const String _kCostBenchmarkKey = 'technical_debt_in_dollars';
const String _kNumberOfDependenciesKey = 'dependencies_count';
89
const String _kNumberOfConsumerDependenciesKey = 'consumer_dependencies_count';
90

91
Future<void> main() async {
92
  await task(() async {
93
    return TaskResult.success(
94 95 96
      <String, dynamic>{
        _kCostBenchmarkKey: await findCostsForRepo(),
        _kNumberOfDependenciesKey: await countDependencies(),
97
        _kNumberOfConsumerDependenciesKey: await countConsumerDependencies(),
98 99 100 101
      },
      benchmarkScoreKeys: <String>[
        _kCostBenchmarkKey,
        _kNumberOfDependenciesKey,
102
        _kNumberOfConsumerDependenciesKey,
103
      ],
104 105 106
    );
  });
}