- Updated class \ncc\Objects\ProjectConfiguration > Dependency to use method calls rather than direct property access

This commit is contained in:
Netkas 2023-08-23 14:23:21 -04:00
parent 628d126baf
commit 9c76d31de9
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
13 changed files with 217 additions and 88 deletions

View file

@ -115,6 +115,7 @@ features and reduced the number of exceptions down to 15 exceptions.
- Updated `\ncc\Classes > GitClient > checkout()` to throw `GitException` instead of `GitCheckoutException` - Updated `\ncc\Classes > GitClient > checkout()` to throw `GitException` instead of `GitCheckoutException`
- Corrected code-smell and code style issues in `\ncc\Objects > PackageLock` - Corrected code-smell and code style issues in `\ncc\Objects > PackageLock`
- Corrected code-smell and code style issues in `\ncc\Classes\PhpExtension > PhpRuntime` - Corrected code-smell and code style issues in `\ncc\Classes\PhpExtension > PhpRuntime`
- Updated class `\ncc\Objects\ProjectConfiguration > Dependency` to use method calls rather than direct property access
### Removed ### Removed
- Removed `FileNotFoundException` and `DirectoryNotFoundException` from `\ncc\Exceptions` - Removed `FileNotFoundException` and `DirectoryNotFoundException` from `\ncc\Exceptions`

View file

@ -36,6 +36,7 @@
use ncc\Utilities\Console; use ncc\Utilities\Console;
use ncc\Utilities\Functions; use ncc\Utilities\Functions;
use ncc\Utilities\Resolver; use ncc\Utilities\Resolver;
use ncc\Utilities\Validate;
class PackageManagerMenu class PackageManagerMenu
{ {
@ -395,14 +396,21 @@
} }
} }
$path = $package;
$parsed_source = new RemotePackageInput($path);
if($parsed_source->vendor !== null && $parsed_source->package !== null && $parsed_source->source !== null) if(Validate::remotePackageInput($package))
{ {
try try
{ {
$path = $package_manager->fetchFromSource($parsed_source->toString(), $credential); $parsed_source = new RemotePackageInput($package);
if($parsed_source->vendor !== null && $parsed_source->package !== null && $parsed_source->source !== null)
{
$package_path = realpath($package_manager->fetchFromSource($parsed_source->toString(), $credential));
}
else
{
Console::outError(sprintf('Invalid remote package input: %s', $package), true, 1);
return;
}
} }
catch (Exception $e) catch (Exception $e)
{ {
@ -410,10 +418,14 @@
return; return;
} }
} }
else
if(!file_exists($path) || !is_file($path) || !is_readable($path))
{ {
Console::outError('The specified file does not exist or is not readable', true, 1); $package_path = realpath($package);
}
if($package_path === false)
{
Console::outError(sprintf('The specified file \'%s\' does not exist or is not readable', $package), true, 1);
} }
$user_confirmation = false; $user_confirmation = false;
@ -436,7 +448,7 @@
try try
{ {
$package = Package::load($path); $package = Package::load($package_path);
} }
catch(Exception $e) catch(Exception $e)
{ {
@ -503,7 +515,7 @@
{ {
try try
{ {
$dependency_package = $package_manager->getPackage($dependency->name); $dependency_package = $package_manager->getPackage($dependency->getName());
} }
catch (IOException $e) catch (IOException $e)
{ {
@ -515,7 +527,7 @@
{ {
try try
{ {
$dependency_version = $dependency_package->getVersion($dependency->version); $dependency_version = $dependency_package->getVersion($dependency->getVersion());
} }
catch (IOException $e) catch (IOException $e)
{ {
@ -533,8 +545,8 @@
if($require_dependency) if($require_dependency)
{ {
$dependencies[] = sprintf(' %s %s', $dependencies[] = sprintf(' %s %s',
Console::formatColor($dependency->name, ConsoleColors::GREEN), Console::formatColor($dependency->getName(), ConsoleColors::GREEN),
Console::formatColor($dependency->version, ConsoleColors::LIGHT_MAGENTA) Console::formatColor($dependency->getVersion(), ConsoleColors::LIGHT_MAGENTA)
); );
} }
} }
@ -575,7 +587,7 @@
{ {
try try
{ {
$package_manager->install($path, $credential, $installer_options); $package_manager->install($package_path, $credential, $installer_options);
Console::out(sprintf('Package %s installed successfully', $package->assembly->package)); Console::out(sprintf('Package %s installed successfully', $package->assembly->package));
} }
catch(Exception $e) catch(Exception $e)

View file

@ -354,10 +354,10 @@
} }
$dependency = new ProjectConfiguration\Dependency(); $dependency = new ProjectConfiguration\Dependency();
$dependency->name = $package_name; $dependency->setName($package_name);
$dependency->source_type = DependencySourceType::LOCAL; $dependency->setSourceType(DependencySourceType::LOCAL);
$dependency->version = self::versionMap($item->PackageName, $version_map); $dependency->setVersion(self::versionMap($item->PackageName, $version_map));
$dependency->source = $package_name . '.ncc'; $dependency->setSource($package_name . '.ncc');
$project_configuration->build->addDependency($dependency); $project_configuration->build->addDependency($dependency);
} }
} }

View file

@ -259,30 +259,31 @@
} }
Console::outVerbose('Scanning for dependencies... '); Console::outVerbose('Scanning for dependencies... ');
/** @var \ncc\Objects\ProjectConfiguration\Dependency $dependency */
foreach($selected_dependencies as $dependency) foreach($selected_dependencies as $dependency)
{ {
Console::outVerbose(sprintf('processing dependency %s', $dependency->Name)); Console::outVerbose(sprintf('processing dependency %s', $dependency->getName()));
switch($dependency->SourceType) switch($dependency->getSourceType())
{ {
case DependencySourceType::STATIC: case DependencySourceType::STATIC:
try try
{ {
$out_path = $lib_path . DIRECTORY_SEPARATOR . sprintf('%s=%s.lib', $dependency->Name, $dependency->Version); $out_path = $lib_path . DIRECTORY_SEPARATOR . sprintf('%s=%s.lib', $dependency->getName(), $dependency->getVersion());
$package = $package_lock_manager->getPackageLock()?->getPackage($dependency->Name); $package = $package_lock_manager->getPackageLock()?->getPackage($dependency->getName());
if($package === null) if($package === null)
{ {
throw new IOException('Cannot find package lock for dependency ' . $dependency->Name); throw new IOException('Cannot find package lock for dependency ' . $dependency->getName());
} }
$version = $package->getVersion($dependency->Version); $version = $package->getVersion($dependency->getVersion());
if($version === null) if($version === null)
{ {
throw new OperationException('Cannot find version ' . $dependency->Version . ' for dependency ' . $dependency->Name); throw new OperationException('Cannot find version ' . $dependency->getVersion() . ' for dependency ' . $dependency->getName());
} }
Console::outDebug(sprintf('copying shadow package %s=%s to %s', $dependency->Name, $dependency->Version, $out_path)); Console::outDebug(sprintf('copying shadow package %s=%s to %s', $dependency->getName(), $dependency->getVersion(), $out_path));
if(!$filesystem->exists($lib_path)) if(!$filesystem->exists($lib_path))
{ {
@ -290,12 +291,12 @@
} }
$filesystem->copy($version->Location, $out_path); $filesystem->copy($version->Location, $out_path);
$dependency->Source = 'libs' . DIRECTORY_SEPARATOR . sprintf('%s=%s.lib', $dependency->Name, $dependency->Version); $dependency->Source = 'libs' . DIRECTORY_SEPARATOR . sprintf('%s=%s.lib', $dependency->getName(), $dependency->getVersion());
} }
catch (IOException $e) catch (IOException $e)
{ {
throw new PackageException('Static linking not possible, cannot find package lock for dependency ' . $dependency->Name, $e); throw new PackageException('Static linking not possible, cannot find package lock for dependency ' . $dependency->getName(), $e);
} }
break; break;
@ -324,9 +325,10 @@
* Executes the compile process in the correct order and returns the finalized Package object * Executes the compile process in the correct order and returns the finalized Package object
* *
* @return Package|null * @return Package|null
* @throws BuildException * @throws \ncc\Exceptions\BuildException
* @throws IOException * @throws \ncc\Exceptions\IOException
* @throws PathNotFoundException * @throws \ncc\Exceptions\NotSupportedException
* @throws \ncc\Exceptions\PathNotFoundException
*/ */
public function build(): ?Package public function build(): ?Package
{ {
@ -466,8 +468,9 @@
/** /**
* @return void * @return void
* @throws IOException * @throws \ncc\Exceptions\IOException
* @throws PathNotFoundException * @throws \ncc\Exceptions\NotSupportedException
* @throws \ncc\Exceptions\PathNotFoundException
*/ */
public function compileExecutionPolicies(): void public function compileExecutionPolicies(): void
{ {

View file

@ -159,15 +159,15 @@
foreach($package->dependencies as $dependency) foreach($package->dependencies as $dependency)
{ {
// Uninstall the dependency if the option Reinstall is passed on // Uninstall the dependency if the option Reinstall is passed on
if(in_array(InstallPackageOptions::REINSTALL, $options, true) && $this->getPackageLockManager()?->getPackageLock()?->packageExists($dependency->name, $dependency->version)) if(in_array(InstallPackageOptions::REINSTALL, $options, true) && $this->getPackageLockManager()?->getPackageLock()?->packageExists($dependency->getName(), $dependency->getVersion()))
{ {
if($dependency->version === null) if($dependency->getVersion() === 'latest')
{ {
$this->uninstallPackage($dependency->name); $this->uninstallPackage($dependency->getName());
} }
else else
{ {
$this->uninstallPackageVersion($dependency->name, $dependency->version); $this->uninstallPackageVersion($dependency->getName(), $dependency->getVersion());
} }
} }
@ -642,67 +642,67 @@
*/ */
private function processDependency(Dependency $dependency, Package $package, string $package_path, ?Entry $entry=null, array $options=[]): void private function processDependency(Dependency $dependency, Package $package, string $package_path, ?Entry $entry=null, array $options=[]): void
{ {
if(RuntimeCache::get(sprintf('dependency_installed.%s=%s', $dependency->name, $dependency->version ?? 'null'))) if(RuntimeCache::get(sprintf('dependency_installed.%s=%s', $dependency->getName(), $dependency->getVersion())))
{ {
Console::outDebug(sprintf('dependency %s=%s already processed, skipping', $dependency->name, $dependency->version ?? 'null')); Console::outDebug(sprintf('dependency %s=%s already processed, skipping', $dependency->getName(), $dependency->getVersion()));
return; return;
} }
Console::outVerbose('processing dependency ' . $dependency->name . ' (' . $dependency->version . ')'); Console::outVerbose('processing dependency ' . $dependency->getVersion() . ' (' . $dependency->getVersion() . ')');
$dependent_package = $this->getPackage($dependency->name); $dependent_package = $this->getPackage($dependency->getName());
$dependency_met = false; $dependency_met = false;
if ($dependent_package !== null && $dependency->version !== null && Validate::version($dependency->version)) if ($dependent_package !== null && $dependency->getVersion() !== null && Validate::version($dependency->getVersion()))
{ {
Console::outDebug('dependency has version constraint, checking if package is installed'); Console::outDebug('dependency has version constraint, checking if package is installed');
$dependent_version = $this->getPackageVersion($dependency->name, $dependency->version); $dependent_version = $this->getPackageVersion($dependency->getName(), $dependency->getVersion());
if ($dependent_version !== null) if ($dependent_version !== null)
{ {
$dependency_met = true; $dependency_met = true;
} }
} }
elseif ($dependent_package !== null && $dependency->version === null) elseif ($dependent_package !== null && $dependency->getVersion() === null)
{ {
Console::outDebug(sprintf('dependency %s has no version specified, assuming dependency is met', $dependency->name)); Console::outDebug(sprintf('dependency %s has no version specified, assuming dependency is met', $dependency->getName()));
$dependency_met = true; $dependency_met = true;
} }
Console::outDebug('dependency met: ' . ($dependency_met ? 'true' : 'false')); Console::outDebug('dependency met: ' . ($dependency_met ? 'true' : 'false'));
if ($dependency->source_type !== null && !$dependency_met) if ($dependency->getSourceType() !== null && !$dependency_met)
{ {
Console::outVerbose(sprintf('Installing dependency %s=%s for %s=%s', $dependency->name, $dependency->version, $package->assembly->package, $package->assembly->version)); Console::outVerbose(sprintf('Installing dependency %s=%s for %s=%s', $dependency->getName(), $dependency->getVersion(), $package->assembly->package, $package->assembly->version));
switch ($dependency->source_type) switch ($dependency->getSourceType())
{ {
case DependencySourceType::LOCAL: case DependencySourceType::LOCAL:
Console::outDebug('installing from local source ' . $dependency->source); Console::outDebug('installing from local source ' . $dependency->getSource());
$basedir = dirname($package_path); $basedir = dirname($package_path);
if (!file_exists($basedir . DIRECTORY_SEPARATOR . $dependency->source)) if (!file_exists($basedir . DIRECTORY_SEPARATOR . $dependency->getSourceType()))
{ {
throw new PathNotFoundException($basedir . DIRECTORY_SEPARATOR . $dependency->source); throw new PathNotFoundException($basedir . DIRECTORY_SEPARATOR . $dependency->getSource());
} }
$this->install($basedir . DIRECTORY_SEPARATOR . $dependency->source, null, $options); $this->install($basedir . DIRECTORY_SEPARATOR . $dependency->getSource(), null, $options);
RuntimeCache::set(sprintf('dependency_installed.%s=%s', $dependency->name, $dependency->version), true); RuntimeCache::set(sprintf('dependency_installed.%s=%s', $dependency->getName(), $dependency->getVersion()), true);
break; break;
case DependencySourceType::STATIC: case DependencySourceType::STATIC:
throw new PackageException('Static linking not possible, package ' . $dependency->name . ' is not installed'); throw new PackageException('Static linking not possible, package ' . $dependency->getName() . ' is not installed');
case DependencySourceType::REMOTE: case DependencySourceType::REMOTE:
Console::outDebug('installing from remote source ' . $dependency->source); Console::outDebug('installing from remote source ' . $dependency->getSource());
$this->installFromSource($dependency->source, $entry, $options); $this->installFromSource($dependency->getSource(), $entry, $options);
RuntimeCache::set(sprintf('dependency_installed.%s=%s', $dependency->name, $dependency->version), true); RuntimeCache::set(sprintf('dependency_installed.%s=%s', $dependency->getName(), $dependency->getVersion()), true);
break; break;
default: default:
throw new NotSupportedException(sprintf('Dependency source type %s is not supported', $dependency->source_type)); throw new NotSupportedException(sprintf('Dependency source type %s is not supported', $dependency->getSourceType()));
} }
} }
elseif(!$dependency_met) elseif(!$dependency_met)
{ {
throw new PackageException(sprintf('Required dependency %s=%s is not installed', $dependency->name, $dependency->version)); throw new PackageException(sprintf('Required dependency %s=%s is not installed', $dependency->getName(), $dependency->getVersion()));
} }
} }

View file

@ -133,9 +133,9 @@
{ {
foreach($this->dependencies as $dep) foreach($this->dependencies as $dep)
{ {
if($dep->name === $dependency->name) if($dep->getName() === $dependency->getName())
{ {
$this->removeDependency($dep->name); $this->removeDependency($dep->getName());
break; break;
} }
} }
@ -153,7 +153,7 @@
{ {
foreach($this->dependencies as $key => $dep) foreach($this->dependencies as $key => $dep)
{ {
if($dep->name === $name) if($dep->getName() === $name)
{ {
unset($this->dependencies[$key]); unset($this->dependencies[$key]);
return; return;

View file

@ -47,8 +47,8 @@
{ {
if($dependency !== null) if($dependency !== null)
{ {
$this->PackageName = $dependency->name; $this->PackageName = $dependency->getName();
$this->Version = $dependency->version; $this->Version = $dependency->getVersion();
} }
} }

View file

@ -365,7 +365,7 @@
* @param string $path * @param string $path
* @param bool $bytecode * @param bool $bytecode
* @return void * @return void
* @noinspection PhpUnused * @throws IOException
*/ */
public function toFile(string $path, bool $bytecode=false): void public function toFile(string $path, bool $bytecode=false): void
{ {
@ -481,10 +481,30 @@
{ {
$object = new self(); $object = new self();
$object->project = Project::fromArray(Functions::array_bc($data, 'project')); $object->project = Functions::array_bc($data, 'project');
$object->assembly = Assembly::fromArray(Functions::array_bc($data, 'assembly')); if($object->project !== null)
$object->build = Build::fromArray(Functions::array_bc($data, 'build')); {
$object->installer = Installer::fromArray(Functions::array_bc($data, 'installer')); $object->project = Project::fromArray($object->project);
}
$object->assembly = Functions::array_bc($data, 'assembly');
if($object->assembly !== null)
{
$object->assembly = Assembly::fromArray($object->assembly);
}
$object->build = Functions::array_bc($data, 'build');
if($object->build !== null)
{
$object->build = Build::fromArray($object->build);
}
$object->installer = Functions::array_bc($data, 'installer');
if($object->installer !== null)
{
$object->installer = Installer::fromArray($object->installer);
}
$execution_policies = Functions::array_bc($data, 'execution_policies'); $execution_policies = Functions::array_bc($data, 'execution_policies');
if(!is_null($execution_policies)) if(!is_null($execution_policies))

View file

@ -136,9 +136,9 @@
{ {
foreach($this->dependencies as $dep) foreach($this->dependencies as $dep)
{ {
if($dep->name === $dependency->name) if($dep->getName() === $dependency->getName())
{ {
$this->removeDependency($dep->name); $this->removeDependency($dep->getName());
break; break;
} }
} }
@ -156,7 +156,7 @@
{ {
foreach($this->dependencies as $key => $dep) foreach($this->dependencies as $key => $dep)
{ {
if($dep->name === $name) if($dep->getName() === $name)
{ {
unset($this->dependencies[$key]); unset($this->dependencies[$key]);
return; return;

View file

@ -36,32 +36,108 @@
class Dependency implements BytecodeObjectInterface class Dependency implements BytecodeObjectInterface
{ {
/** /**
* The name of the dependency
*
* @var string * @var string
*/ */
public $name; private $name;
/** /**
* Optional. The type of source from where ncc can fetch the dependency from
*
* @var string|null * @var string|null
*/ */
public $source_type; private $source_type;
/** /**
* Optional. The actual source where NCC can fetch the dependency from
*
* @var string|null * @var string|null
*/ */
public $source; private $source;
/**
* @var string|null
*/
private $version;
/**
* Returns the name of the dependency
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Sets the name of the dependency
*
* @param string $name
* @return void
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* Optional. Returns the type of source from where ncc can fetch the dependency from
*
* @return string|null
*/
public function getSourceType(): ?string
{
return $this->source_type;
}
/**
* Sets the type of source from where ncc can fetch the dependency from
*
* @param string|null $source_type
* @return void
*/
public function setSourceType(?string $source_type): void
{
$this->source_type = $source_type;
}
/**
* Optional. Returns The actual source where NCC can fetch the dependency from
*
* @return string|null
*/
public function getSource(): ?string
{
return $this->source;
}
/**
* Sets the actual source where NCC can fetch the dependency from
*
* @param string|null $source
* @return void
*/
public function setSource(?string $source): void
{
$this->source = $source;
}
/** /**
* Optional. The required version of the dependency or "latest" * Optional. The required version of the dependency or "latest"
* *
* @var string|null * @return string
*/ */
public $version; public function getVersion(): string
{
return $this->version ?? 'latest';
}
/**
* Returns the required version of the dependency or null if no version is required
*
* @param string|null $version
* @return void
*/
public function setVersion(?string $version): void
{
$this->version = $version;
}
/** /**
* Validates the dependency configuration * Validates the dependency configuration

View file

@ -146,7 +146,7 @@
/** @var Dependency $dependency */ /** @var Dependency $dependency */
foreach($version_entry->Dependencies as $dependency) foreach($version_entry->Dependencies as $dependency)
{ {
self::import($dependency->PackageName, $dependency->version, $options); self::import($dependency->PackageName, $dependency->getVersion(), $options);
} }
} }

View file

@ -118,11 +118,11 @@
* *
* @param string $path * @param string $path
* @param int $flags * @param int $flags
* @return mixed * @return array
* @throws IOException * @throws IOException
* @throws PathNotFoundException * @throws PathNotFoundException
*/ */
public static function loadJsonFile(string $path, int $flags=0): mixed public static function loadJsonFile(string $path, int $flags=0): array
{ {
if(!file_exists($path)) if(!file_exists($path))
{ {
@ -137,10 +137,10 @@
* *
* @param string $json * @param string $json
* @param int $flags * @param int $flags
* @return mixed * @return array
* @throws IOException * @throws IOException
*/ */
public static function loadJson(string $json, int $flags=0): mixed public static function loadJson(string $json, int $flags=0): array
{ {
try try
{ {
@ -184,6 +184,7 @@
* @param string $path * @param string $path
* @param int $flags * @param int $flags
* @return void * @return void
* @throws IOException
*/ */
public static function encodeJsonFile(mixed $value, string $path, int $flags=0): void public static function encodeJsonFile(mixed $value, string $path, int $flags=0): void
{ {

View file

@ -282,7 +282,7 @@ namespace ncc\Utilities;
*/ */
public static function pathName(string $input): bool public static function pathName(string $input): bool
{ {
if(strlen($input) === 0) if($input === '')
{ {
return false; return false;
} }
@ -299,4 +299,20 @@ namespace ncc\Utilities;
return true; return true;
} }
/***
* Returns True if the given remote package input is valid
*
* @param string $input
* @return bool
*/
public static function remotePackageInput(string $input): bool
{
if(preg_match(RegexPatterns::REMOTE_PACKAGE, $input))
{
return true;
}
return false;
}
} }