Skip to content

[tools] Don't check license of generated Swift package #8137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions script/tool/lib/src/license_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const Set<String> _ignoredFullBasenameList = <String>{
'resource.h', // Generated by VS.
};

// Path parts to ignore. Used to ignore entire subdirectories.
const Set<String> _ignorePathPartList = <String>{
'FlutterGeneratedPluginSwiftPackage', // Generated by Flutter tool.
};

// Third-party packages where the code doesn't have file-level annotation, just
// the package-level LICENSE file. Each entry must be a directory relative to
// third_party/packages, as that is the only directory where this is allowed.
Expand Down Expand Up @@ -227,7 +232,7 @@ class LicenseCheckCommand extends PackageCommand {
final List<File> unrecognizedThirdPartyFiles = <File>[];

// Most code file types in the repository use '//' comments.
final String defaultFirstParyLicenseBlock = _generateLicenseBlock('// ');
final String defaultFirstPartyLicenseBlock = _generateLicenseBlock('// ');
// A few file types have a different comment structure.
final Map<String, String> firstPartyLicenseBlockByExtension =
<String, String>{
Expand Down Expand Up @@ -258,19 +263,19 @@ class LicenseCheckCommand extends PackageCommand {
// still pass since they will be converted back on commit.
content = content.replaceAll('\r\n', '\n');

final String firstParyLicense =
final String firstPartyLicense =
firstPartyLicenseBlockByExtension[p.extension(file.path)] ??
defaultFirstParyLicenseBlock;
defaultFirstPartyLicenseBlock;
if (_isThirdParty(file)) {
// Third-party directories allow either known third-party licenses, our
// the first-party license, as there may be local additions.
if (!_thirdPartyLicenseBlockRegexes
.any((RegExp regex) => regex.hasMatch(content)) &&
!content.contains(firstParyLicense)) {
!content.contains(firstPartyLicense)) {
unrecognizedThirdPartyFiles.add(file);
}
} else {
if (!content.contains(firstParyLicense)) {
if (!content.contains(firstPartyLicense)) {
incorrectFirstPartyFiles.add(file);
}
}
Expand Down Expand Up @@ -305,11 +310,24 @@ class LicenseCheckCommand extends PackageCommand {
}

bool _shouldIgnoreFile(File file) {
final String path = file.path;
return _ignoreBasenameList.contains(p.basenameWithoutExtension(path)) ||
_ignoreSuffixList.any((String suffix) =>
path.endsWith(suffix) ||
_ignoredFullBasenameList.contains(p.basename(path)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrectly nested in _ignoreSuffixList.any

if (_ignoreBasenameList.contains(p.basenameWithoutExtension(file.path))) {
return true;
}

if (_ignoreSuffixList.any(file.path.endsWith)) {
return true;
}

if (_ignoredFullBasenameList.contains(p.basename(file.path))) {
return true;
}

final List<String> parts = path.split(file.path);
if (parts.any(_ignorePathPartList.contains)) {
return true;
}

return false;
}

bool _isThirdParty(File file) {
Expand Down
18 changes: 18 additions & 0 deletions script/tool/test/license_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ void main() {
}
});

test('ignores FlutterGeneratedPluginSwiftPackage', () async {
final Directory packageDir = root
.childDirectory('FlutterGeneratedPluginSwiftPackage')
..createSync();
packageDir.childFile('Package.swift').createSync();
packageDir
.childDirectory('Sources')
.childDirectory('FlutterGeneratedPluginSwiftPackage')
.childFile('FlutterGeneratedPluginSwiftPackage.swift')
.createSync(recursive: true);

final List<String> output =
await runCapturingPrint(runner, <String>['license-check']);

expect(output,
isNot(contains('Checking FlutterGeneratedPluginSwiftPackage')));
});

test('passes if all checked files have license blocks', () async {
final File checked = root.childFile('checked.cc');
checked.createSync();
Expand Down