Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Hans Muller | 74c3e74 | 2016-05-09 11:00:54 -0700 | [diff] [blame] | 5 | /// Make `n` copies of flutter_gallery. |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 6 | |
| 7 | import 'dart:io'; |
| 8 | |
| 9 | import 'package:args/args.dart'; |
| 10 | import 'package:path/path.dart' as path; |
| 11 | |
| 12 | /// If no `copies` param is passed in, we scale the generated app up to 60k lines. |
| 13 | const int kTargetLineCount = 60 * 1024; |
| 14 | |
| 15 | void main(List<String> args) { |
| 16 | // If we're run from the `tools` dir, set the cwd to the repo root. |
| 17 | if (path.basename(Directory.current.path) == 'tools') |
| 18 | Directory.current = Directory.current.parent.parent; |
| 19 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 20 | final ArgParser argParser = new ArgParser(); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 21 | // ../mega_gallery? dev/benchmarks/mega_gallery? |
| 22 | argParser.addOption('out', defaultsTo: _normalize('dev/benchmarks/mega_gallery')); |
| 23 | argParser.addOption('copies'); |
| 24 | argParser.addFlag('delete', negatable: false); |
| 25 | argParser.addFlag('help', abbr: 'h', negatable: false); |
| 26 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 27 | final ArgResults results = argParser.parse(args); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 28 | |
| 29 | if (results['help']) { |
Hans Muller | 74c3e74 | 2016-05-09 11:00:54 -0700 | [diff] [blame] | 30 | print('Generate n copies of flutter_gallery.\n'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 31 | print('usage: dart mega_gallery.dart <options>'); |
| 32 | print(argParser.usage); |
| 33 | exit(0); |
| 34 | } |
| 35 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 36 | final Directory source = new Directory(_normalize('examples/flutter_gallery')); |
| 37 | final Directory out = new Directory(_normalize(results['out'])); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 38 | |
| 39 | if (results['delete']) { |
| 40 | if (out.existsSync()) { |
| 41 | print('Deleting ${out.path}'); |
| 42 | out.deleteSync(recursive: true); |
| 43 | } |
| 44 | |
| 45 | exit(0); |
| 46 | } |
| 47 | |
| 48 | int copies; |
| 49 | if (!results.wasParsed('copies')) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 50 | final SourceStats stats = getStatsFor(_dir(source, 'lib')); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 51 | copies = (kTargetLineCount / stats.lines).round(); |
| 52 | } else { |
| 53 | copies = int.parse(results['copies']); |
| 54 | } |
| 55 | |
Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 56 | print('Making $copies copies of flutter_gallery.'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 57 | print(''); |
Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 58 | print('Stats:'); |
| 59 | print(' packages/flutter : ${getStatsFor(new Directory("packages/flutter"))}'); |
| 60 | print(' examples/flutter_gallery : ${getStatsFor(new Directory("examples/flutter_gallery"))}'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 61 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 62 | final Directory lib = _dir(out, 'lib'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 63 | if (lib.existsSync()) |
| 64 | lib.deleteSync(recursive: true); |
| 65 | |
| 66 | // Copy everything that's not a symlink, dot directory, or build/. |
| 67 | _copy(source, out); |
| 68 | |
| 69 | // Make n - 1 copies. |
| 70 | for (int i = 1; i < copies; i++) |
| 71 | _copyGallery(out, i); |
| 72 | |
| 73 | // Create a new entry-point. |
| 74 | _createEntry(_file(out, 'lib/main.dart'), copies); |
| 75 | |
| 76 | // Update the pubspec. |
| 77 | String pubspec = _file(out, 'pubspec.yaml').readAsStringSync(); |
| 78 | pubspec = pubspec.replaceAll('../../packages/flutter', '../../../packages/flutter'); |
| 79 | _file(out, 'pubspec.yaml').writeAsStringSync(pubspec); |
| 80 | |
| 81 | _file(out, '.dartignore').writeAsStringSync(''); |
| 82 | |
| 83 | // Count source lines and number of files; tell how to run it. |
Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 84 | print(' ${path.relative(results["out"])} : ${getStatsFor(out)}'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies. |
| 88 | void _createEntry(File mainFile, int copies) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 89 | final StringBuffer imports = new StringBuffer(); |
| 90 | final StringBuffer importRefs = new StringBuffer(); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 91 | |
| 92 | for (int i = 1; i < copies; i++) { |
| 93 | imports.writeln("import 'gallery_$i/main.dart' as main_$i;"); |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 94 | importRefs.writeln(' main_$i.main;'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 97 | final String contents = ''' |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 98 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 99 | // Use of this source code is governed by a BSD-style license that can be |
| 100 | // found in the LICENSE file. |
| 101 | |
| 102 | import 'package:flutter/widgets.dart'; |
| 103 | |
| 104 | import 'gallery/app.dart'; |
| 105 | ${imports.toString().trim()} |
| 106 | |
| 107 | void main() { |
| 108 | // Make sure the imports are not marked as unused. |
| 109 | ${importRefs.toString().trim()} |
| 110 | |
Phil Quitslund | f21abb6 | 2017-05-22 10:01:22 -0700 | [diff] [blame] | 111 | runApp(const GalleryApp()); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 112 | } |
| 113 | '''; |
| 114 | |
| 115 | mainFile.writeAsStringSync(contents); |
| 116 | } |
| 117 | |
| 118 | void _copyGallery(Directory galleryDir, int index) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 119 | final Directory lib = _dir(galleryDir, 'lib'); |
| 120 | final Directory dest = _dir(lib, 'gallery_$index'); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 121 | dest.createSync(); |
| 122 | |
| 123 | // Copy demo/, gallery/, and main.dart. |
| 124 | _copy(_dir(lib, 'demo'), _dir(dest, 'demo')); |
| 125 | _copy(_dir(lib, 'gallery'), _dir(dest, 'gallery')); |
| 126 | _file(dest, 'main.dart').writeAsBytesSync(_file(lib, 'main.dart').readAsBytesSync()); |
| 127 | } |
| 128 | |
| 129 | void _copy(Directory source, Directory target) { |
| 130 | if (!target.existsSync()) |
| 131 | target.createSync(); |
| 132 | |
| 133 | for (FileSystemEntity entity in source.listSync(followLinks: false)) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 134 | final String name = path.basename(entity.path); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 135 | |
| 136 | if (entity is Directory) { |
| 137 | if (name == 'build' || name.startsWith('.')) |
| 138 | continue; |
| 139 | _copy(entity, new Directory(path.join(target.path, name))); |
| 140 | } else if (entity is File) { |
| 141 | if (name == '.packages' || name == 'pubspec.lock') |
| 142 | continue; |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 143 | final File dest = new File(path.join(target.path, name)); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 144 | dest.writeAsBytesSync(entity.readAsBytesSync()); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | Directory _dir(Directory parent, String name) => new Directory(path.join(parent.path, name)); |
| 150 | File _file(Directory parent, String name) => new File(path.join(parent.path, name)); |
| 151 | String _normalize(String filePath) => path.normalize(path.absolute(filePath)); |
| 152 | |
| 153 | class SourceStats { |
| 154 | int files = 0; |
| 155 | int lines = 0; |
| 156 | |
Devon Carew | e464a81 | 2016-05-04 11:43:01 -0700 | [diff] [blame] | 157 | @override |
Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 158 | String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines'; |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | SourceStats getStatsFor(Directory dir, [SourceStats stats]) { |
| 162 | stats ??= new SourceStats(); |
| 163 | |
| 164 | for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 165 | final String name = path.basename(entity.path); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 166 | if (entity is File && name.endsWith('.dart')) { |
| 167 | stats.files += 1; |
| 168 | stats.lines += _lineCount(entity); |
| 169 | } else if (entity is Directory && !name.startsWith('.')) { |
| 170 | getStatsFor(entity, stats); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return stats; |
| 175 | } |
| 176 | |
| 177 | int _lineCount(File file) { |
| 178 | return file.readAsLinesSync().where((String line) { |
| 179 | line = line.trim(); |
| 180 | if (line.isEmpty || line.startsWith('//')) |
| 181 | return false; |
| 182 | return true; |
| 183 | }).length; |
| 184 | } |
| 185 | |
| 186 | String _comma(int count) { |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 187 | final String str = count.toString(); |
Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 188 | if (str.length > 3) |
| 189 | return str.substring(0, str.length - 3) + ',' + str.substring(str.length - 3); |
| 190 | return str; |
| 191 | } |