Add PackageInstaller::isPackageInstalled() API

This commit is contained in:
crazywhalecc 2025-12-08 11:01:36 +08:00 committed by Jerry Ma
parent df6c27c98d
commit abd6c2fa3a
2 changed files with 41 additions and 7 deletions

View File

@ -108,8 +108,10 @@ class PackageInstaller
*/
public function run(bool $interactive = true, bool $disable_delay_msg = false): void
{
// resolve input, make dependency graph
$this->resolvePackages();
if (empty($this->packages)) {
// resolve input, make dependency graph
$this->resolvePackages();
}
if ($interactive && !$disable_delay_msg) {
// show install or build options in terminal with beautiful output
@ -148,7 +150,10 @@ class PackageInstaller
}
$builder = ApplicationContext::get(PackageBuilder::class);
foreach ($this->packages as $package) {
if ($this->isBuildPackage($package) || $package instanceof LibraryPackage && $package->hasStage('build')) {
if (
$this->isBuildPackage($package) ||
$package instanceof LibraryPackage && $package->hasStage('build') && !$package->getArtifact()->shouldUseBinary()
) {
if ($interactive) {
InteractiveTerm::indicateProgress('Building package: ' . ConsoleColor::yellow($package->getName()));
}
@ -213,6 +218,31 @@ class PackageInstaller
return isset($this->packages[$package_name]);
}
public function isPackageInstalled(Package|string $package_name): bool
{
if (empty($this->packages)) {
$this->resolvePackages();
}
if (is_string($package_name)) {
$package = $this->getPackage($package_name);
if ($package === null) {
throw new WrongUsageException("Package '{$package_name}' is not resolved.");
}
} else {
$package = $package_name;
}
// check if package is built/installed
if ($this->isBuildPackage($package)) {
return $package->isInstalled();
}
if ($package instanceof LibraryPackage && $package->getArtifact()->shouldUseBinary()) {
$artifact = $package->getArtifact();
return $artifact->isBinaryExtracted();
}
return false;
}
/**
* Returns the download status of all artifacts for the resolved packages.
*

View File

@ -225,11 +225,15 @@ class SPCConfigUtil
// convert all static-libs to short names
$libs = array_reverse(PackageConfig::get($package, 'static-libs', []));
foreach ($libs as $lib) {
// check file existence
if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) {
throw new WrongUsageException("Library file '{$lib}' for lib [{$package}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first.");
if (FileSystem::isRelativePath($lib)) {
// check file existence
if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) {
throw new WrongUsageException("Library file '{$lib}' for lib [{$package}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first.");
}
$lib_names[] = $this->getShortLibName($lib);
} else {
$lib_names[] = $lib;
}
$lib_names[] = $this->getShortLibName($lib);
}
// add frameworks for macOS
if (SystemTarget::getTargetOS() === 'Darwin') {