technical_debt__cost.dart 2.29 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 19 20 21

final RegExp todoPattern = new RegExp(r'(?://|#) *TODO');
final RegExp ignorePattern = new RegExp(r'// *ignore:');

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

const String _kBenchmarkKey = 'technical_debt_in_dollars';

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