Skip to content

Commit

Permalink
Use await-generator for I/O functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Dec 2, 2023
1 parent 453e887 commit c07e7f9
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 82 deletions.
10 changes: 9 additions & 1 deletion src/thebigcrafter/omp/commands/subcommands/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use Generator;
use pocketmine\command\CommandSender;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
Expand Down Expand Up @@ -46,7 +48,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
return;
}

Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath));
Await::f2c(function () use ($oldPluginFilePath, $newPluginFilePath, $sender) : Generator {
try {
yield from Filesystem::rename($oldPluginFilePath, $newPluginFilePath);
} catch (IOException $e) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()]));
}
});

$sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name]));
}
Expand Down
10 changes: 9 additions & 1 deletion src/thebigcrafter/omp/commands/subcommands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use Generator;
use pocketmine\command\CommandSender;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
Expand Down Expand Up @@ -46,7 +48,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
return;
}

Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath));
Await::f2c(function () use ($oldPluginFilePath, $newPluginFilePath, $sender) : Generator {
try {
yield from Filesystem::rename($oldPluginFilePath, $newPluginFilePath);
} catch (IOException $e) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()]));
}
});

$sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name]));
}
Expand Down
22 changes: 15 additions & 7 deletions src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use Phar;
use Generator;
use pocketmine\command\CommandSender;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\helpers\PharHelper;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\Utils;
use Throwable;

class ExtractCommand extends BaseSubCommand {
class ExtractCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.extract");
Expand All @@ -39,14 +43,18 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
$name = $args["name"];
$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");

if(!$fs->exists($pluginFilePath)) {
if (!$fs->exists($pluginFilePath)) {
$sender->sendMessage(Language::translate("commands.extract.failed", ["name" => $name]));
return;
}

$phar = new Phar($pluginFilePath);
$phar->extractTo(Path::join(Utils::getPluginsFolder(), $name));

$sender->sendMessage(Language::translate("commands.extract.successfully", ["name" => $name]));
Await::f2c(function () use ($pluginFilePath, $name, $sender) : Generator {
try {
yield from PharHelper::extract($pluginFilePath, Path::join(Utils::getPluginsFolder(), $name));
$sender->sendMessage(Language::translate("commands.extract.successfully", ["name" => $name]));
} catch (Throwable $e) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()]));
}
});
}
}
13 changes: 10 additions & 3 deletions src/thebigcrafter/omp/commands/subcommands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use Exception;
use Generator;
use pocketmine\command\CommandSender;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetRequestResult;
Expand Down Expand Up @@ -71,8 +73,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v

$pharPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar");

Await::g2c(PharHelper::writePhar($pharPath, $res->getBody()));

$sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion]));
Await::f2c(function () use ($pharPath, $res, $sender, $name, $latestVersion) : Generator {
try {
yield from PharHelper::create($pharPath, $res->getBody());
$sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion]));
} catch (Exception $e) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()]));
}
});
}
}
20 changes: 11 additions & 9 deletions src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use CortexPE\Commando\args\BooleanArgument;
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use Generator;
use pocketmine\command\CommandSender;
use SOFe\AwaitGenerator\Await;
use thebigcrafter\omp\helpers\PluginHelper;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\tasks\RemovePluginTask;

class RemoveCommand extends BaseSubCommand
{
Expand All @@ -38,13 +40,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
$name = $args["name"];
$wipeData = isset($args["wipeData"]) ? (bool) $args["wipeData"] : false;

$exec = (new RemovePluginTask($name, $wipeData))->execute();

if (!$exec) {
$sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name]));
return;
}
$sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name]));
return;
Await::f2c(function () use ($name, $wipeData, $sender) : Generator {
try {
yield from PluginHelper::remove($name, $wipeData);
$sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name]));
} catch (\Throwable $th) {
$sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name]));
}
});
}
}
46 changes: 33 additions & 13 deletions src/thebigcrafter/omp/helpers/PharHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@

namespace thebigcrafter\omp\helpers;

use Closure;
use Exception;
use Generator;
use Closure;
use Exception;
use Generator;
use Phar;
use SOFe\AwaitGenerator\Await;
use Throwable;
use function file_put_contents;

class PharHelper
{
public static function writePhar(string $path, string $content) : Generator
{
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path, $content) {
class PharHelper
{
/**
* Create a Phar file
* Returns the number of bytes that were written to the file, or throw an Exception on failure.
*/
public static function create(string $path, string $content) : Generator
{
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path, $content) {
$exec = file_put_contents($path, $content);

if ($exec === false) {
$reject(new Exception("Cannot create Phar file"));
if ($exec === false) {
$reject(new Exception("Cannot create Phar file"));
return;
}

$resolve($exec);
});
}
$resolve($exec);
});
}

public static function extract(string $filePath, string $to) : Generator
{
return Await::promise(function (Closure $resolve, Closure $reject) use ($filePath, $to) {
$phar = new Phar($filePath);
try {
$phar->extractTo($to);
$resolve(true);
} catch (Throwable $e) {
$reject($e);
}
});
}
}
47 changes: 47 additions & 0 deletions src/thebigcrafter/omp/helpers/PluginHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Utils;
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);
});
}
}
48 changes: 0 additions & 48 deletions src/thebigcrafter/omp/tasks/RemovePluginTask.php

This file was deleted.

0 comments on commit c07e7f9

Please sign in to comment.