|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +import 'package:args/args.dart'; |
| 5 | +import 'package:path/path.dart' as p; |
| 6 | + |
| 7 | +/// What percentage of files should be copied over. Used to take a random |
| 8 | +/// sample of a corpus. |
| 9 | +int _samplePercent = 100; |
| 10 | + |
| 11 | +final _random = Random(); |
| 12 | + |
| 13 | +const _ignoreDirs = [ |
| 14 | + 'pkg/dev_compiler/gen/', |
| 15 | + 'tests/co19/', |
| 16 | + 'third_party/observatory_pub_packages/', |
| 17 | + 'tools/sdks/', |
| 18 | + 'out/', |
| 19 | + 'xcodebuild/', |
| 20 | + |
| 21 | + // Redundant stuff in Flutter. |
| 22 | + 'bin/cache/', |
| 23 | + |
| 24 | + // Redundant packages that are in the SDK. |
| 25 | + 'analyzer-', |
| 26 | + 'compiler_unsupported-', |
| 27 | + 'dev_compiler-', |
| 28 | +]; |
| 29 | + |
| 30 | +// Note! Assumes the Dart SDK and Flutter repos have been cloned in |
| 31 | +// directories next to the corpus repo. Also assumes this script has been run |
| 32 | +// from the root directory of this repo. |
| 33 | +const _corpora = [ |
| 34 | + ('apps', 'download/apps'), |
| 35 | + ('dart', '../../../dart/sdk'), |
| 36 | + ('flutter', '../../../flutter'), |
| 37 | + ('pub', 'download/pub'), |
| 38 | + ('widgets', 'download/widgets'), |
| 39 | +]; |
| 40 | + |
| 41 | +final generatedSuffixes = ['.g.dart', '.freezed.dart']; |
| 42 | + |
| 43 | +void main(List<String> arguments) async { |
| 44 | + var argParser = ArgParser(); |
| 45 | + argParser.addFlag('omit-slow'); |
| 46 | + argParser.addOption('sample', abbr: 's', defaultsTo: '100'); |
| 47 | + |
| 48 | + var argResults = argParser.parse(arguments); |
| 49 | + _samplePercent = int.parse(argResults['sample']); |
| 50 | + |
| 51 | + for (var (name, directory) in _corpora) { |
| 52 | + if (arguments.contains(name)) await copyDir(directory, name); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +Future<void> copyDir(String fromDirectory, String toDirectory) async { |
| 57 | + // If we're taking a random sample, put that in a separate directory. |
| 58 | + if (_samplePercent != 100) { |
| 59 | + toDirectory += '-$_samplePercent'; |
| 60 | + } |
| 61 | + |
| 62 | + var i = 0; |
| 63 | + var inDir = Directory(fromDirectory); |
| 64 | + |
| 65 | + await inDir.list(recursive: true, followLinks: false).listen((entry) async { |
| 66 | + var relative = p.relative(entry.path, from: inDir.path); |
| 67 | + |
| 68 | + if (entry is Link) return; |
| 69 | + if (entry is! File || !entry.path.endsWith('.dart')) return; |
| 70 | + |
| 71 | + // Skip redundant stuff. |
| 72 | + for (var ignore in _ignoreDirs) { |
| 73 | + if (relative.startsWith(ignore)) return; |
| 74 | + } |
| 75 | + |
| 76 | + if (_random.nextInt(100) >= _samplePercent) return; |
| 77 | + |
| 78 | + // If the path is in a subdirectory starting with '.', ignore it. |
| 79 | + var parts = p.split(relative); |
| 80 | + if (parts.any((part) => part.startsWith('.'))) return; |
| 81 | + |
| 82 | + var outPath = p.join('out', toDirectory, relative); |
| 83 | + |
| 84 | + var outDir = Directory(p.dirname(outPath)); |
| 85 | + if (!await outDir.exists()) await outDir.create(recursive: true); |
| 86 | + |
| 87 | + await entry.copy(outPath); |
| 88 | + |
| 89 | + i++; |
| 90 | + if (i % 100 == 0) print(relative); |
| 91 | + }).asFuture(); |
| 92 | +} |
0 commit comments