Add license installer for build mode

This commit is contained in:
crazywhalecc 2024-07-08 12:55:41 +08:00 committed by Jerry Ma
parent 386e9adf44
commit eec8ee03bf
2 changed files with 35 additions and 6 deletions

View File

@ -217,6 +217,7 @@ abstract class LibraryBase
} }
$this->getBuilder()->emitPatchPoint('before-library[ ' . static::NAME . ']-build'); $this->getBuilder()->emitPatchPoint('before-library[ ' . static::NAME . ']-build');
$this->build(); $this->build();
$this->installLicense();
$this->getBuilder()->emitPatchPoint('after-library[ ' . static::NAME . ']-build'); $this->getBuilder()->emitPatchPoint('after-library[ ' . static::NAME . ']-build');
return LIB_STATUS_OK; return LIB_STATUS_OK;
} }
@ -311,4 +312,26 @@ abstract class LibraryBase
{ {
return str_replace('-', '_', static::NAME); 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");
}
}
}
} }

View File

@ -107,9 +107,9 @@ class LicenseDumper
} }
foreach ($licenses as $index => $license) { foreach ($licenses as $index => $license) {
yield ($license['suffix'] ?? $index) => match ($license['type']) { yield $index => match ($license['type']) {
'text' => $license['text'], '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'), default => throw new RuntimeException('source [' . $source_name . '] license type is not allowed'),
}; };
} }
@ -118,15 +118,21 @@ class LicenseDumper
/** /**
* @throws RuntimeException * @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)) { if (is_null($in_path)) {
throw new RuntimeException('source [' . $source_name . '] license file is not set, please check config/source.json'); 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');
} }
} }