flutter/dev/tools/mega_gallery.dart
auto-submit[bot] 4372bfbc6c
Reverts "Add workspace (#169451)" (#169468)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#169451
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Broke a number of post-submit tests
(ios_app_extension, packages_autoroller).
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: mosuem
<!-- end_original_pr_author -->

<!-- start_reviewers -->
Reviewed By: {matanlurey}
<!-- end_reviewers -->

<!-- start_revert_body -->
This change reverts the following previous change:
Reland after #169357.

Switch Flutter to use pub workspaces as a preparation to unpin selected
packages.

Assumptions:

1. No packages in this repository are published to pub.dev --> We can
use `any` dependencies in most local pubspecs, as the global constraint
defines the version. An exception are the packages used outside of this
repo with an `sdk` dependency, namely `flutter_localizations`,
`flutter_test`, and `flutter`.
2. The "universes" `{flutter_tools}` and `{flutter,
flutter_localizations, flutter_goldens}` can use different packages
versions, as they are not resolved together. --> We do not need to
upgrade them in sync, we can first do one "universe", then the other.

Based on these assumptions, we use
https://github.com/mosuem/pubspec_merger.dart to merge all packages in
the `flutter` universe into a top-level pub workspace.

The `flutter` and `flutter_tools` workspaces being separate also ensures
that changes to `flutter` will not inadvertently break `flutter_tools`,
with not-so-nice consequences for our users which would be unable to run
`flutter upgrade`.

There is a third "top-level" pubspec besides `./pubspec.yaml` and
`packages/flutter_tools/pubspec.yaml`, namely
`packages/flutter_tools/.../widget_preview_scaffold/pubspec.yaml`. This
is an artifact due to it living under `flutter_tools`, so it can't be
part of the `./pubspec.yaml` workspace. Moving it would be a larger
change, and out of the scope of this PR.

This required a rewrite of the update-packages tool, but the main
functionality stays the same, as well as the argument names, to ensure a
seamless transition.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

<!-- end_revert_body -->

Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
2025-05-26 14:07:27 +00:00

207 lines
6.1 KiB
Dart

// Copyright 2014 The Flutter 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 '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;
/// Make `n` copies of flutter_gallery.
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;
}
final ArgParser argParser = ArgParser();
argParser.addOption('out');
argParser.addOption('copies');
argParser.addFlag('delete', negatable: false);
argParser.addFlag('help', abbr: 'h', negatable: false);
final ArgResults results = argParser.parse(args);
if (results['help'] as bool) {
print('Generate n copies of flutter_gallery.\n');
print('usage: dart mega_gallery.dart <options>');
print(argParser.usage);
exit(0);
}
final Directory source = Directory(_normalize('dev/integration_tests/flutter_gallery'));
final Directory out = Directory(_normalize(results['out'] as String));
if (results['delete'] as bool) {
if (out.existsSync()) {
print('Deleting ${out.path}');
out.deleteSync(recursive: true);
}
exit(0);
}
if (!results.wasParsed('out')) {
print('The --out parameter is required.');
print(argParser.usage);
exit(1);
}
int copies;
if (!results.wasParsed('copies')) {
final SourceStats stats = getStatsFor(_dir(source, 'lib'));
copies = (kTargetLineCount / stats.lines).round();
} else {
copies = int.parse(results['copies'] as String);
}
print('Making $copies copies of flutter_gallery.');
print('');
print('Stats:');
print(' packages/flutter : ${getStatsFor(Directory("packages/flutter"))}');
print(
' dev/integration_tests/flutter_gallery : ${getStatsFor(Directory("dev/integration_tests/flutter_gallery"))}',
);
final Directory lib = _dir(out, 'lib');
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);
// 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
''');
_file(out, '.dartignore').writeAsStringSync('');
// Count source lines and number of files; tell how to run it.
print(' ${path.relative(results["out"] as String)} : ${getStatsFor(out)}');
}
// TODO(devoncarew): Create an entry-point that builds a UI with all `n` copies.
void _createEntry(File mainFile, int copies) {
final StringBuffer imports = StringBuffer();
for (int i = 1; i < copies; i++) {
imports.writeln('// ignore: unused_import');
imports.writeln("import 'gallery_$i/main.dart' as main_$i;");
}
final String contents = '''
// Copyright 2014 The Flutter 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() {
runApp(const GalleryApp());
}
''';
mainFile.writeAsStringSync(contents);
}
void _copyGallery(Directory galleryDir, int index) {
final Directory lib = _dir(galleryDir, 'lib');
final Directory dest = _dir(lib, 'gallery_$index');
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(recursive: true);
}
for (final FileSystemEntity entity in source.listSync(followLinks: false)) {
final String name = path.basename(entity.path);
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());
}
}
}
Directory _dir(Directory parent, String name) => Directory(path.join(parent.path, name));
File _file(Directory parent, String name) => File(path.join(parent.path, name));
String _normalize(String filePath) => path.normalize(path.absolute(filePath));
class SourceStats {
int files = 0;
int lines = 0;
@override
String toString() => '${_comma(files).padLeft(3)} files, ${_comma(lines).padLeft(6)} lines';
}
SourceStats getStatsFor(Directory dir, [SourceStats? stats]) {
stats ??= SourceStats();
for (final FileSystemEntity entity in dir.listSync(followLinks: false)) {
final String name = path.basename(entity.path);
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) {
final String str = count.toString();
if (str.length > 3) {
return '${str.substring(0, str.length - 3)},${str.substring(str.length - 3)}';
}
return str;
}