global_count.dart 1.25 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter 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:io';
import 'package:path/path.dart' as path;

8
/// Count the number of libraries that import globals.dart in lib and test.
9 10 11 12 13
///
/// This must be run from the flutter_tools project root directory.
void main() {
  final Directory sources = Directory(path.join(Directory.current.path, 'lib'));
  final Directory tests = Directory(path.join(Directory.current.path, 'test'));
14 15 16 17
  final int sourceGlobals = countGlobalImports(sources);
  final int testGlobals = countGlobalImports(tests);
  print('lib/ contains $sourceGlobals libraries with global usage');
  print('test/ contains $testGlobals libraries with global usage');
18 19
}

20
final RegExp globalImport = RegExp("import.*globals.dart' as globals;");
21

22
int countGlobalImports(Directory directory) {
23 24 25 26 27
  int count = 0;
  for (final FileSystemEntity file in directory.listSync(recursive: true)) {
    if (!file.path.endsWith('.dart') || file is! File) {
      continue;
    }
28
    final bool hasImport = file.readAsLinesSync().any((String line) {
29 30 31 32 33 34
      return globalImport.hasMatch(line);
    });
    if (hasImport) {
      count += 1;
    }
  }
35
  return count;
36
}