2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2016-05-01 22:52:51 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:args/args.dart';
|
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
|
|
|
|
/// If no `copies` param is passed in, we scale the generated app up to 60k lines.
|
|
|
|
const int kTargetLineCount = 60 * 1024;
|
|
|
|
|
2022-12-21 00:03:21 +00:00
|
|
|
/// Make `n` copies of flutter_gallery.
|
2016-05-01 22:52:51 +00:00
|
|
|
void main(List<String> args) {
|
|
|
|
// If we're run from the `tools` dir, set the cwd to the repo root.
|
2022-07-28 16:07:49 +00:00
|
|
|
if (path.basename(Directory.current.path) == 'tools') {
|
2016-05-01 22:52:51 +00:00
|
|
|
Directory.current = Directory.current.parent.parent;
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2018-09-12 06:29:29 +00:00
|
|
|
final ArgParser argParser = ArgParser();
|
2018-08-06 16:26:32 +00:00
|
|
|
argParser.addOption('out');
|
2016-05-01 22:52:51 +00:00
|
|
|
argParser.addOption('copies');
|
|
|
|
argParser.addFlag('delete', negatable: false);
|
|
|
|
argParser.addFlag('help', abbr: 'h', negatable: false);
|
|
|
|
|
2017-03-04 02:06:08 +00:00
|
|
|
final ArgResults results = argParser.parse(args);
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2019-12-05 21:34:06 +00:00
|
|
|
if (results['help'] as bool) {
|
2016-05-09 18:00:54 +00:00
|
|
|
print('Generate n copies of flutter_gallery.\n');
|
2016-05-01 22:52:51 +00:00
|
|
|
print('usage: dart mega_gallery.dart <options>');
|
|
|
|
print(argParser.usage);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-03-16 09:31:42 +00:00
|
|
|
final Directory source = Directory(_normalize('dev/integration_tests/flutter_gallery'));
|
2019-12-05 21:34:06 +00:00
|
|
|
final Directory out = Directory(_normalize(results['out'] as String));
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2019-12-05 21:34:06 +00:00
|
|
|
if (results['delete'] as bool) {
|
2016-05-01 22:52:51 +00:00
|
|
|
if (out.existsSync()) {
|
|
|
|
print('Deleting ${out.path}');
|
|
|
|
out.deleteSync(recursive: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:26:32 +00:00
|
|
|
if (!results.wasParsed('out')) {
|
|
|
|
print('The --out parameter is required.');
|
|
|
|
print(argParser.usage);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:52:51 +00:00
|
|
|
int copies;
|
|
|
|
if (!results.wasParsed('copies')) {
|
2017-03-04 02:06:08 +00:00
|
|
|
final SourceStats stats = getStatsFor(_dir(source, 'lib'));
|
2016-05-01 22:52:51 +00:00
|
|
|
copies = (kTargetLineCount / stats.lines).round();
|
|
|
|
} else {
|
2019-12-05 21:34:06 +00:00
|
|
|
copies = int.parse(results['copies'] as String);
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:46:50 +00:00
|
|
|
print('Making $copies copies of flutter_gallery.');
|
2016-05-01 22:52:51 +00:00
|
|
|
print('');
|
2016-05-13 19:46:50 +00:00
|
|
|
print('Stats:');
|
2018-09-12 06:29:29 +00:00
|
|
|
print(' packages/flutter : ${getStatsFor(Directory("packages/flutter"))}');
|
2020-03-16 09:31:42 +00:00
|
|
|
print(' dev/integration_tests/flutter_gallery : ${getStatsFor(Directory("dev/integration_tests/flutter_gallery"))}');
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2017-03-04 02:06:08 +00:00
|
|
|
final Directory lib = _dir(out, 'lib');
|
2022-07-28 16:07:49 +00:00
|
|
|
if (lib.existsSync()) {
|
2016-05-01 22:52:51 +00:00
|
|
|
lib.deleteSync(recursive: true);
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
|
|
|
|
// Copy everything that's not a symlink, dot directory, or build/.
|
|
|
|
_copy(source, out);
|
|
|
|
|
|
|
|
// Make n - 1 copies.
|
2022-07-28 16:07:49 +00:00
|
|
|
for (int i = 1; i < copies; i++) {
|
2016-05-01 22:52:51 +00:00
|
|
|
_copyGallery(out, i);
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
|
|
|
|
// Create a new entry-point.
|
|
|
|
_createEntry(_file(out, 'lib/main.dart'), copies);
|
|
|
|
|
|
|
|
// Update the pubspec.
|
|
|
|
String pubspec = _file(out, 'pubspec.yaml').readAsStringSync();
|
|
|
|
pubspec = pubspec.replaceAll('../../packages/flutter', '../../../packages/flutter');
|
|
|
|
_file(out, 'pubspec.yaml').writeAsStringSync(pubspec);
|
|
|
|
|
2024-02-14 20:31:08 +00:00
|
|
|
// Replace the (flutter_gallery specific) analysis_options.yaml file with a default one.
|
|
|
|
_file(out, 'analysis_options.yaml').writeAsStringSync(
|
|
|
|
'''
|
|
|
|
analyzer:
|
|
|
|
errors:
|
|
|
|
# See analysis_options.yaml in the flutter root for context.
|
|
|
|
deprecated_member_use: ignore
|
|
|
|
deprecated_member_use_from_same_package: ignore
|
|
|
|
'''
|
|
|
|
);
|
2018-11-06 15:59:33 +00:00
|
|
|
|
2016-05-01 22:52:51 +00:00
|
|
|
_file(out, '.dartignore').writeAsStringSync('');
|
|
|
|
|
|
|
|
// Count source lines and number of files; tell how to run it.
|
2019-12-05 21:34:06 +00:00
|
|
|
print(' ${path.relative(results["out"] as String)} : ${getStatsFor(out)}');
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
|
|
|
|
void _createEntry(File mainFile, int copies) {
|
2018-09-12 06:29:29 +00:00
|
|
|
final StringBuffer imports = StringBuffer();
|
2016-05-01 22:52:51 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < copies; i++) {
|
2018-08-06 23:31:57 +00:00
|
|
|
imports.writeln('// ignore: unused_import');
|
2016-05-01 22:52:51 +00:00
|
|
|
imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
|
|
|
|
}
|
|
|
|
|
2017-03-04 02:06:08 +00:00
|
|
|
final String contents = '''
|
2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2016-05-01 22:52:51 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
|
|
|
|
import 'gallery/app.dart';
|
|
|
|
${imports.toString().trim()}
|
|
|
|
|
|
|
|
void main() {
|
2017-05-22 17:01:22 +00:00
|
|
|
runApp(const GalleryApp());
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
''';
|
|
|
|
|
|
|
|
mainFile.writeAsStringSync(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _copyGallery(Directory galleryDir, int index) {
|
2017-03-04 02:06:08 +00:00
|
|
|
final Directory lib = _dir(galleryDir, 'lib');
|
|
|
|
final Directory dest = _dir(lib, 'gallery_$index');
|
2016-05-01 22:52:51 +00:00
|
|
|
dest.createSync();
|
|
|
|
|
|
|
|
// Copy demo/, gallery/, and main.dart.
|
|
|
|
_copy(_dir(lib, 'demo'), _dir(dest, 'demo'));
|
|
|
|
_copy(_dir(lib, 'gallery'), _dir(dest, 'gallery'));
|
|
|
|
_file(dest, 'main.dart').writeAsBytesSync(_file(lib, 'main.dart').readAsBytesSync());
|
|
|
|
}
|
|
|
|
|
|
|
|
void _copy(Directory source, Directory target) {
|
2022-07-28 16:07:49 +00:00
|
|
|
if (!target.existsSync()) {
|
2018-08-06 16:26:32 +00:00
|
|
|
target.createSync(recursive: true);
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2020-01-07 15:32:04 +00:00
|
|
|
for (final FileSystemEntity entity in source.listSync(followLinks: false)) {
|
2017-03-04 02:06:08 +00:00
|
|
|
final String name = path.basename(entity.path);
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2024-07-01 23:36:32 +00:00
|
|
|
switch (entity) {
|
|
|
|
case Directory() when name != 'build' && !name.startsWith('.'):
|
|
|
|
_copy(entity, Directory(path.join(target.path, name)));
|
|
|
|
|
|
|
|
case File() when name != '.packages' && name != 'pubspec.lock':
|
|
|
|
final File dest = File(path.join(target.path, name));
|
|
|
|
dest.writeAsBytesSync(entity.readAsBytesSync());
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 06:29:29 +00:00
|
|
|
Directory _dir(Directory parent, String name) => Directory(path.join(parent.path, name));
|
|
|
|
File _file(Directory parent, String name) => File(path.join(parent.path, name));
|
2016-05-01 22:52:51 +00:00
|
|
|
String _normalize(String filePath) => path.normalize(path.absolute(filePath));
|
|
|
|
|
|
|
|
class SourceStats {
|
|
|
|
int files = 0;
|
|
|
|
int lines = 0;
|
|
|
|
|
2016-05-04 18:43:01 +00:00
|
|
|
@override
|
2016-05-13 19:46:50 +00:00
|
|
|
String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
|
2016-05-01 22:52:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 17:34:04 +00:00
|
|
|
SourceStats getStatsFor(Directory dir, [SourceStats? stats]) {
|
2018-09-12 06:29:29 +00:00
|
|
|
stats ??= SourceStats();
|
2016-05-01 22:52:51 +00:00
|
|
|
|
2021-10-08 16:25:14 +00:00
|
|
|
for (final FileSystemEntity entity in dir.listSync(followLinks: false)) {
|
2017-03-04 02:06:08 +00:00
|
|
|
final String name = path.basename(entity.path);
|
2016-05-01 22:52:51 +00:00
|
|
|
if (entity is File && name.endsWith('.dart')) {
|
|
|
|
stats.files += 1;
|
|
|
|
stats.lines += _lineCount(entity);
|
|
|
|
} else if (entity is Directory && !name.startsWith('.')) {
|
|
|
|
getStatsFor(entity, stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _lineCount(File file) {
|
|
|
|
return file.readAsLinesSync().where((String line) {
|
|
|
|
line = line.trim();
|
2022-07-28 16:07:49 +00:00
|
|
|
if (line.isEmpty || line.startsWith('//')) {
|
2016-05-01 22:52:51 +00:00
|
|
|
return false;
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
return true;
|
|
|
|
}).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
String _comma(int count) {
|
2017-03-04 02:06:08 +00:00
|
|
|
final String str = count.toString();
|
2022-07-28 16:07:49 +00:00
|
|
|
if (str.length > 3) {
|
2021-06-01 18:14:06 +00:00
|
|
|
return '${str.substring(0, str.length - 3)},${str.substring(str.length - 3)}';
|
2022-07-28 16:07:49 +00:00
|
|
|
}
|
2016-05-01 22:52:51 +00:00
|
|
|
return str;
|
|
|
|
}
|