Skip to content

Commit

Permalink
Add show command
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Nov 24, 2023
1 parent 95f2405 commit e8b6222
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 16 deletions.
16 changes: 15 additions & 1 deletion resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@

"commands.list.form.name": "Name: {{name}}",
"commands.list.form.license": "License: {{license}}",
"commands.list.form.versions": "Versions: {{versions}}"
"commands.list.form.versions": "Versions: {{versions}}",

"commands.show.failed_1": "{{name}} not found",
"commands.show.failed_2": "{{version}} not found",
"commands.show.form.name": "Name: {{name}}",
"commands.show.form.version": "Version: {{version}}",
"commands.show.form.license": "License: {{license}}",
"commands.show.form.homepage": "Homepage: {{homepage}}",
"commands.show.form.download_url": "Download URL: {{download_url}}",
"commands.show.form.downloads": "Downloads: {{downloads}}",
"commands.show.form.score": "Score: {{score}}",
"commands.show.form.description_url": "Description URL: {{description_url}}",
"commands.show.form.changelog_url": "Changelog URL: {{changelog_url}}",
"commands.show.form.api": "Supports from {{from}} to {{to}}",
"commands.show.form.deps": "Dependencies: "
}
4 changes: 3 additions & 1 deletion src/commands/OMPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use pocketmine\command\CommandSender;
use thebigcrafter\omp\commands\subcommands\ListCommand;
use thebigcrafter\omp\commands\subcommands\RemoveCommand;
use thebigcrafter\omp\commands\subcommands\ShowCommand;
use thebigcrafter\omp\commands\subcommands\VersionCommand;

class OMPCommand extends BaseCommand
Expand All @@ -28,7 +29,8 @@ protected function prepare() : void
$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 ListCommand($this->getOwningPlugin(), "list", "List available plugins", ["l", "-l", "--list"]),
new ShowCommand($this->getOwningPlugin(), "show", "Get details about a plugin", ["s", "-s", "--show"])
];

foreach ($subcommands as $subcommand) {
Expand Down
73 changes: 73 additions & 0 deletions src/commands/subcommands/ShowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\subcommands;

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\pool\PoggitPluginsPool;
use function is_null;

class ShowCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.show");

$this->registerArgument(0, new RawStringArgument("name", false));
$this->registerArgument(1, new RawStringArgument("version", true));
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$name = $args["name"];
$version = isset($args["version"]) ? $args["version"] : null;

$plugin = PoggitPluginsPool::getItem($name);

if (!isset($plugin)) {
$sender->sendMessage(Language::translate("commands.show.failed_1", ["name" => $name]));
return;
}

$pluginVersion = $plugin->getVersion($version);

if (is_null($pluginVersion["plugin"])) {
$sender->sendMessage(Language::translate("commands.show.failed_2", ["version" => $version]));
return;
}

$info = $pluginVersion["plugin"];

// aka $version if user provides a specified version
$latestVersion = $pluginVersion["version"];

$sender->sendMessage(Language::translate("commands.show.form.name", ["name" => $name]));
$sender->sendMessage(Language::translate("commands.show.form.version", ["version" => $latestVersion]));
$sender->sendMessage(Language::translate("commands.show.form.homepage", ["homepage" => $info->getHtmlUrl()]));
$sender->sendMessage(Language::translate("commands.show.form.license", ["license" => $plugin->getLicense()]));
$sender->sendMessage(Language::translate("commands.show.form.download_url", ["download_url" => $info->getArtifactUrl()]));
$sender->sendMessage(Language::translate("commands.show.form.downloads", ["downloads" => $info->getDownloads()]));
$sender->sendMessage(Language::translate("commands.show.form.score", ["score" => $info->getScore()]));
$sender->sendMessage(Language::translate("commands.show.form.description_url", ["description_url" => $info->getDescriptionUrl()]));
$sender->sendMessage(Language::translate("commands.show.form.changelog_url", ["changelog_url" => $info->getChangelogUrl()]));
$sender->sendMessage(Language::translate("commands.show.form.api", ["name" => $name]));
$sender->sendMessage(Language::translate("commands.show.form.deps", ["name" => $name]));
$sender->sendMessage("====================");
}
}
48 changes: 34 additions & 14 deletions src/types/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,75 @@
namespace thebigcrafter\omp\types;

use function array_keys;
use function is_null;
use function version_compare;

class Plugin
{
/** @var array{version: string, info: PluginVersion} $versions */
/** @var array{version: string, plugin: PluginVersion} $versions */
private array $versions;

public function __construct(
private string $license,
) {
}

/**
* Get plugin's license
*/
public function getLicense() : string
{
return $this->license;
}
/**
* @return array{version: string, info: PluginVersion}
* Get available versions
*
* @return array{version: string, plugin: PluginVersion}
*/
public function getVersions() : array
{
return $this->versions;
}

public function getVersionsOnly() : array {
/**
* Return an array of version. Example: ["1.0.0", "1.0.1"]
*/
public function getVersionsOnly() : array
{
return array_keys($this->getVersions());
}

/**
* Return the latest version if no specific version is provided.
* Return the latest version if no specific version is provided. $arr["version"] is null if no specific version is provided
*
* @return array{version: ?string, plugin: ?PluginVersion}
*/
public function getVersion(string $version = null) : PluginVersion|null
public function getVersion(string $version = null) : array
{
if (!is_null($version)) {
return $this->versions[$version] ?? null;
if (isset($version)) {
return ["version" => $version, "plugin" => isset($this->versions[$version]) ? $this->versions[$version] : null];
}

return $this->getLatestVersion();
}

public function addVersion(string $version, PluginVersion $info) : void
{
$this->versions[$version] = $info;
}

/** @return array{version: string, plugin: PluginVersion} */
public function getLatestVersion() : array
{
$latestVersion = null;
foreach ($this->getVersions() as $version) {
foreach ($this->getVersionsOnly() as $version) {
if ($latestVersion === null || version_compare($version, $latestVersion, '>')) {
$latestVersion = $version;
}
}
return $this->getVersions()[$latestVersion];
}

public function addVersion(string $version, PluginVersion $info) : void
{
$this->versions[$version] = $info;
return [
"version" => $latestVersion,
"plugin" => $this->getVersions()[$latestVersion]
];
}
}

0 comments on commit e8b6222

Please sign in to comment.