blob: 7ce1faabe5020cd321df51d9db9e60738cd39fba [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
5/// Make `n` copies of material_gallery.
6
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
20 ArgParser argParser = new ArgParser();
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
27 ArgResults results = argParser.parse(args);
28
29 if (results['help']) {
30 print('Generate n copies of material_gallery.\n');
31 print('usage: dart mega_gallery.dart <options>');
32 print(argParser.usage);
33 exit(0);
34 }
35
36 Directory source = new Directory(_normalize('examples/material_gallery'));
37 Directory out = new Directory(_normalize(results['out']));
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')) {
50 SourceStats stats = getStatsFor(_dir(source, 'lib'));
51 copies = (kTargetLineCount / stats.lines).round();
52 } else {
53 copies = int.parse(results['copies']);
54 }
55
56 print('Stats:');
57 print(' packages/flutter : ${getStatsFor(new Directory("packages/flutter"))}');
58 print(' examples/material_gallery : ${getStatsFor(new Directory("examples/material_gallery"))}');
59 print('');
60
61 print('Making $copies copies of material_gallery:');
62
63 Directory lib = _dir(out, 'lib');
64 if (lib.existsSync())
65 lib.deleteSync(recursive: true);
66
67 // Copy everything that's not a symlink, dot directory, or build/.
68 _copy(source, out);
69
70 // Make n - 1 copies.
71 for (int i = 1; i < copies; i++)
72 _copyGallery(out, i);
73
74 // Create a new entry-point.
75 _createEntry(_file(out, 'lib/main.dart'), copies);
76
77 // Update the pubspec.
78 String pubspec = _file(out, 'pubspec.yaml').readAsStringSync();
79 pubspec = pubspec.replaceAll('../../packages/flutter', '../../../packages/flutter');
80 _file(out, 'pubspec.yaml').writeAsStringSync(pubspec);
81
82 _file(out, '.dartignore').writeAsStringSync('');
83
84 // Count source lines and number of files; tell how to run it.
85 print(' ${path.relative(results["out"])}: ${getStatsFor(out)}');
86}
87
88// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
89void _createEntry(File mainFile, int copies) {
90 StringBuffer imports = new StringBuffer();
91 StringBuffer importRefs = new StringBuffer();
92
93 for (int i = 1; i < copies; i++) {
94 imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
95 importRefs.writeln(" main_$i.main;");
96 }
97
98 String contents = '''
99// Copyright 2016 The Chromium Authors. All rights reserved.
100// Use of this source code is governed by a BSD-style license that can be
101// found in the LICENSE file.
102
103import 'package:flutter/widgets.dart';
104
105import 'gallery/app.dart';
106${imports.toString().trim()}
107
108void main() {
109 // Make sure the imports are not marked as unused.
110 ${importRefs.toString().trim()}
111
112 runApp(new GalleryApp());
113}
114''';
115
116 mainFile.writeAsStringSync(contents);
117}
118
119void _copyGallery(Directory galleryDir, int index) {
120 Directory lib = _dir(galleryDir, 'lib');
121 Directory dest = _dir(lib, 'gallery_$index');
122 dest.createSync();
123
124 // Copy demo/, gallery/, and main.dart.
125 _copy(_dir(lib, 'demo'), _dir(dest, 'demo'));
126 _copy(_dir(lib, 'gallery'), _dir(dest, 'gallery'));
127 _file(dest, 'main.dart').writeAsBytesSync(_file(lib, 'main.dart').readAsBytesSync());
128}
129
130void _copy(Directory source, Directory target) {
131 if (!target.existsSync())
132 target.createSync();
133
134 for (FileSystemEntity entity in source.listSync(followLinks: false)) {
135 String name = path.basename(entity.path);
136
137 if (entity is Directory) {
138 if (name == 'build' || name.startsWith('.'))
139 continue;
140 _copy(entity, new Directory(path.join(target.path, name)));
141 } else if (entity is File) {
142 if (name == '.packages' || name == 'pubspec.lock')
143 continue;
144 File dest = new File(path.join(target.path, name));
145 dest.writeAsBytesSync(entity.readAsBytesSync());
146 }
147 }
148}
149
150Directory _dir(Directory parent, String name) => new Directory(path.join(parent.path, name));
151File _file(Directory parent, String name) => new File(path.join(parent.path, name));
152String _normalize(String filePath) => path.normalize(path.absolute(filePath));
153
154class SourceStats {
155 int files = 0;
156 int lines = 0;
157
158 String toString() => '${_comma(files)} files, ${_comma(lines)} lines';
159}
160
161SourceStats getStatsFor(Directory dir, [SourceStats stats]) {
162 stats ??= new SourceStats();
163
164 for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) {
165 String name = path.basename(entity.path);
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
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) {
187 String str = count.toString();
188 if (str.length > 3)
189 return str.substring(0, str.length - 3) + ',' + str.substring(str.length - 3);
190 return str;
191}