Skip to content

Commit

Permalink
Add upgrade command
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Dec 2, 2023
1 parent 4c2396b commit eda0c03
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 84 deletions.
5 changes: 4 additions & 1 deletion resources/lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"commands.enable.successfully": "Enabled {{name}} successfully",

"commands.disable.failed": "{{name}} not found",
"commands.disable.successfully": "Disabled {{name}} successfully"
"commands.disable.successfully": "Disabled {{name}} successfully",

"commands.upgrade.failed": "{{name}} not found",
"commands.upgrade.successfully": "Upgraded {{name}} successfully"
}
92 changes: 47 additions & 45 deletions src/thebigcrafter/omp/commands/OMPCommand.php
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;
Expand All @@ -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 src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php
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;
}
}
89 changes: 51 additions & 38 deletions src/thebigcrafter/omp/helpers/PluginHelper.php
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));
}
}
9 changes: 9 additions & 0 deletions src/thebigcrafter/omp/types/PluginType.php
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;
}

0 comments on commit eda0c03

Please sign in to comment.