|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:pubspec_parse/pubspec_parse.dart'; |
| 6 | + |
| 7 | +import '../package_config.dart'; |
| 8 | +import '../root_config.dart'; |
| 9 | +import 'mono_repo_command.dart'; |
| 10 | + |
| 11 | +class ListCommand extends MonoRepoCommand { |
| 12 | + ListCommand() { |
| 13 | + argParser |
| 14 | + ..addFlag( |
| 15 | + 'only-published', |
| 16 | + abbr: 'p', |
| 17 | + help: 'Only list packages with a version and without publish_to set to ' |
| 18 | + 'none.', |
| 19 | + ) |
| 20 | + ..addMultiOption( |
| 21 | + 'show', |
| 22 | + abbr: 's', |
| 23 | + help: |
| 24 | + 'The properties of the package to show in a comma-seperated list.', |
| 25 | + allowed: Column.values.map((e) => e.name), |
| 26 | + allowedHelp: {for (var item in Column.values) item.name: item.help}, |
| 27 | + defaultsTo: Column.values |
| 28 | + .where((element) => element.defaultsTo) |
| 29 | + .map((e) => e.name), |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + String get name => 'list'; |
| 35 | + |
| 36 | + @override |
| 37 | + String get description => 'List all packages configured for mono_repo.'; |
| 38 | + |
| 39 | + @override |
| 40 | + void run() => print( |
| 41 | + listPackages( |
| 42 | + rootConfig(), |
| 43 | + onlyPublished: argResults!['only-published'] as bool, |
| 44 | + showItems: (argResults!['show'] as List<String>) |
| 45 | + .map( |
| 46 | + (e) => |
| 47 | + Column.values.singleWhere((element) => element.name == e), |
| 48 | + ) |
| 49 | + .toSet(), |
| 50 | + ).join('\n'), |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +enum Column { |
| 55 | + name( |
| 56 | + help: 'The name of the package as specified in the "name" field.', |
| 57 | + defaultsTo: true, |
| 58 | + ), |
| 59 | + path( |
| 60 | + help: 'The path to the package relative to the root of the repository.', |
| 61 | + defaultsTo: true, |
| 62 | + ), |
| 63 | + version( |
| 64 | + help: 'The version of the package as specified in the "version" field.', |
| 65 | + defaultsTo: false, |
| 66 | + ), |
| 67 | + publishTo( |
| 68 | + help: 'The value of the "publish_to" field.', |
| 69 | + defaultsTo: false, |
| 70 | + ); |
| 71 | + |
| 72 | + const Column({ |
| 73 | + required this.help, |
| 74 | + required this.defaultsTo, |
| 75 | + }); |
| 76 | + |
| 77 | + final String help; |
| 78 | + final bool defaultsTo; |
| 79 | + |
| 80 | + String valueFor(PackageConfig cfg) { |
| 81 | + switch (this) { |
| 82 | + case Column.name: |
| 83 | + return cfg.pubspec.name; |
| 84 | + case Column.path: |
| 85 | + return cfg.relativePath; |
| 86 | + case Column.version: |
| 87 | + return cfg.pubspec.version?.toString() ?? ''; |
| 88 | + case Column.publishTo: |
| 89 | + return cfg.pubspec.publishTo ?? ''; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +Iterable<String> listPackages( |
| 95 | + RootConfig rootConfig, { |
| 96 | + required bool onlyPublished, |
| 97 | + required Set<Column> showItems, |
| 98 | +}) sync* { |
| 99 | + for (var pkg in rootConfig) { |
| 100 | + if (onlyPublished && !_published(pkg.pubspec)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + yield showItems.map((e) => e.valueFor(pkg)).join(','); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +bool _published(Pubspec pubspec) => |
| 108 | + pubspec.version != null && pubspec.publishTo != 'none'; |
0 commit comments