Skip to content

Commit

Permalink
Implement pup workflows and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
test committed Apr 25, 2024
1 parent 9094b4a commit 1032be0
Show file tree
Hide file tree
Showing 25 changed files with 379 additions and 11 deletions.
1 change: 1 addition & 0 deletions .puprc-defaults
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"build": [],
"build_dev": [],
"workflows": {},
"checks": {
"tbd": {
"fail_method": "error",
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ This is a CLI utility built by [StellarWP](https://stellarwp.com) for running pr
* [`pup check:tbd`](/docs/commands.md#pup-checktbd)
* [`pup check:version-conflict`](/docs/commands.md#pup-checkversion-conflict)
* [`pup clean`](/docs/commands.md#pup-clean)
* [`pup do`](/docs/commands.md#pup-do)
* [`pup get-version`](/docs/commands.md#pup-get-version)
* [`pup help`](/docs/commands.md#pup-help)
* [`pup i18n`](/docs/commands.md#pup-i18n)
* [`pup info`](/docs/commands.md#pup-info)
* [`pup package`](/docs/commands.md#pup-package)
* [`pup workflow`](/docs/commands.md#pup-workflow)
* [`pup zip`](/docs/commands.md#pup-zip)
* [`pup zip-name`](/docs/commands.md#pup-zip-name)
* [Command flow for `pup zip`](/docs/flow.md)
Expand All @@ -35,5 +37,9 @@ This is a CLI utility built by [StellarWP](https://stellarwp.com) for running pr
* [Creating custom checks](#creating-custom-checks)
* [Simple checks](#simple-checks)
* [Class-based checks](#class-based-checks)
* [Workflows](/docs/workflows.md)
* [Defining workflows](/docs/workflows.md#defining-workflows)
* [Calling workflows](/docs/workflows.md#calling-workflows)
* [Pseudo-workflows](/docs/workflows.md#pseudo-workflows)
* Examples
* [GitHub Workflow: Zipping](/examples/workflows/zip.yml) - Breaks up the `pup zip` command into multiple steps so debugging is easy.
26 changes: 26 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ composer -- pup package <version>
## `pup workflow`
Run a command workflow.

An example workflow might look like this:

```json
{
"workflow": {
"my-workflow": [
"npm ci",
"npm run build",
"@composer run some-script"
]
}
}
```

Executing this workflow would work like this:

```bash
pup workflow my-workflow
# OR
pup do my-workflow
# OR
composer -- pup workflow my-workflow
# OR
composer -- pup do my-workflow
```

### Usage
```bash
pup workflow <workflow>
Expand Down
21 changes: 11 additions & 10 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ root of the project. This file is a JSON file that contains the configuration op

## Top-level properties

| Property | Type | Description |
|-------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------|
| `build` | `array` | An array of CLI commands to execute for the build process of your project. |
| `build_dev` | `array` | An array of CLI commands to execute for the `--dev` build process of your project. If empty, it defaults to the value of `build` |
| `checks` | `object` | An object of check configurations indexed by the check's slug. See the [docs for checks](/docs/checks.md) for more info. |
| `paths` | `object` | An object containing paths used by `pup`. [See below](#paths). |
| `repo` | `string`/`null` | The git repo used to clone the project. If not provided, at github URL is generated based on the `name` property of `composer.json` |
| `zip_use_default_ignore` | `boolean` | Whether or not additionally ignore files based on the [`.distignore-defaults`](/.distignore-defaults) file. Defaults to `true`. |
| `zip_name` | `string` | The name of the zip file to be generated. Defaults to the name of the project as set in `composer.json`. |
| Property | Type | Description |
|-------------|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `build` | `array` | An array of CLI commands to execute for the build process of your project. |
| `build_dev` | `array` | An array of CLI commands to execute for the `--dev` build process of your project. If empty, it defaults to the value of `build` |
| `checks` | `object` | An object of check configurations indexed by the check's slug. See the [docs for checks](/docs/checks.md) for more info. |
| `paths` | `object` | An object containing paths used by `pup`. [See below](#paths). |
| `repo` | `string`/`null` | The git repo used to clone the project in the format of `<org>/<repo>`. If not provided, at github URL is generated based on the `name` property of `composer.json` |
| `workflows` | `object` | An object of workflow configurations. The index is the workflow slug and the values are arrays of strings that hold commands. See the [docs for workflows](/docs/workflows.md) for more info. |
| `zip_use_default_ignore` | `boolean` | Whether or not additionally ignore files based on the [`.distignore-defaults`](/.distignore-defaults) file. Defaults to `true`. |
| `zip_name` | `string` | The name of the zip file to be generated. Defaults to the name of the project as set in `composer.json`. |

## Paths

Expand Down Expand Up @@ -135,4 +136,4 @@ This is what you should add as a `paths.versions` entry:
]
}
}
```
```
46 changes: 46 additions & 0 deletions docs/workflows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Workflows

Workflows are a way to declare a series of commands that you want to run in a specific order. This allows you to specify
workflows that differ from the `build` and `build_dev` commands.

* [Defining workflows](#defining-workflows)
* [Calling workflows](#calling-workflows)
* [Pseudo-workflows](#pseudo-workflows)

## Defining workflows

Workflows are defined in the `workflows` property of your `.puprc` file.

```json
{
"workflows": {
"my-workflow": [
"npm ci",
"npm run build",
"@composer run some-script"
],
"my-other-workflow": [
"@composer run some-other-script",
"@composer run make-pot"
]
}
}
```

## Calling workflows

You can call a workflow by running the `workflow` command (or its alias `do`) with the name of the workflow as an argument.

```bash
pup workflow my-workflow
# OR
pup do my-workflow
```

## Pseudo-workflows

The `build` and `build_dev` properties within your `.puprc` file are also callable via the `workflow` command.

```bash
pup workflow build
```
2 changes: 1 addition & 1 deletion src/Commands/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {

$workflow = $collection->get( $workflow_slug );
if ( ! $workflow ) {
$io->writeln( "<error>The workflow '{$workflow_slug}' does not exist.</error>" );
$io->writeln( "<error>The workflow '{$workflow_slug}' does not exist.</error>" );
return 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Workflow/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function offsetExists( $offset ): bool {
*/
#[\ReturnTypeWillChange]
public function offsetGet( $offset ) {
if ( ! isset( $this->workflows[ $offset ] ) ) {
return null;
}
return $this->workflows[ $offset ];
}

Expand Down
2 changes: 2 additions & 0 deletions tests/cli/Commands/HelpCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ protected function topicProvider(): array {
[ 'topic' => 'check' ],
[ 'topic' => 'check:tbd' ],
[ 'topic' => 'check:version-conflict' ],
[ 'topic' => 'do' ],
[ 'topic' => 'help' ],
[ 'topic' => 'i18n' ],
[ 'topic' => 'get-version' ],
[ 'topic' => 'package' ],
[ 'topic' => 'workflow' ],
[ 'topic' => 'zip' ],
[ 'topic' => 'zip-name' ],
];
Expand Down
130 changes: 130 additions & 0 deletions tests/cli/Commands/WorkflowCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace StellarWP\Pup\Tests\Cli\Commands;

use StellarWP\Pup\Tests\Cli\AbstractBase;
use StellarWP\Pup\Tests\CliTester;

class WorkflowCest extends AbstractBase {
public function _before( CliTester $I ) {
parent::_before( $I );
}

/**
* @test
*/
public function it_should_run_workflow( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['workflows'] = [];
$puprc['workflows']['bork'] = [];
$puprc['workflows']['bork'][] = 'echo "fake project, yo"';
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} workflow bork" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'fake project, yo' );
$I->seeInShellOutput( 'Workflow complete.' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_run_do( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['workflows'] = [];
$puprc['workflows']['bork'] = [];
$puprc['workflows']['bork'][] = 'echo "fake project, yo"';
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} do bork" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'fake project, yo' );
$I->seeInShellOutput( 'Workflow complete.' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_run_build_as_workflow( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['build'][] = 'echo "fake project, yo"';
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} do build" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'fake project, yo' );
$I->seeInShellOutput( 'Workflow complete.' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_run_build_dev_as_workflow( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['build_dev'][] = 'echo "fake project, yo"';
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} do build_dev" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'fake project, yo' );
$I->seeInShellOutput( 'Workflow complete.' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_run_an_empty_workflow( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['workflows'] = [];
$puprc['workflows']['bork'] = [];
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} do bork" );
$I->seeResultCodeIs( 0 );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_fail_non_existent_workflow( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['workflows'] = [];
$puprc['workflows']['bork'] = [];
$puprc['workflows']['bork'][] = 'echo "fake project, yo"';
$this->write_puprc( $puprc );

chdir( $this->tests_root . '/_data/fake-project' );

$I->runShellCommand( "php {$this->pup} workflow whee", false );
$I->seeResultCodeIs( 1 );
$I->seeInShellOutput( '\'whee\' does not exist.' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ Run pup help <topic> for more information on a specific command.
check:version-conflict Verifies that all of your version numbers match.
check Runs all registered check commands.
clean This command cleans up any directories that pup creates.
do Alias for pup workflow. See pup help workflow for more information.
get-version Gets your project's version number.
help Shows the help menu.
i18n Pulls in translations from a GlotPress instance.
info Gets pup details for the current project.
package Packages your project into a zip file with the passed in version number.
workflow Run a command workflow.
zip-name Gets your project's zip name (sans the .zip extension).
zip Runs the full pup set of commands to create a zip file.
------------------------ -------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ Run pup help <topic> for more information on a specific command.
check:version-conflict Verifies that all of your version numbers match.
check Runs all registered check commands.
clean This command cleans up any directories that pup creates.
do Alias for pup workflow. See pup help workflow for more information.
get-version Gets your project's version number.
help Shows the help menu.
i18n Pulls in translations from a GlotPress instance.
info Gets pup details for the current project.
package Packages your project into a zip file with the passed in version number.
workflow Run a command workflow.
zip-name Gets your project's zip name (sans the .zip extension).
zip Runs the full pup set of commands to create a zip file.
------------------------ -------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Help: pup build
===============

Runs the build commands from the .puprc file.


If you want your dev builds to build differently, you can add a build_dev property to your .puprc file.

> Usage:
--------

..................................................
pup build [--dev]
# or
composer -- pup build [--dev]
..................................................

> Arguments:
------------

---------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --
Argument Description
---------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --
--dev Optional. Whether or not this is a dev build. Using this option will run the build_dev commands from your .puprc file if they exist, otherwise it will run build commands.
--root Optional. Run the command from a different directory from the current.
---------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --


> Specifying build commands:
----------------------------

You can specify build commands within your .puprc file by adding to either the build or build_dev properties. These
commands will be run in the order they are specified. By default, if any command fails, the build will fail. You can,
however, prepend your commands with @ and that will tell pup to ignore failures for that step. Here's an example:

..................................................
{
"build": [
"npm ci",
"npm run build",
"@composer run some-script"
]
}
..................................................

In the above example, npm ci and npm run build will need to complete successfully for the build to succeed, but the
composer run some-script is prepended by @ so if it fails, the build will continue forward.
Loading

0 comments on commit 1032be0

Please sign in to comment.