Skip to content

Instantly share code, notes, and snippets.

@VixikHD
Last active March 11, 2023 22:21
Show Gist options
  • Save VixikHD/3dd2857d873bbfd55aa892d55434218f to your computer and use it in GitHub Desktop.
Save VixikHD/3dd2857d873bbfd55aa892d55434218f to your computer and use it in GitHub Desktop.
Simple script which generates PocketMine plugin workspace
<?php
declare(strict_types=1);
namespace vixikhd\bootstrap {
use function exec;
use function fgets;
use function file_put_contents;
use function getcwd;
use function gmdate;
use function json_encode;
use function mkdir;
use function strtolower;
use function trim;
use function yaml_emit_file;
use const JSON_PRETTY_PRINT;
use const STDIN;
const BASE_VERSION = "1.0.0";
const BASE_API = "4.0.0";
out("Welcome in PocketMine plugin bootstrap!");
out("Specify plugin name: ");
in($name);
out("Specify plugin author:");
in($author);
out("Specify plugin description:");
in($description);
echo "\n\n";
out("Generating plugin.yml...");
yaml_emit_file("plugin.yml", [
"name" => $name,
"description" => $description,
"main" => strtolower("$author\\$name\\") . $name,
"version" => BASE_VERSION,
"api" => BASE_API,
"author" => $author
]);
out("Generating composer.json");
file_put_contents("composer.json", json_encode([
"name" => strtolower($author . "/" . $name),
"description" => $description,
"minimum-stability" => "dev",
"require" => [
"pocketmine/pocketmine-mp" => "^" . BASE_API
]
], JSON_PRETTY_PRINT));
out("Would you like to install composer? [Y/n]");
in($composer);
if($composer === "Y") {
out("Installing composer...");
exec("composer update");
out("Execute composer update command.");
}
$resources = false;
out("Would you like to generate Main class? [Y/n]");
in($generate);
if($generate === "Y") {
out("Generating workspace...");
mkdir($dir = strtolower("src/$author/$name"), 0777, true);
$namespace = strtolower("$author\\$name");
file_put_contents("$dir/$name.php",
// Main class
<<<PHP
<?php
declare(strict_types=1);
namespace $namespace;
use pocketmine\\event\\Listener;
use pocketmine\\plugin\\PluginBase;
class $name extends PluginBase implements Listener {
protected function onEnable(): void {
}
}
PHP
);
out("Would you like to generate config workspace too? [Y/n]");
in($config);
if($config === "Y") {
$resources = true;
mkdir("resources");
yaml_emit_file("resources/config.yml", []);
}
out("Workspace generated!");
}
out("Would you like to generate plugin build script? [Y/n]");
in($buildScript);
if($buildScript) {
out("Would you like to specify custom output path? [Y/n]");
in($customOutPath);
$out = getcwd() . "/out/";
if($customOutPath === "Y") {
out("Specify output path (eg. C:/PocketMine/plugins/):");
in($out);
}
$out .= $name . ".phar";
out("Plugin will be generated as $out");
$resourcePart = "";
if($resources) {
$resourcePart =
// Plugin build script - resources
<<<PHP
mkdir("out/resources");
/** @var SplFileInfo \$file */
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator("resources", FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as \$file) {
if(\$file->isDir()) {
++\$i;
mkdir("out/" . \$file->getPath() . "/" . \$file->getFilename(), 0777, true);
} else {
++\$j;
copy(\$file->getPath() . "/" . \$file->getFilename(), "out/" . \$file->getPath() . "/" . \$file->getFilename());
}
}
PHP;
}
mkdir("tools");
file_put_contents("tools/build-plugin-phar.php",
// Plugin build script
<<<PHP
<?php
const out = "$out";
chdir("..");
if(file_exists("out")) {
echo "Cleaning workspace...\\n";
/** @var SplFileInfo \$file */
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator("out", FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as \$file) {
if(is_file(\$file)) {
unlink(\$file->getPath() . "/" . \$file->getFilename());
} else {
rmdir(\$file->getPath() . "/" . \$file->getFilename());
}
}
}
echo "Building phar from sources...\\n";
\$time = microtime(true);
@mkdir("out");
\$i = \$j = 0;
mkdir("out/src");
/** @var SplFileInfo \$file */
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator("src", FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as \$file) {
if(\$file->isDir()) {
++\$i;
mkdir("out/" . \$file->getPath() . "/" . \$file->getFilename(), 0777, true);
} else {
++\$j;
copy(\$file->getPath() . "/" . \$file->getFilename(), "out/" . \$file->getPath() . "/" . \$file->getFilename());
}
}
\$i += 1;
$resourcePart
copy("plugin.yml", "out/plugin.yml");
\$phar = new Phar(out);
\$phar->buildFromDirectory("out");
\$phar->compressFiles(Phar::GZ);
echo "Done (took " . round(microtime(true) - \$time, 2) . " sec). Added \$i directories and \$j files!\\n";
PHP
);
}
function out(string $message): void {
echo "[".gmdate("H:i:s")."] $message\n";
}
function in(?string &$command = ""): void {
$command = trim(fgets(STDIN));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment