Skip to content

Commit eb88c79

Browse files
committed
feat: add support for bedrock projects
1 parent 09e6fab commit eb88c79

12 files changed

+106
-65
lines changed

config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ services:
5252
arguments:
5353
$buildSteps:
5454
- '@Ymir\Cli\Build\CopyWordPressFilesStep'
55+
- '@Ymir\Cli\Build\ExecuteBuildCommandsStep'
5556
- '@Ymir\Cli\Build\DownloadWpCliStep'
5657
- '@Ymir\Cli\Build\EnsurePluginIsInstalledStep'
5758
- '@Ymir\Cli\Build\CopyMustUsePluginStep'
58-
- '@Ymir\Cli\Build\ExecuteBuildCommandsStep'
5959
- '@Ymir\Cli\Build\ModifyWordPressConfigurationStep'
6060
- '@Ymir\Cli\Build\ExtractAssetFilesStep'
6161
- '@Ymir\Cli\Build\CompressBuildFilesStep'

src/Build/BuildStepInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Ymir\Cli\Build;
1515

16+
use Ymir\Cli\ProjectConfiguration;
17+
1618
interface BuildStepInterface
1719
{
1820
/**
@@ -23,5 +25,5 @@ public function getDescription(): string;
2325
/**
2426
* Perform the build step.
2527
*/
26-
public function perform(string $environment);
28+
public function perform(string $environment, ProjectConfiguration $projectConfiguration);
2729
}

src/Build/CompressBuildFilesStep.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Exception\RuntimeException;
1717
use Symfony\Component\Finder\Finder;
1818
use Symfony\Component\Finder\SplFileInfo;
19+
use Ymir\Cli\ProjectConfiguration;
1920

2021
class CompressBuildFilesStep implements BuildStepInterface
2122
{
@@ -53,7 +54,7 @@ public function getDescription(): string
5354
/**
5455
* {@inheritdoc}
5556
*/
56-
public function perform(string $environment)
57+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
5758
{
5859
$archive = new \ZipArchive();
5960
$archive->open($this->buildArtifactPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

src/Build/CopyMustUsePluginStep.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Symfony\Component\Console\Exception\RuntimeException;
1717
use Symfony\Component\Filesystem\Filesystem;
18+
use Ymir\Cli\ProjectConfiguration;
1819

1920
class CopyMustUsePluginStep implements BuildStepInterface
2021
{
@@ -60,7 +61,7 @@ public function getDescription(): string
6061
/**
6162
* {@inheritdoc}
6263
*/
63-
public function perform(string $environment)
64+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
6465
{
6566
$mupluginStub = 'activate-ymir-plugin.php';
6667
$mupluginStubPath = $this->stubDirectory.'/'.$mupluginStub;
@@ -69,7 +70,7 @@ public function perform(string $environment)
6970
throw new RuntimeException(sprintf('Cannot find "%s" stub file', $mupluginStub));
7071
}
7172

72-
$mupluginsDirectory = $this->buildDirectory.'/wp-content/mu-plugins';
73+
$mupluginsDirectory = 'bedrock' === $projectConfiguration->getProjectType() ? $this->buildDirectory.'/web/app/mu-plugins' : $this->buildDirectory.'/wp-content/mu-plugins';
7374

7475
if (!$this->filesystem->exists($mupluginsDirectory)) {
7576
$this->filesystem->mkdir($mupluginsDirectory);

src/Build/CopyWordPressFilesStep.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ class CopyWordPressFilesStep implements BuildStepInterface
3434
*/
3535
private $filesystem;
3636

37-
/**
38-
* The Ymir project configuration.
39-
*
40-
* @var ProjectConfiguration
41-
*/
42-
private $projectConfiguration;
43-
4437
/**
4538
* The project directory where the project files are copied from.
4639
*
@@ -51,14 +44,44 @@ class CopyWordPressFilesStep implements BuildStepInterface
5144
/**
5245
* Constructor.
5346
*/
54-
public function __construct(string $buildDirectory, ProjectConfiguration $projectConfiguration, string $projectDirectory, Filesystem $filesystem)
47+
public function __construct(string $buildDirectory, Filesystem $filesystem, string $projectDirectory)
5548
{
5649
$this->buildDirectory = rtrim($buildDirectory, '/');
5750
$this->filesystem = $filesystem;
58-
$this->projectConfiguration = $projectConfiguration;
5951
$this->projectDirectory = rtrim($projectDirectory, '/');
6052
}
6153

54+
/**
55+
* List of files we need to append manually because they're in the default Bedrock .gitignore file.
56+
*/
57+
public function getBedrockFilesToAppend(): array
58+
{
59+
// Need the .env file for WP-CLI to work during the build
60+
$files = [new SplFileInfo($this->projectDirectory.'/.env', $this->projectDirectory, '/.env')];
61+
62+
// Finder can't seem to honor the .gitignore path ignoring child folders in the mu-plugins
63+
// folder while keeping the files at the root of the mu-plugins folder.
64+
$finder = Finder::create()->in($this->projectDirectory)
65+
->path('/^web\/app\/mu-plugins/')
66+
->depth('== 3')
67+
->files();
68+
69+
foreach ($finder as $file) {
70+
$files[] = $file;
71+
}
72+
73+
// TODO: Remove once we can install with Composer
74+
$finder = Finder::create()->in($this->projectDirectory)
75+
->path('/^web\/app\/plugins\/ymir/')
76+
->files();
77+
78+
foreach ($finder as $file) {
79+
$files[] = $file;
80+
}
81+
82+
return $files;
83+
}
84+
6285
/**
6386
* {@inheritdoc}
6487
*/
@@ -70,15 +93,15 @@ public function getDescription(): string
7093
/**
7194
* {@inheritdoc}
7295
*/
73-
public function perform(string $environment)
96+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
7497
{
7598
if ($this->filesystem->exists($this->buildDirectory)) {
7699
$this->filesystem->remove($this->buildDirectory);
77100
}
78101

79102
$this->filesystem->mkdir($this->buildDirectory, 0755);
80103

81-
foreach ($this->getProjectFiles() as $file) {
104+
foreach ($this->getProjectFiles($projectConfiguration->getProjectType()) as $file) {
82105
$this->copyFile($file);
83106
}
84107
}
@@ -98,7 +121,7 @@ private function copyFile(SplFileInfo $file)
98121
/**
99122
* Get the Finder object for finding all the project files.
100123
*/
101-
private function getProjectFiles(): Finder
124+
private function getProjectFiles(string $projectType): Finder
102125
{
103126
$finder = Finder::create()
104127
->in($this->projectDirectory)
@@ -107,14 +130,16 @@ private function getProjectFiles(): Finder
107130
->followLinks()
108131
->ignoreVcs(true)
109132
->ignoreDotFiles(false);
110-
$projectType = $this->projectConfiguration->getProjectType();
111133

112134
if (is_readable($this->projectDirectory.'/.gitignore')) {
113135
$finder->ignoreVCSIgnored(true);
114136
}
115137

116138
if ('wordpress' === $projectType) {
117139
$finder->exclude('wp-content/uploads');
140+
} elseif ('bedrock' === $projectType) {
141+
$finder->exclude('web/app/uploads');
142+
$finder->append($this->getBedrockFilesToAppend());
118143
}
119144

120145
return $finder;

src/Build/DownloadWpCliStep.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ymir\Cli\Build;
1515

1616
use Symfony\Component\Filesystem\Filesystem;
17+
use Ymir\Cli\ProjectConfiguration;
1718

1819
class DownloadWpCliStep implements BuildStepInterface
1920
{
@@ -51,7 +52,7 @@ public function getDescription(): string
5152
/**
5253
* {@inheritdoc}
5354
*/
54-
public function perform(string $environment)
55+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
5556
{
5657
$wpCliPath = $this->binDirectory.'/wp';
5758

src/Build/EnsurePluginIsInstalledStep.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ymir\Cli\Build;
1515

1616
use Symfony\Component\Console\Exception\RuntimeException;
17+
use Ymir\Cli\ProjectConfiguration;
1718
use Ymir\Cli\WpCli;
1819

1920
class EnsurePluginIsInstalledStep implements BuildStepInterface
@@ -44,7 +45,7 @@ public function getDescription(): string
4445
/**
4546
* {@inheritdoc}
4647
*/
47-
public function perform(string $environment)
48+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
4849
{
4950
if (!WpCli::isPluginInstalled('ymir', $this->binDirectory, 'wp')) {
5051
throw new RuntimeException('Ymir plugin not found');

src/Build/ExecuteBuildCommandsStep.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,12 @@ class ExecuteBuildCommandsStep implements BuildStepInterface
2626
*/
2727
private $buildDirectory;
2828

29-
/**
30-
* The Ymir project configuration.
31-
*
32-
* @var ProjectConfiguration
33-
*/
34-
private $projectConfiguration;
35-
3629
/**
3730
* Constructor.
3831
*/
39-
public function __construct(string $buildDirectory, ProjectConfiguration $projectConfiguration)
32+
public function __construct(string $buildDirectory)
4033
{
4134
$this->buildDirectory = rtrim($buildDirectory, '/');
42-
$this->projectConfiguration = $projectConfiguration;
4335
}
4436

4537
/**
@@ -53,9 +45,9 @@ public function getDescription(): string
5345
/**
5446
* {@inheritdoc}
5547
*/
56-
public function perform(string $environment)
48+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
5749
{
58-
$environment = $this->projectConfiguration->getEnvironment($environment);
50+
$environment = $projectConfiguration->getEnvironment($environment);
5951

6052
if (empty($environment['build']) || !is_array($environment)) {
6153
return;

src/Build/ExtractAssetFilesStep.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,38 @@
1616
use Symfony\Component\Filesystem\Filesystem;
1717
use Symfony\Component\Finder\Finder;
1818
use Symfony\Component\Finder\SplFileInfo;
19+
use Ymir\Cli\ProjectConfiguration;
1920

2021
class ExtractAssetFilesStep implements BuildStepInterface
2122
{
2223
/**
23-
* The assets directory where the asset files are copied to.
24+
* The file system.
2425
*
25-
* @var string
26+
* @var Filesystem
2627
*/
27-
private $assetsDirectory;
28+
private $filesystem;
2829

2930
/**
3031
* The build directory where the asset files are extracted from.
3132
*
3233
* @var string
3334
*/
34-
private $buildDirectory;
35+
private $fromDirectory;
3536

3637
/**
37-
* The file system.
38+
* The assets directory where the asset files are copied to.
3839
*
39-
* @var Filesystem
40+
* @var string
4041
*/
41-
private $filesystem;
42+
private $toDirectory;
4243

4344
/**
4445
* Constructor.
4546
*/
4647
public function __construct(string $assetsDirectory, string $buildDirectory, Filesystem $filesystem)
4748
{
48-
$this->assetsDirectory = rtrim($assetsDirectory, '/');
49-
$this->buildDirectory = rtrim($buildDirectory, '/');
49+
$this->toDirectory = rtrim($assetsDirectory, '/');
50+
$this->fromDirectory = rtrim($buildDirectory, '/');
5051
$this->filesystem = $filesystem;
5152
}
5253

@@ -61,28 +62,34 @@ public function getDescription(): string
6162
/**
6263
* {@inheritdoc}
6364
*/
64-
public function perform(string $environment)
65+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
6566
{
66-
if ($this->filesystem->exists($this->assetsDirectory)) {
67-
$this->filesystem->remove($this->assetsDirectory);
67+
if ($this->filesystem->exists($this->toDirectory)) {
68+
$this->filesystem->remove($this->toDirectory);
6869
}
6970

70-
$this->filesystem->mkdir($this->buildDirectory, 0755);
71+
$this->filesystem->mkdir($this->toDirectory, 0755);
72+
73+
$fromDirectory = $this->fromDirectory;
74+
75+
if ('bedrock' === $projectConfiguration->getProjectType()) {
76+
$fromDirectory .= '/web';
77+
}
7178

72-
foreach ($this->getAssetFiles() as $file) {
79+
foreach ($this->getAssetFiles($fromDirectory) as $file) {
7380
$this->moveAssetFile($file);
7481
}
7582
}
7683

7784
/**
7885
* Get the asset files that we want to extract.
7986
*/
80-
private function getAssetFiles(): Finder
87+
private function getAssetFiles(string $fromDirectory): Finder
8188
{
8289
return Finder::create()
83-
->in($this->buildDirectory)
90+
->in($fromDirectory)
8491
->files()
85-
->notName(['.htaccess', '*.php'])
92+
->notName(['*.php'])
8693
->followLinks()
8794
->ignoreVcs(true)
8895
->ignoreDotFiles(true);
@@ -97,6 +104,6 @@ private function moveAssetFile(SplFileInfo $file)
97104
return;
98105
}
99106

100-
$this->filesystem->copy($file->getRealPath(), $this->assetsDirectory.'/'.$file->getRelativePathname());
107+
$this->filesystem->copy($file->getRealPath(), $this->toDirectory.'/'.$file->getRelativePathname());
101108
}
102109
}

0 commit comments

Comments
 (0)