Skip to content

Commit 8f7c278

Browse files
committed
add 'vendors' flag to packages to only run composer install if enabled
1 parent 584b702 commit 8f7c278

7 files changed

Lines changed: 203 additions & 13 deletions

File tree

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ WordPress installation types.
4444
2. Choose one of the [recipes](#recipes) and copy the corresponding example files (`examples/base` or
4545
`examples/advanced`) into your root directory - **or** write your own.
4646
3. Read through the recipe and customize it to your needs - here's a checklist:
47-
- [ ] Check localhost configuration
48-
- [ ] Set paths to your directory structure
49-
- [ ] If you have a custom theme set it's name - if not remove the configuration and the theme build-tasks
50-
- [ ] If you have a custom mu-plugin set it's name - if not remove the configuration and the mu-plugin build-tasks
51-
- [ ] Check if the deployment flow meets your needs and maybe delete/add/overwrite tasks
47+
48+
- [ ] Check localhost configuration
49+
- [ ] Set paths to your directory structure
50+
- [ ] If you have a custom theme set it's name - if not remove the configuration and the theme build-tasks
51+
- [ ] If you have a custom mu-plugin set it's name - if not remove the configuration and the mu-plugin build-tasks
52+
- [ ] Check if the deployment flow meets your needs and maybe delete/add/overwrite tasks
53+
5254
4. Make your remote hosts ready for deployment (install composer, WP CLI; setup paths,...). Allthough the library checks
5355
for most of them and installs them.
5456
5. Make a **test deployment** to a test/staging server. Do not directly deploy to your production site, you may break
@@ -308,6 +310,7 @@ set('packages', [
308310
'remote:path' => 'path/on/remote',
309311
'assets' => true,
310312
'assets:build_script' => 'build',
313+
'vendors' => true,
311314
],
312315
// Add more packages as needed
313316
]);
@@ -327,7 +330,8 @@ set('packages', [
327330
],
328331
'core-functionality' => [
329332
'path' => '{{mu-plugins/dir}}/core-functionality',
330-
'remote:path' => '{{mu-plugins/dir}}/core-functionality'
333+
'remote:path' => '{{mu-plugins/dir}}/core-functionality',
334+
'vendors' => true,
331335
],
332336
]);
333337
```

examples/bedrock/deploy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
],
4141
'core-functionality' => [
4242
'path' => '{{mu-plugins/dir}}/core-functionality',
43-
'remote:path' => '{{mu-plugins/dir}}/core-functionality'
43+
'remote:path' => '{{mu-plugins/dir}}/core-functionality',
4444
],
4545
]);
4646

examples/multisite/deploy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
],
3636
'core-functionality' => [
3737
'path' => '{{mu-plugins/dir}}/core-functionality',
38-
'remote:path' => '{{mu-plugins/dir}}/core-functionality'
38+
'remote:path' => '{{mu-plugins/dir}}/core-functionality',
39+
'vendors' => true,
3940
],
4041
]);
4142

examples/simple/deploy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
],
3434
'core-functionality' => [
3535
'path' => '{{mu-plugins/dir}}/core-functionality',
36-
'remote:path' => '{{mu-plugins/dir}}/core-functionality'
36+
'remote:path' => '{{mu-plugins/dir}}/core-functionality',
37+
'vendors' => true,
3738
],
3839
]);
3940

recipes/simple.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
namespace Gaambo\DeployerWordpress\Recipes\Simple;
1010

1111
use function Deployer\add;
12-
use function Deployer\get;
13-
use function Deployer\run;
14-
use function Deployer\set;
12+
use function Deployer\after;
1513
use function Deployer\task;
16-
use function Deployer\test;
1714

1815
require __DIR__ . '/common.php';
1916

@@ -22,6 +19,8 @@
2219
task('deploy:update_code', ['packages:push'])
2320
->desc('Pushes local packages to the remote hosts');
2421

22+
after('packages:push', 'packages:vendors');
23+
2524
task('deploy', [
2625
'deploy:prepare',
2726
'deploy:build_assets',

tasks/packages.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@
8787
*
8888
* Configuration per package:
8989
* - path: Path of package relative to release_path/current_path
90+
* - vendors: Whether the package has composer vendors to install
9091
*
9192
* Example:
9293
* dep packages:vendors prod
9394
*/
9495
task('packages:vendors', function () {
9596
foreach (get('packages', []) as $package) {
9697
$packagePath = $package['path'];
98+
if (empty($package['vendors'])) {
99+
continue;
100+
}
97101
Composer::runDefault(
98102
"{{release_or_current_path}}/$packagePath"
99103
);
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Gaambo\DeployerWordpress\Tests\Functional\Tasks;
4+
5+
use Gaambo\DeployerWordpress\Tests\Functional\FunctionalTestCase;
6+
use RuntimeException;
7+
8+
class PackagesTasksFunctionalTest extends FunctionalTestCase
9+
{
10+
private string $localMuPluginsDir;
11+
private string $remoteMuPluginsDir;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
// Set up temporary directories
18+
$this->localMuPluginsDir = $this->localDocRootDir . '/wp-content/mu-plugins';
19+
$this->remoteMuPluginsDir = $this->remoteReleaseDir . '/wp-content/mu-plugins';
20+
21+
// Create directories
22+
mkdir($this->localMuPluginsDir, 0755, true);
23+
mkdir($this->remoteMuPluginsDir, 0755, true);
24+
}
25+
26+
public function testListAvailableTasks(): void
27+
{
28+
$this->dep('list', null);
29+
$output = $this->tester->getDisplay();
30+
31+
$this->assertStringContainsString('packages:assets:vendors', $output);
32+
$this->assertStringContainsString('packages:assets:build', $output);
33+
$this->assertStringContainsString('packages:vendors', $output);
34+
$this->assertStringContainsString('packages:assets', $output);
35+
$this->assertStringContainsString('packages', $output);
36+
}
37+
38+
public function testPackagesAssetsVendorsOnlyRunsWhenAssetsIsTrue(): void
39+
{
40+
// Copy test plugin from fixtures to local
41+
$fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin');
42+
43+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-assets';
44+
mkdir($targetPluginDir, 0755, true);
45+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
46+
47+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets';
48+
mkdir($targetPluginDir, 0755, true);
49+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
50+
51+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets-flag';
52+
mkdir($targetPluginDir, 0755, true);
53+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
54+
55+
// Create test packages configuration
56+
$this->deployer->config->set('packages', [
57+
[
58+
'path' => '{{mu-plugins/dir}}/test-plugin-with-assets',
59+
'assets' => true
60+
],
61+
[
62+
'path' => '{{mu-plugins/dir}}/test-plugin-without-assets',
63+
'assets' => false
64+
],
65+
[
66+
'path' => '{{mu-plugins/dir}}/test-plugin-without-assets-flag'
67+
// No assets flag
68+
]
69+
]);
70+
71+
// Mock npm install command
72+
$this->mockCommands([
73+
'npm install' => function ($host, $command) {
74+
if(str_contains($command, 'test-plugin-without-assets') || str_contains($command, 'test-plugin-without-assets-flag')) {
75+
throw new RuntimeException('npm install called for plugin without assets/package.json');
76+
}
77+
return 'npm install completed.';
78+
},
79+
], 'testremote');
80+
81+
// Run the task
82+
$result = $this->dep('packages:assets:vendors');
83+
$this->assertEquals(0, $result);
84+
}
85+
86+
public function testPackagesAssetsBuildOnlyRunsWhenAssetsIsTrue(): void
87+
{
88+
// Copy test plugin from fixtures to local
89+
$fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin');
90+
91+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-assets';
92+
mkdir($targetPluginDir, 0755, true);
93+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
94+
95+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets';
96+
mkdir($targetPluginDir, 0755, true);
97+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
98+
99+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets-flag';
100+
mkdir($targetPluginDir, 0755, true);
101+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
102+
103+
// Create test packages configuration
104+
$this->deployer->config->set('packages', [
105+
[
106+
'path' => '{{mu-plugins/dir}}/test-plugin-with-assets',
107+
'assets' => true
108+
],
109+
[
110+
'path' => '{{mu-plugins/dir}}/test-plugin-without-assets',
111+
'assets' => false
112+
],
113+
[
114+
'path' => '{{mu-plugins/dir}}/test-plugin-without-assets-flag'
115+
// No assets flag
116+
]
117+
]);
118+
119+
// Mock npm build command
120+
$this->mockCommands([
121+
'npm run-script build' => function ($host, $command) {
122+
if(str_contains($command, 'test-plugin-without-assets') || str_contains($command, 'test-plugin-without-assets-flag')) {
123+
throw new RuntimeException('npm build called for plugin without assets/package.json');
124+
}
125+
return 'npm build completed.';
126+
},
127+
], 'testremote');
128+
129+
// Run the task
130+
$result = $this->dep('packages:assets:build');
131+
$this->assertEquals(0, $result);
132+
}
133+
134+
public function testPackagesVendorsOnlyRunsWhenVendorsIsTrue(): void
135+
{
136+
// Copy test plugin from fixtures to local
137+
$fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin');
138+
139+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-vendors';
140+
mkdir($targetPluginDir, 0755, true);
141+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
142+
143+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-vendors';
144+
mkdir($targetPluginDir, 0755, true);
145+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
146+
147+
$targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-vendors-flag';
148+
mkdir($targetPluginDir, 0755, true);
149+
$this->copyDirectory($fixturePluginDir, $targetPluginDir);
150+
151+
// Create test packages configuration
152+
$this->deployer->config->set('packages', [
153+
[
154+
'path' => '{{mu-plugins/dir}}/test-plugin-with-vendors',
155+
'vendors' => true
156+
],
157+
[
158+
'path' => '{{mu-plugins/dir}}/test-plugin-without-vendors',
159+
'vendors' => false
160+
],
161+
[
162+
'path' => '{{mu-plugins/dir}}/test-plugin-without-vendors-flag'
163+
// No assets flag
164+
]
165+
]);
166+
167+
// Mock npm install command
168+
$this->mockCommands([
169+
'composer install' => function ($host, $command) {
170+
if(str_contains($command, 'test-plugin-without-vendors') || str_contains($command, 'test-plugin-without-vendors-flag')) {
171+
throw new RuntimeException('composer install called for plugin without vendors');
172+
}
173+
return 'npm install completed.';
174+
},
175+
], 'testremote');
176+
177+
// Run the task
178+
$result = $this->dep('packages:vendors');
179+
$this->assertEquals(0, $result);
180+
}
181+
}

0 commit comments

Comments
 (0)