godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 5 | import 'dart:core'; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 6 | import 'dart:io'; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 7 | import 'package:path/path.dart' as path; |
| 8 | import 'dart:io' as io_internals show exit; |
| 9 | |
| 10 | final bool hasColor = stdout.supportsAnsiEscapes; |
| 11 | final String bold = hasColor ? '\x1B[1m' : ''; // used for shard titles |
| 12 | final String red = hasColor ? '\x1B[31m' : ''; // used for errors |
| 13 | final String reset = hasColor ? '\x1B[0m' : ''; |
| 14 | final String reverse = hasColor ? '\x1B[7m' : ''; // used for clocks |
| 15 | |
Casey Hillers | 8c85a6a | 2023-03-17 09:43:49 -0700 | [diff] [blame] | 16 | Future<void> main() async { |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 17 | print('$clock STARTING ANALYSIS'); |
| 18 | try { |
Casey Hillers | 8c85a6a | 2023-03-17 09:43:49 -0700 | [diff] [blame] | 19 | await run(); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 20 | } on ExitException catch (error) { |
| 21 | error.apply(); |
| 22 | } |
| 23 | print('$clock ${bold}Analysis successful.$reset'); |
| 24 | } |
| 25 | |
Casey Hillers | 8c85a6a | 2023-03-17 09:43:49 -0700 | [diff] [blame] | 26 | Future<void> run() async { |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 27 | final String cocoonPath = path.join(path.dirname(Platform.script.path), '..'); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 28 | print('$clock Root path: $cocoonPath'); |
| 29 | print('$clock Licenses...'); |
Casey Hillers | 8c85a6a | 2023-03-17 09:43:49 -0700 | [diff] [blame] | 30 | await verifyConsistentLicenses(cocoonPath); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 31 | await verifyNoMissingLicense(cocoonPath); |
| 32 | } |
| 33 | |
| 34 | // TESTS |
| 35 | String _generateLicense(String prefix) { |
Nehal Patel | 34f808c | 2023-01-25 13:43:17 -0800 | [diff] [blame] | 36 | return '${prefix}Copyright (2014|2015|2016|2017|2018|2019|2020|2021|2022|2023) The Flutter Authors. All rights reserved.\n' |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 37 | '${prefix}Use of this source code is governed by a BSD-style license that can be\n' |
| 38 | '${prefix}found in the LICENSE file.'; |
| 39 | } |
| 40 | |
Casey Hillers | 8c85a6a | 2023-03-17 09:43:49 -0700 | [diff] [blame] | 41 | /// Ensure that LICENSES in Cocoon and its packages are consistent with each other. |
| 42 | /// |
| 43 | /// Verifies that every LICENSE file in Cocoon matches cocoon/LICENSE. |
| 44 | Future<void> verifyConsistentLicenses(String workingDirectory) async { |
| 45 | final String goldenLicensePath = '$workingDirectory/LICENSE'; |
| 46 | final String goldenLicense = File(goldenLicensePath).readAsStringSync(); |
| 47 | if (goldenLicense.isEmpty) { |
| 48 | throw Exception('No LICENSE was found at the root of Cocoon'); |
| 49 | } |
| 50 | |
| 51 | final List<String> badLicenses = <String>[]; |
| 52 | for (final FileSystemEntity entity in Directory(workingDirectory).listSync(recursive: true)) { |
| 53 | final String cocoonPath = entity.path.split('/../').last; |
| 54 | if (cocoonPath.contains(RegExp('(\.git)|(\.dart_tool)|(\.plugin_symlinks)'))) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | if (path.basename(entity.path) == 'LICENSE') { |
| 59 | final String license = File(entity.path).readAsStringSync(); |
| 60 | if (license != goldenLicense) { |
| 61 | badLicenses.add(cocoonPath); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (badLicenses.isNotEmpty) { |
| 67 | exitWithError( |
| 68 | <String>['The following LICENSE files do not match the golden LICENSE at root:']..insertAll(1, badLicenses), |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 73 | Future<void> verifyNoMissingLicense(String workingDirectory, {bool checkMinimums = true}) async { |
Casey Hillers | fddb3f5 | 2021-09-27 12:33:01 -0700 | [diff] [blame] | 74 | final int? overrideMinimumMatches = checkMinimums ? null : 0; |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 75 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 76 | workingDirectory, |
| 77 | 'dart', |
| 78 | overrideMinimumMatches ?? 2000, |
| 79 | _generateLicense('// '), |
| 80 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 81 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 82 | workingDirectory, |
| 83 | 'java', |
| 84 | overrideMinimumMatches ?? 39, |
| 85 | _generateLicense('// '), |
| 86 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 87 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 88 | workingDirectory, |
| 89 | 'h', |
| 90 | overrideMinimumMatches ?? 30, |
| 91 | _generateLicense('// '), |
| 92 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 93 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 94 | workingDirectory, |
| 95 | 'm', |
| 96 | overrideMinimumMatches ?? 30, |
| 97 | _generateLicense('// '), |
| 98 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 99 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 100 | workingDirectory, |
| 101 | 'swift', |
| 102 | overrideMinimumMatches ?? 10, |
| 103 | _generateLicense('// '), |
| 104 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 105 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 106 | workingDirectory, |
| 107 | 'gradle', |
| 108 | overrideMinimumMatches ?? 100, |
| 109 | _generateLicense('// '), |
| 110 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 111 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 112 | workingDirectory, |
| 113 | 'gn', |
| 114 | overrideMinimumMatches ?? 0, |
| 115 | _generateLicense('# '), |
| 116 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 117 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 118 | workingDirectory, |
| 119 | 'Dockerfile', |
| 120 | overrideMinimumMatches ?? 1, |
| 121 | _generateLicense('# '), |
| 122 | ); |
godofredoc | 7bcd333 | 2022-03-22 22:15:01 -0700 | [diff] [blame] | 123 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 124 | workingDirectory, |
| 125 | 'sh', |
| 126 | overrideMinimumMatches ?? 1, |
| 127 | '#!/bin/bash\n' + _generateLicense('# '), |
| 128 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 129 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 130 | workingDirectory, |
| 131 | 'bat', |
| 132 | overrideMinimumMatches ?? 1, |
| 133 | _generateLicense(':: '), |
| 134 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 135 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 136 | workingDirectory, |
| 137 | 'ps1', |
| 138 | overrideMinimumMatches ?? 1, |
| 139 | _generateLicense('# '), |
| 140 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 141 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 142 | workingDirectory, |
| 143 | 'html', |
| 144 | overrideMinimumMatches ?? 1, |
| 145 | '<!-- ${_generateLicense('')} -->', |
| 146 | trailingBlank: false, |
| 147 | ); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 148 | await _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 149 | workingDirectory, |
| 150 | 'xml', |
| 151 | overrideMinimumMatches ?? 1, |
| 152 | '<!-- ${_generateLicense('')} -->', |
| 153 | ); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 156 | Future<void> _verifyNoMissingLicenseForExtension( |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 157 | String workingDirectory, |
| 158 | String extension, |
| 159 | int minimumMatches, |
| 160 | String license, { |
| 161 | bool trailingBlank = true, |
| 162 | }) async { |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 163 | assert(!license.endsWith('\n')); |
| 164 | final String licensePattern = license + '\n' + (trailingBlank ? '\n' : ''); |
| 165 | final List<String> errors = <String>[]; |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 166 | for (final File file in _allFiles(workingDirectory, extension, minimumMatches: minimumMatches)) { |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 167 | final String contents = file.readAsStringSync().replaceAll('\r\n', '\n'); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 168 | if (contents.isEmpty) continue; // let's not go down the /bin/true rabbit hole |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 169 | if (!contents.startsWith(RegExp(licensePattern))) errors.add(file.path); |
| 170 | } |
| 171 | // Fail if any errors |
| 172 | if (errors.isNotEmpty) { |
| 173 | final String s = errors.length == 1 ? ' does' : 's do'; |
| 174 | exitWithError(<String>[ |
| 175 | '${bold}The following ${errors.length} file$s not have the right license header:$reset', |
| 176 | ...errors, |
| 177 | 'The expected license header is:', |
| 178 | license, |
| 179 | if (trailingBlank) '...followed by a blank line.', |
| 180 | ]); |
| 181 | } |
| 182 | } |
| 183 | |
Casey Hillers | fddb3f5 | 2021-09-27 12:33:01 -0700 | [diff] [blame] | 184 | Iterable<File> _allFiles(String workingDirectory, String extension, {required int minimumMatches}) sync* { |
| 185 | assert(!extension.startsWith('.'), 'Extension argument should not start with a period.'); |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 186 | final Set<FileSystemEntity> pending = <FileSystemEntity>{Directory(workingDirectory)}; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 187 | int matches = 0; |
| 188 | while (pending.isNotEmpty) { |
| 189 | final FileSystemEntity entity = pending.first; |
| 190 | pending.remove(entity); |
| 191 | if (path.extension(entity.path) == '.tmpl') continue; |
| 192 | if (entity is File) { |
| 193 | if (_isGeneratedPluginRegistrant(entity)) continue; |
Greg Spencer | 3973775 | 2021-01-14 09:49:59 -0800 | [diff] [blame] | 194 | if (path.basename(entity.path) == 'AppDelegate.h') continue; |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 195 | if (path.basename(entity.path) == 'flutter_export_environment.sh') continue; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 196 | if (path.basename(entity.path) == 'gradlew.bat') continue; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 197 | if (path.basename(entity.path) == 'Runner-Bridging-Header.h') continue; |
| 198 | if (path.basename(entity.path).endsWith('g.dart')) continue; |
Casey Hillers | c81dd66 | 2021-09-23 16:02:59 -0700 | [diff] [blame] | 199 | if (path.basename(entity.path).endsWith('mocks.mocks.dart')) continue; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 200 | if (path.basename(entity.path).endsWith('pb.dart')) continue; |
Greg Spencer | 3973775 | 2021-01-14 09:49:59 -0800 | [diff] [blame] | 201 | if (path.basename(entity.path).endsWith('pbenum.dart')) continue; |
Casey Hillers | de0c55b | 2021-03-25 14:36:30 -0700 | [diff] [blame] | 202 | if (path.basename(entity.path).endsWith('pbjson.dart')) continue; |
Casey Hillers | d8b9424 | 2021-06-29 12:31:00 -0700 | [diff] [blame] | 203 | if (path.basename(entity.path).endsWith('pbserver.dart')) continue; |
Casey Hillers | fddb3f5 | 2021-09-27 12:33:01 -0700 | [diff] [blame] | 204 | if (path.extension(entity.path) == '.$extension') { |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 205 | matches += 1; |
| 206 | yield entity; |
| 207 | } |
godofredoc | 7bcd333 | 2022-03-22 22:15:01 -0700 | [diff] [blame] | 208 | if (path.basename(entity.path) == 'Dockerfile' && extension == 'Dockerfile') { |
| 209 | matches += 1; |
| 210 | yield entity; |
| 211 | } |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 212 | } else if (entity is Directory) { |
| 213 | if (File(path.join(entity.path, '.dartignore')).existsSync()) continue; |
| 214 | if (path.basename(entity.path) == '.git') continue; |
| 215 | if (path.basename(entity.path) == '.gradle') continue; |
| 216 | if (path.basename(entity.path) == '.dart_tool') continue; |
Greg Spencer | 2191ed1 | 2021-01-19 20:14:44 -0800 | [diff] [blame] | 217 | if (_isPartOfAppTemplate(entity)) continue; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 218 | pending.addAll(entity.listSync()); |
| 219 | } |
| 220 | } |
Ricardo Amador | fa3d7c1 | 2022-11-03 13:15:05 -0700 | [diff] [blame] | 221 | assert( |
| 222 | matches >= minimumMatches, |
| 223 | 'Expected to find at least $minimumMatches files with extension ".$extension" in "$workingDirectory", but only found $matches.', |
| 224 | ); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Greg Spencer | 2191ed1 | 2021-01-19 20:14:44 -0800 | [diff] [blame] | 227 | bool _isPartOfAppTemplate(Directory directory) { |
| 228 | const Set<String> templateDirs = <String>{ |
| 229 | 'android', |
| 230 | 'build', |
| 231 | 'ios', |
| 232 | 'linux', |
| 233 | 'macos', |
| 234 | 'web', |
| 235 | 'windows', |
| 236 | }; |
| 237 | // Project directories will have a metadata file in them. |
| 238 | if (File(path.join(directory.parent.path, '.metadata')).existsSync()) { |
| 239 | return templateDirs.contains(path.basename(directory.path)); |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 244 | bool _isGeneratedPluginRegistrant(File file) { |
Greg Spencer | 3973775 | 2021-01-14 09:49:59 -0800 | [diff] [blame] | 245 | final String filename = path.basenameWithoutExtension(file.path); |
| 246 | return !path.split(file.path).contains('.pub-cache') && |
| 247 | (filename == 'generated_plugin_registrant' || filename == 'GeneratedPluginRegistrant'); |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void exitWithError(List<String> messages) { |
Kate Lovett | 96770cc | 2020-07-17 12:57:13 -0700 | [diff] [blame] | 251 | final String redLine = '$red━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━$reset'; |
godofredoc | a6db0e2 | 2020-05-18 17:44:39 -0700 | [diff] [blame] | 252 | print(redLine); |
| 253 | messages.forEach(print); |
| 254 | print(redLine); |
| 255 | exit(1); |
| 256 | } |
| 257 | |
| 258 | class ExitException implements Exception { |
| 259 | ExitException(this.exitCode); |
| 260 | |
| 261 | final int exitCode; |
| 262 | |
| 263 | void apply() { |
| 264 | io_internals.exit(exitCode); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | String get clock { |
| 269 | final DateTime now = DateTime.now(); |
| 270 | return '$reverse▌' |
| 271 | '${now.hour.toString().padLeft(2, "0")}:' |
| 272 | '${now.minute.toString().padLeft(2, "0")}:' |
| 273 | '${now.second.toString().padLeft(2, "0")}' |
| 274 | '▐$reset'; |
| 275 | } |