Skip to content

Commit

Permalink
rename the 'map' command to 'run'
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Jan 10, 2025
1 parent 25c57b8 commit b601c31
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 71 deletions.
9 changes: 7 additions & 2 deletions pkgs/puppy/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## What's this?

A package for miscellaneous Dart CLI tools.

### Running

Activate locally using:

```console
Expand All @@ -7,5 +13,4 @@ dart pub global activate --source=path .

### Commands

- `run`: runs a command in every directory containing
`pubspec.yaml`.
- `run`: runs a command in every directory containing `pubspec.yaml`.
4 changes: 2 additions & 2 deletions pkgs/puppy/bin/puppy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import 'package:args/command_runner.dart';
import 'package:puppy/src/constants.dart';
import 'package:puppy/src/map_command.dart';
import 'package:puppy/src/run_command.dart';

Future<void> main(List<String> args) async {
var runner = CommandRunner<void>(cmdName, 'Dart repository management tools.')
..addCommand(MapCommand());
..addCommand(RunCommand());

await runner.run(args);
}
34 changes: 0 additions & 34 deletions pkgs/puppy/lib/src/map_command.g.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import 'package:build_cli_annotations/build_cli_annotations.dart';
import 'package:io/ansi.dart';

import 'constants.dart';
import 'utils.dart';

part 'map_command.g.dart';
part 'run_command.g.dart';

class MapCommand extends _$MapArgsCommand<void> {
class RunCommand extends _$RunArgsCommand<void> {
@override
String get description =>
'Run the provided command in each subdirectory containing '
Expand All @@ -29,13 +30,13 @@ class MapCommand extends _$MapArgsCommand<void> {
}

@CliOptions(createCommand: true)
class MapArgs {
class RunArgs {
@CliOption(abbr: 'd', help: 'Keep looking for "nested" pubspec files.')
final bool deep;

final List<String> rest;

MapArgs({
RunArgs({
this.deep = false,
required this.rest,
}) {
Expand All @@ -48,41 +49,25 @@ class MapArgs {
}
}

Future<void> _doMap(MapArgs args) async {
Future<void> _doMap(RunArgs args) async {
final exe = args.rest.first;
final extraArgs = args.rest.skip(1).toList();

final packages = findPackages(Directory.current, deep: args.deep);
final exits = <String, int>{};

Future<void> inspectDirectory(Directory dir, {required bool deep}) async {
final pubspecs = dir
.listSync()
.whereType<File>()
.where((element) => element.uri.pathSegments.last == 'pubspec.yaml')
.toList();
for (final packageDir in packages) {
print(green.wrap(packageDir.path));
final proc = await Process.start(
exe,
extraArgs,
mode: ProcessStartMode.inheritStdio,
workingDirectory: packageDir.path,
);

final pubspecHere = pubspecs.isNotEmpty;
if (pubspecHere) {
print(green.wrap(dir.path));
final proc = await Process.start(
exe,
extraArgs,
mode: ProcessStartMode.inheritStdio,
workingDirectory: dir.path,
);

// TODO(kevmoo): display a summary of results on completion
exits[dir.path] = await proc.exitCode;
}
// TODO(kevmoo): display a summary of results on completion
exits[packageDir.path] = await proc.exitCode;

if (!pubspecHere || deep) {
for (var subDir in dir.listSync().whereType<Directory>().where(
(element) => !element.uri.pathSegments
.any((element) => element.startsWith('.')))) {
await inspectDirectory(subDir, deep: deep);
}
}
print('');
}

await inspectDirectory(Directory.current, deep: args.deep);
}
34 changes: 34 additions & 0 deletions pkgs/puppy/lib/src/run_command.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions pkgs/puppy/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. 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';

List<Directory> findPackages(Directory root, {bool deep = false}) {
final results = <Directory>[];

void traverse(Directory dir, {required bool deep}) {
final pubspecs = dir
.listSync()
.whereType<File>()
.where((element) => element.uri.pathSegments.last == 'pubspec.yaml')
.toList();

if (pubspecs.isNotEmpty) {
results.add(dir);
}

if (!pubspecs.isNotEmpty || deep) {
for (var subDir in dir.listSync().whereType<Directory>().where(
(element) => !element.uri.pathSegments
.any((element) => element.startsWith('.')))) {
traverse(subDir, deep: deep);
}
}
}

traverse(Directory.current, deep: deep);

return results;
}
1 change: 1 addition & 0 deletions pkgs/puppy/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: puppy
description: A package for miscellaneous Dart CLI tools.
publish_to: none

environment:
Expand Down

0 comments on commit b601c31

Please sign in to comment.