technical_debt__cost.dart 2.49 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 asDynamicCost = 2003.0; // same as ignoring analyzer warning
19 20 21

final RegExp todoPattern = new RegExp(r'(?://|#) *TODO');
final RegExp ignorePattern = new RegExp(r'// *ignore:');
22
final RegExp asDynamicPattern = new RegExp(r'as dynamic');
23

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

const String _kBenchmarkKey = 'technical_debt_in_dollars';

Future<Null> main() async {
  await task(() async {
50
    final Process git = await startProcess(
51 52 53 54
      'git',
      <String>['ls-files', '--full-name', flutterDirectory.path],
      workingDirectory: flutterDirectory.path,
    );
55
    double total = 0.0;
56
    await for (String entry in git.stdout.transform(utf8.decoder).transform(const LineSplitter()))
57
      total += await findCostsForFile(new File(path.join(flutterDirectory.path, entry)));
58
    final int gitExitCode = await git.exitCode;
59 60 61 62 63 64 65 66
    if (gitExitCode != 0)
      throw new Exception('git exit with unexpected error code $gitExitCode');
    return new TaskResult.success(
      <String, dynamic>{_kBenchmarkKey: total},
      benchmarkScoreKeys: <String>[_kBenchmarkKey],
    );
  });
}