blob: 86be7fd280862ea7065abf5f3ef40dec9f851d07 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
Devon Carewa807b002016-05-01 15:52:51 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Devon Carewa807b002016-05-01 15:52:51 -07005import 'dart:io';
6
7import 'package:args/args.dart';
8import 'package:path/path.dart' as path;
Moritz70aa4b32025-06-03 10:48:14 +02009import 'package:yaml_edit/yaml_edit.dart';
Devon Carewa807b002016-05-01 15:52:51 -070010
11/// If no `copies` param is passed in, we scale the generated app up to 60k lines.
12const int kTargetLineCount = 60 * 1024;
13
Michael Goderbauerb3085552022-12-20 16:03:21 -080014/// Make `n` copies of flutter_gallery.
Devon Carewa807b002016-05-01 15:52:51 -070015void main(List<String> args) {
16 // If we're run from the `tools` dir, set the cwd to the repo root.
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -070017 if (path.basename(Directory.current.path) == 'tools') {
Devon Carewa807b002016-05-01 15:52:51 -070018 Directory.current = Directory.current.parent.parent;
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -070019 }
Devon Carewa807b002016-05-01 15:52:51 -070020
Kate Lovett9d96df22025-11-25 19:10:39 -060021 final argParser = ArgParser();
Moritz70aa4b32025-06-03 10:48:14 +020022 argParser.addOption('out', mandatory: true);
Devon Carewa807b002016-05-01 15:52:51 -070023 argParser.addOption('copies');
24 argParser.addFlag('delete', negatable: false);
25 argParser.addFlag('help', abbr: 'h', negatable: false);
26
Chris Bracken6c97dd22017-03-03 18:06:08 -080027 final ArgResults results = argParser.parse(args);
Devon Carewa807b002016-05-01 15:52:51 -070028
Alexandre Ardhuinec1a0152019-12-05 22:34:06 +010029 if (results['help'] as bool) {
Hans Muller74c3e742016-05-09 11:00:54 -070030 print('Generate n copies of flutter_gallery.\n');
Devon Carewa807b002016-05-01 15:52:51 -070031 print('usage: dart mega_gallery.dart <options>');
32 print(argParser.usage);
33 exit(0);
34 }
35
Kate Lovett9d96df22025-11-25 19:10:39 -060036 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 Carewa807b002016-05-01 15:52:51 -070039
Alexandre Ardhuinec1a0152019-12-05 22:34:06 +010040 if (results['delete'] as bool) {
Moritz70aa4b32025-06-03 10:48:14 +020041 if (outParent.existsSync()) {
42 print('Deleting ${outParent.path}');
43 outParent.deleteSync(recursive: true);
Devon Carewa807b002016-05-01 15:52:51 -070044 }
45
46 exit(0);
47 }
48
Devon Carew53bd0e22018-08-06 09:26:32 -070049 if (!results.wasParsed('out')) {
50 print('The --out parameter is required.');
51 print(argParser.usage);
52 exit(1);
53 }
54
Devon Carewa807b002016-05-01 15:52:51 -070055 int copies;
56 if (!results.wasParsed('copies')) {
Chris Bracken6c97dd22017-03-03 18:06:08 -080057 final SourceStats stats = getStatsFor(_dir(source, 'lib'));
Devon Carewa807b002016-05-01 15:52:51 -070058 copies = (kTargetLineCount / stats.lines).round();
59 } else {
Alexandre Ardhuinec1a0152019-12-05 22:34:06 +010060 copies = int.parse(results['copies'] as String);
Devon Carewa807b002016-05-01 15:52:51 -070061 }
62
Devon Carew2f642ce2016-05-13 12:46:50 -070063 print('Making $copies copies of flutter_gallery.');
Devon Carewa807b002016-05-01 15:52:51 -070064 print('');
Devon Carew2f642ce2016-05-13 12:46:50 -070065 print('Stats:');
Alexandre Ardhuind927c932018-09-12 08:29:29 +020066 print(' packages/flutter : ${getStatsFor(Directory("packages/flutter"))}');
Michael Goderbauer5491c8c2024-12-19 12:06:21 -080067 print(
68 ' dev/integration_tests/flutter_gallery : ${getStatsFor(Directory("dev/integration_tests/flutter_gallery"))}',
69 );
Devon Carewa807b002016-05-01 15:52:51 -070070
Chris Bracken6c97dd22017-03-03 18:06:08 -080071 final Directory lib = _dir(out, 'lib');
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -070072 if (lib.existsSync()) {
Devon Carewa807b002016-05-01 15:52:51 -070073 lib.deleteSync(recursive: true);
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -070074 }
Devon Carewa807b002016-05-01 15:52:51 -070075
76 // Copy everything that's not a symlink, dot directory, or build/.
77 _copy(source, out);
78
79 // Make n - 1 copies.
Kate Lovett9d96df22025-11-25 19:10:39 -060080 for (var i = 1; i < copies; i++) {
Devon Carewa807b002016-05-01 15:52:51 -070081 _copyGallery(out, i);
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -070082 }
Devon Carewa807b002016-05-01 15:52:51 -070083
84 // Create a new entry-point.
85 _createEntry(_file(out, 'lib/main.dart'), copies);
86
87 // Update the pubspec.
Moritz70aa4b32025-06-03 10:48:14 +020088 final String pubspec = _file(Directory(''), 'pubspec.yaml').readAsStringSync();
89
Kate Lovett9d96df22025-11-25 19:10:39 -060090 final yamlEditor = YamlEditor(pubspec);
Moritz70aa4b32025-06-03 10:48:14 +020091 yamlEditor.update(<String>['workspace'], <String>['packages']);
92 File(path.join(outParent.path, 'pubspec.yaml')).writeAsStringSync(yamlEditor.toString());
Devon Carewa807b002016-05-01 15:52:51 -070093
Michael Goderbauer1b8b4302024-02-14 12:31:08 -080094 // Replace the (flutter_gallery specific) analysis_options.yaml file with a default one.
Michael Goderbauer5491c8c2024-12-19 12:06:21 -080095 _file(out, 'analysis_options.yaml').writeAsStringSync('''
Michael Goderbauer1b8b4302024-02-14 12:31:08 -080096analyzer:
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 Goderbauer5491c8c2024-12-19 12:06:21 -0800101''');
Devon Carewcb3dee72018-11-06 07:59:33 -0800102
Devon Carewa807b002016-05-01 15:52:51 -0700103 _file(out, '.dartignore').writeAsStringSync('');
104
105 // Count source lines and number of files; tell how to run it.
Alexandre Ardhuinec1a0152019-12-05 22:34:06 +0100106 print(' ${path.relative(results["out"] as String)} : ${getStatsFor(out)}');
Devon Carewa807b002016-05-01 15:52:51 -0700107}
108
109// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
110void _createEntry(File mainFile, int copies) {
Kate Lovett9d96df22025-11-25 19:10:39 -0600111 final imports = StringBuffer();
Devon Carewa807b002016-05-01 15:52:51 -0700112
Kate Lovett9d96df22025-11-25 19:10:39 -0600113 for (var i = 1; i < copies; i++) {
Michael R Fairhurste77b1862018-08-06 16:31:57 -0700114 imports.writeln('// ignore: unused_import');
Devon Carewa807b002016-05-01 15:52:51 -0700115 imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
Devon Carewa807b002016-05-01 15:52:51 -0700116 }
117
Kate Lovett9d96df22025-11-25 19:10:39 -0600118 final contents =
Kate Lovetta04fb322025-07-07 12:58:32 -0500119 '''
Ian Hickson449f4a62019-11-27 15:04:02 -0800120// Copyright 2014 The Flutter Authors. All rights reserved.
Devon Carewa807b002016-05-01 15:52:51 -0700121// Use of this source code is governed by a BSD-style license that can be
122// found in the LICENSE file.
123
124import 'package:flutter/widgets.dart';
125
126import 'gallery/app.dart';
127${imports.toString().trim()}
128
129void main() {
Phil Quitslundf21abb62017-05-22 10:01:22 -0700130 runApp(const GalleryApp());
Devon Carewa807b002016-05-01 15:52:51 -0700131}
132''';
133
134 mainFile.writeAsStringSync(contents);
135}
136
137void _copyGallery(Directory galleryDir, int index) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800138 final Directory lib = _dir(galleryDir, 'lib');
139 final Directory dest = _dir(lib, 'gallery_$index');
Devon Carewa807b002016-05-01 15:52:51 -0700140 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
148void _copy(Directory source, Directory target) {
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700149 if (!target.existsSync()) {
Devon Carew53bd0e22018-08-06 09:26:32 -0700150 target.createSync(recursive: true);
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700151 }
Devon Carewa807b002016-05-01 15:52:51 -0700152
Alexandre Ardhuin4f9b6cf2020-01-07 16:32:04 +0100153 for (final FileSystemEntity entity in source.listSync(followLinks: false)) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800154 final String name = path.basename(entity.path);
Devon Carewa807b002016-05-01 15:52:51 -0700155
Nate Wilsond07a1652024-07-01 17:36:32 -0600156 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 Lovett9d96df22025-11-25 19:10:39 -0600161 final dest = File(path.join(target.path, name));
Nate Wilsond07a1652024-07-01 17:36:32 -0600162 dest.writeAsBytesSync(entity.readAsBytesSync());
Devon Carewa807b002016-05-01 15:52:51 -0700163 }
164 }
165}
166
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200167Directory _dir(Directory parent, String name) => Directory(path.join(parent.path, name));
168File _file(Directory parent, String name) => File(path.join(parent.path, name));
Devon Carewa807b002016-05-01 15:52:51 -0700169String _normalize(String filePath) => path.normalize(path.absolute(filePath));
170
171class SourceStats {
172 int files = 0;
173 int lines = 0;
174
Devon Carewe464a812016-05-04 11:43:01 -0700175 @override
Devon Carew2f642ce2016-05-13 12:46:50 -0700176 String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
Devon Carewa807b002016-05-01 15:52:51 -0700177}
178
Abhishek Ghaskata243805e2021-06-07 23:04:04 +0530179SourceStats getStatsFor(Directory dir, [SourceStats? stats]) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200180 stats ??= SourceStats();
Devon Carewa807b002016-05-01 15:52:51 -0700181
Ian Hickson61a0add2021-10-08 09:25:14 -0700182 for (final FileSystemEntity entity in dir.listSync(followLinks: false)) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800183 final String name = path.basename(entity.path);
Devon Carewa807b002016-05-01 15:52:51 -0700184 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
195int _lineCount(File file) {
196 return file.readAsLinesSync().where((String line) {
197 line = line.trim();
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700198 if (line.isEmpty || line.startsWith('//')) {
Devon Carewa807b002016-05-01 15:52:51 -0700199 return false;
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700200 }
Devon Carewa807b002016-05-01 15:52:51 -0700201 return true;
202 }).length;
203}
204
205String _comma(int count) {
Kate Lovett9d96df22025-11-25 19:10:39 -0600206 final str = count.toString();
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700207 if (str.length > 3) {
Alexandre Ardhuin34059ee2021-06-01 20:14:06 +0200208 return '${str.substring(0, str.length - 3)},${str.substring(str.length - 3)}';
Michael Goderbauer10a7c9b2022-07-28 09:07:49 -0700209 }
Devon Carewa807b002016-05-01 15:52:51 -0700210 return str;
211}