Added CLI functions
This commit is contained in:
parent
265cfce65b
commit
a3236ba1ee
9 changed files with 295 additions and 14 deletions
29
LICENSE
29
LICENSE
|
@ -125,3 +125,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
------------------------
|
||||||
|
dealnews.com, Inc. - Inline ProgressBar CLI
|
||||||
|
|
||||||
|
Copyright (c) 2010, dealnews.com, Inc. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in the
|
||||||
|
documentation and/or other materials provided with the distribution.
|
||||||
|
* Neither the name of dealnews.com, Inc. nor the names of its contributors
|
||||||
|
may be used to endorse or promote products derived from this software
|
||||||
|
without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
|
@ -29,6 +29,7 @@ a PPM extension may be built in the future to allow for backwards compatibility.
|
||||||
## Copyright
|
## Copyright
|
||||||
- Copyright (c) 2022-2022, Nosial - All Rights Reserved
|
- Copyright (c) 2022-2022, Nosial - All Rights Reserved
|
||||||
- Copyright (c) 2004-2022, Fabien Potencier
|
- Copyright (c) 2004-2022, Fabien Potencier
|
||||||
|
- Copyright (c) 2010, dealnews.com, Inc. All rights reserved.
|
||||||
|
|
||||||
# Licenses
|
# Licenses
|
||||||
|
|
||||||
|
|
40
src/ncc/Abstracts/ConsoleColors.php
Normal file
40
src/ncc/Abstracts/ConsoleColors.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ncc\Abstracts;
|
||||||
|
|
||||||
|
abstract class ConsoleColors
|
||||||
|
{
|
||||||
|
const Default = "\e[39m";
|
||||||
|
|
||||||
|
const Black = "\e[30m";
|
||||||
|
|
||||||
|
const Red = "\e[31m";
|
||||||
|
|
||||||
|
const Green = "\e[32m";
|
||||||
|
|
||||||
|
const Yellow = "\e[33m";
|
||||||
|
|
||||||
|
const Blue = "\e[34m";
|
||||||
|
|
||||||
|
const Magenta = "\e[35m";
|
||||||
|
|
||||||
|
const Cyan = "\e[36m";
|
||||||
|
|
||||||
|
const LightGray = "\e[37m";
|
||||||
|
|
||||||
|
const DarkGrey = "\e[90m";
|
||||||
|
|
||||||
|
const LightRed = "\e[91m";
|
||||||
|
|
||||||
|
const LightGreen = "\e[92m";
|
||||||
|
|
||||||
|
const LightYellow = "\e[93m";
|
||||||
|
|
||||||
|
const LightBlue = "\e[94m";
|
||||||
|
|
||||||
|
const LightMagenta = "\e[95m";
|
||||||
|
|
||||||
|
const LightCyan = "\e[96m";
|
||||||
|
|
||||||
|
const White = "\e[97m";
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
namespace ncc\CLI;
|
namespace ncc\CLI;
|
||||||
|
|
||||||
use ncc\Objects\CliHelpSection;
|
use ncc\Objects\CliHelpSection;
|
||||||
|
use ncc\Utilities\Console;
|
||||||
|
|
||||||
class HelpMenu
|
class HelpMenu
|
||||||
{
|
{
|
||||||
|
@ -24,9 +25,9 @@
|
||||||
// TODO: Make copyright not hard-coded.
|
// TODO: Make copyright not hard-coded.
|
||||||
print(\ncc\Utilities\Functions::getBanner(NCC_VERSION_BRANCH . ' ' . NCC_VERSION_NUMBER, 'Copyright (c) 2022-2022 Nosial', $basic_ascii) . PHP_EOL);
|
print(\ncc\Utilities\Functions::getBanner(NCC_VERSION_BRANCH . ' ' . NCC_VERSION_NUMBER, 'Copyright (c) 2022-2022 Nosial', $basic_ascii) . PHP_EOL);
|
||||||
|
|
||||||
print('Usage: ncc COMMAND [options]' . PHP_EOL);
|
Console::out('Usage: ncc COMMAND [options]' . PHP_EOL);
|
||||||
print('Alternative Usage: ncc.php --ncc-cli=COMMAND [options]' . PHP_EOL . PHP_EOL);
|
Console::out('Alternative Usage: ncc.php --ncc-cli=COMMAND [options]' . PHP_EOL . PHP_EOL);
|
||||||
print('Nosial Code Compiler / Project Toolkit' . PHP_EOL . PHP_EOL);
|
Console::out('Nosial Code Compiler / Project Toolkit' . PHP_EOL . PHP_EOL);
|
||||||
|
|
||||||
self::displayMainOptions();
|
self::displayMainOptions();
|
||||||
self::displayManagementCommands();
|
self::displayManagementCommands();
|
||||||
|
@ -52,10 +53,10 @@
|
||||||
];
|
];
|
||||||
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
|
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
|
||||||
|
|
||||||
print('Options:' . PHP_EOL);
|
Console::out('Options:' . PHP_EOL);
|
||||||
foreach($options as $option)
|
foreach($options as $option)
|
||||||
{
|
{
|
||||||
print(' ' . $option->toString($options_padding) . PHP_EOL);
|
Console::out(' ' . $option->toString($options_padding) . PHP_EOL);
|
||||||
}
|
}
|
||||||
print(PHP_EOL);
|
print(PHP_EOL);
|
||||||
}
|
}
|
||||||
|
@ -75,10 +76,10 @@
|
||||||
];
|
];
|
||||||
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
|
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
|
||||||
|
|
||||||
print('Management Commands:' . PHP_EOL);
|
Console::out('Management Commands:' . PHP_EOL);
|
||||||
foreach($commands as $command)
|
foreach($commands as $command)
|
||||||
{
|
{
|
||||||
print(' ' . $command->toString($commands_padding) . PHP_EOL);
|
Console::out(' ' . $command->toString($commands_padding) . PHP_EOL);
|
||||||
}
|
}
|
||||||
print(PHP_EOL);
|
print(PHP_EOL);
|
||||||
}
|
}
|
||||||
|
@ -96,10 +97,10 @@
|
||||||
];
|
];
|
||||||
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
|
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
|
||||||
|
|
||||||
print('Commands:' . PHP_EOL);
|
Console::out('Commands:' . PHP_EOL);
|
||||||
foreach($commands as $command)
|
foreach($commands as $command)
|
||||||
{
|
{
|
||||||
print(' ' . $command->toString($commands_padding) . PHP_EOL);
|
Console::out(' ' . $command->toString($commands_padding) . PHP_EOL);
|
||||||
}
|
}
|
||||||
print(PHP_EOL);
|
print(PHP_EOL);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use ncc\ncc;
|
use ncc\ncc;
|
||||||
|
use ncc\Utilities\Console;
|
||||||
use ncc\Utilities\Resolver;
|
use ncc\Utilities\Resolver;
|
||||||
|
|
||||||
class Main
|
class Main
|
||||||
|
@ -28,7 +29,7 @@
|
||||||
switch(strtolower($args['ncc-cli']))
|
switch(strtolower($args['ncc-cli']))
|
||||||
{
|
{
|
||||||
default:
|
default:
|
||||||
print('Unknown command ' . strtolower($args['ncc-cli']) . PHP_EOL);
|
Console::out('Unknown command ' . strtolower($args['ncc-cli']) . PHP_EOL);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
case 'credential':
|
case 'credential':
|
||||||
|
@ -43,7 +44,7 @@
|
||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
print('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')' . PHP_EOL);
|
Console::out('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')' . PHP_EOL);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
src/ncc/CLI/ProjectMenu.php
Normal file
65
src/ncc/CLI/ProjectMenu.php
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ncc\CLI;
|
||||||
|
|
||||||
|
use ncc\Abstracts\Scopes;
|
||||||
|
use ncc\Exceptions\AccessDeniedException;
|
||||||
|
use ncc\Objects\CliHelpSection;
|
||||||
|
use ncc\Utilities\Console;
|
||||||
|
use ncc\Utilities\Resolver;
|
||||||
|
|
||||||
|
class ProjectMenu
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Displays the main help menu
|
||||||
|
*
|
||||||
|
* @param $args
|
||||||
|
* @return void
|
||||||
|
* @throws AccessDeniedException
|
||||||
|
*/
|
||||||
|
public static function start($args): void
|
||||||
|
{
|
||||||
|
if(isset($args['create']))
|
||||||
|
{
|
||||||
|
self::createProject($args);
|
||||||
|
}
|
||||||
|
|
||||||
|
self::displayOptions();
|
||||||
|
exit(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $args
|
||||||
|
* @return void
|
||||||
|
* @throws AccessDeniedException
|
||||||
|
*/
|
||||||
|
public static function createProject($args): void
|
||||||
|
{
|
||||||
|
Console::out('end' . PHP_EOL);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the main options section
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private static function displayOptions(): void
|
||||||
|
{
|
||||||
|
$options = [
|
||||||
|
new CliHelpSection(['help'], 'Displays this help menu about the value command'),
|
||||||
|
new CliHelpSection(['create'], 'Creates a new NCC project'),
|
||||||
|
];
|
||||||
|
|
||||||
|
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
|
||||||
|
|
||||||
|
Console::out('Usage: ncc project {command} [options]');
|
||||||
|
Console::out('Options:' . PHP_EOL);
|
||||||
|
foreach($options as $option)
|
||||||
|
{
|
||||||
|
Console::out(' ' . $option->toString($options_padding) . PHP_EOL);
|
||||||
|
}
|
||||||
|
print(PHP_EOL);
|
||||||
|
}
|
||||||
|
}
|
|
@ -126,6 +126,21 @@
|
||||||
// Finally create project.json
|
// Finally create project.json
|
||||||
$Project->toFile($this->ProjectPath . DIRECTORY_SEPARATOR . 'project.json');
|
$Project->toFile($this->ProjectPath . DIRECTORY_SEPARATOR . 'project.json');
|
||||||
|
|
||||||
|
// And create the project directory for additional assets/resources
|
||||||
|
$Folders = [
|
||||||
|
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc',
|
||||||
|
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc' . DIRECTORY_SEPARATOR . 'cache',
|
||||||
|
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc' . DIRECTORY_SEPARATOR . 'config',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach($Folders as $folder)
|
||||||
|
{
|
||||||
|
if(file_exists($folder) == false)
|
||||||
|
{
|
||||||
|
mkdir($folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process options
|
// Process options
|
||||||
foreach($options as $option)
|
foreach($options as $option)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
namespace ncc\Objects;
|
namespace ncc\Objects;
|
||||||
|
|
||||||
|
use ncc\Abstracts\ConsoleColors;
|
||||||
|
use ncc\Utilities\Console;
|
||||||
|
|
||||||
class CliHelpSection
|
class CliHelpSection
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -80,7 +83,7 @@
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function toString(int $param_padding=0)
|
public function toString(int $param_padding=0, bool $basic=false)
|
||||||
{
|
{
|
||||||
$out = [];
|
$out = [];
|
||||||
|
|
||||||
|
@ -88,12 +91,26 @@
|
||||||
{
|
{
|
||||||
if($param_padding > 0)
|
if($param_padding > 0)
|
||||||
{
|
{
|
||||||
$out[] .= str_pad(implode(' ', $this->Parameters), $param_padding, ' ', STR_PAD_RIGHT);
|
$result = str_pad(implode(' ', $this->Parameters), $param_padding, ' ', STR_PAD_RIGHT);
|
||||||
|
|
||||||
|
if($basic == false)
|
||||||
|
{
|
||||||
|
$result = Console::formatColor($result, ConsoleColors::Green);
|
||||||
|
}
|
||||||
|
|
||||||
|
$out[] .= $result;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if($basic)
|
||||||
{
|
{
|
||||||
$out[] .= implode(' ', $this->Parameters);
|
$out[] .= implode(' ', $this->Parameters);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$out[] .= Console::formatColor(implode(' ', $this->Parameters), ConsoleColors::Green);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->Description !== null)
|
if($this->Description !== null)
|
||||||
|
|
112
src/ncc/Utilities/Console.php
Normal file
112
src/ncc/Utilities/Console.php
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ncc\Utilities;
|
||||||
|
|
||||||
|
use ncc\Abstracts\ConsoleColors;
|
||||||
|
|
||||||
|
class Console
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Inline Progress bar, created by dealnews.com.
|
||||||
|
*
|
||||||
|
* // TODO: Add non-inline option
|
||||||
|
* @copyright Copyright (c) 2010, dealnews.com, Inc. All rights reserved.
|
||||||
|
* @param int $value
|
||||||
|
* @param int $total
|
||||||
|
* @param int $size
|
||||||
|
* @param array $flags
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function inlineProgressBar(int $value, int $total, int $size=38, array $flags=[])
|
||||||
|
{
|
||||||
|
static $start_time;
|
||||||
|
|
||||||
|
// if we go over our bound, just ignore it
|
||||||
|
if($value > $total) return;
|
||||||
|
|
||||||
|
if(empty($start_time)) $start_time=time();
|
||||||
|
$now = time();
|
||||||
|
|
||||||
|
$perc=(double)($value/$total);
|
||||||
|
|
||||||
|
$bar=floor($perc*$size);
|
||||||
|
|
||||||
|
$status_bar="\r[ ";
|
||||||
|
$status_bar.=str_repeat("=", $bar);
|
||||||
|
if($bar<$size){
|
||||||
|
$status_bar.=">";
|
||||||
|
$status_bar.=str_repeat(" ", $size-$bar);
|
||||||
|
} else {
|
||||||
|
$status_bar.="=";
|
||||||
|
}
|
||||||
|
|
||||||
|
$disp=number_format($perc*100, 0);
|
||||||
|
|
||||||
|
$status_bar.=" ] $disp% $value/$total";
|
||||||
|
|
||||||
|
$rate = ($now-$start_time)/$value;
|
||||||
|
$left = $total - $value;
|
||||||
|
$eta = round($rate * $left, 2);
|
||||||
|
|
||||||
|
$elapsed = $now - $start_time;
|
||||||
|
|
||||||
|
$status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec.";
|
||||||
|
|
||||||
|
echo "$status_bar ";
|
||||||
|
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// when done, send a newline
|
||||||
|
if($value == $total) {
|
||||||
|
echo "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple output function
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
* @param bool $newline
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function out(string $message, bool $newline=true)
|
||||||
|
{
|
||||||
|
if($newline)
|
||||||
|
{
|
||||||
|
print($message . PHP_EOL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
print($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats the text to have a different color and returns the formatted value
|
||||||
|
*
|
||||||
|
* @param string $input The input of the text value
|
||||||
|
* @param string $color_code The color code of the escaped character (\e[91m)
|
||||||
|
* @param bool $persist If true, the formatting will terminate in the default color
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function formatColor(string $input, string $color_code, bool $persist=true): string
|
||||||
|
{
|
||||||
|
if($persist)
|
||||||
|
{
|
||||||
|
return $color_code . $input . ConsoleColors::Default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $color_code . $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints out a warning output
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
* @param bool $newline
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function outWarning(string $message, bool $newline=true)
|
||||||
|
{
|
||||||
|
self::out(self::formatColor(ConsoleColors::Yellow, 'Warning: ') . $message, $newline);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue