From 656bbcdb2004296f37408988e8f8997380b705ba Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 5 Nov 2023 22:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Adds=20Melos=20Packages=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- pubspec.yaml | 5 ++- scripts/melos_packages.dart | 76 +++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 scripts/melos_packages.dart diff --git a/.gitignore b/.gitignore index 335cd5440..a2376a977 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ .example/flutter.png build/ # Remove the following pattern if you wish to check in your lock file -**/pubspec.lock +pubspec.lock # Directory created by dartdoc doc/api/ diff --git a/pubspec.yaml b/pubspec.yaml index d5d91906d..b3184f3db 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,4 +7,7 @@ environment: dev_dependencies: lints: any - melos: ^3.1.0 + melos: any + cli_util: any # Required by custom melos command. + pub_semver: any # Required by custom melos command. + yaml: any # Required by custom melos command. diff --git a/scripts/melos_packages.dart b/scripts/melos_packages.dart new file mode 100644 index 000000000..787fdb91e --- /dev/null +++ b/scripts/melos_packages.dart @@ -0,0 +1,76 @@ +import 'dart:io'; + +import 'package:cli_util/cli_logging.dart' show Logger; +import 'package:melos/melos.dart' + show MelosLogger, MelosWorkspace, MelosWorkspaceConfig; +import 'package:pub_semver/pub_semver.dart'; +import 'package:yaml/yaml.dart'; + +void main(List arguments) async { + final root = Platform.environment['MELOS_ROOT_PATH'] as String; + final config = MelosWorkspaceConfig.fromYaml( + loadYamlNode( + File('$root/melos.yaml').readAsStringSync(), + ).toPlainObject() as Map, + path: root, + ); + final workspace = await MelosWorkspace.fromConfig( + config, + logger: MelosLogger(Logger.standard()), + ); + final packages = workspace.filteredPackages.values; + final current = Version.parse( + RegExp(r'\d*\.\d*\.\d*').firstMatch(Platform.version)!.group(0)!, + ); + if (arguments.first == 'true') { + final satisfiedPackages = packages + .map( + (package) { + if (package.pubSpec.environment!.sdkConstraint!.allows(current)) { + return package.name; + } + return null; + }, + ) + .whereType() + .join(','); + print(satisfiedPackages); + } else { + final ignoresPackages = packages + .map( + (package) { + if (package.pubSpec.environment!.sdkConstraint!.allows(current)) { + return null; + } + return package.name; + }, + ) + .whereType() + .map((e) => '--ignore="$e"') + .join(' '); + print(ignoresPackages); + } +} + +extension YamlUtils on YamlNode { + /// Converts a YAML node to a regular mutable Dart object. + Object? toPlainObject() { + final node = this; + if (node is YamlScalar) { + return node.value; + } + if (node is YamlMap) { + return { + for (final entry in node.nodes.entries) + (entry.key as YamlNode).toPlainObject(): entry.value.toPlainObject(), + }; + } + if (node is YamlList) { + return node.nodes.map((node) => node.toPlainObject()).toList(); + } + throw FormatException( + 'Unsupported YAML node type encountered: ${node.runtimeType}', + this, + ); + } +}