From f36ba8fe1f068a0681b8589578fa66e01fa9b3ee Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Mon, 18 Sep 2023 08:35:27 +0100 Subject: [PATCH] Add a plugin generator Add a new generator to clone code in our WordPress plugin repo: https://github.com/dxw/wordpress-plugin-template --- CHANGELOG.md | 1 + docs/generate.md | 14 ++++++++++ generators/plugin/generate.php | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 generators/plugin/generate.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c8a3a0..af14610 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Support for PHP 8 +- `whippet generate plugin` to generate a plugin based on our template repo https://github.com/dxw/wordpress-plugin-template/ ## [2.3.0] - 2023-06-14 ### Added diff --git a/docs/generate.md b/docs/generate.md index 167f38e..12ef922 100644 --- a/docs/generate.md +++ b/docs/generate.md @@ -84,3 +84,17 @@ This will generate a new Whippet-compliant WordPress theme in `./whippet-theme`. You can change the location with the `-d` option. The generated theme is based on the theme in the [dxw WordPress template](https://github.com/dxw/wordpress-template/). + +## Generating a Whippet plugin + +To create a new Whippet plugin, run: + +``` +$ whippet generate plugin +``` + +This will generate a new Whippet-compliant WordPress plugin in `./whippet-plugin`. + +You can change the location with the `-d` option. + +The generated plugin is based on code in the [dxw WordPress plugin template](https://github.com/dxw/wordpress-plugin-template/). diff --git a/generators/plugin/generate.php b/generators/plugin/generate.php new file mode 100644 index 0000000..062d76a --- /dev/null +++ b/generators/plugin/generate.php @@ -0,0 +1,51 @@ +options = $options; + + if(isset($this->options->directory)) { + $this->target_dir = getcwd() . '/' . $this->options->directory; + } + else { + $this->target_dir = getcwd() . "/whippet-plugin"; + } + + $this->unique_temp_id = uniqid(); + } + + function generate() { + echo "Creating a new whippet plugin in {$this->target_dir}\n"; + + if(!file_exists($this->target_dir)) { + mkdir($this->target_dir); + } + + $this->downloadAndUnzipTemplate(); + $this->copyThemeAndRemoveTemplate(); + } + + private function downloadAndUnzipTemplate() + { + $this->download_url_to_file($this->plugin_template_zip, '/tmp/plugin_template_' . $this->unique_temp_id . '.zip'); + $this->unzip_to_folder('/tmp/plugin_template_' . $this->unique_temp_id . '.zip', '/tmp/plugin_template_' . $this->unique_temp_id); + } + + private function copyThemeAndRemoveTemplate() + { + $this->recurse_copy('/tmp/plugin_template_' . $this->unique_temp_id . '/wordpress-plugin-template-main', $this->target_dir); + copy('/tmp/plugin_template_' . $this->unique_temp_id . '/wordpress-plugin-template-main/.gitignore', $this->target_dir . '/.gitignore'); + if(isset($this->options->nogitignore)) { + unlink($this->target_dir . '/.gitignore'); + } + $this->recurse_rm('/tmp/plugin_template_' . $this->unique_temp_id); + } +};