Skip to content

Commit

Permalink
More generators were made
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Dec 1, 2023
1 parent 37f543e commit 453e887
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/thebigcrafter/omp/commands/subcommands/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Filesystem;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;
use thebigcrafter\omp\utils\Filesystem;

class DisableCommand extends BaseSubCommand
{
Expand All @@ -36,7 +37,6 @@ protected function prepare() : void
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$fs = new Filesystem();
$name = $args["name"];
$oldPluginFilePath = Utils::getPluginFilePath($name);
$newPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar");
Expand All @@ -46,7 +46,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
return;
}

$fs->rename($oldPluginFilePath, $newPluginFilePath);
Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath));

$sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name]));
}
Expand Down
8 changes: 4 additions & 4 deletions src/thebigcrafter/omp/commands/subcommands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Filesystem;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;
use thebigcrafter\omp\utils\Filesystem;

class EnableCommand extends BaseSubCommand
{
Expand All @@ -36,17 +37,16 @@ protected function prepare() : void
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$fs = new Filesystem();
$name = $args["name"];
$newPluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");
$oldPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar");

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

$fs->rename($oldPluginFilePath, $newPluginFilePath);
Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath));

$sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name]));
}
Expand Down
5 changes: 3 additions & 2 deletions src/thebigcrafter/omp/commands/subcommands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
use pocketmine\command\CommandSender;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetRequestResult;
use SOFe\AwaitGenerator\Await;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\helpers\PharHelper;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\pool\PoggitPluginsPool;
use function file_put_contents;
use function is_null;

class InstallCommand extends BaseSubCommand
Expand Down Expand Up @@ -70,7 +71,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v

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

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

$sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion]));
}
Expand Down
36 changes: 36 additions & 0 deletions src/thebigcrafter/omp/helpers/PharHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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 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) {
$exec = file_put_contents($path, $content);

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

$resolve($exec);
});
}
}
62 changes: 31 additions & 31 deletions src/thebigcrafter/omp/tasks/CheckForUpdates.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?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\tasks;


/*
* 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\tasks;

use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\Internet;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use function json_decode;
use function version_compare;

use function version_compare;

final class CheckForUpdates extends AsyncTask
{
private string $highestVersion;
Expand All @@ -28,33 +28,33 @@ public function __construct(private readonly string $name, private string $curre
{
$this->highestVersion = $currentVersion;
$this->artifactUrl = "";
}

}

public function onRun() : void
{
$res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name);

$releases = (array) json_decode($res->getBody(), true);

$res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name);

$releases = (array) json_decode($res->getBody(), true);

if ($releases !== null) {
/**
* @var array{'version': string, 'artifact_url': string} $release
/**
* @var array{'version': string, 'artifact_url': string} $release
*/
foreach ($releases as $release) {
if (version_compare($this->highestVersion, $release["version"], ">")) {
continue;
}

}

$this->highestVersion = $release["version"];
$this->artifactUrl = $release["artifact_url"];
}
}

}

if ($this->highestVersion !== $this->currentVersion) {
$this->setResult(false);
}
}

}

public function onCompletion() : void
{
if (!$this->getResult()) {
Expand All @@ -67,4 +67,4 @@ public function onCompletion() : void
);
}
}
}
}
13 changes: 13 additions & 0 deletions src/thebigcrafter/omp/utils/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public static function remove(string $path) : Generator
});
}

public static function rename(string $origin, string $target) : Generator
{
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($origin, $target) {
$fs = new \Symfony\Component\Filesystem\Filesystem();
try {
// @phpstan-ignore-next-line
$resolve($fs->rename($origin, $target));
} catch (IOException $e) {
$reject($e);
}
});
}

// @phpstan-ignore-next-line
public static function exists(iterable|string $files) : bool
{
Expand Down

0 comments on commit 453e887

Please sign in to comment.