[flutter_plugin_tools] Adds update-excerpts command (#5339)

This commit is contained in:
stuartmorgan
2022-04-28 12:34:11 -04:00
committed by GitHub
parent 88829b67ac
commit 4cecb9b264
9 changed files with 604 additions and 6 deletions

View File

@ -3,7 +3,9 @@
// found in the LICENSE file.
import 'package:file/file.dart';
import 'package:git/git.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
import 'common/core.dart';
import 'common/plugin_command.dart';
@ -105,7 +107,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Validates that code files have copyright and license blocks.
class LicenseCheckCommand extends PluginCommand {
/// Creates a new license check command for [packagesDir].
LicenseCheckCommand(Directory packagesDir) : super(packagesDir);
LicenseCheckCommand(Directory packagesDir,
{Platform platform = const LocalPlatform(), GitDir? gitDir})
: super(packagesDir, platform: platform, gitDir: gitDir);
@override
final String name = 'license-check';
@ -116,7 +120,14 @@ class LicenseCheckCommand extends PluginCommand {
@override
Future<void> run() async {
final Iterable<File> allFiles = await _getAllFiles();
// Create a set of absolute paths to submodule directories, with trailing
// separator, to do prefix matching with to test directory inclusion.
final Iterable<String> submodulePaths = (await _getSubmoduleDirectories())
.map(
(Directory dir) => '${dir.absolute.path}${platform.pathSeparator}');
final Iterable<File> allFiles = (await _getAllFiles()).where(
(File file) => !submodulePaths.any(file.absolute.path.startsWith));
final Iterable<File> codeFiles = allFiles.where((File file) =>
_codeFileExtensions.contains(p.extension(file.path)) &&
@ -275,6 +286,24 @@ class LicenseCheckCommand extends PluginCommand {
.where((FileSystemEntity entity) => entity is File)
.map((FileSystemEntity file) => file as File)
.toList();
// Returns the directories containing mapped submodules, if any.
Future<Iterable<Directory>> _getSubmoduleDirectories() async {
final List<Directory> submodulePaths = <Directory>[];
final Directory repoRoot =
packagesDir.fileSystem.directory((await gitDir).path);
final File submoduleSpec = repoRoot.childFile('.gitmodules');
if (submoduleSpec.existsSync()) {
final RegExp pathLine = RegExp(r'path\s*=\s*(.*)');
for (final String line in submoduleSpec.readAsLinesSync()) {
final RegExpMatch? match = pathLine.firstMatch(line);
if (match != null) {
submodulePaths.add(repoRoot.childDirectory(match.group(1)!.trim()));
}
}
}
return submodulePaths;
}
}
enum _LicenseFailureType { incorrectFirstParty, unknownThirdParty }