-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nmtho
committed
Dec 2, 2023
1 parent
4c2396b
commit eda0c03
Showing
5 changed files
with
153 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* | ||
* (c) thebigcrafter <[email protected]> | ||
* | ||
* This source file is subject to the GPL-3.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\omp\commands; | ||
|
||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* | ||
* (c) thebigcrafter <[email protected]> | ||
* | ||
* This source file is subject to the GPL-3.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\omp\commands; | ||
|
||
use CortexPE\Commando\BaseCommand; | ||
use pocketmine\command\CommandSender; | ||
use thebigcrafter\omp\commands\subcommands\DisableCommand; | ||
|
@@ -23,36 +23,38 @@ | |
use thebigcrafter\omp\commands\subcommands\RemoveCommand; | ||
use thebigcrafter\omp\commands\subcommands\ShowCommand; | ||
use thebigcrafter\omp\commands\subcommands\UpdateCommand; | ||
use thebigcrafter\omp\commands\subcommands\VersionCommand; | ||
|
||
use thebigcrafter\omp\commands\subcommands\UpgradeCommand; | ||
use thebigcrafter\omp\commands\subcommands\VersionCommand; | ||
|
||
class OMPCommand extends BaseCommand | ||
{ | ||
protected function prepare() : void | ||
{ | ||
$this->setPermission("oh-my-pmmp.cmds"); | ||
|
||
$subcommands = [ | ||
new VersionCommand($this->getOwningPlugin(), "version", "Print oh-my-pmmp and PHP version", ["v", "-v", "--version"]), | ||
new RemoveCommand($this->getOwningPlugin(), "remove", "Remove a plugin", ["r", "-r", "--remove"]), | ||
new ListCommand($this->getOwningPlugin(), "list", "List available plugins", ["l", "-l", "--list"]), | ||
new ShowCommand($this->getOwningPlugin(), "show", "Get details about a plugin", ["s", "-s", "--show"]), | ||
new InstallCommand($this->getOwningPlugin(), "install", "Install a plugin", ["i", "-i", "--install"]), | ||
new ExtractCommand($this->getOwningPlugin(), "extract", "Extract a plugin", ["e", "-e", "--extract"]), | ||
new UpdateCommand($this->getOwningPlugin(), "update", "Update fetched data", ["ud", "-ud", "--update"]), | ||
new EnableCommand($this->getOwningPlugin(), "enable", "Enable plugin"), | ||
new DisableCommand($this->getOwningPlugin(), "disable", "Disable plugin") | ||
]; | ||
|
||
foreach ($subcommands as $subcommand) { | ||
$this->registerSubcommand($subcommand); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string> $args | ||
*/ | ||
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void | ||
{ | ||
$this->sendUsage(); | ||
} | ||
} | ||
protected function prepare(): void | ||
{ | ||
$this->setPermission("oh-my-pmmp.cmds"); | ||
|
||
$subcommands = [ | ||
new VersionCommand($this->getOwningPlugin(), "version", "Print oh-my-pmmp and PHP version", ["v", "-v", "--version"]), | ||
new RemoveCommand($this->getOwningPlugin(), "remove", "Remove a plugin", ["r", "-r", "--remove"]), | ||
new ListCommand($this->getOwningPlugin(), "list", "List available plugins", ["l", "-l", "--list"]), | ||
new ShowCommand($this->getOwningPlugin(), "show", "Get details about a plugin", ["s", "-s", "--show"]), | ||
new InstallCommand($this->getOwningPlugin(), "install", "Install a plugin", ["i", "-i", "--install"]), | ||
new ExtractCommand($this->getOwningPlugin(), "extract", "Extract a plugin", ["e", "-e", "--extract"]), | ||
new UpdateCommand($this->getOwningPlugin(), "update", "Update fetched data", ["ud", "-ud", "--update"]), | ||
new EnableCommand($this->getOwningPlugin(), "enable", "Enable plugin"), | ||
new DisableCommand($this->getOwningPlugin(), "disable", "Disable plugin"), | ||
new UpgradeCommand($this->getOwningPlugin(), "upgrade", "Upgrade plugin", ["u", "-u", "--upgrade"]) | ||
]; | ||
|
||
foreach ($subcommands as $subcommand) { | ||
$this->registerSubcommand($subcommand); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string> $args | ||
*/ | ||
public function onRun(CommandSender $sender, string $aliasUsed, array $args): void | ||
{ | ||
$this->sendUsage(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\commands\subcommands; | ||
|
||
use CortexPE\Commando\args\RawStringArgument; | ||
use CortexPE\Commando\BaseSubCommand; | ||
use Exception; | ||
use Generator; | ||
use PluginType; | ||
use pocketmine\command\CommandSender; | ||
use SOFe\AwaitGenerator\Await; | ||
use thebigcrafter\omp\helpers\PluginHelper; | ||
use thebigcrafter\omp\Language; | ||
use thebigcrafter\omp\OhMyPMMP; | ||
|
||
class UpgradeCommand extends BaseSubCommand | ||
{ | ||
protected function prepare(): void | ||
{ | ||
$this->setPermission("oh-my-pmmp.upgrade"); | ||
|
||
$this->registerArgument(0, new RawStringArgument("name", false)); | ||
} | ||
|
||
/** | ||
* @param array<string> $args | ||
*/ | ||
public function onRun(CommandSender $sender, string $aliasUsed, array $args): void | ||
{ | ||
$name = $args["name"]; | ||
|
||
Await::f2c(function () use ($name, $sender): Generator { | ||
try { | ||
yield from PluginHelper::remove($name, false); | ||
} catch (Exception $e) { | ||
$sender->sendMessage(Language::translate("commands.upgrade.failed", ["name" => $name])); | ||
} | ||
}); | ||
OhMyPMMP::getInstance()->getServer()->dispatchCommand($sender, "omp i $name"); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* | ||
* (c) thebigcrafter <[email protected]> | ||
* | ||
* This source file is subject to the GPL-3.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\omp\helpers; | ||
|
||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* | ||
* (c) thebigcrafter <[email protected]> | ||
* | ||
* This source file is subject to the GPL-3.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\omp\helpers; | ||
|
||
use Closure; | ||
use Exception; | ||
use Generator; | ||
use SOFe\AwaitGenerator\Await; | ||
use Symfony\Component\Filesystem\Path; | ||
use thebigcrafter\omp\OhMyPMMP; | ||
use thebigcrafter\omp\types\PluginType; | ||
use thebigcrafter\omp\Utils; | ||
use thebigcrafter\omp\utils\Filesystem; | ||
|
||
use thebigcrafter\omp\utils\Filesystem; | ||
|
||
class PluginHelper | ||
{ | ||
public static function remove(string $name, bool $wipeData) : Generator | ||
{ | ||
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($name, $wipeData) { | ||
$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); | ||
$pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); | ||
|
||
if (Filesystem::exists($pluginFilePath)) { | ||
Await::g2c(Filesystem::remove($pluginFilePath)); | ||
} elseif (Filesystem::exists($pluginFolderPath)) { | ||
Await::g2c(Filesystem::remove($pluginFolderPath)); | ||
} else { | ||
$reject(new Exception("Plugin not found")); | ||
} | ||
if ($wipeData) { | ||
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); | ||
Await::g2c(Filesystem::remove($pluginDataFolder)); | ||
} | ||
$resolve(true); | ||
}); | ||
} | ||
} | ||
public static function remove(string $name, bool $wipeData): Generator | ||
{ | ||
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($name, $wipeData) { | ||
$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); | ||
$pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); | ||
|
||
if (Filesystem::exists($pluginFilePath)) { | ||
Await::g2c(Filesystem::remove($pluginFilePath)); | ||
} elseif (Filesystem::exists($pluginFolderPath)) { | ||
Await::g2c(Filesystem::remove($pluginFolderPath)); | ||
} else { | ||
$reject(new Exception("Plugin not found")); | ||
} | ||
if ($wipeData) { | ||
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); | ||
Await::g2c(Filesystem::remove($pluginDataFolder)); | ||
} | ||
$resolve(true); | ||
}); | ||
} | ||
|
||
/** | ||
* Return true if plugin exists, false on failure | ||
*/ | ||
public static function exists(string $name, PluginType $type = PluginType::FILE_TYPE): bool | ||
{ | ||
if ($type === PluginType::FILE_TYPE) { | ||
return Filesystem::exists(Path::join(Utils::getPluginsFolder(), "$name.phar")); | ||
} | ||
|
||
return Filesystem::exists(Path::join(Utils::getPluginsFolder(), $name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\types; | ||
|
||
enum PluginType: int | ||
{ | ||
case FOLDER_TYPE = 1; | ||
case FILE_TYPE = 2; | ||
} |