Skip to content

Commit

Permalink
Merge pull request #218 from dxw/feature/generate-plugin
Browse files Browse the repository at this point in the history
Add a plugin generator
  • Loading branch information
snim2 authored Sep 18, 2023
2 parents 26657ce + 5f083e7 commit 1e434b6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
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;

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);
}
};
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

0 comments on commit 1e434b6

Please sign in to comment.