diff --git a/src/SPC/builder/LibraryBase.php b/src/SPC/builder/LibraryBase.php index 939ca11e..1da2336a 100644 --- a/src/SPC/builder/LibraryBase.php +++ b/src/SPC/builder/LibraryBase.php @@ -217,6 +217,7 @@ abstract class LibraryBase } $this->getBuilder()->emitPatchPoint('before-library[ ' . static::NAME . ']-build'); $this->build(); + $this->installLicense(); $this->getBuilder()->emitPatchPoint('after-library[ ' . static::NAME . ']-build'); return LIB_STATUS_OK; } @@ -311,4 +312,26 @@ abstract class LibraryBase { return str_replace('-', '_', static::NAME); } + + /** + * Install license files in buildroot directory + */ + protected function installLicense(): void + { + FileSystem::createDir(BUILD_ROOT_PATH . '/source-licenses/' . $this->getName()); + $source = Config::getLib($this->getName(), 'source'); + $license_files = Config::getSource($source)['license'] ?? []; + if (is_assoc_array($license_files)) { + $license_files = [$license_files]; + } + foreach ($license_files as $index => $license) { + if ($license['type'] === 'text') { + FileSystem::writeFile(BUILD_ROOT_PATH . '/source-licenses/' . $this->getName() . "/{$index}.txt", $license['text']); + continue; + } + if ($license['type'] === 'file') { + copy($this->source_dir . '/' . $license['path'], BUILD_ROOT_PATH . '/source-licenses/' . $this->getName() . "/{$index}.txt"); + } + } + } } diff --git a/src/SPC/util/LicenseDumper.php b/src/SPC/util/LicenseDumper.php index 61a6dc06..4c9d63fe 100644 --- a/src/SPC/util/LicenseDumper.php +++ b/src/SPC/util/LicenseDumper.php @@ -107,9 +107,9 @@ class LicenseDumper } foreach ($licenses as $index => $license) { - yield ($license['suffix'] ?? $index) => match ($license['type']) { + yield $index => match ($license['type']) { 'text' => $license['text'], - 'file' => $this->loadSourceFile($source_name, $license['path'], Config::getSource($source_name)['path'] ?? null), + 'file' => $this->loadSourceFile($source_name, $index, $license['path'], Config::getSource($source_name)['path'] ?? null), default => throw new RuntimeException('source [' . $source_name . '] license type is not allowed'), }; } @@ -118,15 +118,21 @@ class LicenseDumper /** * @throws RuntimeException */ - private function loadSourceFile(string $source_name, ?string $in_path, ?string $custom_base_path = null): string + private function loadSourceFile(string $source_name, int $index, ?string $in_path, ?string $custom_base_path = null): string { if (is_null($in_path)) { throw new RuntimeException('source [' . $source_name . '] license file is not set, please check config/source.json'); } - if (!file_exists(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path)) { - throw new RuntimeException('source [' . $source_name . '] license file [' . $in_path . '] not exist'); + + if (file_exists(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path)) { + return file_get_contents(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path); } - return file_get_contents(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path); + if (file_exists(BUILD_ROOT_PATH . '/source-licenses/' . $source_name . '/' . $index . '.txt')) { + logger()->debug('source [' . $source_name . '] license file [' . $index . ':' . $in_path . '] not exist, use installed version instead'); + return file_get_contents(BUILD_ROOT_PATH . '/source-licenses/' . $source_name . '/' . $index . '.txt'); + } + + throw new RuntimeException('source [' . $source_name . '] license file [' . $in_path . '] not exist'); } }