Browse Source

Such simpler.

tags/v1.0
Ad5001 3 years ago
parent
commit
7f803ecd49
6 changed files with 282 additions and 260 deletions
  1. 4
    0
      config.yml
  2. 0
    1
      resources/Windows/bin
  3. 4
    0
      resources/win.yml
  4. 11
    47
      src/Ad5001/Gitable/GitClient.php
  5. 118
    100
      src/Ad5001/Gitable/Main.php
  6. 145
    112
      src/Ad5001/Gitable/Windows.php

+ 4
- 0
config.yml 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

+ 0
- 1
resources/Windows/bin

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

+ 4
- 0
resources/win.yml 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

+ 11
- 47
src/Ad5001/Gitable/GitClient.php 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 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 abstract function gitExec(string $args) : string;

public function getDir() {
public function pwd() : string {
return $this->dir;
}


public abstract function cd($path) : 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 cd(string $path) : string;

/*
Return the list of files in directory.
*/
public abstract function ls() : string;

public abstract function pull($to = "github", $from = "master") : string;
public abstract function initcheck();


}

+ 118
- 100
src/Ad5001/Gitable/Main.php View File

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


}
}
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 != '';
}
}
}

+ 145
- 112
src/Ad5001/Gitable/Windows.php View File

@@ -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 !";
}
}
public function clone($from) : string {
$handle = popen($this->getDataFolder() . "bin/git.exe clone $from", 'r');
return fread($handle, 2096);
}
public function log() : string {
$handle = popen($this->getDataFolder() . "bin/git.exe log", 'r');
return fread($handle, 2096);
}
public function remove($path) : string {
$handle = popen($this->getDataFolder() . "bin/git.exe rm $path", 'r');
return fread($handle, 2096);
}
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);
$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";
}
}


/*
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 !";
}
}


/*
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;
}
}


/*
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);
}
}
}



Loading…
Cancel
Save