Added CLI stuff
This commit is contained in:
parent
50d522dac6
commit
2d066c6189
12 changed files with 428 additions and 2 deletions
12
src/ncc/Abstracts/StringPaddingMethod.php
Normal file
12
src/ncc/Abstracts/StringPaddingMethod.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\Abstracts;
|
||||
|
||||
abstract class StringPaddingMethod
|
||||
{
|
||||
const LEFT = 'LEFT';
|
||||
|
||||
const RIGHT = 'RIGHT';
|
||||
|
||||
const BOTH = 'BOTH';
|
||||
}
|
31
src/ncc/CLI/Functions.php
Normal file
31
src/ncc/CLI/Functions.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\CLI;
|
||||
|
||||
class Functions
|
||||
{
|
||||
/**
|
||||
* Simple print function with builtin EOL terminator
|
||||
*
|
||||
* @param string $out
|
||||
* @param bool $eol
|
||||
* @param int $padding
|
||||
* @param int $pad_type
|
||||
* @param string $pad_string
|
||||
* @return void
|
||||
*/
|
||||
public static function print(string $out, bool $eol=true, int $padding=0, int $pad_type=0, string $pad_string=' ')
|
||||
{
|
||||
if($padding > 0)
|
||||
{
|
||||
$out = str_pad($out, $padding, $pad_string, $pad_type);
|
||||
}
|
||||
|
||||
if($eol)
|
||||
{
|
||||
$out = $out . PHP_EOL;
|
||||
}
|
||||
|
||||
print($out);
|
||||
}
|
||||
}
|
120
src/ncc/CLI/HelpMenu.php
Normal file
120
src/ncc/CLI/HelpMenu.php
Normal file
|
@ -0,0 +1,120 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\CLI;
|
||||
|
||||
use ncc\Objects\CliHelpSection;
|
||||
use ncc\Utilities\Resolver;
|
||||
|
||||
class HelpMenu
|
||||
{
|
||||
/**
|
||||
* Displays the main help menu
|
||||
*
|
||||
* @param $argv
|
||||
* @return void
|
||||
*/
|
||||
public static function start($argv)
|
||||
{
|
||||
print('Usage: ncc [options] COMMAND' . PHP_EOL . PHP_EOL);
|
||||
print('Nosial Code Compiler / Project Toolkit' . PHP_EOL . PHP_EOL);
|
||||
|
||||
self::displayMainOptions();
|
||||
self::displayManagementCommands();
|
||||
self::displayMainCommands();
|
||||
self::displayExtensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the main options section
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function displayMainOptions(): void
|
||||
{
|
||||
$options = [
|
||||
new CliHelpSection(['{command} --help'], 'Displays help information about a specific command'),
|
||||
new CliHelpSection(['-v', '--version'], 'Display NCC version information'),
|
||||
new CliHelpSection(['-D', '--debug'], 'Enables debug mode'),
|
||||
new CliHelpSection(['-l', '--log-level={debug|info|warn|error|fatal}'], 'Set the logging level', 'info'),
|
||||
new CliHelpSection(['--basic-ascii'], 'Uses basic ascii characters'),
|
||||
new CliHelpSection(['--no-color'], 'Omits the use of colors'),
|
||||
new CliHelpSection(['--no-banner'], 'Omits displaying the NCC ascii banner'),
|
||||
new CliHelpSection(['--no-ui'], 'Omits displaying a user-interface for wizards/dialogs'),
|
||||
new CliHelpSection(['--require-scope={user|system}'], 'Enforces the requirement of a access scope', 'user'),
|
||||
];
|
||||
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
|
||||
|
||||
print('Options:' . PHP_EOL);
|
||||
foreach($options as $option)
|
||||
{
|
||||
print(' ' . $option->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);
|
||||
}
|
||||
}
|
44
src/ncc/CLI/Main.php
Normal file
44
src/ncc/CLI/Main.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\CLI;
|
||||
|
||||
use ncc\Utilities\Resolver;
|
||||
|
||||
class Main
|
||||
{
|
||||
/**
|
||||
* Executes the main CLI process
|
||||
*
|
||||
* @param $argv
|
||||
* @return void
|
||||
*/
|
||||
public static function start($argv): void
|
||||
{
|
||||
$args = Resolver::parseArguments(implode(' ', $argv));
|
||||
|
||||
if(isset($args['ncc-cli']))
|
||||
{
|
||||
$banner = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'banner_extended');
|
||||
|
||||
$banner_version = 'Master 1.0.0';
|
||||
$banner_version = str_pad($banner_version, 21);
|
||||
|
||||
$banner_copyright = 'Copyright (c) 2022-2022 Nosial';
|
||||
$banner_copyright = str_pad($banner_copyright, 30);
|
||||
|
||||
$banner = str_ireplace('%A', $banner_version, $banner);
|
||||
$banner = str_ireplace('%B', $banner_copyright, $banner);
|
||||
|
||||
print($banner . PHP_EOL);
|
||||
|
||||
switch(strtolower($args['ncc-cli']))
|
||||
{
|
||||
default:
|
||||
case 'help':
|
||||
HelpMenu::start($argv);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
111
src/ncc/Objects/CliHelpSection.php
Normal file
111
src/ncc/Objects/CliHelpSection.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\Objects;
|
||||
|
||||
class CliHelpSection
|
||||
{
|
||||
/**
|
||||
* An array of parameters that are accepted to invoke the option
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
public $Parameters;
|
||||
|
||||
/**
|
||||
* A description of the option
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $Description;
|
||||
|
||||
/**
|
||||
* The default value of the option
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $Default;
|
||||
|
||||
/**
|
||||
* Public Constructor
|
||||
*
|
||||
* @param array|null $parameters
|
||||
* @param string|null $description
|
||||
* @param string|null $default
|
||||
*/
|
||||
public function __construct(?array $parameters=null, ?string $description=null, ?string $default=null)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/";
|
||||
preg_match_all($regex, $flags, $matches, PREG_SET_ORDER);
|
||||
|
||||
foreach ($matches as $index => $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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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',
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
+--------Version--------++-------------Copyright----------+
|
||||
| %V% %B% || %C |
|
||||
| %A || %B |
|
||||
+-----------------------++--------------------------------+
|
||||
| ______ |
|
||||
| d8b db .o88b. .o88b. |_,.,--\ |
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
┌────────Version────────┬┬─────────────Copyright──────────┐
|
||||
│ %V% %B% ││ %C │
|
||||
│ %A ││ %B │
|
||||
├───────────────────────┴┴────────────────────────────────┤
|
||||
│ ______ │
|
||||
│ d8b db .o88b. .o88b. |_,.,--\ │
|
||||
|
|
5
src/ncc/ncc
Normal file
5
src/ncc/ncc
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
require('autoload.php');
|
||||
|
||||
\ncc\CLI\Main::start($argv);
|
1
src/ncc/ncc.sh
Executable file
1
src/ncc/ncc.sh
Executable file
|
@ -0,0 +1 @@
|
|||
php ncc --ncc-cli "$@"
|
Loading…
Add table
Reference in a new issue