Skip to content

Commit

Permalink
🔨 Adds Melos Packages script
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Nov 5, 2023
1 parent d9d90f3 commit 656bbcd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
76 changes: 76 additions & 0 deletions scripts/melos_packages.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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<String>()
.join(',');
print(satisfiedPackages);
} else {
final ignoresPackages = packages
.map(
(package) {
if (package.pubSpec.environment!.sdkConstraint!.allows(current)) {
return null;
}
return package.name;
},
)
.whereType<String>()
.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,
);
}
}

0 comments on commit 656bbcd

Please sign in to comment.