Some bug fixes, added options --reinstall & --skip-dependencies for the ncc package install command.

This commit is contained in:
Netkas 2022-12-19 16:42:48 -05:00
parent dc4ad1d08d
commit b39d0469cc
5 changed files with 138 additions and 26 deletions

View file

@ -0,0 +1,20 @@
<?php
namespace ncc\Abstracts\Options;
abstract class InstallPackageOptions
{
/**
* Skips the installation of dependencies of the package
*
* @warning This will cause the package to fail to import of
* the dependencies are not met
*/
const SkipDependencies = 'skip_dependencies';
/**
* Reinstall all packages if they are already installed
* Including dependencies if they are being processed.
*/
const Reinstall = 'reinstall';
}

View file

@ -4,6 +4,7 @@
use Exception;
use ncc\Abstracts\ConsoleColors;
use ncc\Abstracts\Options\InstallPackageOptions;
use ncc\Abstracts\Scopes;
use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\PackageLockException;
@ -263,6 +264,18 @@
$user_confirmation = (bool)($args['y'] ?? $args['Y']);
}
$installer_options = [];
if((Functions::cbool($args['skip-dependencies'] ?? false)))
{
$installer_options[] = InstallPackageOptions::SkipDependencies;
}
if(Functions::cbool($args['reinstall'] ?? false))
{
$installer_options[] = InstallPackageOptions::Reinstall;
}
try
{
$package = Package::load($path);
@ -294,13 +307,17 @@
Console::out(' Trademark: ' . Console::formatColor($package->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)
{
if(in_array(InstallPackageOptions::Reinstall, $installer_options))
{
$require_dependency = true;
}
else
{
try
{
$dependency_package = $package_manager->getPackage($dependency->Name);
@ -328,6 +345,7 @@
$require_dependency = false;
}
}
}
if($require_dependency)
{
@ -338,9 +356,12 @@
}
}
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',
Console::formatColor($package->Header->CompilerExtension->Extension, ConsoleColors::Green)
@ -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)

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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
*