From b39d0469cc8142b578b807daf4425e0c01b8790d Mon Sep 17 00:00:00 2001 From: Netkas Date: Mon, 19 Dec 2022 16:42:48 -0500 Subject: [PATCH] Some bug fixes, added options `--reinstall` & `--skip-dependencies` for the `ncc package install` command. --- .../Options/InstallPackageOptions.php | 20 ++++++ src/ncc/CLI/Management/PackageManagerMenu.php | 63 +++++++++++++------ src/ncc/Classes/PhpExtension/PhpRuntime.php | 2 +- src/ncc/Managers/PackageManager.php | 44 +++++++++++-- src/ncc/Objects/PackageLock.php | 35 +++++++++++ 5 files changed, 138 insertions(+), 26 deletions(-) create mode 100644 src/ncc/Abstracts/Options/InstallPackageOptions.php diff --git a/src/ncc/Abstracts/Options/InstallPackageOptions.php b/src/ncc/Abstracts/Options/InstallPackageOptions.php new file mode 100644 index 0000000..dd5207c --- /dev/null +++ b/src/ncc/Abstracts/Options/InstallPackageOptions.php @@ -0,0 +1,20 @@ +Assembly->Trademark, ConsoleColors::LightGreen)); Console::out((string)null); - if(count($package->Dependencies) > 0) + if(count($package->Dependencies) > 0 && !in_array(InstallPackageOptions::Reinstall, $installer_options)) { $dependencies = []; foreach($package->Dependencies as $dependency) { - $require_dependency = true; - - try + if(in_array(InstallPackageOptions::Reinstall, $installer_options)) { - $dependency_package = $package_manager->getPackage($dependency->Name); + $require_dependency = true; } - catch (PackageLockException $e) - { - unset($e); - $dependency_package = null; - } - - if($dependency_package !== null) + else { try { - $dependency_version = $dependency_package->getVersion($dependency->Version); + $dependency_package = $package_manager->getPackage($dependency->Name); } - catch (VersionNotFoundException $e) + catch (PackageLockException $e) { unset($e); - $dependency_version = null; + $dependency_package = null; } - if($dependency_version !== null) + if($dependency_package !== null) { - $require_dependency = false; + try + { + $dependency_version = $dependency_package->getVersion($dependency->Version); + } + catch (VersionNotFoundException $e) + { + unset($e); + $dependency_version = null; + } + + if($dependency_version !== null) + { + $require_dependency = false; + } } } @@ -338,8 +356,11 @@ } } - Console::out('The package requires the following dependencies:'); - Console::out(sprintf('%s', implode(PHP_EOL, $dependencies))); + if($dependencies !== null && count($dependencies) > 0) + { + Console::out('The package requires the following dependencies:'); + Console::out(sprintf('%s', implode(PHP_EOL, $dependencies))); + } } Console::out(sprintf('Extension: %s', @@ -359,11 +380,13 @@ if(!$user_confirmation) $user_confirmation = Console::getBooleanInput(sprintf('Do you want to install %s', $package->Assembly->Package)); + + if($user_confirmation) { try { - $package_manager->install($path, $credential); + $package_manager->install($path, $credential, $installer_options); Console::out(sprintf('Package %s installed successfully', $package->Assembly->Package)); } catch(Exception $e) diff --git a/src/ncc/Classes/PhpExtension/PhpRuntime.php b/src/ncc/Classes/PhpExtension/PhpRuntime.php index 20aeb4f..1e5cf22 100644 --- a/src/ncc/Classes/PhpExtension/PhpRuntime.php +++ b/src/ncc/Classes/PhpExtension/PhpRuntime.php @@ -2,7 +2,7 @@ namespace ncc\Classes\PhpExtension; - use ncc\Abstracts\RuntimeImportOptions; + use ncc\Abstracts\Options\RuntimeImportOptions; use ncc\Exceptions\AccessDeniedException; use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\IOException; diff --git a/src/ncc/Managers/PackageManager.php b/src/ncc/Managers/PackageManager.php index f0b0257..c3e846f 100644 --- a/src/ncc/Managers/PackageManager.php +++ b/src/ncc/Managers/PackageManager.php @@ -10,6 +10,7 @@ use ncc\Abstracts\ConstantReferences; use ncc\Abstracts\DependencySourceType; use ncc\Abstracts\LogLevel; + use ncc\Abstracts\Options\InstallPackageOptions; use ncc\Abstracts\RemoteSourceType; use ncc\Abstracts\Scopes; use ncc\Abstracts\Versions; @@ -83,6 +84,7 @@ * * @param string $package_path * @param Entry|null $entry + * @param array $options * @return string * @throws AccessDeniedException * @throws FileNotFoundException @@ -98,7 +100,7 @@ * @throws UnsupportedRunnerException * @throws VersionNotFoundException */ - public function install(string $package_path, ?Entry $entry=null): string + public function install(string $package_path, ?Entry $entry=null, array $options=[]): string { if(Resolver::resolveScope() !== Scopes::System) throw new AccessDeniedException('Insufficient permission to install packages'); @@ -108,9 +110,6 @@ $package = Package::load($package_path); - if($this->getPackageVersion($package->Assembly->Package, $package->Assembly->Version) !== null) - throw new PackageAlreadyInstalledException('The package ' . $package->Assembly->Package . '=' . $package->Assembly->Version . ' is already installed'); - $extension = $package->Header->CompilerExtension->Extension; $installation_paths = new InstallationPaths($this->PackagesPath . DIRECTORY_SEPARATOR . $package->Assembly->Package . '=' . $package->Assembly->Version); @@ -120,16 +119,51 @@ default => throw new UnsupportedCompilerExtensionException('The compiler extension \'' . $extension . '\' is not supported'), }; + if($this->getPackageVersion($package->Assembly->Package, $package->Assembly->Version) !== null) + { + if(in_array(InstallPackageOptions::Reinstall, $options)) + { + if($this->getPackageLockManager()->getPackageLock()->packageExists( + $package->Assembly->Package, $package->Assembly->Version + )) + { + $this->getPackageLockManager()->getPackageLock()->removePackageVersion( + $package->Assembly->Package, $package->Assembly->Version + ); + } + } + else + { + throw new PackageAlreadyInstalledException('The package ' . $package->Assembly->Package . '=' . $package->Assembly->Version . ' is already installed'); + } + } + $execution_pointer_manager = new ExecutionPointerManager(); PackageCompiler::compilePackageConstants($package, [ ConstantReferences::Install => $installation_paths ]); // Process all the required dependencies before installing the package - if($package->Dependencies !== null && count($package->Dependencies) > 0) + if($package->Dependencies !== null && count($package->Dependencies) > 0 && !in_array(InstallPackageOptions::SkipDependencies, $options)) { foreach($package->Dependencies as $dependency) { + if(in_array(InstallPackageOptions::Reinstall, $options)) + { + // Uninstall the dependency if the option Reinstall is passed on + if($this->getPackageLockManager()->getPackageLock()->packageExists($dependency->Name, $dependency->Version)) + { + if($dependency->Version == null) + { + $this->uninstallPackage($dependency->Name); + } + else + { + $this->uninstallPackageVersion($dependency->Name, $dependency->Version); + } + } + } + $this->processDependency($dependency, $package, $package_path, $entry); } } diff --git a/src/ncc/Objects/PackageLock.php b/src/ncc/Objects/PackageLock.php index 7f84302..5415d8e 100644 --- a/src/ncc/Objects/PackageLock.php +++ b/src/ncc/Objects/PackageLock.php @@ -5,6 +5,7 @@ namespace ncc\Objects; use ncc\Abstracts\Versions; + use ncc\Exceptions\VersionNotFoundException; use ncc\Objects\PackageLock\PackageEntry; use ncc\Utilities\Functions; @@ -134,6 +135,40 @@ return null; } + /** + * Determines if the requested package exists in the package lock + * + * @param string $package + * @param string|null $version + * @return bool + */ + public function packageExists(string $package, ?string $version=null): bool + { + $package_entry = $this->getPackage($package); + if($package_entry == null) + return false; + + if($version !== null) + { + try + { + $version_entry = $package_entry->getVersion($version); + } + catch (VersionNotFoundException $e) + { + unset($e); + return false; + } + + if($version_entry == null) + { + return false; + } + } + + return true; + } + /** * Returns an array of all packages and their installed versions *