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

43
docs/package_name.md Normal file
View file

@ -0,0 +1,43 @@
# Naming a package
**Updated on Tuesday, July 26, 2022**
NCC Follows the same naming convention as Java's naming
convention. The purpose of naming a package this way is
to easily create a "Name" of the package, this string
of information contains
- The developer/organization behind the package
- The package name itself
# Naming conventions
Package names are written in all lower-case due to the
fact that some operating systems treats file names
differently, for example on Linux `Aa.txt` and `aa.txt`
are two entirely different file names because of the
capitalization and on Windows it's treated as the same
file name.
Organizations or small developers use their domain name
in reverse to begin their package names, for example
`net.nosial.example` is a package named `example`
created by a programmer at `nosial.net`
Just like the Java naming convention, to avoid conflicts
of the same package name developers can use something
different, for example as pointed out in Java's package
naming convention developers can instead use something
like a region to name packages, for example
`net.nosial.region.example`
# References
For Java's package naming conventions see
[Naming a Package](https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html)
from the Oracle's Java documentation resource, as the
same rules apply to NCC except for *some* illegal naming
conventions such as packages not being able to begin
with `int` or numbers

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