From 4a1ae8e14c99da04b9166ac2fd27d2a272fc1d72 Mon Sep 17 00:00:00 2001 From: Netkas Date: Tue, 26 Jul 2022 19:40:30 -0400 Subject: [PATCH] Added package name validation --- docs/package_name.md | 43 +++++++++++++++++++ src/ncc/Abstracts/ExceptionCodes.php | 23 ++++++++++ .../InvalidPackageNameException.php | 19 ++++++++ src/ncc/Managers/ProjectManager.php | 20 ++++++--- src/ncc/Utilities/Console.php | 37 ++++++++++++++++ 5 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 docs/package_name.md create mode 100644 src/ncc/Exceptions/InvalidPackageNameException.php diff --git a/docs/package_name.md b/docs/package_name.md new file mode 100644 index 0000000..0b4586e --- /dev/null +++ b/docs/package_name.md @@ -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 \ No newline at end of file diff --git a/src/ncc/Abstracts/ExceptionCodes.php b/src/ncc/Abstracts/ExceptionCodes.php index 528e29b..2ccbc6d 100644 --- a/src/ncc/Abstracts/ExceptionCodes.php +++ b/src/ncc/Abstracts/ExceptionCodes.php @@ -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 + ]; } \ No newline at end of file diff --git a/src/ncc/Exceptions/InvalidPackageNameException.php b/src/ncc/Exceptions/InvalidPackageNameException.php new file mode 100644 index 0000000..da9ebaa --- /dev/null +++ b/src/ncc/Exceptions/InvalidPackageNameException.php @@ -0,0 +1,19 @@ +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); } diff --git a/src/ncc/Utilities/Console.php b/src/ncc/Utilities/Console.php index 99757d7..236aa96 100644 --- a/src/ncc/Utilities/Console.php +++ b/src/ncc/Utilities/Console.php @@ -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()); + } } \ No newline at end of file