mega_gallery.dart 5.87 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
/// Make `n` copies of flutter_gallery.
6 7 8 9 10 11 12 13 14 15 16 17 18 19

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;

void main(List<String> args) {
  // If we're run from the `tools` dir, set the cwd to the repo root.
  if (path.basename(Directory.current.path) == 'tools')
    Directory.current = Directory.current.parent.parent;

20
  final ArgParser argParser = new ArgParser();
21 22 23 24 25 26
  // ../mega_gallery? dev/benchmarks/mega_gallery?
  argParser.addOption('out', defaultsTo: _normalize('dev/benchmarks/mega_gallery'));
  argParser.addOption('copies');
  argParser.addFlag('delete', negatable: false);
  argParser.addFlag('help', abbr: 'h', negatable: false);

27
  final ArgResults results = argParser.parse(args);
28 29

  if (results['help']) {
30
    print('Generate n copies of flutter_gallery.\n');
31 32 33 34 35
    print('usage: dart mega_gallery.dart <options>');
    print(argParser.usage);
    exit(0);
  }

36 37
  final Directory source = new Directory(_normalize('examples/flutter_gallery'));
  final Directory out = new Directory(_normalize(results['out']));
38 39 40 41 42 43 44 45 46 47 48 49

  if (results['delete']) {
    if (out.existsSync()) {
      print('Deleting ${out.path}');
      out.deleteSync(recursive: true);
    }

    exit(0);
  }

  int copies;
  if (!results.wasParsed('copies')) {
50
    final SourceStats stats = getStatsFor(_dir(source, 'lib'));
51 52 53 54 55
    copies = (kTargetLineCount / stats.lines).round();
  } else {
    copies = int.parse(results['copies']);
  }

56
  print('Making $copies copies of flutter_gallery.');
57
  print('');
58 59 60
  print('Stats:');
  print('  packages/flutter            : ${getStatsFor(new Directory("packages/flutter"))}');
  print('  examples/flutter_gallery    : ${getStatsFor(new Directory("examples/flutter_gallery"))}');
61

62
  final Directory lib = _dir(out, 'lib');
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  if (lib.existsSync())
    lib.deleteSync(recursive: true);

  // Copy everything that's not a symlink, dot directory, or build/.
  _copy(source, out);

  // Make n - 1 copies.
  for (int i = 1; i < copies; i++)
    _copyGallery(out, i);

  // 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);

  _file(out, '.dartignore').writeAsStringSync('');

  // Count source lines and number of files; tell how to run it.
84
  print('  ${path.relative(results["out"])} : ${getStatsFor(out)}');
85 86 87 88
}

// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
void _createEntry(File mainFile, int copies) {
89 90
  final StringBuffer imports = new StringBuffer();
  final StringBuffer importRefs = new StringBuffer();
91 92 93 94 95 96

  for (int i = 1; i < copies; i++) {
    imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
    importRefs.writeln("  main_$i.main;");
  }

97
  final String contents = '''
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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() {
  // Make sure the imports are not marked as unused.
  ${importRefs.toString().trim()}

  runApp(new GalleryApp());
}
''';

  mainFile.writeAsStringSync(contents);
}

void _copyGallery(Directory galleryDir, int index) {
119 120
  final Directory lib = _dir(galleryDir, 'lib');
  final Directory dest = _dir(lib, 'gallery_$index');
121 122 123 124 125 126 127 128 129 130 131 132 133
  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) {
  if (!target.existsSync())
    target.createSync();

  for (FileSystemEntity entity in source.listSync(followLinks: false)) {
134
    final String name = path.basename(entity.path);
135 136 137 138 139 140 141 142

    if (entity is Directory) {
      if (name == 'build' || name.startsWith('.'))
        continue;
      _copy(entity, new Directory(path.join(target.path, name)));
    } else if (entity is File) {
      if (name == '.packages' || name == 'pubspec.lock')
        continue;
143
      final File dest = new File(path.join(target.path, name));
144 145 146 147 148 149 150 151 152 153 154 155 156
      dest.writeAsBytesSync(entity.readAsBytesSync());
    }
  }
}

Directory _dir(Directory parent, String name) => new Directory(path.join(parent.path, name));
File _file(Directory parent, String name) => new File(path.join(parent.path, name));
String _normalize(String filePath) => path.normalize(path.absolute(filePath));

class SourceStats {
  int files = 0;
  int lines = 0;

157
  @override
158
  String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
159 160 161 162 163 164
}

SourceStats getStatsFor(Directory dir, [SourceStats stats]) {
  stats ??= new SourceStats();

  for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) {
165
    final String name = path.basename(entity.path);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    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();
    if (line.isEmpty || line.startsWith('//'))
      return false;
    return true;
  }).length;
}

String _comma(int count) {
187
  final String str = count.toString();
188 189 190 191
  if (str.length > 3)
    return str.substring(0, str.length - 3) + ',' + str.substring(str.length - 3);
  return str;
}