From 191b121542f7de71377b63fca2a470f8c1696402 Mon Sep 17 00:00:00 2001 From: Netkas Date: Thu, 20 Oct 2022 14:16:58 -0400 Subject: [PATCH] Updated ExampleLibrary and fixed the project creation command to use the correct source directory --- src/installer/installer | 3 ++ src/ncc/CLI/ProjectMenu.php | 15 +++++--- src/ncc/Managers/ProjectManager.php | 5 ++- src/ncc/Utilities/Functions.php | 26 +++++++++++++ tests/example_project/README.md | 31 ++++++++++++++++ tests/example_project/project.json | 57 +++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 tests/example_project/project.json diff --git a/src/installer/installer b/src/installer/installer index f558d32..e90cf03 100644 --- a/src/installer/installer +++ b/src/installer/installer @@ -726,5 +726,8 @@ } file_put_contents($NCC_INSTALL_PATH . DIRECTORY_SEPARATOR . 'ncc.yaml', Yaml::dump($config_obj)); + Console::out('NCC version: ' . NCC_VERSION_NUMBER . ' has been successfully installed'); + Console::out('For licensing information see \'' . $NCC_INSTALL_PATH . DIRECTORY_SEPARATOR . 'LICENSE\' or run \'ncc help --license\''); + exit(0); \ No newline at end of file diff --git a/src/ncc/CLI/ProjectMenu.php b/src/ncc/CLI/ProjectMenu.php index 8951128..fcc1fce 100644 --- a/src/ncc/CLI/ProjectMenu.php +++ b/src/ncc/CLI/ProjectMenu.php @@ -38,7 +38,8 @@ public static function createProject($args): void { // First determine the source directory of the project - $src = getcwd(); + $current_directory = getcwd(); + $real_src = $current_directory; if(isset($args['src'])) { // Make sure directory separators are corrected @@ -55,7 +56,7 @@ if(file_exists($full_path) && is_dir($full_path)) { - $src = getcwd() . DIRECTORY_SEPARATOR . $args['src']; + $real_src = getcwd() . DIRECTORY_SEPARATOR . $args['src']; } else { @@ -63,6 +64,9 @@ } } + // Remove basename from real_src + $real_src = \ncc\Utilities\Functions::removeBasename($real_src, $current_directory); + // 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. @@ -76,10 +80,11 @@ $Compiler->MinimumVersion = '7.4'; // Now create the project - $ProjectManager = new ProjectManager($src); + $ProjectManager = new ProjectManager($current_directory); + try { - $ProjectManager->initializeProject($Compiler, $project_name, $package_name); + $ProjectManager->initializeProject($Compiler, $project_name, $package_name, $real_src); } catch (InvalidPackageNameException $e) { @@ -91,7 +96,7 @@ } catch (ProjectAlreadyExistsException $e) { - Console::outException('A project has already been initialized in \'' . $src . '\'', $e, 1); + Console::outException('A project has already been initialized in \'' . $current_directory . '\'', $e, 1); } catch(Exception $e) { diff --git a/src/ncc/Managers/ProjectManager.php b/src/ncc/Managers/ProjectManager.php index 1e1be7b..a9d0e42 100644 --- a/src/ncc/Managers/ProjectManager.php +++ b/src/ncc/Managers/ProjectManager.php @@ -84,13 +84,14 @@ * @param Compiler $compiler * @param string $name * @param string $package + * @param string $src * @param array $options * @throws InvalidPackageNameException * @throws InvalidProjectNameException * @throws MalformedJsonException * @throws ProjectAlreadyExistsException */ - public function initializeProject(Compiler $compiler, string $name, string $package, array $options=[]): void + public function initializeProject(Compiler $compiler, string $name, string $package, string $src, array $options=[]): void { // Validate the project information first if(!Validate::packageName($package)) @@ -120,7 +121,7 @@ $Project->Assembly->UUID = Uuid::v1()->toRfc4122(); // Set the build information - $Project->Build->SourcePath = $this->SelectedDirectory; + $Project->Build->SourcePath = $src; $Project->Build->DefaultConfiguration = 'debug'; // Assembly constants if the program wishes to check for this diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index d0c3821..2635f28 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -184,4 +184,30 @@ return $banner; } + + /** + * Removes the basename from a path, eg; extracts "foo/bar.txt" from "/root/foo/bar.txt" if "/root" is the + * current working directory, optionally accepts a different basename using the $basename parameter. + * + * @param string $path + * @param string|null $basename + * @return string + */ + public static function removeBasename(string $path, ?string $basename=null): string + { + if($basename == null) + $basename = getcwd(); + + // Append the trailing slash if it's not already there + // "/etc/foo" becomes "/etc/foo/" + if(substr($basename, -1) !== DIRECTORY_SEPARATOR) + { + $basename .= DIRECTORY_SEPARATOR; + } + + var_dump($basename); + + // If the path is "/etc/foo/text.txt" and the basename is "/etc" then the returned path will be "foo/test.txt" + return str_replace($basename, (string)null, $path); + } } \ No newline at end of file diff --git a/tests/example_project/README.md b/tests/example_project/README.md index e602f79..47d6aa8 100644 --- a/tests/example_project/README.md +++ b/tests/example_project/README.md @@ -6,3 +6,34 @@ this project is a means to test the compiler capabilities of NCC ### Generating a project configuration file. +You can generate a project configuration file using the NCC +command-line interface, you should run the command in the +root directory of your project rather than the source directory. + +The argument `src` allows you to specify the source directory +which is required, otherwise NCC will assume the src directory is the current working directory. + +```shell +$ ls +README.md src + +$ ncc project create --src="src/ExampleLibrary" --package="com.example.library" --name="ExampleLibrary" +Project successfully created + +$ ls +ncc project.json README.md src +``` + +Upon creating the project, you will see a directory named `ncc` +and a file named `project.json`, the `ncc` directory will simply +contain extra information about the project, the file `project.json` +contains information about the project itself, you can modify +and change the contents of the project accordingly + +This process only needs to be done once, any additional changes +can be done manually by editing project.json + +Once project.json is created, the project can be compiled using NCC + + +### Compiling the project \ No newline at end of file diff --git a/tests/example_project/project.json b/tests/example_project/project.json new file mode 100644 index 0000000..411fc38 --- /dev/null +++ b/tests/example_project/project.json @@ -0,0 +1,57 @@ +{ + "project": { + "compiler": { + "extension": "php", + "minimum_version": "7.4", + "maximum_version": "8.1" + }, + "options": [] + }, + "assembly": { + "name": "ExampleLibrary", + "package": "com.example.library", + "description": null, + "company": null, + "product": null, + "copyright": null, + "trademark": null, + "version": "1.0.0", + "uid": "4a37f1c8-50a2-11ed-9ce4-170520693532" + }, + "build": { + "source_path": "src/ExampleLibrary", + "default_configuration": "debug", + "exclude_files": [], + "options": [], + "scope": null, + "define_constants": { + "ASSEMBLY_NAME": "%ASSEMBLY.NAME%", + "ASSEMBLY_PACKAGE": "%ASSEMBLY.PACKAGE%", + "ASSEMBLY_VERSION": "%ASSEMBLY.VERSION%", + "ASSEMBLY_UID": "%ASSEMBLY.UID%" + }, + "dependencies": [], + "configurations": [ + { + "name": "debug", + "options": [], + "output_path": "build/debug", + "define_constants": { + "DEBUG": "1" + }, + "exclude_files": [], + "dependencies": [] + }, + { + "name": "release", + "options": [], + "output_path": "build/release", + "define_constants": { + "DEBUG": "0" + }, + "exclude_files": [], + "dependencies": [] + } + ] + } +} \ No newline at end of file