Skip to content

Commit 29b7f61

Browse files
committed
feat: add option to include files into the build artifact
1 parent f35b5c2 commit 29b7f61

File tree

3 files changed

+101
-47
lines changed

3 files changed

+101
-47
lines changed

src/Build/CompressBuildFilesStep.php

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

1616
use Symfony\Component\Finder\Finder;
1717
use Symfony\Component\Finder\SplFileInfo;
18+
use Tightenco\Collect\Support\Arr;
1819
use Ymir\Cli\ProjectConfiguration;
1920

2021
class CompressBuildFilesStep implements BuildStepInterface
@@ -57,12 +58,18 @@ public function perform(string $environment, ProjectConfiguration $projectConfig
5758
{
5859
$archive = new \ZipArchive();
5960
$archive->open($this->buildArtifactPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
61+
$environment = (array) $projectConfiguration->getEnvironment($environment);
6062

61-
foreach ($this->getWordPressCoreFiles($projectConfiguration->getProjectType()) as $file) {
62-
$this->addFileToArchive($archive, $file);
63+
$files = Finder::create()
64+
->append($this->getRequiredFiles())
65+
->append($this->getRequiredFileTypes())
66+
->append($this->getWordPressCoreFiles($projectConfiguration->getProjectType()));
67+
68+
if (Arr::has($environment, 'build.include')) {
69+
$files->append($this->getIncludedFiles(Arr::get($environment, 'build.include')));
6370
}
6471

65-
foreach ($this->getPhpFiles() as $file) {
72+
foreach ($files as $file) {
6673
$this->addFileToArchive($archive, $file);
6774
}
6875

@@ -84,30 +91,54 @@ private function addFileToArchive(\ZipArchive $archive, SplFileInfo $file)
8491
}
8592

8693
/**
87-
* Get the Finder object for finding all the PHP files.
94+
* Get base Finder object.
8895
*/
89-
private function getPhpFiles(): Finder
96+
private function getBaseFinder(): Finder
9097
{
9198
return Finder::create()
9299
->in($this->buildDirectory)
93-
->files()
94-
->name(['*.php']);
100+
->files();
101+
}
102+
103+
/**
104+
* Get files from "include" node.
105+
*/
106+
private function getIncludedFiles(array $paths): Finder
107+
{
108+
return $this->getBaseFinder()
109+
->path($paths);
110+
}
111+
112+
/**
113+
* Get the Finder object for finding all the required files.
114+
*/
115+
private function getRequiredFiles(): Finder
116+
{
117+
return $this->getBaseFinder()
118+
->path(['/^wp-cli\.yml/']);
119+
}
120+
121+
/**
122+
* Get the Finder object for finding all the required file types.
123+
*/
124+
private function getRequiredFileTypes(): Finder
125+
{
126+
return $this->getBaseFinder()
127+
->name(['*.mo', '*.php']);
95128
}
96129

97130
/**
98131
* Get the Finder object for finding all the WordPress core files.
99132
*/
100133
private function getWordPressCoreFiles(string $projectType): Finder
101134
{
102-
return Finder::create()
103-
->in($this->buildDirectory)
135+
return $this->getBaseFinder()
104136
->path(collect(['wp-includes\/', 'wp-admin\/'])->map(function (string $path) use ($projectType) {
105137
if ('bedrock' === $projectType) {
106138
$path = 'web\/wp\/'.$path;
107139
}
108140

109141
return sprintf('/^%s/', $path);
110-
})->add('/^bin\//')->all())
111-
->files();
142+
})->add('/^bin\//')->all());
112143
}
113144
}

src/Build/CopyWordPressFilesStep.php

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Filesystem\Filesystem;
1717
use Symfony\Component\Finder\Finder;
1818
use Symfony\Component\Finder\SplFileInfo;
19+
use Tightenco\Collect\Support\Arr;
1920
use Ymir\Cli\ProjectConfiguration;
2021

2122
class CopyWordPressFilesStep implements BuildStepInterface
@@ -51,37 +52,6 @@ public function __construct(string $buildDirectory, Filesystem $filesystem, stri
5152
$this->projectDirectory = rtrim($projectDirectory, '/');
5253
}
5354

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-
8555
/**
8656
* {@inheritdoc}
8757
*/
@@ -104,6 +74,10 @@ public function perform(string $environment, ProjectConfiguration $projectConfig
10474
foreach ($this->getProjectFiles($projectConfiguration->getProjectType()) as $file) {
10575
$this->copyFile($file);
10676
}
77+
78+
foreach ($this->getIncludedFiles(Arr::get((array) $projectConfiguration->getEnvironment($environment), 'build.include', [])) as $file) {
79+
$this->copyFile($file);
80+
}
10781
}
10882

10983
/**
@@ -118,13 +92,53 @@ private function copyFile(SplFileInfo $file)
11892
}
11993
}
12094

95+
/**
96+
* Get base Finder object.
97+
*/
98+
private function getBaseFinder(): Finder
99+
{
100+
return Finder::create()
101+
->in($this->projectDirectory)
102+
->files();
103+
}
104+
105+
/**
106+
* List of files we need to append manually because they're in the default Bedrock .gitignore file.
107+
*/
108+
private function getBedrockFilesToAppend(): array
109+
{
110+
// Need the .env file for WP-CLI to work during the build
111+
$files = [new SplFileInfo($this->projectDirectory.'/.env', $this->projectDirectory, '/.env')];
112+
113+
// Finder can't seem to honor the .gitignore path ignoring child folders in the mu-plugins
114+
// folder while keeping the files at the root of the mu-plugins folder.
115+
$finder = $this->getBaseFinder()
116+
->path('/^web\/app\/mu-plugins/')
117+
->depth('== 3');
118+
119+
foreach ($finder as $file) {
120+
$files[] = $file;
121+
}
122+
123+
return $files;
124+
}
125+
126+
/**
127+
* Get files from "include" node.
128+
*/
129+
private function getIncludedFiles(array $paths): Finder
130+
{
131+
return $this->getBaseFinder()
132+
->path($paths)
133+
->followLinks();
134+
}
135+
121136
/**
122137
* Get the Finder object for finding all the project files.
123138
*/
124139
private function getProjectFiles(string $projectType): Finder
125140
{
126-
$finder = Finder::create()
127-
->in($this->projectDirectory)
141+
$finder = $this->getBaseFinder()
128142
->notName(['ymir.yml'])
129143
->followLinks();
130144

src/Build/ExecuteBuildCommandsStep.php

Lines changed: 12 additions & 3 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\Process\Process;
18+
use Tightenco\Collect\Support\Arr;
1819
use Ymir\Cli\ProjectConfiguration;
1920

2021
class ExecuteBuildCommandsStep implements BuildStepInterface
@@ -47,13 +48,21 @@ public function getDescription(): string
4748
*/
4849
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
4950
{
50-
$environment = $projectConfiguration->getEnvironment($environment);
51+
$environment = (array) $projectConfiguration->getEnvironment($environment);
5152

52-
if (empty($environment['build']) || !is_array($environment)) {
53+
if (empty($environment['build'])) {
5354
return;
5455
}
5556

56-
foreach ($environment['build'] as $command) {
57+
$commands = [];
58+
59+
if (Arr::has($environment, 'build.commands')) {
60+
$commands = (array) Arr::get($environment, 'build.commands');
61+
} elseif (!Arr::has($environment, 'build.include')) {
62+
$commands = (array) $environment['build'];
63+
}
64+
65+
foreach ($commands as $command) {
5766
$this->runCommand($command);
5867
}
5968
}

0 commit comments

Comments
 (0)