import "dart:io"; import "package:build/build.dart"; import "package:dart_pubspec_licenses/dart_pubspec_licenses.dart"; import "package:yaml/yaml.dart"; final googleBSD1 = [ "Redistribution and use in source and binary forms, with or without", "modification, are permitted provided that the following conditions are", "met:" ]; final googleBSD2 = [ "Redistribution and use in source and binary forms, with or without modification," "are permitted provided that the following conditions are met:" ]; /// Google's provided BSD-3-Clause does not contain the fact that it is a /// BSD-3-Clause license, so handle that one explicitly. bool matchesGoogleBSD3(String text) { return googleBSD1.every(text.contains) || googleBSD2.every(text.contains); } /// uuid has a weirdly formatted MIT. bool matchesMIT(String text) { return [ 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:', "MIT" ].any(text.contains) && !text.contains("GNU GENERAL PUBLIC LICENSE"); } String _licenseFromText(String? text) { if (text == null) { return "Unknown"; } if (text.contains("Apache-2.0") || (text.contains("Apache License") && text.contains("Version 2.0"))) { return "Apache-2.0"; } else if (["BSD-3-Clause", "BSD 3-Clause"].any(text.contains) || matchesGoogleBSD3(text)) { return "BSD-3-Clause"; } else if (matchesMIT(text)) { return "MIT"; } else if (text.contains("GNU GENERAL PUBLIC LICENSE") && text.contains("Version 3")) { return "GPL3"; } else { return "Unknown"; } } class LicenseBuilder implements Builder { @override Future build(BuildStep step) async { final info = await generateLicenseInfo( pubspecLockPath: "./pubspec.lock" ); String fileContent = ''' //// AUTO-GENERATED by build_runner //// /// DO NOT EDIT BY HAND part of "licenses.dart"; const List usedLibraryList = [ '''; for (final pkg in info) { if (!pkg.isDirectDependency) { continue; } final license = _licenseFromText(pkg.license); if (license == "Unknown") { print("Failed to guess license for ${pkg.name}"); } fileContent += '\tLibrary(name: "${pkg.name}", license: "$license", url: "${pkg.homepage}"),\n'; } final file = File("pubspec.yaml"); final data = loadYaml(await file.readAsString()); for (final extra in data["extra_licenses"]) { fileContent += '\tLibrary(name: "${extra["name"]!}", license: "${extra["license"]!}", url: "${extra["url"]!}"),\n'; } fileContent += "];"; await step.writeAsString(step.allowedOutputs.first, fileContent); } @override final buildExtensions = const { "pubspec.yaml": [ "lib/ui/pages/settings/licenses.moxxy.dart" ] }; } Builder licenseBuilder(BuilderOptions _) => LicenseBuilder();