From dda710119ce63cf262260cc4858206775602a56d Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 20 Sep 2024 17:04:30 -0400 Subject: [PATCH] Add option to force package build from source --- src/ncc/CLI/Management/PackageManagerMenu.php | 6 ++ .../Enums/Options/InstallPackageOptions.php | 10 +++- src/ncc/Managers/PackageManager.php | 58 +++++++++++-------- src/ncc/Managers/ProjectManager.php | 6 ++ 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/ncc/CLI/Management/PackageManagerMenu.php b/src/ncc/CLI/Management/PackageManagerMenu.php index 58dc746..2d98853 100644 --- a/src/ncc/CLI/Management/PackageManagerMenu.php +++ b/src/ncc/CLI/Management/PackageManagerMenu.php @@ -168,6 +168,11 @@ $options[InstallPackageOptions::SKIP_DEPENDENCIES->value] = true; } + if(isset($args['build-source'])) + { + $options[InstallPackageOptions::BUILD_SOURCE->value] = true; + } + if($authentication !== null) { $entry = (new CredentialManager())->getVault()?->getEntry($authentication); @@ -560,6 +565,7 @@ new CliHelpSection(['install', '-p', '--skip-dependencies'], 'Installs a specified ncc package but skips the installation of dependencies'), new CliHelpSection(['install', '-p', '--reinstall'], 'Installs a specified ncc package, reinstall if already installed'), new CliHelpSection(['install', '--prefer-static', '--static'], 'Installs a static version of the package from the remote repository if available'), + new CliHelpSection(['install', '--build-source'], 'Forces ncc to build the packages from source rather than trying to use a pre-built binary'), new CliHelpSection(['uninstall', '--package', '-p'], 'Uninstalls a specified ncc package'), new CliHelpSection(['uninstall', '--package', '-p', '--version', '-v'], 'Uninstalls a specified ncc package version'), new CliHelpSection(['uninstall-all'], 'Uninstalls all packages'), diff --git a/src/ncc/Enums/Options/InstallPackageOptions.php b/src/ncc/Enums/Options/InstallPackageOptions.php index e074245..f217380 100644 --- a/src/ncc/Enums/Options/InstallPackageOptions.php +++ b/src/ncc/Enums/Options/InstallPackageOptions.php @@ -30,7 +30,7 @@ * @warning This will cause the package to fail to import of * the dependencies are not met */ - case SKIP_DEPENDENCIES = 'skip_dependencies'; + case SKIP_DEPENDENCIES = 'skip-dependencies'; /** * Reinstall all packages if they are already installed, @@ -42,5 +42,11 @@ * Installs a static version of the package if it's available * otherwise it will install non-static version */ - case PREFER_STATIC = 'prefer_static'; + case PREFER_STATIC = 'prefer-static'; + + /** + * Forces ncc to build packages from source rather than trying to obtain + * a pre-built version of the package + */ + case BUILD_SOURCE = 'build-source'; } \ No newline at end of file diff --git a/src/ncc/Managers/PackageManager.php b/src/ncc/Managers/PackageManager.php index a2837d5..d5f2678 100644 --- a/src/ncc/Managers/PackageManager.php +++ b/src/ncc/Managers/PackageManager.php @@ -521,7 +521,7 @@ )); /** @noinspection SlowArrayOperationsInLoopInspection */ - $installed_packages = array_merge($installed_packages, $this->install($dependency->getSource(), $authentication)); + $installed_packages = array_merge($installed_packages, $this->install($dependency->getSource(), $authentication, $options)); } } @@ -550,35 +550,45 @@ Console::out(sprintf('Fetching package %s/%s=%s from %s', $input->getVendor(), $input->getPackage(), $input->getVersion(), $input->getRepository())); - try + if(isset($options[InstallPackageOptions::BUILD_SOURCE->value])) { Console::outVerbose(sprintf( - 'Attempting to fetch a pre-built ncc package for %s=%s from %s', - $input->getPackage(), $input->getVersion(), $input->getRepository() + 'Forcing ncc to build package %s/%s=%s from source', + $input->getVendor(), $input->getPackage(), $input->getVersion() )); - - // First try to fetch a pre-built package from the repository - $results = $this->repository_manager->getRepository($input->getRepository())->fetchPackage( - $input->getVendor(), $input->getPackage(), $input->getVersion(), $authentication, $options - ); - - $package_path = $this->downloadFile($results->getUrl(), PathFinder::getCachePath()); } - catch(Exception $e) + else { - Console::outVerbose(sprintf( - 'Failed to fetch a pre-built ncc package for %s=%s from %s: %s', - $input->getPackage(), $input->getVersion(), $input->getRepository(), $e->getMessage() - )); - - // Clean up the package file if it exists - if(isset($package_path) && is_file($package_path)) + try { - ShutdownHandler::declareTemporaryPath($package_path); - } + Console::outVerbose(sprintf( + 'Attempting to fetch a pre-built ncc package for %s=%s from %s', + $input->getPackage(), $input->getVersion(), $input->getRepository() + )); - // This is a warning because we can still attempt to build from source - unset($results, $package_path); + // First try to fetch a pre-built package from the repository + $results = $this->repository_manager->getRepository($input->getRepository())->fetchPackage( + $input->getVendor(), $input->getPackage(), $input->getVersion(), $authentication, $options + ); + + $package_path = $this->downloadFile($results->getUrl(), PathFinder::getCachePath()); + } + catch(Exception $e) + { + Console::outVerbose(sprintf( + 'Failed to fetch a pre-built ncc package for %s=%s from %s: %s', + $input->getPackage(), $input->getVersion(), $input->getRepository(), $e->getMessage() + )); + + // Clean up the package file if it exists + if(isset($package_path) && is_file($package_path)) + { + ShutdownHandler::declareTemporaryPath($package_path); + } + + // This is a warning because we can still attempt to build from source + unset($results, $package_path); + } } if(!isset($package_path)) @@ -848,7 +858,7 @@ } default: - throw new NotSupportedException(sprintf('Cannot build from source %s, the project type %s is not supported', $archive, $project_detection->getProjectType())); + throw new NotSupportedException(sprintf('Cannot build from source %s, the project type %s is not supported', $archive, $project_detection->getProjectType()->value)); } } diff --git a/src/ncc/Managers/ProjectManager.php b/src/ncc/Managers/ProjectManager.php index 5f09005..6d6951f 100644 --- a/src/ncc/Managers/ProjectManager.php +++ b/src/ncc/Managers/ProjectManager.php @@ -96,6 +96,12 @@ $path = substr($path, 0, -1); } + if(is_file($path)) + { + // We can assume the user is trying to load a project file + $path = dirname($path); + } + // Detect if the folder exists or not if(!is_dir($path)) {