From 8c9a77dca7503c8aef5b3ffe0e0ff4ca1f7efa8e Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 17 Jan 2017 15:57:19 -0800 Subject: [PATCH] Search full license root subtree in search by type (flutter/engine#3343) Previously, search by license type only searched the contents of the chain of direct ancestor subdirectories. This change broadens the search to the full subtree of the current license root when no license is found in the chain of direct ancestor directories. --- .../src/flutter/tools/licenses/lib/main.dart | 56 +++++++++++++++++-- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/tools/licenses/lib/main.dart b/engine/src/flutter/tools/licenses/lib/main.dart index 44b5985425..c51301c2ea 100644 --- a/engine/src/flutter/tools/licenses/lib/main.dart +++ b/engine/src/flutter/tools/licenses/lib/main.dart @@ -1024,21 +1024,65 @@ class RepositoryDirectory extends RepositoryEntry implements LicenseSource { @override License nearestLicenseOfType(LicenseType type) { + License result = _nearestAncestorLicenseWithType(type); + if (result == null) { + for (RepositoryDirectory directory in _subdirectories) { + result = directory._localLicenseWithType(type); + if (result != null) + break; + } + } + result ??= _fullWalkUpForLicenseWithType(type); + return result; + } + + /// Searches the current and all parent directories (up to the license root) + /// for a license of the specified type. + License _nearestAncestorLicenseWithType(type) { + License result = _localLicenseWithType(type); + if (result != null) + return result; + if (_canGoUp(null)) + return parent._nearestAncestorLicenseWithType(type); + return null; + } + + /// Searches all subdirectories below the current license root for a license + /// of the specified type. + License _fullWalkUpForLicenseWithType(LicenseType type) { + return _canGoUp(null) + ? parent._fullWalkUpForLicenseWithType(type) + : _fullWalkDownForLicenseWithType(type); + } + + /// Searches the current directory and all subdirectories for a license of + /// the specified type. + License _fullWalkDownForLicenseWithType(LicenseType type) { + License result = _localLicenseWithType(type); + if (result == null) { + for (RepositoryDirectory directory in _subdirectories) { + result = directory._fullWalkDownForLicenseWithType(type); + if (result != null) + break; + } + } + return result; + } + + /// Searches the current directory for licenses of the specified type. + License _localLicenseWithType(LicenseType type) { List licenses = _licenses.expand/*License*/((RepositoryLicenseFile license) sync* { License result = license.licenseOfType(type); if (result != null) yield result; }).toList(); - if (licenses.isEmpty) { - if (_canGoUp(null)) - return parent.nearestLicenseOfType(type); - return null; - } if (licenses.length > 1) { print('unexpectedly found multiple matching licenses in $name of type $type'); return null; } - return licenses.single; + if (licenses.isNotEmpty) + return licenses.single; + return null; } @override