Added package name validation

This commit is contained in:
Netkas 2022-07-26 19:40:30 -04:00
parent 697d10e4d7
commit 4a1ae8e14c
5 changed files with 137 additions and 5 deletions

View file

@ -8,6 +8,7 @@
use ncc\Exceptions\DirectoryNotFoundException;
use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\InvalidCredentialsEntryException;
use ncc\Exceptions\InvalidPackageNameException;
use ncc\Exceptions\InvalidProjectConfigurationException;
use ncc\Exceptions\InvalidScopeException;
use ncc\Exceptions\MalformedJsonException;
@ -68,4 +69,26 @@
* @see ConstantReadonlyException
*/
const ConstantReadonlyException = -1709;
/**
* @see InvalidPackageNameException
*/
const InvalidPackageNameException = -1710;
/**
* All the exception codes from NCC
*/
const All = [
self::InvalidProjectConfigurationException,
self::FileNotFoundException,
self::DirectoryNotFoundException,
self::InvalidScopeException,
self::AccessDeniedException,
self::MalformedJsonException,
self::RuntimeException,
self::InvalidCredentialsEntryException,
self::ComponentVersionNotFoundException,
self::ConstantReadonlyException,
self::InvalidPackageNameException
];
}

View file

@ -0,0 +1,19 @@
<?php
namespace ncc\Exceptions;
use Exception;
use ncc\Abstracts\ExceptionCodes;
use Throwable;
class InvalidPackageNameException extends Exception
{
/**
* @param string $message
* @param Throwable|null $previous
*/
public function __construct(string $message = "", ?Throwable $previous = null)
{
parent::__construct($message, ExceptionCodes::InvalidPackageNameException, $previous);
}
}

View file

@ -3,10 +3,12 @@
namespace ncc\Managers;
use ncc\Abstracts\Options\InitializeProjectOptions;
use ncc\Exceptions\InvalidPackageNameException;
use ncc\Exceptions\MalformedJsonException;
use ncc\Objects\ProjectConfiguration;
use ncc\Objects\ProjectConfiguration\Compiler;
use ncc\Symfony\Component\Uid\Uuid;
use ncc\Utilities\Validate;
class ProjectManager
{
@ -20,14 +22,14 @@
/**
* The path that points to the project's main project.json file
*
* @var string|null
* @var string
*/
private string $ProjectFilePath;
/**
* The path that points the project's main directory
*
* @var string|null
* @var string
*/
private string $ProjectPath;
@ -69,7 +71,7 @@
}
// Detect if project.json exists in the directory
if(file_exists($selected_directory . 'project.json') == true)
if(file_exists($selected_directory . 'project.json'))
{
$this->ProjectPath = $selected_directory;
$this->ProjectFilePath = $selected_directory . 'project.json';
@ -80,17 +82,24 @@
}
/**
* Initializes the project sturcture
* Initializes the project structure
*
* @param Compiler $compiler
* @param string $source
* @param string $name
* @param string $package
* @param array $options
* @throws InvalidPackageNameException
* @throws MalformedJsonException
*/
public function initializeProject(Compiler $compiler, string $source, string $name, string $package, array $options=[])
{
// Validate the project information first
if(!Validate::packageName($package))
{
throw new InvalidPackageNameException('The given package name \'' . $package . '\' is not a valid package name');
}
$Project = new ProjectConfiguration();
// Set the compiler information
@ -99,6 +108,7 @@
// Set the assembly information
$Project->Assembly->Name = $name;
$Project->Assembly->Package = $package;
$Project->Assembly->Version = '1.0.0';
$Project->Assembly->UUID = Uuid::v1()->toRfc4122();
// Set the build information
@ -135,7 +145,7 @@
foreach($Folders as $folder)
{
if(file_exists($folder) == false)
if(!file_exists($folder))
{
mkdir($folder);
}

View file

@ -2,6 +2,7 @@
namespace ncc\Utilities;
use Exception;
use ncc\Abstracts\ConsoleColors;
class Console
@ -109,4 +110,40 @@
{
self::out(self::formatColor(ConsoleColors::Yellow, 'Warning: ') . $message, $newline);
}
/**
* Prints out an exception message and exits the program if needed
*
* @param string $message
* @param Exception $e
* @param int|null $exit_code
* @return void
*/
public static function outException(string $message, Exception $e, ?int $exit_code=null)
{
if(strlen($message) > 0)
{
self::out(self::formatColor('Error: ' . $message, ConsoleColors::Red));
}
self::outExceptionDetails($e);
if($exit_code !== null)
{
exit($exit_code);
}
}
/**
* Prints out a detailed exception display (unfinished)
*
* @param Exception $e
* @return void
*/
private static function outExceptionDetails(Exception $e)
{
$trace_header = self::formatColor($e->getFile() . ':' . $e->getLine(), ConsoleColors::Magenta);
$trace_error = self::formatColor('error: ', ConsoleColors::Red);
self::out($trace_header . ' ' . $trace_error . $e->getMessage());
}
}