From 7f803ecd491b299618c12e438516af1d002809a0 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Fri, 18 Nov 2016 18:32:03 +0100 Subject: [PATCH] Such simpler. --- config.yml | 4 + resources/Windows/bin | 1 - resources/win.yml | 4 + src/Ad5001/Gitable/GitClient.php | 58 ++----- src/Ad5001/Gitable/Main.php | 218 +++++++++++++++------------ src/Ad5001/Gitable/Windows.php | 251 +++++++++++++++++-------------- 6 files changed, 279 insertions(+), 257 deletions(-) create mode 100644 config.yml delete mode 160000 resources/Windows/bin create mode 100644 resources/win.yml diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..2d96cd0 --- /dev/null +++ b/config.yml @@ -0,0 +1,4 @@ +# Welcome to windows Gitable's config ! +# Select here the path of the git executable (if you did +# not modify it while installing git, it should work.) +executable_path: C:\Program Files\Git\bin\git.exe \ No newline at end of file diff --git a/resources/Windows/bin b/resources/Windows/bin deleted file mode 160000 index 2209df7..0000000 --- a/resources/Windows/bin +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2209df713ae0598aca431eecad48e8ec8ddf7aec diff --git a/resources/win.yml b/resources/win.yml new file mode 100644 index 0000000..2d96cd0 --- /dev/null +++ b/resources/win.yml @@ -0,0 +1,4 @@ +# Welcome to windows Gitable's config ! +# Select here the path of the git executable (if you did +# not modify it while installing git, it should work.) +executable_path: C:\Program Files\Git\bin\git.exe \ No newline at end of file diff --git a/src/Ad5001/Gitable/GitClient.php b/src/Ad5001/Gitable/GitClient.php index 5ea492f..46b8262 100644 --- a/src/Ad5001/Gitable/GitClient.php +++ b/src/Ad5001/Gitable/GitClient.php @@ -10,7 +10,7 @@ use pocketmine\Server; use pocketmine\Player; - +define("DEFAULT_GIT_DIR", \pocketmine\Server::getInstance()->getFilePath(), true); abstract class GitClient { @@ -35,63 +35,27 @@ abstract class GitClient { $this->cd($dir); + $this->initcheck(); + } - public abstract function commit(string $message) : string; + public abstract function gitExec(string $args) : string; - - public abstract function push(string $to = "github", string $from = "master") : string; - - - public abstract function checkout($branch = null) : string; - - - public abstract function getBranch() : string; - - - public abstract function branch($branch = '') : string; - - - public abstract function start() : string; - - - public abstract function init() : string; - - public function getDir() { + public function pwd() : string { return $this->dir; } - public abstract function cd($path) : string; + public abstract function cd(string $path) : string; + /* + Return the list of files in directory. + */ + public abstract function ls() : string; - public abstract function clone($from) : string; - - - public abstract function log() : string; - - - public abstract function remove($path) : string; - - - public abstract function move($path, $newpath) : string; - - - public abstract function add($path) : string; - - - public abstract function diff() : string; - - - public abstract function status($path) : string; - - - public abstract function remote($name, $url) : string; - - - public abstract function pull($to = "github", $from = "master") : string; + public abstract function initcheck(); } \ No newline at end of file diff --git a/src/Ad5001/Gitable/Main.php b/src/Ad5001/Gitable/Main.php index 006cd75..2ee6e56 100644 --- a/src/Ad5001/Gitable/Main.php +++ b/src/Ad5001/Gitable/Main.php @@ -32,108 +32,126 @@ use pocketmine\Player; class Main extends PluginBase implements Listener { - protected $git; - - const PREFIX = C::BLACK .C::BOLD . C::ITALIC . "[" . C::RESET . C::BOLD .C::GRAY . "Git" . C::BLACK . C::ITALIC . "] " . C::RESET . C::GRAY; - - - public function onEnable() { - - @mkdir($this->getDataFolder()); - - @mkdir($this->getDataFolder() . "bin"); - - $this->getServer()->getPluginManager()->registerEvents($this, $this); - $zip = new ZipArchive(); - - if(Utils::getOS() == "win") { - if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Windows.zip")) { - $zip->extractTo($this->getDataFolder() . "bin"); - $zip->close(); - } - $this->git = new Windows($this, $this->getDataFolder()); - } elseif(Utils::getOS() == "linux") { - if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Linux.zip")) { - $zip->extractTo($this->getDataFolder() . "bin"); - $zip->close(); - } - $this->git = new Linux($this, $this->getDataFolder()); - } elseif(Utils::getOS() == "mac") { - if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Mac.zip")) { - $zip->extractTo($this->getDataFolder() . "bin"); - $zip->close(); - } - $this->git = new Mac($this, $this->getDataFolder()); - } else { - $this->getLogger()->critical("Unsuported device ! Please refer to the download page to see the list of available devices."); - $this->setEnable(false); - } - - } - - - - - public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ - - - switch($cmd->getName()){ - - - case 'git': - - if(count($args) >= 2) { - - switch($args[0]) { - - case "cd": - $this->git->cd($args[1]); - $sender->sendMessage(self::PREFIX . "New path: " . $this->git->getDir()); + protected $git; + + const PREFIX = C::BLACK .C::BOLD . C::ITALIC . "[" . C::RESET . C::BOLD .C::GRAY . "Git" . C::BLACK . C::ITALIC . "] " . C::RESET . C::GRAY; + + + public function onEnable() { + + @mkdir($this->getDataFolder()); + + $this->getServer()->getPluginManager()->registerEvents($this, $this); + + if(Utils::getOS() == "win") { + if(!file_exists($this->getDataFolder() . "config.yml")) { + file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/win.yml")); + } + $this->git = new Windows($this, $this->getDataFolder()); + } + elseif(Utils::getOS() == "linux") { + if(!file_exists($this->getDataFolder() . "config.yml")) { + file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/linux.yml")); + } + $this->git = new Linux($this, $this->getDataFolder()); + } + elseif(Utils::getOS() == "mac") { + if(!file_exists($this->getDataFolder() . "config.yml")) { + file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/mac.yml")); + } + $this->git = new Mac($this, $this->getDataFolder()); + } else { + $this->getLogger()->critical("Unsuported device ! Please refer to the download page to see the list of available devices."); + $this->setEnable(false); + } + + } + + + + + public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ + + + switch($cmd->getName()){ + + + case 'git': + + if(count($args) >= 2) { + + switch($args[0]) { + + case "cd": + $sender->sendMessage(self::PREFIX . $this->git->cd($args[1])); + break; + + case "ls": + $sender->sendMessage(self::PREFIX . $this->git->ls()); break; - case "commit": - unset($args[0]); - $sender->sendMessage(self::PREFIX . $this->git->commit(implode(" ", $args))); - $sender->sendMessage(self::PREFIX . "Commited !"); + case "pwd": + $sender->sendMessage(self::PREFIX . "§aPath: " . $this->git->pwd()); break; - case "checkout": - $sender->sendMessage(self::PREFIX . $this->git->checkout($args[1])); + default: + $sender->sendMessage(self::PREFIX . $this->git->gitExec(implode(" ", $args))); break; - - case "dir": - $sender->sendMessage(self::PREFIX . $this->git->getDir()); - break; - - case "clone": - $sender->sendMessage(self::PREFIX . $this->git->clone($args[1])); - break; - } - return true; - } - - - break; - - - } - - - return false; - - - } - - - public function getGitClient() { - return $this->git; - } - - - public function setGitClient(GitClient $client) { - $this->git = $client; - return $this->git == $client; - } - - -} \ No newline at end of file + } + return true; + } + + + break; + + + } + + + return false; + + + } + + + public function getGitClient() { + return $this->git; + } + + + public function setGitClient(GitClient $client) { + $this->git = $client; + return $this->git == $client; + } + + + + /* + Check if a command exists + @param $command string + */ + public function command_exists ($command) { + $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which'; + + $process = proc_open( + "$whereIsCommand $command", + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $pipes + ); + if ($process !== false) { + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + return $stdout != ''; + } + } + + +} diff --git a/src/Ad5001/Gitable/Windows.php b/src/Ad5001/Gitable/Windows.php index 42e72d9..9ce3898 100644 --- a/src/Ad5001/Gitable/Windows.php +++ b/src/Ad5001/Gitable/Windows.php @@ -23,120 +23,153 @@ use Ad5001\Gitable\Main; class Windows extends GitClient { - - - - public function commit(string $message) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe commit -m \"$message\"", 'r'); - return fread($handle, 2096); - } - - - public function push(string $to ="github", string $from = "master") : string { - $handle = popen($this->getDataFolder() . "bin/git.exe push $to $from", 'r'); - return fread($handle, 2096); - } - - - public function checkout($branch = null) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe checkout " . (!is_null($branch) ? $this->getBranch() : $branch), 'r'); - return fread($handle, 2096); - } - - - public function getBranch() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe branch", 'r'); - $read = fread($handle, 2096); - return explode(" ", $read)[1]; - } - - - public function branch($branch = '') : string { - $handle = popen($this->getDataFolder() . "bin/git.exe branch " . $branch, 'r'); - return fread($handle, 2096); - } - - - public function start() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe init", 'r'); - return fread($handle, 2096); - } - - - public function init() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe init", 'r'); - return fread($handle, 2096); - } - - public function getDir() { - return $this->dir; - } - - - public function cd($path) : string { - if(is_dir($path)) { - $dir = chdir($path); - $this->dir = getcwd(); - return (string) $dir; + /* + Exec's a git request with defined args + @param $args string + */ + public function gitExec(string $args) : string { + proc_open( + "cd " . $this->dir, + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $a + ); + $process = proc_open( + "git " . $args, + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $pipes + ); + proc_open( + "cd " . DEFAULT_GIT_DIR, + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $a + ); + if ($process !== false) { + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + return (string) $stdout; } else { - return "Directory $path not found !"; - } + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + return "Error while executing command "."git " . $args . ": $stderr"; + } } - - - public function clone($from) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe clone $from", 'r'); - return fread($handle, 2096); + + + /* + Changes current directory + @param $path string + */ + public function cd(string $path): string { + if(is_dir($this->dir . $path)) { + $this->dir .= $path; + if(substr($this->dir, strlen($this->dir) - 1) !== "/") { + $this->dir .= "/"; + } + return "§aPath set to $this->dir"; + } elseif(is_dir($path)) { + $this->dir = $path; + if(substr($this->dir, strlen($this->dir) - 1) !== "/") { + $this->dir .= "/"; + } + return "§aPath set to $path"; + } else { + return "§4Directory $path not found !"; + } } - - - public function log() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe log", 'r'); - return fread($handle, 2096); + + + /* + Return all files and dirs from the current directory + */ + public function ls() : string { + proc_open( + "cd " . $this->dir, + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $a + ); + $whereIsCommand = (PHP_OS == 'WINNT') ? 'dir' : 'ls'; + + $process = proc_open( + "$whereIsCommand $command", + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $pipes + ); + proc_open( + "cd " . DEFAULT_GIT_DIR, + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $a + ); + if ($process !== false) { + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + return $stdout; + } } - - - public function remove($path) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe rm $path", 'r'); - return fread($handle, 2096); + + + /* + Checks if the executable exists. + */ + public function initcheck() { + $this->executable = $this->main->getConfig()->get("executable_path"); + $process = proc_open( + '"$this->executable"'." --version", + array( + 0 => array("pipe", "r"), //S TDIN + 1 => array("pipe", "w"), //S TDOUT + 2 => array("pipe", "w"), //S TDERR + ), + $pipes + ); + if ($process !== false) { + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + if(strpos($stdout, "git version") == false) { + $this->main->getLogger()->critical("Executable wasn't found at path $this->executable. Be sure that you installed git and that the path in the config is the executable path."); + $this->main->setEnabled(false); + } + } } - - - public function move($path, $newpath) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe mv $path $newpath", 'r'); - return fread($handle, 2096); - } - - - public function add($path) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe add $path", 'r'); - return fread($handle, 2096); - } - - - public function diff() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe diff $path", 'r'); - return fread($handle, 2096); - } - - - public function status() : string { - $handle = popen($this->getDataFolder() . "bin/git.exe status -s", 'r'); - return fread($handle, 2096); - } - - - public function remote($name, $url) : string { - $handle = popen($this->getDataFolder() . "bin/git.exe remote $name $url", 'r'); - return fread($handle, 2096); - } - - - public function pull($to = "github", $from = "master") : string { - $handle = popen($this->getDataFolder() . "bin/git.exe pull $to $from", 'r'); - return fread($handle, 2096); - } - + +