From cb49dfb229a3541899339063d4a07d966b00a34a Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Mon, 18 Sep 2023 08:35:27 +0100 Subject: [PATCH 1/2] Add a plugin generator Fixes #162 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 5bb6548..a898fc2 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/ ### Removed - `whippet migrate` commands. 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); + } +}; From 5f083e784d8a631fc04feb3b36634ad3277c84a9 Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Mon, 18 Sep 2023 11:21:28 +0100 Subject: [PATCH 2/2] Remove dynamic properties for all generators This is to avoid errors in PHP8.x. --- generators/app/generate.php | 2 ++ generators/plugin/generate.php | 2 ++ generators/theme/generate.php | 2 ++ 3 files changed, 6 insertions(+) diff --git a/generators/app/generate.php b/generators/app/generate.php index 12fca74..90bf197 100644 --- a/generators/app/generate.php +++ b/generators/app/generate.php @@ -4,6 +4,8 @@ class AppGenerator extends \Dxw\Whippet\WhippetGenerator { use \Dxw\Whippet\Modules\Helpers\WhippetHelpers; protected $wordpress_template_zip = 'https://github.com/dxw/wordpress-template/archive/main.zip'; + private $target_dir; + private $options = array(); function __construct($options) { $this->options = $options; diff --git a/generators/plugin/generate.php b/generators/plugin/generate.php index 062d76a..3e2ba45 100644 --- a/generators/plugin/generate.php +++ b/generators/plugin/generate.php @@ -8,6 +8,8 @@ class PluginGenerator extends \Dxw\Whippet\WhippetGenerator { protected $plugin_template_zip = 'https://github.com/dxw/wordpress-plugin-template/archive/main.zip'; private $unique_temp_id; + private $target_dir; + private $options = array(); function __construct($options) { $this->options = $options; diff --git a/generators/theme/generate.php b/generators/theme/generate.php index f090cd0..d87cc12 100644 --- a/generators/theme/generate.php +++ b/generators/theme/generate.php @@ -8,6 +8,8 @@ class ThemeGenerator extends \Dxw\Whippet\WhippetGenerator { protected $wordpress_template_zip = 'https://github.com/dxw/wordpress-template/archive/main.zip'; private $unique_temp_id; + private $target_dir; + private $options = array(); function __construct($options) { $this->options = $options;