From 2d066c6189a4a45123db9a4e35ef98af5817b7cc Mon Sep 17 00:00:00 2001 From: Zi Xing Date: Mon, 18 Apr 2022 18:45:38 -0400 Subject: [PATCH] Added CLI stuff --- src/ncc/Abstracts/StringPaddingMethod.php | 12 +++ src/ncc/CLI/Functions.php | 31 ++++++ src/ncc/CLI/HelpMenu.php | 120 ++++++++++++++++++++++ src/ncc/CLI/Main.php | 44 ++++++++ src/ncc/Objects/CliHelpSection.php | 111 ++++++++++++++++++++ src/ncc/Utilities/Functions.php | 26 +++++ src/ncc/Utilities/Resolver.php | 71 +++++++++++++ src/ncc/autoload.php | 5 + src/ncc/banner_basic | 2 +- src/ncc/banner_extended | 2 +- src/ncc/ncc | 5 + src/ncc/ncc.sh | 1 + 12 files changed, 428 insertions(+), 2 deletions(-) create mode 100644 src/ncc/Abstracts/StringPaddingMethod.php create mode 100644 src/ncc/CLI/Functions.php create mode 100644 src/ncc/CLI/HelpMenu.php create mode 100644 src/ncc/CLI/Main.php create mode 100644 src/ncc/Objects/CliHelpSection.php create mode 100644 src/ncc/ncc create mode 100755 src/ncc/ncc.sh diff --git a/src/ncc/Abstracts/StringPaddingMethod.php b/src/ncc/Abstracts/StringPaddingMethod.php new file mode 100644 index 0000000..a9e113e --- /dev/null +++ b/src/ncc/Abstracts/StringPaddingMethod.php @@ -0,0 +1,12 @@ + 0) + { + $out = str_pad($out, $padding, $pad_string, $pad_type); + } + + if($eol) + { + $out = $out . PHP_EOL; + } + + print($out); + } + } \ No newline at end of file diff --git a/src/ncc/CLI/HelpMenu.php b/src/ncc/CLI/HelpMenu.php new file mode 100644 index 0000000..e29daed --- /dev/null +++ b/src/ncc/CLI/HelpMenu.php @@ -0,0 +1,120 @@ +toString($options_padding) . PHP_EOL); + } + print(PHP_EOL); + + + } + + /** + * Displays the management commands section + * + * @return void + */ + private static function displayManagementCommands(): void + { + $commands = [ + new CliHelpSection(['project'], 'Manages the current project'), + new CliHelpSection(['package'], 'Manages the package system'), + new CliHelpSection(['cache'], 'Manages the system cache'), + new CliHelpSection(['credential'], 'Manages credentials'), + ]; + $commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2; + + print('Management Commands:' . PHP_EOL); + foreach($commands as $command) + { + print(' ' . $command->toString($commands_padding) . PHP_EOL); + } + print(PHP_EOL); + } + + /** + * Displays the main commands section + * + * @return void + */ + private static function displayMainCommands(): void + { + $commands = [ + new CliHelpSection(['build'], 'Builds the current project'), + new CliHelpSection(['main'], 'Executes the main entrypoint of a package') + ]; + $commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2; + + print('Commands:' . PHP_EOL); + foreach($commands as $command) + { + print(' ' . $command->toString($commands_padding) . PHP_EOL); + } + print(PHP_EOL); + } + + /** + * Displays the main commands section + * + * @return void + */ + private static function displayExtensions(): void + { + $extensions = [ + new CliHelpSection(['exphp'], 'The PHP compiler extension') + ]; + $extensions_padding = \ncc\Utilities\Functions::detectParametersPadding($extensions) + 2; + + print('Extensions:' . PHP_EOL); + foreach($extensions as $command) + { + print(' ' . $command->toString($extensions_padding) . PHP_EOL); + } + print(PHP_EOL); + } + } \ No newline at end of file diff --git a/src/ncc/CLI/Main.php b/src/ncc/CLI/Main.php new file mode 100644 index 0000000..769f230 --- /dev/null +++ b/src/ncc/CLI/Main.php @@ -0,0 +1,44 @@ +Parameters = $parameters; + $this->Description = $description; + $this->Default = $default; + } + + /** + * Returns an array representation of the object + * + * @return array + */ + public function toArray(): array + { + return [ + 'parameters' => $this->Parameters, + 'description' => $this->Description, + 'default' => $this->Default + ]; + } + + /** + * Constructs object from an array representation + * + * @param array $data + * @return CliHelpSection + */ + public static function fromArray(array $data): CliHelpSection + { + $Object = new CliHelpSection(); + + if(isset($data['parameters'])) + $Object->Parameters = $data['parameters']; + + if(isset($data['description'])) + $Object->Description = $data['description']; + + if(isset($data['default'])) + $Object->Default = $data['default']; + + return $Object; + } + + /** + * Returns a string representation of the object + * + * @return string + */ + public function toString(int $param_padding=0) + { + $out = []; + + if(count($this->Parameters) > 0) + { + if($param_padding > 0) + { + $out[] .= str_pad(implode(' ', $this->Parameters), $param_padding, ' ', STR_PAD_RIGHT); + } + else + { + $out[] .= implode(' ', $this->Parameters); + } + } + + if($this->Description !== null) + { + $out[] = $this->Description; + } + + if($this->Default !== null) + { + $out[] = '(Default: ' . $this->Default . ')'; + } + + return implode(' ', $out); + } + } \ No newline at end of file diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index 388956a..b391bb6 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -4,6 +4,7 @@ use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\MalformedJsonException; + use ncc\Objects\CliHelpSection; /** * @author Zi Xing Narrakas @@ -126,4 +127,29 @@ { file_put_contents($path, self::encodeJson($value, $flags)); } + + /** + * @param CliHelpSection[] $input + * @return int + */ + public static function detectParametersPadding(array $input): int + { + $current_count = 0; + + foreach($input as $optionsSection) + { + if(count($optionsSection->Parameters) > 0) + { + foreach($optionsSection->Parameters as $parameter) + { + if($current_count < strlen($parameter)) + { + $current_count = strlen($parameter); + } + } + } + } + + return $current_count; + } } \ No newline at end of file diff --git a/src/ncc/Utilities/Resolver.php b/src/ncc/Utilities/Resolver.php index 0873daa..96ce931 100644 --- a/src/ncc/Utilities/Resolver.php +++ b/src/ncc/Utilities/Resolver.php @@ -41,4 +41,75 @@ return $input; } + + /** + * Parse arguments + * + * @param array|string [$message] input arguments + * @param int $max_arguments + * @return array Configs Key/Value + */ + public static function parseArguments($message=null, int $max_arguments=1000): array + { + if (is_string($message)) + { + $flags = $message; + } + elseif(is_array($message)) + { + $flags = implode(' ', $message); + } + else + { + global $argv; + if(isset($argv) && count($argv) > 1) + { + array_shift($argv); + } + $flags = implode(' ', $argv); + } + + $configs = array(); + $regex = "/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((? $match) + { + if (isset($match['value']) && $match['value'] !== '') + { + $value = $match['value']; + } + else if (isset($match['string']) && $match['string'] !== '') + { + // fix escaped quotes + $value = str_replace("\\\"", "\"", $match['string']); + $value = str_replace("\\'", "'", $value); + } + else + { + $value = true; + } + + if (isset($match['bigflag']) && $match['bigflag'] !== '') + { + $configs[$match['bigflag']] = $value; + } + + if (isset($match['smallflag']) && $match['smallflag'] !== '') + { + $configs[$match['smallflag']] = $value; + } + + if (isset($match['unmatched']) && $match['unmatched'] !== '') + { + $configs[$match['unmatched']] = true; + } + + if ($index >= $max_arguments) + break; + } + + return $configs; + } + } \ No newline at end of file diff --git a/src/ncc/autoload.php b/src/ncc/autoload.php index ae60038..ea08b02 100644 --- a/src/ncc/autoload.php +++ b/src/ncc/autoload.php @@ -10,6 +10,7 @@ spl_autoload_register( 'ncc\\abstracts\\exceptioncodes' => '/Abstracts/ExceptionCodes.php', 'ncc\\abstracts\\regexpatterns' => '/Abstracts/RegexPatterns.php', 'ncc\\abstracts\\scopes' => '/Abstracts/Scopes.php', + 'ncc\\abstracts\\stringpaddingmethod' => '/Abstracts/StringPaddingMethod.php', 'ncc\\assert\\assert' => '/ThirdParty/beberlei/assert/Assert.php', 'ncc\\assert\\assertion' => '/ThirdParty/beberlei/assert/Assertion.php', 'ncc\\assert\\assertionchain' => '/ThirdParty/beberlei/assert/AssertionChain.php', @@ -17,6 +18,9 @@ spl_autoload_register( 'ncc\\assert\\invalidargumentexception' => '/ThirdParty/beberlei/assert/InvalidArgumentException.php', 'ncc\\assert\\lazyassertion' => '/ThirdParty/beberlei/assert/LazyAssertion.php', 'ncc\\assert\\lazyassertionexception' => '/ThirdParty/beberlei/assert/LazyAssertionException.php', + 'ncc\\cli\\functions' => '/CLI/Functions.php', + 'ncc\\cli\\helpmenu' => '/CLI/HelpMenu.php', + 'ncc\\cli\\main' => '/CLI/Main.php', 'ncc\\exceptions\\accessdeniedexception' => '/Exceptions/AccessDeniedException.php', 'ncc\\exceptions\\directorynotfoundexception' => '/Exceptions/DirectoryNotFoundException.php', 'ncc\\exceptions\\filenotfoundexception' => '/Exceptions/FileNotFoundException.php', @@ -27,6 +31,7 @@ spl_autoload_register( 'ncc\\ncc\\ziproto\\typetransformer\\binarytransformer' => '/Extensions/ZiProto/TypeTransformer/BinaryTransformer.php', 'ncc\\ncc\\ziproto\\typetransformer\\extension' => '/Extensions/ZiProto/TypeTransformer/Extension.php', 'ncc\\ncc\\ziproto\\typetransformer\\validator' => '/Extensions/ZiProto/TypeTransformer/Validator.php', + 'ncc\\objects\\clihelpsection' => '/Objects/CliHelpSection.php', 'ncc\\objects\\projectconfiguration' => '/Objects/ProjectConfiguration.php', 'ncc\\objects\\projectconfiguration\\assembly' => '/Objects/ProjectConfiguration/Assembly.php', 'ncc\\objects\\projectconfiguration\\build' => '/Objects/ProjectConfiguration/Build.php', diff --git a/src/ncc/banner_basic b/src/ncc/banner_basic index 65cc66e..b73bd9e 100644 --- a/src/ncc/banner_basic +++ b/src/ncc/banner_basic @@ -1,5 +1,5 @@ +--------Version--------++-------------Copyright----------+ -| %V% %B% || %C | +| %A || %B | +-----------------------++--------------------------------+ | ______ | | d8b db .o88b. .o88b. |_,.,--\ | diff --git a/src/ncc/banner_extended b/src/ncc/banner_extended index ce8ca53..25716df 100644 --- a/src/ncc/banner_extended +++ b/src/ncc/banner_extended @@ -1,5 +1,5 @@ ┌────────Version────────┬┬─────────────Copyright──────────┐ -│ %V% %B% ││ %C │ +│ %A ││ %B │ ├───────────────────────┴┴────────────────────────────────┤ │ ______ │ │ d8b db .o88b. .o88b. |_,.,--\ │ diff --git a/src/ncc/ncc b/src/ncc/ncc new file mode 100644 index 0000000..826662f --- /dev/null +++ b/src/ncc/ncc @@ -0,0 +1,5 @@ +