-
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 1, 2023
1 parent
3090c22
commit 7484f0d
Showing
5 changed files
with
122 additions
and
90 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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\omp\helpers; | ||
|
||
|
||
/* | ||
* 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 pocketmine\plugin\Plugin; | ||
use pocketmine\Server; | ||
use thebigcrafter\omp\OhMyPMMP; | ||
use thebigcrafter\omp\tasks\CheckForUpdates; | ||
|
||
use thebigcrafter\omp\tasks\CheckForUpdates; | ||
|
||
class UpdateChecker | ||
{ | ||
public static function init(Plugin $plugin): void | ||
{ | ||
OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion())); | ||
} | ||
} | ||
public static function init(Plugin $plugin) : void | ||
{ | ||
OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion())); | ||
} | ||
} |
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,57 +1,70 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\tasks; | ||
|
||
<?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; | ||
|
||
use pocketmine\scheduler\AsyncTask; | ||
use pocketmine\utils\Internet; | ||
use thebigcrafter\omp\Language; | ||
use thebigcrafter\omp\OhMyPMMP; | ||
|
||
use thebigcrafter\omp\OhMyPMMP; | ||
use function json_decode; | ||
use function version_compare; | ||
|
||
final class CheckForUpdates extends AsyncTask | ||
{ | ||
private string $highestVersion; | ||
private string $artifactUrl; | ||
public function __construct(private readonly string $name, private string $currentVersion) | ||
{ | ||
$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); | ||
|
||
if ($releases !== null) { | ||
/** | ||
* @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()) { | ||
$this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar"; | ||
OhMyPMMP::getInstance()->getLogger()->notice( | ||
Language::translate( | ||
"messages.new_update", | ||
["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl] | ||
) | ||
); | ||
} | ||
} | ||
} | ||
private string $highestVersion; | ||
private string $artifactUrl; | ||
public function __construct(private readonly string $name, private string $currentVersion) | ||
{ | ||
$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); | ||
|
||
if ($releases !== null) { | ||
/** | ||
* @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()) { | ||
$this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar"; | ||
OhMyPMMP::getInstance()->getLogger()->notice( | ||
Language::translate( | ||
"messages.new_update", | ||
["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl] | ||
) | ||
); | ||
} | ||
} | ||
} |
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,5 +1,14 @@ | ||
<?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\utils; | ||
|
@@ -16,16 +25,18 @@ public static function remove(string $path): Generator | |
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path) { | ||
$fs = new \Symfony\Component\Filesystem\Filesystem(); | ||
try { | ||
// @phpstan-ignore-next-line | ||
$resolve($fs->remove($path)); | ||
} catch (IOException $e) { | ||
$reject($e); | ||
} | ||
}); | ||
} | ||
|
||
public static function exists(\Traversable|array|string $files): bool | ||
// @phpstan-ignore-next-line | ||
public static function exists(iterable|string $files): bool | ||
{ | ||
$fs = new \Symfony\Component\Filesystem\Filesystem(); | ||
return $fs->exists($files); | ||
} | ||
} | ||
} |