]> BookStack Code Mirror - bookstack/blob - dev/licensing/gen-php-licenses
Licensing: Updated license gen scripts to share logic
[bookstack] / dev / licensing / gen-php-licenses
1 #!/usr/bin/env php
2 <?php
3
4 // This script reads the project composer.lock file to generate
5 // clear license details for our PHP dependencies.
6
7 declare(strict_types=1);
8 require "gen-licenses-shared.php";
9
10 $rootPath = dirname(__DIR__, 2);
11 $outputPath = "{$rootPath}/dev/licensing/php-library-licenses.txt";
12 $composerLock = json_decode(file_get_contents($rootPath . "/composer.lock"));
13 $outputSeparator = "\n-----------\n";
14
15 $packages = $composerLock->packages;
16 $packageOutput = array_map(packageToOutput(...), $packages);
17
18 $licenseInfo =  implode($outputSeparator, $packageOutput) . "\n";
19 file_put_contents($outputPath, $licenseInfo);
20
21 echo "License information written to {$outputPath}\n";
22 echo implode("\n", getWarnings());
23
24 function packageToOutput(stdClass $package) : string {
25     global $rootPath;
26     $output = ["{$package->name}"];
27
28     $licenses = is_array($package->license) ? $package->license : [$package->license];
29     $output[] = "License: " . implode(' ', $licenses);
30
31     $packagePath = "{$rootPath}/vendor/{$package->name}/package.json";
32     $licenseFile = findLicenseFile($package->name, $packagePath);
33     if ($licenseFile) {
34         $relLicenseFile = str_replace("{$rootPath}/", '', $licenseFile);
35         $output[] = "License File: {$relLicenseFile}";
36         $copyright = findCopyright($licenseFile);
37         if ($copyright) {
38             $output[] = "Copyright: {$copyright}";
39         } else {
40             warn("Package {$package->name}: no copyright found in its license");
41         }
42     }
43
44     $source = $package->source->url;
45     if ($source) {
46         $output[] = "Source: {$source}";
47     }
48
49     $link = $package->homepage ?? $package->source->url ?? '';
50     if ($link) {
51         $output[] = "Link: {$link}";
52     }
53
54     return implode("\n", $output);
55 }