blob: 34bb04bf97b47bed7a4616c26bad538d578aa4ba [file] [log] [blame]
Devon Carewa807b002016-05-01 15:52:51 -07001// 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 Muller74c3e742016-05-09 11:00:54 -07005/// Make `n` copies of flutter_gallery.
Devon Carewa807b002016-05-01 15:52:51 -07006
7import 'dart:io';
8
9import 'package:args/args.dart';
10import 'package:path/path.dart' as path;
11
12/// If no `copies` param is passed in, we scale the generated app up to 60k lines.
13const int kTargetLineCount = 60 * 1024;
14
15void 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 Bracken6c97dd22017-03-03 18:06:08 -080020 final ArgParser argParser = new ArgParser();
Devon Carewa807b002016-05-01 15:52:51 -070021 // ../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 Bracken6c97dd22017-03-03 18:06:08 -080027 final ArgResults results = argParser.parse(args);
Devon Carewa807b002016-05-01 15:52:51 -070028
29 if (results['help']) {
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
Chris Bracken6c97dd22017-03-03 18:06:08 -080036 final Directory source = new Directory(_normalize('examples/flutter_gallery'));
37 final Directory out = new Directory(_normalize(results['out']));
Devon Carewa807b002016-05-01 15:52:51 -070038
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 Bracken6c97dd22017-03-03 18:06:08 -080050 final SourceStats stats = getStatsFor(_dir(source, 'lib'));
Devon Carewa807b002016-05-01 15:52:51 -070051 copies = (kTargetLineCount / stats.lines).round();
52 } else {
53 copies = int.parse(results['copies']);
54 }
55
Devon Carew2f642ce2016-05-13 12:46:50 -070056 print('Making $copies copies of flutter_gallery.');
Devon Carewa807b002016-05-01 15:52:51 -070057 print('');
Devon Carew2f642ce2016-05-13 12:46:50 -070058 print('Stats:');
59 print(' packages/flutter : ${getStatsFor(new Directory("packages/flutter"))}');
60 print(' examples/flutter_gallery : ${getStatsFor(new Directory("examples/flutter_gallery"))}');
Devon Carewa807b002016-05-01 15:52:51 -070061
Chris Bracken6c97dd22017-03-03 18:06:08 -080062 final Directory lib = _dir(out, 'lib');
Devon Carewa807b002016-05-01 15:52:51 -070063 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 Carew2f642ce2016-05-13 12:46:50 -070084 print(' ${path.relative(results["out"])} : ${getStatsFor(out)}');
Devon Carewa807b002016-05-01 15:52:51 -070085}
86
87// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
88void _createEntry(File mainFile, int copies) {
Chris Bracken6c97dd22017-03-03 18:06:08 -080089 final StringBuffer imports = new StringBuffer();
90 final StringBuffer importRefs = new StringBuffer();
Devon Carewa807b002016-05-01 15:52:51 -070091
92 for (int i = 1; i < copies; i++) {
93 imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
Alexandre Ardhuin1fce14a2017-10-22 18:11:36 +020094 importRefs.writeln(' main_$i.main;');
Devon Carewa807b002016-05-01 15:52:51 -070095 }
96
Chris Bracken6c97dd22017-03-03 18:06:08 -080097 final String contents = '''
Devon Carewa807b002016-05-01 15:52:51 -070098// 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
102import 'package:flutter/widgets.dart';
103
104import 'gallery/app.dart';
105${imports.toString().trim()}
106
107void main() {
108 // Make sure the imports are not marked as unused.
109 ${importRefs.toString().trim()}
110
Phil Quitslundf21abb62017-05-22 10:01:22 -0700111 runApp(const GalleryApp());
Devon Carewa807b002016-05-01 15:52:51 -0700112}
113''';
114
115 mainFile.writeAsStringSync(contents);
116}
117
118void _copyGallery(Directory galleryDir, int index) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800119 final Directory lib = _dir(galleryDir, 'lib');
120 final Directory dest = _dir(lib, 'gallery_$index');
Devon Carewa807b002016-05-01 15:52:51 -0700121 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
129void _copy(Directory source, Directory target) {
130 if (!target.existsSync())
131 target.createSync();
132
133 for (FileSystemEntity entity in source.listSync(followLinks: false)) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800134 final String name = path.basename(entity.path);
Devon Carewa807b002016-05-01 15:52:51 -0700135
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 Bracken6c97dd22017-03-03 18:06:08 -0800143 final File dest = new File(path.join(target.path, name));
Devon Carewa807b002016-05-01 15:52:51 -0700144 dest.writeAsBytesSync(entity.readAsBytesSync());
145 }
146 }
147}
148
149Directory _dir(Directory parent, String name) => new Directory(path.join(parent.path, name));
150File _file(Directory parent, String name) => new File(path.join(parent.path, name));
151String _normalize(String filePath) => path.normalize(path.absolute(filePath));
152
153class SourceStats {
154 int files = 0;
155 int lines = 0;
156
Devon Carewe464a812016-05-04 11:43:01 -0700157 @override
Devon Carew2f642ce2016-05-13 12:46:50 -0700158 String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
Devon Carewa807b002016-05-01 15:52:51 -0700159}
160
161SourceStats getStatsFor(Directory dir, [SourceStats stats]) {
162 stats ??= new SourceStats();
163
164 for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800165 final String name = path.basename(entity.path);
Devon Carewa807b002016-05-01 15:52:51 -0700166 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
177int _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
186String _comma(int count) {
Chris Bracken6c97dd22017-03-03 18:06:08 -0800187 final String str = count.toString();
Devon Carewa807b002016-05-01 15:52:51 -0700188 if (str.length > 3)
189 return str.substring(0, str.length - 3) + ',' + str.substring(str.length - 3);
190 return str;
191}