global_count.dart 1.57 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_null_migrated.dart and 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
  countGlobalImports(sources);
  countGlobalImports(tests);
16 17
}

18
final RegExp globalImport = RegExp(r"import.*(?:globals|globals_null_migrated)\.dart' as globals;");
19
final RegExp globalNullUnsafeImport = RegExp("import.*globals.dart' as globals;");
20

21
void countGlobalImports(Directory directory) {
22
  int count = 0;
23
  int nullUnsafeImportCount = 0;
24 25 26 27
  for (final FileSystemEntity file in directory.listSync(recursive: true)) {
    if (!file.path.endsWith('.dart') || file is! File) {
      continue;
    }
28 29
    final List<String> fileLines = file.readAsLinesSync();
    final bool hasImport = fileLines.any((String line) {
30 31 32 33 34
      return globalImport.hasMatch(line);
    });
    if (hasImport) {
      count += 1;
    }
35 36 37 38 39 40
    final bool hasUnsafeImport = fileLines.any((String line) {
      return globalNullUnsafeImport.hasMatch(line);
    });
    if (hasUnsafeImport) {
      nullUnsafeImportCount += 1;
    }
41
  }
42
  print('${path.basename(directory.path)} contains $count libraries with global usage ($nullUnsafeImportCount unsafe)');
43
}