diff --git a/spish.bat b/spish.bat index 9913425..2931a16 100644 --- a/spish.bat +++ b/spish.bat @@ -1,5 +1,5 @@ @echo OFF -:: in case DelayedExpansion is on and a path contains ! +:: in case DelayedExpansion is on and a path contains ! setlocal DISABLEDELAYEDEXPANSION -php "%~dp0spish.phar" %* +php "%~dp0spish.pha" %* diff --git a/spish.pha b/spish.pha new file mode 100644 index 0000000..19b7962 --- /dev/null +++ b/spish.pha @@ -0,0 +1,459 @@ +#!/usr/bin/env php +listCommand = [ + "create" => [ + "description" => "Créer un nouveau projet", + "function" => 'create' + ], + "-version/version" => [ + "description" => "Donne la version actuelle", + "function" => 'version;newUpdatePublish' + ], + "-update" => [ + "description" => "Met a jour le CLI", + "function" => function(){ + new update(); + } + + ], + "-uninstall" => [ + "description" => "Desinstalle le CLI du terminal", + "function" => 'uninstall' + ], + "-install" => [ + "description" => "Install le CLI", + "function" => 'iniSystemPath', + "helpShow" => false + ], + "-help/help" => [ + "description" => "affiche la liste des commande", + "function" => 'help' + ], + "load" => [ + "function" => 'load', + "-show" => false + ], + "timer" => [ + "function" => 'timer', + "-show" => false + ], + "default" => [ + "function" => function () { + $argv = ARGV; + if (isset($argv[1]) && strlen($argv[1]) > 0) { + $this->printStyle(" Commande inconnue", "red")->newline()->print("(Utilisez \"" . CLI . " help\" pour connaitre la liste des commandes)"); + } elseif (!$this->iniSystemPath()) { + $this->help(); + } + }, + "-show" => false + ] + ]; + } + + public function version(){ + $this->print("current version ")->printStyle(VERSION,"green","",true); + return $this; + } + + public function help(){ + $this->newline()->printStyle(" LISTE DES COMMANDES ","black","yellow")->newline()->newline(); + foreach ($this->listCommand as $key=>$value){ + if(!isset($value['-show'])||$value['-show']){ + $this->printStyle(" $key ","green","",true) + ->minWidth(" $key ",30) + ->print($value['description']??"")->newline(); + } + + } + } + + public function load($msg="Load",$endMsg="End load"){ + $a='-\|/'; + foreach(range(0,15) as $e){ + time_nanosleep(0,100000000); + echo "\r[".$a[$e%4].'] '.$msg.'..'; + } + $this->print("\r".$endMsg)->minWidth("",50)->newline(); + } + + public function timer($endMsg="End Timer"){ + $l=ARGV[2]??600; + foreach(range(0,$l) as $e){ + time_nanosleep(0,1000000); + echo "\r".date("H:i:s").".".substr(explode(' ',microtime())[0],2); + } + $this->print("\r".$endMsg)->minWidth("",50)->newline(); + } + + public function create(){ + $argv=ARGV; + $this->clear(); + $this->printStyle("[ New Project ]","dark_gray","",true)->newline()->newline(); + $name=$this->printStyle("Project name : ","yellow")->endStyle()->input(); + $path=FOLDER.'\\'.$name; + if($this->printStyle("Create project in ","yellow")->printStyle($path,"yellow","",true,true,true)->printStyle(" ? ","yellow")->inputBool()){ + //$pathProject=$this->input("path: "); + $this->generateProject($name,$path); + }else{ + $this->newline()->printStyle("ø Cancelling","red"); + //$pathProject=$this->print("path: ".FOLDER.'\\'.$name)->newline(); + } + } + private function generateProject($name,$path){ + if (!file_exists($path)) { + mkdir($path, 0777, true); + $this->newline()->printStyle("√ New project created","green"); + return true; + } + $this->newline()->printStyle("ø Cancelling, this path is already used","red"); + return false; + } + + public function iniSystemPath(){ + $a='"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path'; + $b='reg query '.$a; + $handle = popen($b, 'r'); + usleep(100); + $r = fread($handle, 2096); + $sysPath=trim(explode(" ",$r)[3]??""); + if($sysPath[strlen($sysPath)-1]!==";") $sysPath .= ";"; + pclose($handle); + if(!str_contains($sysPath, CLIDIR."\;")&&!str_contains(exec('echo %path%'), CLIDIR."\;")){ + $sysPath.=CLIDIR."\;"; + if($sysPath[strlen($sysPath)-1]==";"){ + exec('reg add '.$a.' /d "'.$sysPath.'" /f'); + exec('setx /m PATH "'.$sysPath.'"'); + exec('set path="'.$sysPath.'"'); + echo "Command CLI installed\n"; + system("pause"); + return true; + } + } + echo "Command CLI \"".CLI."\" already installed\n"; + return false; + } + + public function uninstall(){ + $a='"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path'; + $b='reg query '.$a; + $handle = popen($b, 'r'); + usleep(100); + $r = fread($handle, 2096); + $sysPath=trim(explode(" ",$r)[3]); + pclose($handle); + if(str_contains($sysPath, CLIDIR."\;")){ + $sysPath=str_replace(CLIDIR."\;","",$sysPath); + if($sysPath[strlen($sysPath)-1]==";"){ + exec('reg add '.$a.' /d "'.$sysPath.'" /f'); + exec('setx /m PATH "'.$sysPath.'"'); + system('set path="'.$sysPath.'"'); + echo "Command CLI uninstalled :(\n"; + return true; + } + } + return false; + } +} + +class main extends command{ + + public function __construct() + { + parent::__construct(); + $listCommand=$this->listCommand; + $argv=ARGV; + $argv[1] = strtolower($argv[1] ?? ""); + + $command=$this->maybeThis($argv[1]); + if($command){ + $this->runCommand($listCommand[$command]['function']); + }else{ + $this->runCommand($listCommand['default']['function']); + } + $this->newline()->newline(); + } + + private function maybeThis($command):string|false{ + $maybe=false;$xMatch=0;$match=""; + foreach($this->listCommand as $key=>$value){ + $currentCommands=explode("/",$key); + foreach ($currentCommands as $k){ + if($command===$k){ + return $key; + }else{ + if(str_starts_with($k,$command)){ + $maybe=$key; + $match=$k; + $xMatch++; + } + } + } + } + if($xMatch>1||$xMatch==0){ + return false; + } + $this->printStyle(" Commande \"$command\" à été remplacé par \"$match\" ",'black','light_gray')->newline(); + return $maybe; + } + + private function runCommand($command){ + if(is_string($command)){ + $commands=explode(";",$command); + foreach ($commands as $command){ + try { + if(method_exists($this,$command)){ + $this->{$command}(); + }else{ + echo "function '$command' not exist"; + } + + } catch (Error $e) { + echo $e; + } + } + }else{ + $command(); + } + + } +} + +class script_object { + public $foreground_colors = array(); + public $background_colors = array(); + public function __construct() { + // Set up shell colors + $this->foreground_colors['black'] = '0;30'; + $this->foreground_colors['dark_gray'] = '1;30'; + $this->foreground_colors['blue'] = '0;34'; + $this->foreground_colors['light_blue'] = '1;34'; + $this->foreground_colors['green'] = '0;32'; + $this->foreground_colors['light_green'] = '1;32'; + $this->foreground_colors['cyan'] = '0;36'; + $this->foreground_colors['light_cyan'] = '1;36'; + $this->foreground_colors['red'] = '0;31'; + $this->foreground_colors['light_red'] = '1;31'; + $this->foreground_colors['purple'] = '0;35'; + $this->foreground_colors['light_purple'] = '1;35'; + $this->foreground_colors['brown'] = '0;33'; + $this->foreground_colors['yellow'] = '1;33'; + $this->foreground_colors['light_gray'] = '0;37'; + $this->foreground_colors['white'] = '1;37'; + + $this->background_colors['black'] = '40'; + $this->background_colors['red'] = '41'; + $this->background_colors['green'] = '42'; + $this->background_colors['yellow'] = '43'; + $this->background_colors['blue'] = '44'; + $this->background_colors['magenta'] = '45'; + $this->background_colors['cyan'] = '46'; + $this->background_colors['light_gray'] = '47'; + } + + // Returns colored string + public function startStyle($foreground_color = null, $background_color = null,$bold=false,$italic=false,$underline=false,$strike=false){ + $colored_string = ""; + + // Check if given foreground color found + + + if (isset($this->foreground_colors[$foreground_color])) { + $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; + } + // Check if given background color found + if (isset($this->background_colors[$background_color])) { + $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; + } + if($bold) { + $colored_string .= "\e[1m"; + } + if($italic) { + $colored_string .= "\e[3m"; + } + if($underline){ + $colored_string .= "\e[4m"; + } + if($strike){ + $colored_string .= "\e[9m"; + } + echo $colored_string; + } + + public function endStyle(){ + echo "\033[0m\e[0m"; + return $this; + } + public function print($string){ + echo $string; + return $this; + } + + public function printStyle($string, $foreground_color = null, $background_color = null,$bold=false,$italic=false,$underline=false,$strike=false){ + self::startStyle($foreground_color, $background_color,$bold,$italic,$underline,$strike); + echo $string . "\033[0m\e[0m"; + return $this; + } + + public function minWidth($string,$size=9){ + for ($i=0,$l=$size-strlen($string);$i<$l;$i++){ + echo " "; + } + return $this; + } + + // Returns all foreground color names + public function getForegroundColors() { return array_keys($this->foreground_colors); } + + // Returns all background color names + public function getBackgroundColors() { return array_keys($this->background_colors); } + + public function clear(): static + { + echo "\n"; + echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; + return $this; + } + + public function newline(): static + { + echo "\n"; + return $this; + } + + public function hideKey(){ system('stty -echo'); } + + public function showKey(){ system('stty echo'); } + public function inputBool($prompt = ''): bool + { + $prompt && print $prompt." "; + $this->printStyle("(y/n) ","green"); + $bool=strtolower($this->input()); + if($bool!=="n"&&$bool!=="y"&&$bool!=="no"&&$bool!=="yes"){ + $bool=$this->inputBool(); + } + if($bool=="yes"||$bool=="y"){ + return true; + } + return false; + } + + public function input($prompt = '') { + $prompt && print $prompt; + $line=fgets(STDIN); + $line=trim($line); + if($line==""){ + $line=$this->input(); + } + return $line; + } +} +class gitInfo extends script_object{ + public function getLastVersion(){ + $json=$this->getInfoUpdate(); + return $json["tag_name"]??VERSION; + } + public function getNameRelease(){ + $json=$this->getInfoUpdate(); + return str_replace(" ","-",$json["name"])??""; + } + public function newUpdatePublish(){ + $lastV=$this->getLastVersion(); + if(VERSION<$lastV){ + $this->newline()->printStyle("Release ".$lastV." disponible","brown")->newline(); + } + return $this; + } + private function getInfoUpdate(){ + $url="https://api.github.com/repos/".GIT."/releases/latest"; + $ua="node.js"; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch,CURLOPT_USERAGENT,$ua); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $head = curl_exec($ch); + curl_close($ch); + $json=json_decode($head,true); + return $json; + } +} +class update extends gitInfo{ + public function __construct(){ + parent::__construct(); + if(!$this->getUpdate()){ + $this->print("\r")->printStyle("Erreur lors de la mise a jour ","red")->newline(); + }; + } + public function getUpdate(){ + $lastV=$this->getLastVersion(); + if(VERSION>=$lastV){ + $this->printStyle("Votre possedez deja la version la plus recente","brown")->print(" (".VERSION.")"); + return true; + }else{ + $this->newUpdatePublish()->newline()->printStyle(" Mise a jour en cours..","yellow"); + $this->newline()->printStyle(" Telechargement..","yellow"); + if(!$this->downloadUpdate($lastV)){ + return false; + } + $this->newline()->printStyle(" Installation..","yellow"); + if(!$this->extractUpdate($lastV)){ + return false; + } + $this->newline()->printStyle("Mise a jour terminee","green"); + return true; + } + } + private function downloadUpdate($version){ + $file = 'https://github.com/'.GIT.'/archive/refs/tags/'.$version.'.zip'; + $tmp = $_SERVER["TEMP"].'\tmp_update.zip'; + if (!copy($file, $tmp)) return false; + return true; + } + private function extractUpdate(){ + $tmp = $_SERVER["TEMP"].'\tmp_update.zip'; + $destination_dir=dirname(__FILE__); + $zip = new ZipArchive(); + if ($zip->open($tmp)) { + for ($i = 0; $i < $zip->numFiles; $i++) { + if (!$zip->extractTo($destination_dir, array($zip->getNameIndex($i)))) return false; + } + $zip->close(); + // Clear zip from local storage: + unlink($tmp); + $nameRelease=$this->getNameRelease(); + $this->replaceFile($destination_dir,$nameRelease); + exec("php ".$destination_dir."\\update.phar"); + return true; + } + return false; + } + private function replaceFile($destination_dir,$nameRelease){ + $update=fopen($destination_dir."\\update.phar", 'w+'); + $content='version()->newUpdatePublish(); - break; - case "help": - $this->help(); - break; - case "create": - $this->create($argv); - break; - case "a": - $this->printStyle("azer","brown"); - break; - case "uninstall": - $this->uninstall(); - break; - case "install": - $this->iniSystemPath(); - break; - case "update": - new update(); - break; - default: - if(strlen($argv[1])>0){ - $this->printStyle(" Commande inconnue","red")->newline()->print("(Utilisez \"".CLI." help\" pour connaitre la liste des commandes)"); - }elseif(!$this->iniSystemPath()){ - $this->help(); - } - break; - } - $this->newline()->newline(); - } -} - -class script_object { - public $foreground_colors = array(); - public $background_colors = array(); - public function __construct($argv=ARGV) { - // Set up shell colors - $this->foreground_colors['black'] = '0;30'; - $this->foreground_colors['dark_gray'] = '1;30'; - $this->foreground_colors['blue'] = '0;34'; - $this->foreground_colors['light_blue'] = '1;34'; - $this->foreground_colors['green'] = '0;32'; - $this->foreground_colors['light_green'] = '1;32'; - $this->foreground_colors['cyan'] = '0;36'; - $this->foreground_colors['light_cyan'] = '1;36'; - $this->foreground_colors['red'] = '0;31'; - $this->foreground_colors['light_red'] = '1;31'; - $this->foreground_colors['purple'] = '0;35'; - $this->foreground_colors['light_purple'] = '1;35'; - $this->foreground_colors['brown'] = '0;33'; - $this->foreground_colors['yellow'] = '1;33'; - $this->foreground_colors['light_gray'] = '0;37'; - $this->foreground_colors['white'] = '1;37'; - - $this->background_colors['black'] = '40'; - $this->background_colors['red'] = '41'; - $this->background_colors['green'] = '42'; - $this->background_colors['yellow'] = '43'; - $this->background_colors['blue'] = '44'; - $this->background_colors['magenta'] = '45'; - $this->background_colors['cyan'] = '46'; - $this->background_colors['light_gray'] = '47'; - $this->main($argv); - } - public function main($argv){return;} - - // Returns colored string - public function startStyle($foreground_color = null, $background_color = null,$bold=false,$italic=false,$underline=false,$strike=false){ - $colored_string = ""; - - // Check if given foreground color found - - - if (isset($this->foreground_colors[$foreground_color])) { - $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; - } - // Check if given background color found - if (isset($this->background_colors[$background_color])) { - $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; - } - if($bold) { - $colored_string .= "\e[1m"; - } - if($italic) { - $colored_string .= "\e[3m"; - } - if($underline){ - $colored_string .= "\e[4m"; - } - if($strike){ - $colored_string .= "\e[9m"; - } - echo $colored_string; - } - public function endStyle(){ - echo "\033[0m\e[0m"; - } - public function print($string){ - echo $string; - return $this; - } - public function printStyle($string, $foreground_color = null, $background_color = null,$bold=false,$italic=false,$underline=false,$strike=false){ - self::startStyle($foreground_color, $background_color,$bold,$italic,$underline,$strike); - echo $string . "\033[0m\e[0m"; - return $this; - } - - // Returns all foreground color names - public function getForegroundColors() { return array_keys($this->foreground_colors); } - - // Returns all background color names - public function getBackgroundColors() { return array_keys($this->background_colors); } - public function clear(){ - echo "\n"; - echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; - return $this; - } - public function newline(){ - echo "\n"; - return $this; - } - public function hideKey(){ system('stty -echo'); } - public function showKey(){ system('stty echo'); } - public function inputbool($prompt = '') { - $prompt && print $prompt." "; - $this->printStyle("(y/n) ","green"); - $bool=strtolower($this->input()); - if($bool!=="n"&&$bool!=="y"&&$bool!=="no"&&$bool!=="yes"){ - $bool=$this->inputbool(); - } - if($bool=="yes"||$bool=="y"){ - return true; - } - return false; - } - public function input($prompt = '') { - $prompt && print $prompt; - $line=fgets(STDIN); - $line=trim($line); - return $line; - } -} -class gitInfo extends script_object{ - public function getLastVersion(){ - $json=$this->getInfoUpdate(); - return $json["tag_name"]??VERSION; - } - public function getNameRelease(){ - $json=$this->getInfoUpdate(); - return str_replace(" ","-",$json["name"])??""; - } - public function newUpdatePublish(){ - $lastV=$this->getLastVersion(); - if(VERSION<$lastV){ - $this->newline()->printStyle("Release ".$lastV." disponible","brown")->newline(); - } - return $this; - } - private function getInfoUpdate(){ - $url="https://api.github.com/repos/".GIT."/releases/latest"; - $ua="node.js"; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch,CURLOPT_USERAGENT,$ua); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - $head = curl_exec($ch); - curl_close($ch); - $json=json_decode($head,true); - return $json; - } -} -class command extends gitInfo{ - public function version(){ - $this->print("current version ")->printStyle(VERSION,"green","",true); - return $this; - } - public function help(){ - $this->newline()->printStyle(" LISTE DES COMMANDES ","black","yellow")->newline()->newline(); - $this->printStyle(" create ","green","",true)->print(" Créer un nouveau projet")->newline(); - $this->printStyle(" -v ","green","",true)->print(" Donne la version actuelle")->newline(); - $this->printStyle(" update ","green","",true)->print(" Met a jour le CLI")->newline(); - $this->printStyle(" uninstall ","green","",true)->print(" Desinstalle le CLI du terminal")->newline(); - } - public function create($argv){ - $this->clear(); - $this->printStyle("[ New Project ]","dark_gray","",true)->newline()->newline(); - $name=$this->printStyle("Project name : ","yellow")->input(); - $path=FOLDER.'\\'.$name; - if($this->printStyle("Create project in ","yellow")->printStyle($path,"yellow","",true,true,true)->printStyle(" ? ","yellow")->inputbool()){ - //$pathProject=$this->input("path: "); - $this->generateProject($name,$path); - }else{ - $this->newline()->printStyle("ø Cancelling","red"); - //$pathProject=$this->print("path: ".FOLDER.'\\'.$name)->newline(); - } - } - private function generateProject($name,$path){ - if (!file_exists($path)) { - mkdir($path, 0777, true); - $this->newline()->printStyle("√ New project created","green"); - return true; - } - $this->newline()->printStyle("ø Cancelling, this path is already used","red"); - return false; - } - public function iniSystemPath(){ - $a='"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path'; - $b='reg query '.$a; - $handle = popen($b, 'r'); - usleep(100); - $r = fread($handle, 2096); - $sysPath=trim(explode(" ",$r)[3]??""); - pclose($handle); - if(!str_contains($sysPath, CLIDIR."\;")&&!str_contains(exec('echo %path%'), CLIDIR."\;")){ - $sysPath.=CLIDIR."\;"; - if($sysPath[strlen($sysPath)-1]==";"){ - exec('reg add '.$a.' /d "'.$sysPath.'" /f'); - exec('setx /m PATH "'.$sysPath.'"'); - exec('set path="'.$sysPath.'"'); - echo "Command CLI installed\n"; - system("pause"); - return true; - } - } - return false; - } - public function uninstall(){ - $a='"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path'; - $b='reg query '.$a; - $handle = popen($b, 'r'); - usleep(100); - $r = fread($handle, 2096); - $sysPath=trim(explode(" ",$r)[3]); - pclose($handle); - if(str_contains($sysPath, CLIDIR."\;")){ - $sysPath=str_replace(CLIDIR."\;","",$sysPath); - if($sysPath[strlen($sysPath)-1]==";"){ - exec('reg add '.$a.' /d "'.$sysPath.'" /f'); - exec('setx /m PATH "'.$sysPath.'"'); - system('set path="'.$sysPath.'"'); - echo "Command CLI uninstalled :(\n"; - return true; - } - } - return false; - } -} -class update extends gitInfo{ - public function __construct(){ - parent::__construct(); - if(!$this->getUpdate()){ - $this->print("\r")->printStyle("Erreur lors de la mise a jour ","red")->newline(); - }; - } - public function getUpdate(){ - $lastV=$this->getLastVersion(); - if(VERSION>=$lastV){ - $this->printStyle("Votre possedez deja la version la plus recente","brown")->print(" (".VERSION.")"); - return true; - }else{ - $this->newUpdatePublish()->newline()->printStyle(" Mise a jour en cours..","yellow"); - $this->newline()->printStyle(" Telechargement..","yellow"); - if(!$this->downloadUpdate($lastV)){ - return false; - } - $this->newline()->printStyle(" Installation..","yellow"); - if(!$this->extractUpdate($lastV)){ - return false; - } - $this->newline()->printStyle("Mise a jour terminee","green"); - return true; - } - } - private function downloadUpdate($version){ - $file = 'https://github.com/'.GIT.'/archive/refs/tags/'.$version.'.zip'; - $tmp = $_SERVER["TEMP"].'\tmp_update.zip'; - if (!copy($file, $tmp)) return false; - return true; - } - private function extractUpdate(){ - $tmp = $_SERVER["TEMP"].'\tmp_update.zip'; - $destination_dir=dirname(__FILE__); - $zip = new ZipArchive(); - if ($zip->open($tmp)) { - for ($i = 0; $i < $zip->numFiles; $i++) { - if (!$zip->extractTo($destination_dir, array($zip->getNameIndex($i)))) return false; - } - $zip->close(); - // Clear zip from local storage: - unlink($tmp); - $nameRelease=$this->getNameRelease(); - $this->replaceFile($destination_dir,$nameRelease); - exec("php ".$destination_dir."\\update.phar"); - return true; - } - return false; - } - private function replaceFile($destination_dir,$nameRelease){ - $update=fopen($destination_dir."\\update.phar", 'w+'); - $content='