diff --git a/src/ncc/Abstracts/ExceptionCodes.php b/src/ncc/Abstracts/ExceptionCodes.php index 2ccbc6d..cbd94bc 100644 --- a/src/ncc/Abstracts/ExceptionCodes.php +++ b/src/ncc/Abstracts/ExceptionCodes.php @@ -10,8 +10,11 @@ use ncc\Exceptions\InvalidCredentialsEntryException; use ncc\Exceptions\InvalidPackageNameException; use ncc\Exceptions\InvalidProjectConfigurationException; + use ncc\Exceptions\InvalidProjectNameException; use ncc\Exceptions\InvalidScopeException; + use ncc\Exceptions\InvalidVersionNumberException; use ncc\Exceptions\MalformedJsonException; + use ncc\Exceptions\ProjectAlreadyExistsException; use ncc\Exceptions\RuntimeException; /** @@ -75,6 +78,21 @@ */ const InvalidPackageNameException = -1710; + /** + * @see InvalidVersionNumberException + */ + const InvalidVersionNumberException = -1711; + + /** + * @see InvalidProjectNameException + */ + const InvalidProjectNameException = -1712; + + /** + * @see ProjectAlreadyExistsException + */ + const ProjectAlreadyExistsException = -1713; + /** * All the exception codes from NCC */ @@ -89,6 +107,9 @@ self::InvalidCredentialsEntryException, self::ComponentVersionNotFoundException, self::ConstantReadonlyException, - self::InvalidPackageNameException + self::InvalidPackageNameException, + self::InvalidVersionNumberException, + self::InvalidProjectNameException, + self::ProjectAlreadyExistsException, ]; } \ No newline at end of file diff --git a/src/ncc/CLI/ProjectMenu.php b/src/ncc/CLI/ProjectMenu.php index e08dd20..a7aeeb5 100644 --- a/src/ncc/CLI/ProjectMenu.php +++ b/src/ncc/CLI/ProjectMenu.php @@ -2,11 +2,14 @@ namespace ncc\CLI; - use ncc\Abstracts\Scopes; - use ncc\Exceptions\AccessDeniedException; + use Exception; + use ncc\Exceptions\InvalidPackageNameException; + use ncc\Exceptions\InvalidProjectNameException; + use ncc\Exceptions\ProjectAlreadyExistsException; + use ncc\Managers\ProjectManager; use ncc\Objects\CliHelpSection; + use ncc\Objects\ProjectConfiguration\Compiler; use ncc\Utilities\Console; - use ncc\Utilities\Resolver; class ProjectMenu { @@ -15,7 +18,6 @@ * * @param $args * @return void - * @throws AccessDeniedException */ public static function start($args): void { @@ -32,11 +34,71 @@ /** * @param $args * @return void - * @throws AccessDeniedException */ public static function createProject($args): void { - Console::out('end' . PHP_EOL); + // First determine the source directory of the project + $src = getcwd(); + if(isset($args['src'])) + { + // Make sure directory separators are corrected + $args['src'] = str_ireplace('/', DIRECTORY_SEPARATOR, $args['src']); + $args['src'] = str_ireplace('\\', DIRECTORY_SEPARATOR, $args['src']); + + // Remove the trailing slash + if(substr($args['src'], -1) == DIRECTORY_SEPARATOR) + { + $args['src'] = substr($args['src'], 0, -1); + } + + $full_path = getcwd() . DIRECTORY_SEPARATOR . $args['src']; + + if(file_exists($full_path) && is_dir($full_path)) + { + $src = getcwd() . DIRECTORY_SEPARATOR . $args['src']; + } + else + { + Console::outError('The selected source directory \'' . $full_path . '\' was not found or is not a directory', true, 1); + } + } + + // Fetch the rest of the information needed for the project + //$compiler_extension = Console::getOptionInput($args, 'ce', 'Compiler Extension (php, java): '); + $compiler_extension = 'php'; // Always php, for now. + $package_name = Console::getOptionInput($args, 'package', 'Package Name (com.example.foo): '); + $project_name = Console::getOptionInput($args, 'name', 'Project Name (Foo Bar Library): '); + + // Create the compiler configuration + $Compiler = new Compiler(); + $Compiler->Extension = $compiler_extension; + $Compiler->MaximumVersion = '8.1'; + $Compiler->MinimumVersion = '7.4'; + + // Now create the project + $ProjectManager = new ProjectManager($src); + try + { + $ProjectManager->initializeProject($Compiler, $project_name, $package_name); + } + catch (InvalidPackageNameException $e) + { + Console::outException('The given package name is invalid, the value must follow the standard package naming convention', $e, 1); + } + catch (InvalidProjectNameException $e) + { + Console::outException('The given project name is invalid, cannot be empty or larger than 126 characters', $e, 1); + } + catch (ProjectAlreadyExistsException $e) + { + Console::outException('A project has already been initialized in \'' . $src . '\'', $e, 1); + } + catch(Exception $e) + { + Console::outException('There was an unexpected error while trying to initialize the project', $e, 1); + } + + Console::out('Project successfully created'); exit(0); } diff --git a/src/ncc/Exceptions/InvalidProjectNameException.php b/src/ncc/Exceptions/InvalidProjectNameException.php new file mode 100644 index 0000000..8778bce --- /dev/null +++ b/src/ncc/Exceptions/InvalidProjectNameException.php @@ -0,0 +1,19 @@ +ProjectPath . DIRECTORY_SEPARATOR . 'project.json')) + { + throw new ProjectAlreadyExistsException('A project has already been initialized in \'' . $this->ProjectPath . DIRECTORY_SEPARATOR . 'project.json' . '\''); + } + $Project = new ProjectConfiguration(); // Set the compiler information @@ -112,7 +125,7 @@ $Project->Assembly->UUID = Uuid::v1()->toRfc4122(); // Set the build information - $Project->Build->SourcePath = $source; + $Project->Build->SourcePath = $this->SelectedDirectory; $Project->Build->DefaultConfiguration = 'debug'; // Assembly constants if the program wishes to check for this @@ -157,7 +170,7 @@ switch($option) { case InitializeProjectOptions::CREATE_SOURCE_DIRECTORY: - if(file_exists($source) == false) + if(!file_exists($source)) { mkdir($source); } diff --git a/src/ncc/Utilities/Console.php b/src/ncc/Utilities/Console.php index 236aa96..00ee427 100644 --- a/src/ncc/Utilities/Console.php +++ b/src/ncc/Utilities/Console.php @@ -108,7 +108,25 @@ */ public static function outWarning(string $message, bool $newline=true) { - self::out(self::formatColor(ConsoleColors::Yellow, 'Warning: ') . $message, $newline); + self::out(self::formatColor('Warning: ', ConsoleColors::Yellow) . $message, $newline); + } + + /** + * Prints out a generic error output, optionally exits the process with an exit code. + * + * @param string $message + * @param bool $newline + * @param int|null $exit_code + * @return void + */ + public static function outError(string $message, bool $newline=true, ?int $exit_code=null) + { + self::out(self::formatColor(ConsoleColors::Red, 'Error: ') . $message, $newline); + + if($exit_code !== null) + { + exit($exit_code); + } } /** @@ -146,4 +164,73 @@ $trace_error = self::formatColor('error: ', ConsoleColors::Red); self::out($trace_header . ' ' . $trace_error . $e->getMessage()); } + + /** + * @param string|null $prompt + * @return string + */ + public static function getInput(?string $prompt=null): string + { + if($prompt !== null) + { + print($prompt); + } + + $handle = fopen ("php://stdin","r"); + return fgets($handle); + } + + /** + * @param array $args + * @param string $option + * @param string $prompt + * @return string + */ + public static function getOptionInput(array $args, string $option, string $prompt): string + { + if(isset($args[$option])) + { + return $args[$option]; + } + + return self::getInput($prompt); + } + + /** + * Prompts the user for a yes/no input + * + * @param string $prompt + * @param bool $display_options + * @return bool + */ + public static function getBooleanInput(string $prompt, bool $display_options=true): bool + { + while(true) + { + if($display_options) + { + $r = self::getInput($prompt . ' (Y/N): ', false); + } + else + { + $r = self::getInput($prompt, false); + } + + if(strlen($r) > 0) + { + switch(strtoupper($r)) + { + case '1': + case 'Y': + case 'YES': + return true; + + case '0': + case 'N': + case 'NO': + return false; + } + } + } + } } \ No newline at end of file diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index de0f71d..b8681fb 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -184,35 +184,4 @@ return $banner; } - - /** - * @param string|null $prompt - * @return string - */ - public static function getInput(?string $prompt=null): string - { - if($prompt !== null) - { - print($prompt); - } - - $handle = fopen ("php://stdin","r"); - return fgets($handle); - } - - /** - * @param array $args - * @param string $option - * @param string $prompt - * @return string - */ - public static function getOptionInput(array $args, string $option, string $prompt): string - { - if(isset($args[$option])) - { - return $args[$option]; - } - - return self::getInput($prompt); - } } \ No newline at end of file diff --git a/src/ncc/Utilities/Validate.php b/src/ncc/Utilities/Validate.php index 6c531b7..98485b7 100644 --- a/src/ncc/Utilities/Validate.php +++ b/src/ncc/Utilities/Validate.php @@ -129,4 +129,30 @@ return true; } + /** + * Validates the name of the project + * + * @param $input + * @return bool + */ + public static function projectName($input): bool + { + if($input == null) + { + return false; + } + + if(strlen($input) == 0) + { + return false; + } + + if(strlen($input) > 126) + { + return false; + } + + return true; + } + } \ No newline at end of file