Skip to content

Commit

Permalink
Adjust pup to pass specific ENV vars down to build and workflow steps (
Browse files Browse the repository at this point in the history
…#23)

Some commands need environment variables to be set in order to function
correctly (example: `NODE_AUTH_TOKEN` for use in installing private npm
packages)

This change sets a default set of env vars that can get passed down to
commands (`NODE_AUTH_TOKEN`) and `.puprc` files can add additional `env`
entries to expand the ENV vars that pup will handle.

The _Files Changed_ on this PR looks intimidating, but most of those
changes are the snapshots.
  • Loading branch information
borkweb committed Sep 5, 2024
2 parents df48e4a + 3438881 commit 78e37e6
Show file tree
Hide file tree
Showing 38 changed files with 256 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .puprc-defaults
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"views": [],
"zip_dir": ".pup-zip"
},
"env": [
"NODE_AUTH_TOKEN"
],
"repo": null,
"zip_use_default_ignore": true,
"zip_name": null
Expand Down
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the `env` property in your `.puprc` file._

### Usage
```bash
pup build [--dev]
Expand Down Expand Up @@ -274,6 +276,8 @@ composer -- pup package <version>
## `pup workflow`
Run a command workflow.

_Note: If you need to pass environment variables to your workflow steps, you can add the env var names to the `env` property in your `.puprc` file._

An example workflow might look like this:

```json
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ root of the project. This file is a JSON file that contains the configuration op
| `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. |
| `env` | `array` | An array of environment variable names that, if set, should be passed to the build and workflow commands. |
| `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. |
Expand Down
2 changes: 1 addition & 1 deletion pup
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace StellarWP\Pup;

const PUP_VERSION = '1.3.4';
const PUP_VERSION = '1.3.6';
define( '__PUP_DIR__', __DIR__ );

if ( ! \Phar::running() ) {
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use StellarWP\Pup\App;
use StellarWP\Pup\Command\Command;
use StellarWP\Pup\Utils\Env;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -45,7 +46,8 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$step = substr( $step, 1 );
}
$io->section( "> <fg=cyan>{$step}</>" );
system( $step, $result );

system( Env::set( $step ), $result );
$io->newLine();

if ( $result ) {
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use StellarWP\Pup\App;
use StellarWP\Pup\Exceptions\BaseException;
use StellarWP\Pup\Command\Command;
use StellarWP\Pup\Utils\Env;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$step = substr( $step, 1 );
}
$io->section( "> <fg=cyan>{$step}</>" );
system( $step, $result );
system( Env::set( $step ), $result );
$io->newLine();

if ( $result ) {
Expand Down
30 changes: 30 additions & 0 deletions src/Utils/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace StellarWP\Pup\Utils;

use StellarWP\Pup\App;

class Env {
/**
* Prepend a command with the env vars.
*
* @param string $command Command to prepend.
*
* @return string
*/
public static function set( string $command ): string {
$config = App::getConfig();
$config_items = $config->get();
$env = [];

if ( empty( $config_items->env ) ) {
return $command;
}

foreach ( (array) $config_items->env as $env_item ) {
$env[ $env_item ] = "{$env_item}=" . getenv( $env_item );
}

return implode( ' ', $env ) . ' ' . $command;
}
}
39 changes: 39 additions & 0 deletions tests/cli/Commands/BuildCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,43 @@ public function it_should_run_no_build_steps_when_missing_puprc( CliTester $I )
$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

/**
* @test
*/
public function it_should_run_build_with_default_env_vars( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['build'][] = 'echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN"';
$this->write_puprc( $puprc );

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

$I->runShellCommand( "NODE_AUTH_TOKEN=123 php {$this->pup} build" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'NODE_AUTH_TOKEN=123' );

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

/**
* @test
*/
public function it_should_run_build_with_custom_env_vars( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['env'][] = 'BORK';
$puprc['build'][] = 'echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN"';
$puprc['build'][] = 'echo "BORK=$BORK"';
$this->write_puprc( $puprc );

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

$I->runShellCommand( "NODE_AUTH_TOKEN=123 BORK=abc php {$this->pup} build" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'NODE_AUTH_TOKEN=123' );
$I->seeInShellOutput( 'BORK=abc' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}
}
4 changes: 4 additions & 0 deletions tests/cli/Commands/CloneCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function it_should_clone_the_default_branch_of_a_git_repo( CliTester $I )

system( 'cd ' . $project_path . ' && git init --quiet' );
system( 'cd ' . $project_path . ' && git add .' );
system( 'cd ' . $project_path . ' && git config user.email "fake@fake.fake"' );
system( 'cd ' . $project_path . ' && git config user.name "Fake Fake"' );
system( 'cd ' . $project_path . ' && git commit -m "Initial commit" --quiet' );

$puprc = $this->get_puprc();
Expand Down Expand Up @@ -52,6 +54,8 @@ public function it_should_clone_a_specific_branch_of_a_git_repo( CliTester $I )

system( 'cd ' . $project_path . ' && git init --quiet' );
system( 'cd ' . $project_path . ' && git add .' );
system( 'cd ' . $project_path . ' && git config user.email "fake@fake.fake"' );
system( 'cd ' . $project_path . ' && git config user.name "Fake Fake"' );
system( 'cd ' . $project_path . ' && git commit -m "Initial commit" --quiet' );
system( 'cd ' . $project_path . ' && git checkout -b other-branch --quiet' );
system( 'cd ' . $project_path . ' && touch new-file.txt' );
Expand Down
43 changes: 43 additions & 0 deletions tests/cli/Commands/WorkflowCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,47 @@ public function it_should_fail_non_existent_workflow( CliTester $I ) {
$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}

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

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

$I->runShellCommand( "NODE_AUTH_TOKEN=123 php {$this->pup} do bork" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'NODE_AUTH_TOKEN=123' );

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

/**
* @test
*/
public function it_should_run_build_with_custom_env_vars( CliTester $I ) {
$puprc = $this->get_puprc();
$puprc['env'][] = 'BORK';
$puprc['workflows'] = [];
$puprc['workflows']['bork'] = [];
$puprc['workflows']['bork'][] = 'echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN"';
$puprc['workflows']['bork'][] = 'echo "BORK=$BORK"';
$this->write_puprc( $puprc );

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

$I->runShellCommand( "NODE_AUTH_TOKEN=123 BORK=abc php {$this->pup} do bork" );
$I->seeResultCodeIs( 0 );
$I->seeInShellOutput( 'NODE_AUTH_TOKEN=123' );
$I->seeInShellOutput( 'BORK=abc' );

$output = $I->grabShellOutput();
$this->assertMatchesStringSnapshot( $output );
}
}
4 changes: 4 additions & 0 deletions tests/cli/Commands/ZipCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function it_should_zip_with_repo_using_path( CliTester $I ) {

system( 'cd ' . $project_path . ' && git init --quiet' );
system( 'cd ' . $project_path . ' && git add .' );
system( 'cd ' . $project_path . ' && git config user.email "fake@fake.fake"' );
system( 'cd ' . $project_path . ' && git config user.name "Fake Fake"' );
system( 'cd ' . $project_path . ' && git commit -m "Initial commit" --quiet' );

$puprc = $this->get_puprc();
Expand Down Expand Up @@ -53,6 +55,8 @@ public function it_should_zip_with_repo_using_file_colon_slash_slash( CliTester

system( 'cd ' . $project_path . ' && git init --quiet' );
system( 'cd ' . $project_path . ' && git add .' );
system( 'cd ' . $project_path . ' && git config user.email "fake@fake.fake"' );
system( 'cd ' . $project_path . ' && git config user.name "Fake Fake"' );
system( 'cd ' . $project_path . ' && git commit -m "Initial commit" --quiet' );

$puprc = $this->get_puprc();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Running build steps...

> ls -a
-------

.
..
.puprc
bootstrap.php
other-file.php
package.json
src

> echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN"
-----------------------------------------

NODE_AUTH_TOKEN=123

> echo "BORK=$BORK"
-------------------

BORK=abc

✓ Build complete.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Running build steps...

> ls -a
-------

.
..
.puprc
bootstrap.php
other-file.php
package.json
src

> echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN"
-----------------------------------------

NODE_AUTH_TOKEN=123

✓ Build complete.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Cloning the /home/matt/git/pup/tests/_data/fake-project-git-repo repo into .pup-build...
Cloning the /var/www/html/wp-content/plugins/pup/tests/_data/fake-project-git-repo repo into .pup-build...
✓ Clone complete.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Cloning the /home/matt/git/pup/tests/_data/fake-project-git-repo repo into .pup-build...
Cloning the /var/www/html/wp-content/plugins/pup/tests/_data/fake-project-git-repo repo into .pup-build...
✓ Clone complete.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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.

_Note: If you need to pass environment variables to your build steps, you can add the env var names to the env property in your .puprc file._

> Usage:
--------

Expand Down
Loading

0 comments on commit 78e37e6

Please sign in to comment.