Such simpler.

This commit is contained in:
Ad5001 2016-11-18 18:32:03 +01:00
parent 0388b1afcd
commit 7f803ecd49
6 changed files with 279 additions and 257 deletions

4
config.yml Normal file
View file

@ -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

@ -1 +0,0 @@
Subproject commit 2209df713ae0598aca431eecad48e8ec8ddf7aec

4
resources/win.yml Normal file
View file

@ -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

View file

@ -10,7 +10,7 @@ use pocketmine\Server;
use pocketmine\Player; use pocketmine\Player;
define("DEFAULT_GIT_DIR", \pocketmine\Server::getInstance()->getFilePath(), true);
abstract class GitClient { abstract class GitClient {
@ -35,63 +35,27 @@ abstract class GitClient {
$this->cd($dir); $this->cd($dir);
$this->initcheck();
} }
public abstract function commit(string $message) : string; public abstract function gitExec(string $args) : string;
public function pwd() : 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() {
return $this->dir; 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 initcheck();
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;
} }

View file

@ -32,108 +32,126 @@ use pocketmine\Player;
class Main extends PluginBase implements Listener { class Main extends PluginBase implements Listener {
protected $git; 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; 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() { public function onEnable() {
@mkdir($this->getDataFolder()); @mkdir($this->getDataFolder());
@mkdir($this->getDataFolder() . "bin"); $this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getServer()->getPluginManager()->registerEvents($this, $this); if(Utils::getOS() == "win") {
$zip = new ZipArchive(); if(!file_exists($this->getDataFolder() . "config.yml")) {
file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/win.yml"));
if(Utils::getOS() == "win") { }
if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Windows.zip")) { $this->git = new Windows($this, $this->getDataFolder());
$zip->extractTo($this->getDataFolder() . "bin"); }
$zip->close(); elseif(Utils::getOS() == "linux") {
} if(!file_exists($this->getDataFolder() . "config.yml")) {
$this->git = new Windows($this, $this->getDataFolder()); file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/linux.yml"));
} elseif(Utils::getOS() == "linux") { }
if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Linux.zip")) { $this->git = new Linux($this, $this->getDataFolder());
$zip->extractTo($this->getDataFolder() . "bin"); }
$zip->close(); elseif(Utils::getOS() == "mac") {
} if(!file_exists($this->getDataFolder() . "config.yml")) {
$this->git = new Linux($this, $this->getDataFolder()); file_put_contents($this->getDataFolder() . "config.yml", file_get_contents($this->getFile() . "resources/mac.yml"));
} elseif(Utils::getOS() == "mac") { }
if(!file_exists($this->getDataFolder() . "bin/git.exe") && $zip->open("../../resources/Mac.zip")) { $this->git = new Mac($this, $this->getDataFolder());
$zip->extractTo($this->getDataFolder() . "bin"); } else {
$zip->close(); $this->getLogger()->critical("Unsuported device ! Please refer to the download page to see the list of available devices.");
} $this->setEnable(false);
$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()){
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
case 'git':
switch($cmd->getName()){
if(count($args) >= 2) {
case 'git': switch($args[0]) {
if(count($args) >= 2) { case "cd":
$sender->sendMessage(self::PREFIX . $this->git->cd($args[1]));
switch($args[0]) { break;
case "cd": case "ls":
$this->git->cd($args[1]); $sender->sendMessage(self::PREFIX . $this->git->ls());
$sender->sendMessage(self::PREFIX . "New path: " . $this->git->getDir());
break; break;
case "commit": case "pwd":
unset($args[0]); $sender->sendMessage(self::PREFIX . "§aPath: " . $this->git->pwd());
$sender->sendMessage(self::PREFIX . $this->git->commit(implode(" ", $args)));
$sender->sendMessage(self::PREFIX . "Commited !");
break; break;
case "checkout": default:
$sender->sendMessage(self::PREFIX . $this->git->checkout($args[1])); $sender->sendMessage(self::PREFIX . $this->git->gitExec(implode(" ", $args)));
break; break;
}
case "dir": return true;
$sender->sendMessage(self::PREFIX . $this->git->getDir()); }
break;
case "clone": break;
$sender->sendMessage(self::PREFIX . $this->git->clone($args[1]));
break;
} }
return true;
}
return false;
break;
}
}
public function getGitClient() {
return $this->git;
return false; }
} public function setGitClient(GitClient $client) {
$this->git = $client;
return $this->git == $client;
public function getGitClient() { }
return $this->git;
}
/*
public function setGitClient(GitClient $client) { Check if a command exists
$this->git = $client; @param $command string
return $this->git == $client; */
} 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 != '';
}
}
}

View file

@ -23,120 +23,153 @@ use Ad5001\Gitable\Main;
class Windows extends GitClient { class Windows extends GitClient {
/*
Exec's a git request with defined args
@param $args string
public function commit(string $message) : string { */
$handle = popen($this->getDataFolder() . "bin/git.exe commit -m \"$message\"", 'r'); public function gitExec(string $args) : string {
return fread($handle, 2096); proc_open(
} "cd " . $this->dir,
array(
0 => array("pipe", "r"), //S TDIN
public function push(string $to ="github", string $from = "master") : string { 1 => array("pipe", "w"), //S TDOUT
$handle = popen($this->getDataFolder() . "bin/git.exe push $to $from", 'r'); 2 => array("pipe", "w"), //S TDERR
return fread($handle, 2096); ),
} $a
);
$process = proc_open(
public function checkout($branch = null) : string { "git " . $args,
$handle = popen($this->getDataFolder() . "bin/git.exe checkout " . (!is_null($branch) ? $this->getBranch() : $branch), 'r'); array(
return fread($handle, 2096); 0 => array("pipe", "r"), //S TDIN
} 1 => array("pipe", "w"), //S TDOUT
2 => array("pipe", "w"), //S TDERR
),
public function getBranch() : string { $pipes
$handle = popen($this->getDataFolder() . "bin/git.exe branch", 'r'); );
$read = fread($handle, 2096); proc_open(
return explode(" ", $read)[1]; "cd " . DEFAULT_GIT_DIR,
} array(
0 => array("pipe", "r"), //S TDIN
1 => array("pipe", "w"), //S TDOUT
public function branch($branch = '') : string { 2 => array("pipe", "w"), //S TDERR
$handle = popen($this->getDataFolder() . "bin/git.exe branch " . $branch, 'r'); ),
return fread($handle, 2096); $a
} );
if ($process !== false) {
$stdout = stream_get_contents($pipes[1]);
public function start() : string { $stderr = stream_get_contents($pipes[2]);
$handle = popen($this->getDataFolder() . "bin/git.exe init", 'r'); fclose($pipes[1]);
return fread($handle, 2096); fclose($pipes[2]);
} proc_close($process);
return (string) $stdout;
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;
} else { } 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'); Changes current directory
return fread($handle, 2096); @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 all files and dirs from the current directory
return fread($handle, 2096); */
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'); Checks if the executable exists.
return fread($handle, 2096); */
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);
}