mega_gallery.dart 5.98 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12
// 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;

13
/// Make `n` copies of flutter_gallery.
14 15
void main(List<String> args) {
  // If we're run from the `tools` dir, set the cwd to the repo root.
16
  if (path.basename(Directory.current.path) == 'tools') {
17
    Directory.current = Directory.current.parent.parent;
18
  }
19

20
  final ArgParser argParser = ArgParser();
21
  argParser.addOption('out');
22 23 24 25
  argParser.addOption('copies');
  argParser.addFlag('delete', negatable: false);
  argParser.addFlag('help', abbr: 'h', negatable: false);

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

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

35
  final Directory source = Directory(_normalize('dev/integration_tests/flutter_gallery'));
36
  final Directory out = Directory(_normalize(results['out'] as String));
37

38
  if (results['delete'] as bool) {
39 40 41 42 43 44 45 46
    if (out.existsSync()) {
      print('Deleting ${out.path}');
      out.deleteSync(recursive: true);
    }

    exit(0);
  }

47 48 49 50 51 52
  if (!results.wasParsed('out')) {
    print('The --out parameter is required.');
    print(argParser.usage);
    exit(1);
  }

53 54
  int copies;
  if (!results.wasParsed('copies')) {
55
    final SourceStats stats = getStatsFor(_dir(source, 'lib'));
56 57
    copies = (kTargetLineCount / stats.lines).round();
  } else {
58
    copies = int.parse(results['copies'] as String);
59 60
  }

61
  print('Making $copies copies of flutter_gallery.');
62
  print('');
63
  print('Stats:');
64
  print('  packages/flutter            : ${getStatsFor(Directory("packages/flutter"))}');
65
  print('  dev/integration_tests/flutter_gallery    : ${getStatsFor(Directory("dev/integration_tests/flutter_gallery"))}');
66

67
  final Directory lib = _dir(out, 'lib');
68
  if (lib.existsSync()) {
69
    lib.deleteSync(recursive: true);
70
  }
71 72 73 74 75

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

  // Make n - 1 copies.
76
  for (int i = 1; i < copies; i++) {
77
    _copyGallery(out, i);
78
  }
79 80 81 82 83 84 85 86 87

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

88 89 90
  // Remove the (flutter_gallery specific) analysis_options.yaml file.
  _file(out, 'analysis_options.yaml').deleteSync();

91 92 93
  _file(out, '.dartignore').writeAsStringSync('');

  // Count source lines and number of files; tell how to run it.
94
  print('  ${path.relative(results["out"] as String)} : ${getStatsFor(out)}');
95 96 97 98
}

// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
void _createEntry(File mainFile, int copies) {
99
  final StringBuffer imports = StringBuffer();
100 101

  for (int i = 1; i < copies; i++) {
102
    imports.writeln('// ignore: unused_import');
103 104 105
    imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
  }

106
  final String contents = '''
Ian Hickson's avatar
Ian Hickson committed
107
// Copyright 2014 The Flutter Authors. All rights reserved.
108 109 110 111 112 113 114 115 116
// 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() {
117
  runApp(const GalleryApp());
118 119 120 121 122 123 124
}
''';

  mainFile.writeAsStringSync(contents);
}

void _copyGallery(Directory galleryDir, int index) {
125 126
  final Directory lib = _dir(galleryDir, 'lib');
  final Directory dest = _dir(lib, 'gallery_$index');
127 128 129 130 131 132 133 134 135
  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) {
136
  if (!target.existsSync()) {
137
    target.createSync(recursive: true);
138
  }
139

140
  for (final FileSystemEntity entity in source.listSync(followLinks: false)) {
141
    final String name = path.basename(entity.path);
142 143

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

158 159
Directory _dir(Directory parent, String name) => Directory(path.join(parent.path, name));
File _file(Directory parent, String name) => File(path.join(parent.path, name));
160 161 162 163 164 165
String _normalize(String filePath) => path.normalize(path.absolute(filePath));

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

166
  @override
167
  String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
168 169
}

170
SourceStats getStatsFor(Directory dir, [SourceStats? stats]) {
171
  stats ??= SourceStats();
172

173
  for (final FileSystemEntity entity in dir.listSync(followLinks: false)) {
174
    final String name = path.basename(entity.path);
175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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();
189
    if (line.isEmpty || line.startsWith('//')) {
190
      return false;
191
    }
192 193 194 195 196
    return true;
  }).length;
}

String _comma(int count) {
197
  final String str = count.toString();
198
  if (str.length > 3) {
199
    return '${str.substring(0, str.length - 3)},${str.substring(str.length - 3)}';
200
  }
201 202
  return str;
}