Skip to content

Commit 7507826

Browse files
authored
feat: include license count upon check licenses progress completion (#856)
1 parent a8fac62 commit 7507826

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

lib/src/commands/packages/commands/check/commands/licenses.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,30 +288,39 @@ String _composeReport({
288288
if (licenses.isEmpty) return previousValue;
289289
return previousValue..addAll(licenses);
290290
});
291+
291292
final licenseTypes =
292-
licenses.values.fold(<String>{}, (previousValue, licenses) {
293+
licenses.values.fold(<String>[], (previousValue, licenses) {
293294
if (licenses == null) return previousValue;
294295
return previousValue..addAll(licenses);
295296
});
296-
final coloredLicenseTypes = licenseTypes.map((license) {
297-
if (bannedLicenseTypes != null && bannedLicenseTypes.contains(license)) {
298-
return red.wrap(license)!;
299-
}
300-
return green.wrap(license)!;
301-
});
302297

303-
final licenseCount = licenses.values.fold<int>(0, (previousValue, element) {
304-
if (element == null) return previousValue;
305-
return previousValue + element.length;
298+
final licenseCount = <String, int>{};
299+
for (final license in licenseTypes) {
300+
licenseCount.update(license, (value) => value + 1, ifAbsent: () => 1);
301+
}
302+
final totalLicenseCount = licenseCount.values
303+
.fold(0, (previousValue, count) => previousValue + count);
304+
305+
final formattedLicenseTypes = licenseTypes.toSet().map((license) {
306+
final colorWrapper =
307+
bannedLicenseTypes != null && bannedLicenseTypes.contains(license)
308+
? red.wrap
309+
: green.wrap;
310+
311+
final count = licenseCount[license];
312+
final formattedCount = darkGray.wrap('($count)');
313+
314+
return '${colorWrapper(license)} $formattedCount';
306315
});
307316

308-
final licenseWord = licenseCount == 1 ? 'license' : 'licenses';
317+
final licenseWord = totalLicenseCount == 1 ? 'license' : 'licenses';
309318
final packageWord = licenses.length == 1 ? 'package' : 'packages';
310-
final suffix = coloredLicenseTypes.isEmpty
319+
final suffix = formattedLicenseTypes.isEmpty
311320
? ''
312-
: ' of type: ${coloredLicenseTypes.toList().stringify()}';
321+
: ' of type: ${formattedLicenseTypes.toList().stringify()}';
313322

314-
return '''Retrieved $licenseCount $licenseWord from ${licenses.length} $packageWord$suffix.''';
323+
return '''Retrieved $totalLicenseCount $licenseWord from ${licenses.length} $packageWord$suffix.''';
315324
}
316325

317326
String _composeBannedReport(_BannedDependencyLicenseMap bannedDependencies) {

test/src/commands/packages/commands/check/commands/licenses_test.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void main() {
116116
).called(1);
117117
verify(
118118
() => progress.complete(
119-
'''Retrieved 1 license from 1 package of type: MIT.''',
119+
'''Retrieved 1 license from 1 package of type: MIT (1).''',
120120
),
121121
).called(1);
122122

@@ -151,7 +151,7 @@ void main() {
151151
).called(1);
152152
verify(
153153
() => progress.complete(
154-
'''Retrieved 4 licenses from 2 packages of type: MIT and BSD.''',
154+
'''Retrieved 4 licenses from 2 packages of type: MIT (2) and BSD (2).''',
155155
),
156156
).called(1);
157157

@@ -202,7 +202,7 @@ void main() {
202202
).called(1);
203203
verify(
204204
() => progress.complete(
205-
'Retrieved 1 license from 2 packages of type: MIT.',
205+
'Retrieved 1 license from 2 packages of type: MIT (1).',
206206
),
207207
).called(1);
208208

@@ -247,7 +247,7 @@ void main() {
247247
).called(1);
248248
verify(
249249
() => progress.complete(
250-
'Retrieved 1 license from 2 packages of type: MIT.',
250+
'Retrieved 1 license from 2 packages of type: MIT (1).',
251251
),
252252
).called(1);
253253

@@ -385,7 +385,7 @@ void main() {
385385
).called(1);
386386
verify(
387387
() => progress.complete(
388-
'Retrieved 1 license from 1 package of type: MIT.',
388+
'Retrieved 1 license from 1 package of type: MIT (1).',
389389
),
390390
).called(1);
391391

@@ -434,7 +434,7 @@ void main() {
434434
).called(1);
435435
verify(
436436
() => progress.complete(
437-
'Retrieved 1 license from 1 package of type: MIT.',
437+
'Retrieved 1 license from 1 package of type: MIT (1).',
438438
),
439439
).called(1);
440440

@@ -484,7 +484,7 @@ void main() {
484484
).called(1);
485485
verify(
486486
() => progress.complete(
487-
'Retrieved 1 license from 1 package of type: MIT.',
487+
'Retrieved 1 license from 1 package of type: MIT (1).',
488488
),
489489
).called(1);
490490

@@ -533,7 +533,7 @@ void main() {
533533
).called(1);
534534
verify(
535535
() => progress.complete(
536-
'Retrieved 1 license from 1 package of type: MIT.',
536+
'Retrieved 1 license from 1 package of type: MIT (1).',
537537
),
538538
).called(1);
539539

@@ -592,7 +592,7 @@ void main() {
592592
).called(1);
593593
verify(
594594
() => progress.complete(
595-
'Retrieved 3 licenses from 3 packages of type: MIT.',
595+
'Retrieved 3 licenses from 3 packages of type: MIT (3).',
596596
),
597597
).called(1);
598598

0 commit comments

Comments
 (0)