Repository: VeryGoodOpenSource/very_good_cli
Pull request #856 (2023-10-17 08:03:40)

feat: include license count upon `check licenses` progress completion
<!--
  Thanks for contributing!

  Provide a description of your changes below and a general summary in the title

  Please look at the following checklist to ensure that your PR can be accepted quickly:
-->

## Description

Updates the progress completion message to include the count of dependencies per license.

## Demonstration

https://github.com/VeryGoodOpenSource/very_good_cli/assets/44524995/f5e448f8-85be-46d0-84c5-69b9a8944d4f

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [X] ✨ New feature (non-breaking change which adds functionality)
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
- [ ] 🧹 Code refactor
- [ ] ✅ Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore



Patch:
diff --git a/lib/src/commands/packages/commands/check/commands/licenses.dart b/lib/src/commands/packages/commands/check/commands/licenses.dart
index 7d41a03..d115430 100644
--- a/lib/src/commands/packages/commands/check/commands/licenses.dart
+++ b/lib/src/commands/packages/commands/check/commands/licenses.dart
@@ -288,30 +288,39 @@ String _composeReport({
     if (licenses.isEmpty) return previousValue;
     return previousValue..addAll(licenses);
   });
+
   final licenseTypes =
-      licenses.values.fold(<String>{}, (previousValue, licenses) {
+      licenses.values.fold(<String>[], (previousValue, licenses) {
     if (licenses == null) return previousValue;
     return previousValue..addAll(licenses);
   });
-  final coloredLicenseTypes = licenseTypes.map((license) {
-    if (bannedLicenseTypes != null && bannedLicenseTypes.contains(license)) {
-      return red.wrap(license)!;
-    }
-    return green.wrap(license)!;
-  });
 
-  final licenseCount = licenses.values.fold<int>(0, (previousValue, element) {
-    if (element == null) return previousValue;
-    return previousValue + element.length;
+  final licenseCount = <String, int>{};
+  for (final license in licenseTypes) {
+    licenseCount.update(license, (value) => value + 1, ifAbsent: () => 1);
+  }
+  final totalLicenseCount = licenseCount.values
+      .fold(0, (previousValue, count) => previousValue + count);
+
+  final formattedLicenseTypes = licenseTypes.toSet().map((license) {
+    final colorWrapper =
+        bannedLicenseTypes != null && bannedLicenseTypes.contains(license)
+            ? red.wrap
+            : green.wrap;
+
+    final count = licenseCount[license];
+    final formattedCount = darkGray.wrap('($count)');
+
+    return '${colorWrapper(license)} $formattedCount';
   });
 
-  final licenseWord = licenseCount == 1 ? 'license' : 'licenses';
+  final licenseWord = totalLicenseCount == 1 ? 'license' : 'licenses';
   final packageWord = licenses.length == 1 ? 'package' : 'packages';
-  final suffix = coloredLicenseTypes.isEmpty
+  final suffix = formattedLicenseTypes.isEmpty
       ? ''
-      : ' of type: ${coloredLicenseTypes.toList().stringify()}';
+      : ' of type: ${formattedLicenseTypes.toList().stringify()}';
 
-  return '''Retrieved $licenseCount $licenseWord from ${licenses.length} $packageWord$suffix.''';
+  return '''Retrieved $totalLicenseCount $licenseWord from ${licenses.length} $packageWord$suffix.''';
 }
 
 String _composeBannedReport(_BannedDependencyLicenseMap bannedDependencies) {


Test patch:
diff --git a/test/src/commands/packages/commands/check/commands/licenses_test.dart b/test/src/commands/packages/commands/check/commands/licenses_test.dart
index 3da0f6b..82b25d6 100644
--- a/test/src/commands/packages/commands/check/commands/licenses_test.dart
+++ b/test/src/commands/packages/commands/check/commands/licenses_test.dart
@@ -116,7 +116,7 @@ void main() {
             ).called(1);
             verify(
               () => progress.complete(
-                '''Retrieved 1 license from 1 package of type: MIT.''',
+                '''Retrieved 1 license from 1 package of type: MIT (1).''',
               ),
             ).called(1);
 
@@ -151,7 +151,7 @@ void main() {
             ).called(1);
             verify(
               () => progress.complete(
-                '''Retrieved 4 licenses from 2 packages of type: MIT and BSD.''',
+                '''Retrieved 4 licenses from 2 packages of type: MIT (2) and BSD (2).''',
               ),
             ).called(1);
 
@@ -202,7 +202,7 @@ void main() {
             ).called(1);
             verify(
               () => progress.complete(
-                'Retrieved 1 license from 2 packages of type: MIT.',
+                'Retrieved 1 license from 2 packages of type: MIT (1).',
               ),
             ).called(1);
 
@@ -247,7 +247,7 @@ void main() {
             ).called(1);
             verify(
               () => progress.complete(
-                'Retrieved 1 license from 2 packages of type: MIT.',
+                'Retrieved 1 license from 2 packages of type: MIT (1).',
               ),
             ).called(1);
 
@@ -385,7 +385,7 @@ void main() {
                 ).called(1);
                 verify(
                   () => progress.complete(
-                    'Retrieved 1 license from 1 package of type: MIT.',
+                    'Retrieved 1 license from 1 package of type: MIT (1).',
                   ),
                 ).called(1);
 
@@ -434,7 +434,7 @@ void main() {
                 ).called(1);
                 verify(
                   () => progress.complete(
-                    'Retrieved 1 license from 1 package of type: MIT.',
+                    'Retrieved 1 license from 1 package of type: MIT (1).',
                   ),
                 ).called(1);
 
@@ -484,7 +484,7 @@ void main() {
               ).called(1);
               verify(
                 () => progress.complete(
-                  'Retrieved 1 license from 1 package of type: MIT.',
+                  'Retrieved 1 license from 1 package of type: MIT (1).',
                 ),
               ).called(1);
 
@@ -533,7 +533,7 @@ void main() {
               ).called(1);
               verify(
                 () => progress.complete(
-                  'Retrieved 1 license from 1 package of type: MIT.',
+                  'Retrieved 1 license from 1 package of type: MIT (1).',
                 ),
               ).called(1);
 
@@ -592,7 +592,7 @@ void main() {
               ).called(1);
               verify(
                 () => progress.complete(
-                  'Retrieved 3 licenses from 3 packages of type: MIT.',
+                  'Retrieved 3 licenses from 3 packages of type: MIT (3).',
                 ),
               ).called(1);