| Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 5 | import 'dart:io'; |
| 6 | |
| 7 | import 'package:args/args.dart'; |
| 8 | import 'package:path/path.dart' as path; |
| Moritz | 70aa4b3 | 2025-06-03 10:48:14 +0200 | [diff] [blame] | 9 | import 'package:yaml_edit/yaml_edit.dart'; |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 10 | |
| 11 | /// If no `copies` param is passed in, we scale the generated app up to 60k lines. |
| 12 | const int kTargetLineCount = 60 * 1024; |
| 13 | |
| Michael Goderbauer | b308555 | 2022-12-20 16:03:21 -0800 | [diff] [blame] | 14 | /// Make `n` copies of flutter_gallery. |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 15 | void main(List<String> args) { |
| 16 | // If we're run from the `tools` dir, set the cwd to the repo root. |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 17 | if (path.basename(Directory.current.path) == 'tools') { |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 18 | Directory.current = Directory.current.parent.parent; |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 19 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 20 | |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 21 | final argParser = ArgParser(); |
| Moritz | 70aa4b3 | 2025-06-03 10:48:14 +0200 | [diff] [blame] | 22 | argParser.addOption('out', mandatory: true); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 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 | |
| Alexandre Ardhuin | ec1a015 | 2019-12-05 22:34:06 +0100 | [diff] [blame] | 29 | if (results['help'] as bool) { |
| 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 | |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 36 | final source = Directory(_normalize('dev/integration_tests/flutter_gallery')); |
| 37 | final outParent = Directory(_normalize(results['out'] as String)); |
| 38 | final out = Directory(path.join(outParent.path, 'packages')); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 39 | |
| Alexandre Ardhuin | ec1a015 | 2019-12-05 22:34:06 +0100 | [diff] [blame] | 40 | if (results['delete'] as bool) { |
| Moritz | 70aa4b3 | 2025-06-03 10:48:14 +0200 | [diff] [blame] | 41 | if (outParent.existsSync()) { |
| 42 | print('Deleting ${outParent.path}'); |
| 43 | outParent.deleteSync(recursive: true); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | exit(0); |
| 47 | } |
| 48 | |
| Devon Carew | 53bd0e2 | 2018-08-06 09:26:32 -0700 | [diff] [blame] | 49 | if (!results.wasParsed('out')) { |
| 50 | print('The --out parameter is required.'); |
| 51 | print(argParser.usage); |
| 52 | exit(1); |
| 53 | } |
| 54 | |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 55 | int copies; |
| 56 | if (!results.wasParsed('copies')) { |
| Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 57 | final SourceStats stats = getStatsFor(_dir(source, 'lib')); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 58 | copies = (kTargetLineCount / stats.lines).round(); |
| 59 | } else { |
| Alexandre Ardhuin | ec1a015 | 2019-12-05 22:34:06 +0100 | [diff] [blame] | 60 | copies = int.parse(results['copies'] as String); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 63 | print('Making $copies copies of flutter_gallery.'); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 64 | print(''); |
| Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 65 | print('Stats:'); |
| Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 66 | print(' packages/flutter : ${getStatsFor(Directory("packages/flutter"))}'); |
| Michael Goderbauer | 5491c8c | 2024-12-19 12:06:21 -0800 | [diff] [blame] | 67 | print( |
| 68 | ' dev/integration_tests/flutter_gallery : ${getStatsFor(Directory("dev/integration_tests/flutter_gallery"))}', |
| 69 | ); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 70 | |
| Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 71 | final Directory lib = _dir(out, 'lib'); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 72 | if (lib.existsSync()) { |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 73 | lib.deleteSync(recursive: true); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 74 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 75 | |
| 76 | // Copy everything that's not a symlink, dot directory, or build/. |
| 77 | _copy(source, out); |
| 78 | |
| 79 | // Make n - 1 copies. |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 80 | for (var i = 1; i < copies; i++) { |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 81 | _copyGallery(out, i); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 82 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 83 | |
| 84 | // Create a new entry-point. |
| 85 | _createEntry(_file(out, 'lib/main.dart'), copies); |
| 86 | |
| 87 | // Update the pubspec. |
| Moritz | 70aa4b3 | 2025-06-03 10:48:14 +0200 | [diff] [blame] | 88 | final String pubspec = _file(Directory(''), 'pubspec.yaml').readAsStringSync(); |
| 89 | |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 90 | final yamlEditor = YamlEditor(pubspec); |
| Moritz | 70aa4b3 | 2025-06-03 10:48:14 +0200 | [diff] [blame] | 91 | yamlEditor.update(<String>['workspace'], <String>['packages']); |
| 92 | File(path.join(outParent.path, 'pubspec.yaml')).writeAsStringSync(yamlEditor.toString()); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 93 | |
| Michael Goderbauer | 1b8b430 | 2024-02-14 12:31:08 -0800 | [diff] [blame] | 94 | // Replace the (flutter_gallery specific) analysis_options.yaml file with a default one. |
| Michael Goderbauer | 5491c8c | 2024-12-19 12:06:21 -0800 | [diff] [blame] | 95 | _file(out, 'analysis_options.yaml').writeAsStringSync(''' |
| Michael Goderbauer | 1b8b430 | 2024-02-14 12:31:08 -0800 | [diff] [blame] | 96 | analyzer: |
| 97 | errors: |
| 98 | # See analysis_options.yaml in the flutter root for context. |
| 99 | deprecated_member_use: ignore |
| 100 | deprecated_member_use_from_same_package: ignore |
| Michael Goderbauer | 5491c8c | 2024-12-19 12:06:21 -0800 | [diff] [blame] | 101 | '''); |
| Devon Carew | cb3dee7 | 2018-11-06 07:59:33 -0800 | [diff] [blame] | 102 | |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 103 | _file(out, '.dartignore').writeAsStringSync(''); |
| 104 | |
| 105 | // Count source lines and number of files; tell how to run it. |
| Alexandre Ardhuin | ec1a015 | 2019-12-05 22:34:06 +0100 | [diff] [blame] | 106 | print(' ${path.relative(results["out"] as String)} : ${getStatsFor(out)}'); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies. |
| 110 | void _createEntry(File mainFile, int copies) { |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 111 | final imports = StringBuffer(); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 112 | |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 113 | for (var i = 1; i < copies; i++) { |
| Michael R Fairhurst | e77b186 | 2018-08-06 16:31:57 -0700 | [diff] [blame] | 114 | imports.writeln('// ignore: unused_import'); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 115 | imports.writeln("import 'gallery_$i/main.dart' as main_$i;"); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 118 | final contents = |
| Kate Lovett | a04fb32 | 2025-07-07 12:58:32 -0500 | [diff] [blame] | 119 | ''' |
| Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 120 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 121 | // Use of this source code is governed by a BSD-style license that can be |
| 122 | // found in the LICENSE file. |
| 123 | |
| 124 | import 'package:flutter/widgets.dart'; |
| 125 | |
| 126 | import 'gallery/app.dart'; |
| 127 | ${imports.toString().trim()} |
| 128 | |
| 129 | void main() { |
| Phil Quitslund | f21abb6 | 2017-05-22 10:01:22 -0700 | [diff] [blame] | 130 | runApp(const GalleryApp()); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 131 | } |
| 132 | '''; |
| 133 | |
| 134 | mainFile.writeAsStringSync(contents); |
| 135 | } |
| 136 | |
| 137 | void _copyGallery(Directory galleryDir, int index) { |
| Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 138 | final Directory lib = _dir(galleryDir, 'lib'); |
| 139 | final Directory dest = _dir(lib, 'gallery_$index'); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 140 | dest.createSync(); |
| 141 | |
| 142 | // Copy demo/, gallery/, and main.dart. |
| 143 | _copy(_dir(lib, 'demo'), _dir(dest, 'demo')); |
| 144 | _copy(_dir(lib, 'gallery'), _dir(dest, 'gallery')); |
| 145 | _file(dest, 'main.dart').writeAsBytesSync(_file(lib, 'main.dart').readAsBytesSync()); |
| 146 | } |
| 147 | |
| 148 | void _copy(Directory source, Directory target) { |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 149 | if (!target.existsSync()) { |
| Devon Carew | 53bd0e2 | 2018-08-06 09:26:32 -0700 | [diff] [blame] | 150 | target.createSync(recursive: true); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 151 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 152 | |
| Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 153 | for (final FileSystemEntity entity in source.listSync(followLinks: false)) { |
| Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 154 | final String name = path.basename(entity.path); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 155 | |
| Nate Wilson | d07a165 | 2024-07-01 17:36:32 -0600 | [diff] [blame] | 156 | switch (entity) { |
| 157 | case Directory() when name != 'build' && !name.startsWith('.'): |
| 158 | _copy(entity, Directory(path.join(target.path, name))); |
| 159 | |
| 160 | case File() when name != '.packages' && name != 'pubspec.lock': |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 161 | final dest = File(path.join(target.path, name)); |
| Nate Wilson | d07a165 | 2024-07-01 17:36:32 -0600 | [diff] [blame] | 162 | dest.writeAsBytesSync(entity.readAsBytesSync()); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 167 | Directory _dir(Directory parent, String name) => Directory(path.join(parent.path, name)); |
| 168 | File _file(Directory parent, String name) => File(path.join(parent.path, name)); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 169 | String _normalize(String filePath) => path.normalize(path.absolute(filePath)); |
| 170 | |
| 171 | class SourceStats { |
| 172 | int files = 0; |
| 173 | int lines = 0; |
| 174 | |
| Devon Carew | e464a81 | 2016-05-04 11:43:01 -0700 | [diff] [blame] | 175 | @override |
| Devon Carew | 2f642ce | 2016-05-13 12:46:50 -0700 | [diff] [blame] | 176 | String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines'; |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| Abhishek Ghaskata | 243805e | 2021-06-07 23:04:04 +0530 | [diff] [blame] | 179 | SourceStats getStatsFor(Directory dir, [SourceStats? stats]) { |
| Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 180 | stats ??= SourceStats(); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 181 | |
| Ian Hickson | 61a0add | 2021-10-08 09:25:14 -0700 | [diff] [blame] | 182 | for (final FileSystemEntity entity in dir.listSync(followLinks: false)) { |
| Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 183 | final String name = path.basename(entity.path); |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 184 | if (entity is File && name.endsWith('.dart')) { |
| 185 | stats.files += 1; |
| 186 | stats.lines += _lineCount(entity); |
| 187 | } else if (entity is Directory && !name.startsWith('.')) { |
| 188 | getStatsFor(entity, stats); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return stats; |
| 193 | } |
| 194 | |
| 195 | int _lineCount(File file) { |
| 196 | return file.readAsLinesSync().where((String line) { |
| 197 | line = line.trim(); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 198 | if (line.isEmpty || line.startsWith('//')) { |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 199 | return false; |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 200 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 201 | return true; |
| 202 | }).length; |
| 203 | } |
| 204 | |
| 205 | String _comma(int count) { |
| Kate Lovett | 9d96df2 | 2025-11-25 19:10:39 -0600 | [diff] [blame] | 206 | final str = count.toString(); |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 207 | if (str.length > 3) { |
| Alexandre Ardhuin | 34059ee | 2021-06-01 20:14:06 +0200 | [diff] [blame] | 208 | return '${str.substring(0, str.length - 3)},${str.substring(str.length - 3)}'; |
| Michael Goderbauer | 10a7c9b | 2022-07-28 09:07:49 -0700 | [diff] [blame] | 209 | } |
| Devon Carew | a807b00 | 2016-05-01 15:52:51 -0700 | [diff] [blame] | 210 | return str; |
| 211 | } |