Refactor type handling for CompilerExtensions and RepositoryType

This commit is contained in:
netkas 2024-09-18 15:18:07 -04:00
parent ae8021bb3a
commit 3160ddfa27
6 changed files with 76 additions and 72 deletions

View file

@ -23,6 +23,7 @@
namespace ncc\CLI\Management;
use Exception;
use ncc\Enums\CompilerExtensions;
use ncc\Enums\ProjectTemplates;
use ncc\Enums\Scopes;
use ncc\Managers\CredentialManager;
@ -102,7 +103,12 @@
if(isset($args['ext']))
{
$compiler_extension = $args['ext'];
$compiler_extension = CompilerExtensions::tryFrom($args['ext']);
if($compiler_extension === null)
{
Console::outError('Invalid compiler extension, please specify a valid compiler extension', true, 1);
return 1;
}
}
else
{

View file

@ -158,16 +158,16 @@
{
$configuration = $this->project_configuration->getBuild()->getBuildConfiguration($build_configuration);
return match (strtolower($this->project_configuration->getProject()->getCompiler()->getExtension()))
return match ($this->project_configuration->getProject()->getCompiler()->getExtension())
{
CompilerExtensions::PHP->value => match (strtolower($configuration->getBuildType()))
CompilerExtensions::PHP => match (strtolower($configuration->getBuildType()))
{
BuildOutputType::NCC_PACKAGE->value => (new NccCompiler($this))->build($build_configuration, $options),
BuildOutputType::EXECUTABLE->value => (new ExecutableCompiler($this))->build($build_configuration, $options),
default => throw new BuildException(sprintf('php cannot produce the build type \'%s\'', $configuration->getBuildType())),
},
default => throw new NotSupportedException(sprintf('The compiler extension \'%s\' is not supported', $this->project_configuration->getProject()->getCompiler()->getExtension())),
default => throw new NotSupportedException(sprintf('The compiler extension \'%s\' is not supported', $this->project_configuration->getProject()->getCompiler()->getExtension()->value)),
};
}
@ -361,7 +361,7 @@
* @param string $project_path The directory for the project to be initialized in
* @param string $name The name of the project eg; ProjectLib
* @param string $package The standard package name eg; com.example.project
* @param string $compiler The compiler to use for this project
* @param CompilerExtensions $extension The compiler to use for this project
* @param array $options An array of options to use when initializing the project
* @return ProjectManager
* @throws ConfigurationException
@ -369,7 +369,7 @@
* @throws NotSupportedException
* @throws PathNotFoundException
*/
public static function initializeProject(string $project_path, string $name, string $package, string $compiler, array $options=[]): ProjectManager
public static function initializeProject(string $project_path, string $name, string $package, CompilerExtensions $extension, array $options=[]): ProjectManager
{
if(str_ends_with($project_path, DIRECTORY_SEPARATOR))
{
@ -416,7 +416,7 @@
$build->setDefaultConfiguration('release');
$project_configuration = new ProjectConfiguration(
new ProjectConfiguration\Project($compiler),
new ProjectConfiguration\Project($extension),
new ProjectConfiguration\Assembly($name, $package),
$build
);
@ -485,7 +485,7 @@
throw new IOException(sprintf('Project source directory "%s" was not created', $project_src));
}
$project = new ProjectConfiguration\Project(new ProjectConfiguration\Compiler(CompilerExtensions::PHP->value));
$project = new ProjectConfiguration\Project(CompilerExtensions::PHP);
$assembly = new ProjectConfiguration\Assembly(
Resolver::composerName($composer_json->getName()),
Resolver::composerNameToPackage($composer_json->getName()),

View file

@ -44,40 +44,38 @@
/**
* The compiler extension that the project uses
*
* @var string
* @var CompilerExtensions
*/
private $extension;
private CompilerExtensions $extension;
/**
* The minimum version that is supported
*
* @var string
* @var string|null
*/
private $minimum_version;
private ?string $minimum_version;
/**
* The maximum version that is supported
*
* @var string
* @var string|null
*/
private $maximum_version;
private ?string $maximum_version;
/**
* Compiler constructor.
*
* @param string $extension
* @param CompilerExtensions $extension
* @param string|null $minimum_version
* @param string|null $maximum_version
* @throws NotSupportedException
*/
public function __construct(string $extension, ?string $minimum_version=null, ?string $maximum_version=null)
public function __construct(CompilerExtensions $extension, ?string $minimum_version=null, ?string $maximum_version=null)
{
$extension = strtolower($extension);
/** @noinspection DegradedSwitchInspection */
switch($extension)
{
case CompilerExtensions::PHP->value:
case CompilerExtensions::PHP:
if($minimum_version === null)
{
@ -92,7 +90,7 @@
break;
default:
throw new NotSupportedException(sprintf('The compiler extension \'%s\' is not supported in ncc', $extension));
throw new NotSupportedException(sprintf('The compiler extension \'%s\' is not supported in ncc', $extension->value));
}
$this->extension = $extension;
@ -101,17 +99,18 @@
}
/**
* @return string
* @return CompilerExtensions
*/
public function getExtension(): string
public function getExtension(): CompilerExtensions
{
// TODO: Update usages from here
return $this->extension;
}
/**
* @param string $extension
* @param CompilerExtensions $extension
*/
public function setExtension(string $extension): void
public function setExtension(CompilerExtensions $extension): void
{
$this->extension = $extension;
}
@ -157,11 +156,6 @@
*/
public function validate(): bool
{
if($this->extension === null)
{
throw new ConfigurationException('The property \'extension\' must not be null.');
}
if($this->minimum_version === null)
{
throw new ConfigurationException('The property \'minimum_version\' must not be null.');
@ -184,11 +178,9 @@
throw new ConfigurationException('Version comparison failed: ' . $e->getMessage());
}
/** @noinspection InArrayMissUseInspection */
// TODO: Fix this, not a proper use of cases()
if(!in_array($this->extension, CompilerExtensions::cases(), true))
{
throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported');
throw new NotSupportedException('The compiler extension \'' . $this->extension->value . '\' is not supported');
}
/** @noinspection DegradedSwitchInspection */
@ -198,19 +190,19 @@
if(!in_array($this->maximum_version, CompilerExtensionSupportedVersions::PHP, true))
{
throw new NotSupportedException('The property "project.compiler.maximum_version" does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension);
throw new NotSupportedException('The property "project.compiler.maximum_version" does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension->value);
}
if(!in_array($this->minimum_version, CompilerExtensionSupportedVersions::PHP, true))
{
throw new NotSupportedException('The property "project.compiler.minimum_version" does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension);
throw new NotSupportedException('The property "project.compiler.minimum_version" does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension->value);
}
break;
default:
throw new NotSupportedException('The compiler extension "' . $this->extension . '" is not supported by ncc');
throw new NotSupportedException('The compiler extension "' . $this->extension->value . '" is not supported by ncc');
}
return True;
@ -222,10 +214,7 @@
public function toArray(bool $bytecode = false): array
{
$results = [];
if($this->extension !== null && $this->extension !== '')
{
$results[($bytecode ? Functions::cbc('extension') : 'extension')] = $this->extension;
}
$results[($bytecode ? Functions::cbc('extension') : 'extension')] = $this->extension->value;
if($this->minimum_version !== null && $this->minimum_version !== '')
{

View file

@ -24,6 +24,7 @@
namespace ncc\Objects\ProjectConfiguration;
use ncc\Enums\CompilerExtensions;
use ncc\Exceptions\ConfigurationException;
use ncc\Exceptions\NotSupportedException;
use ncc\Interfaces\BytecodeObjectInterface;
@ -52,17 +53,20 @@
/**
* Public Constructor
* @param string|Compiler $compiler
* @param CompilerExtensions|Compiler $extension
* @throws NotSupportedException
*/
public function __construct(string|Compiler $compiler)
public function __construct(CompilerExtensions|Compiler $extension)
{
if(is_string($compiler))
if($extension instanceof Compiler)
{
$compiler = new Compiler($compiler);
$this->compiler = $extension;
}
else
{
$this->compiler = new Compiler($extension);
}
$this->compiler = $compiler;
$this->options = [];
}

View file

@ -24,6 +24,7 @@
namespace ncc\Objects\ProjectConfiguration\UpdateSource;
use ncc\Enums\Types\RepositoryType;
use ncc\Exceptions\ConfigurationException;
use ncc\Interfaces\BytecodeObjectInterface;
use ncc\Utilities\Functions;
@ -40,7 +41,7 @@
/**
* The type of client that is used to connect to the remote source
*
* @var string|null
* @var RepositoryType|null
*/
private $type;
@ -58,7 +59,7 @@
*/
private $ssl;
public function __construct(string $name, string $host, ?string $type=null, bool $ssl=false)
public function __construct(string $name, string $host, ?RepositoryType $type=null, bool $ssl=false)
{
$this->name = $name;
$this->host = $host;
@ -83,17 +84,17 @@
}
/**
* @return string|null
* @return RepositoryType|null
*/
public function getType(): ?string
public function getType(): ?RepositoryType
{
return $this->type;
}
/**
* @param string|null $type
* @param RepositoryType|null $type
*/
public function setType(?string $type): void
public function setType(?RepositoryType $type): void
{
$this->type = $type;
}
@ -137,7 +138,8 @@
{
return [
($bytecode ? Functions::cbc('name') : 'name') => $this->name,
($bytecode ? Functions::cbc('type') : 'type') => $this->type,
// TODO: Review the ? logic here to see if makes sense
($bytecode ? Functions::cbc('type') : 'type') => ($this->type ? null : $this->type->value),
($bytecode ? Functions::cbc('host') : 'host') => $this->host,
($bytecode ? Functions::cbc('ssl') : 'ssl') => $this->ssl
];
@ -150,7 +152,7 @@
public static function fromArray(array $data): Repository
{
$name = Functions::array_bc($data, 'name');
$type = Functions::array_bc($data, 'type');
$type = RepositoryType::tryFrom(Functions::array_bc($data, 'type'));
$host = Functions::array_bc($data, 'host');
$ssl = Functions::array_bc($data, 'ssl') ?? false;

View file

@ -52,8 +52,7 @@
private $host;
/**
* @var string
* @see RepositoryType
* @var RepositoryType
*/
private $type;
@ -100,11 +99,10 @@
/**
* Returns the type of service ncc should use with this source (gitlab, github, etc...).
*
* @return string
* @see RepositoryType
*
* @return RepositoryType
*/
public function getType(): string
public function getType(): RepositoryType
{
return $this->type;
}
@ -175,13 +173,13 @@
*/
public function fetchPackage(string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return match(strtolower($this->type))
return match($this->type)
{
RepositoryType::GITHUB->value => GithubRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITLAB->value => GitlabRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITEA->value => GiteaRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::PACKAGIST->value => throw new NotSupportedException('Fetching ncc packages from Packagist is not supported'),
default => throw new InvalidArgumentException(sprintf('Invalid repository type \'%s\'', $this->type)),
RepositoryType::GITHUB => GithubRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITLAB => GitlabRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITEA => GiteaRepository::fetchPackage($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::PACKAGIST => throw new NotSupportedException('Fetching ncc packages from Packagist is not supported'),
default => throw new InvalidArgumentException(sprintf('Invalid repository type \'%s\'', $this->type->value)),
};
}
@ -199,13 +197,13 @@
*/
public function fetchSourceArchive(string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return match(strtolower($this->type))
return match($this->type)
{
RepositoryType::GITHUB->value => GithubRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITLAB->value => GitlabRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITEA->value => GiteaRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::PACKAGIST->value => PackagistRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
default => throw new InvalidArgumentException(sprintf('Invalid repository type \'%s\'', $this->type)),
RepositoryType::GITHUB => GithubRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITLAB => GitlabRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::GITEA => GiteaRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
RepositoryType::PACKAGIST => PackagistRepository::fetchSourceArchive($this, $vendor, $project, $version, $authentication, $options),
default => throw new InvalidArgumentException(sprintf('Invalid repository type \'%s\'', $this->type->value)),
};
}
@ -226,7 +224,7 @@
{
return [
($bytecode ? Functions::cbc('name') : 'name') => $this->name,
($bytecode ? Functions::cbc('type') : 'type') => $this->type,
($bytecode ? Functions::cbc('type') : 'type') => ($this->type ? null : $this->type->value),
($bytecode ? Functions::cbc('host') : 'host') => $this->host,
($bytecode ? Functions::cbc('ssl') : 'ssl') => $this->ssl
];
@ -238,10 +236,15 @@
public static function fromArray(array $data): self
{
$name = Functions::array_bc($data, 'name');
$type = Functions::array_bc($data, 'type');
$type = RepositoryType::tryFrom(Functions::array_bc($data, 'type'));
$host = Functions::array_bc($data, 'host');
$ssl = Functions::array_bc($data, 'ssl') ?? true;
if($type === null)
{
throw new InvalidArgumentException(sprintf("Unrecognized repository type %s", Functions::array_bc($data, 'type')));
}
return new self($name, $host, $type, $ssl);
}
}