Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a plugin generator #218

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions docs/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
2 changes: 2 additions & 0 deletions generators/app/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 53 additions & 0 deletions generators/plugin/generate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use Dxw\Whippet\Git\Git;

class PluginGenerator extends \Dxw\Whippet\WhippetGenerator {
use \Dxw\Whippet\Modules\Helpers\WhippetHelpers;

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a dynamic property, this will generate errors in PHP 8.2


if(isset($this->options->directory)) {
$this->target_dir = getcwd() . '/' . $this->options->directory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$target_dir will also need declaring as a property for PHP 8.2

}
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);
}
};
2 changes: 2 additions & 0 deletions generators/theme/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down