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;
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();
}

View file

@ -41,27 +41,23 @@ class Main extends PluginBase implements Listener {
@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();
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() . "bin/git.exe") && $zip->open("../../resources/Linux.zip")) {
$zip->extractTo($this->getDataFolder() . "bin");
$zip->close();
}
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() . "bin/git.exe") && $zip->open("../../resources/Mac.zip")) {
$zip->extractTo($this->getDataFolder() . "bin");
$zip->close();
}
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 {
@ -87,26 +83,19 @@ class Main extends PluginBase implements Listener {
switch($args[0]) {
case "cd":
$this->git->cd($args[1]);
$sender->sendMessage(self::PREFIX . "New path: " . $this->git->getDir());
$sender->sendMessage(self::PREFIX . $this->git->cd($args[1]));
break;
case "commit":
unset($args[0]);
$sender->sendMessage(self::PREFIX . $this->git->commit(implode(" ", $args)));
$sender->sendMessage(self::PREFIX . "Commited !");
case "ls":
$sender->sendMessage(self::PREFIX . $this->git->ls());
break;
case "checkout":
$sender->sendMessage(self::PREFIX . $this->git->checkout($args[1]));
case "pwd":
$sender->sendMessage(self::PREFIX . "§aPath: " . $this->git->pwd());
break;
case "dir":
$sender->sendMessage(self::PREFIX . $this->git->getDir());
break;
case "clone":
$sender->sendMessage(self::PREFIX . $this->git->clone($args[1]));
default:
$sender->sendMessage(self::PREFIX . $this->git->gitExec(implode(" ", $args)));
break;
}
return true;
@ -136,4 +125,33 @@ class Main extends PluginBase implements Listener {
}
/*
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 != '';
}
}
}

View file

@ -23,121 +23,154 @@ use Ad5001\Gitable\Main;
class Windows extends GitClient {
/*
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);
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;
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);
}
}