Skip to content

Commit a38b552

Browse files
committed
feat: add a new build step to clean up the build directory
fixes #16
1 parent 15bfdf8 commit a38b552

15 files changed

Lines changed: 347 additions & 71 deletions

src/Project/Build/BuildZipArchiveStep.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Ymir\Cli\Project\Build;
1515

16-
use Symfony\Component\Finder\Finder;
1716
use Symfony\Component\Finder\SplFileInfo;
1817
use Ymir\Cli\Exception\Project\BuildFailedException;
1918
use Ymir\Cli\Project\EnvironmentConfiguration;
@@ -64,7 +63,7 @@ public function perform(EnvironmentConfiguration $environmentConfiguration, Proj
6463
$totalSize = 0;
6564

6665
if (!empty($includePaths)) {
67-
$files->append($this->getIncludedFiles($includePaths));
66+
$files->append($projectConfiguration->getProjectType()->getIncludedFiles($this->buildDirectory, $includePaths));
6867
}
6968

7069
foreach ($files as $file) {
@@ -92,15 +91,4 @@ private function addFileToArchive(\ZipArchive $archive, SplFileInfo $file): void
9291
$archive->addFile($file->getRealPath(), $relativePathName);
9392
$archive->setExternalAttributesName($relativePathName, \ZipArchive::OPSYS_UNIX, (33060 & 0xFFFF) << 16);
9493
}
95-
96-
/**
97-
* Get files from "include" node.
98-
*/
99-
private function getIncludedFiles(array $paths): Finder
100-
{
101-
return Finder::create()
102-
->in($this->buildDirectory)
103-
->files()
104-
->path($paths);
105-
}
10694
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Project\Build;
15+
16+
use Symfony\Component\Filesystem\Filesystem;
17+
use Symfony\Component\Finder\Finder;
18+
use Ymir\Cli\Project\EnvironmentConfiguration;
19+
use Ymir\Cli\Project\ProjectConfiguration;
20+
21+
class CleanupBuildStep implements BuildStepInterface
22+
{
23+
/**
24+
* The build directory where the project files are copied to.
25+
*
26+
* @var string
27+
*/
28+
private $buildDirectory;
29+
30+
/**
31+
* The file system.
32+
*
33+
* @var Filesystem
34+
*/
35+
private $filesystem;
36+
37+
/**
38+
* Constructor.
39+
*/
40+
public function __construct(string $buildDirectory, Filesystem $filesystem)
41+
{
42+
$this->buildDirectory = rtrim($buildDirectory, '/');
43+
$this->filesystem = $filesystem;
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
public function getDescription(): string
50+
{
51+
return 'Cleaning up build';
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function perform(EnvironmentConfiguration $environmentConfiguration, ProjectConfiguration $projectConfiguration): void
58+
{
59+
$filesToDelete = $projectConfiguration->getProjectType()->getExcludedFiles($this->buildDirectory)
60+
->append($this->getConfiguredExcludedFiles($environmentConfiguration))
61+
->notPath($environmentConfiguration->getBuildIncludePaths());
62+
63+
$this->filesystem->remove($filesToDelete);
64+
}
65+
66+
private function getConfiguredExcludedFiles(EnvironmentConfiguration $environmentConfiguration): iterable
67+
{
68+
$excludePaths = $environmentConfiguration->getBuildExcludePaths();
69+
70+
if (empty($excludePaths)) {
71+
return [];
72+
}
73+
74+
return Finder::create()
75+
->in($this->buildDirectory)
76+
->ignoreDotFiles(false)
77+
->ignoreVCS(false)
78+
->path($excludePaths)
79+
->notPath($environmentConfiguration->getBuildIncludePaths());
80+
}
81+
}

src/Project/Build/CopyProjectFilesStep.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Ymir\Cli\Project\Build;
1515

1616
use Symfony\Component\Filesystem\Filesystem;
17-
use Symfony\Component\Finder\Finder;
1817
use Symfony\Component\Finder\SplFileInfo;
1918
use Ymir\Cli\Project\EnvironmentConfiguration;
2019
use Ymir\Cli\Project\ProjectConfiguration;
@@ -78,7 +77,7 @@ public function perform(EnvironmentConfiguration $environmentConfiguration, Proj
7877
}
7978

8079
if (!empty($includePaths)) {
81-
$files->append($this->getIncludedFiles($includePaths));
80+
$files->append($projectConfiguration->getProjectType()->getIncludedFiles($this->projectDirectory, $includePaths));
8281
}
8382

8483
foreach ($files as $file) {
@@ -103,43 +102,6 @@ private function copyFile(SplFileInfo $file): void
103102
}
104103
}
105104

106-
/**
107-
* Get base Finder object.
108-
*/
109-
private function getBaseFinder(): Finder
110-
{
111-
return Finder::create()
112-
->in($this->projectDirectory)
113-
->files();
114-
}
115-
116-
/**
117-
* Get the Finder object for finding the all the files from "build.include" configuration node.
118-
*/
119-
private function getIncludedFiles(array $patterns): Finder
120-
{
121-
$patterns = collect($patterns)->map(function (string $pattern) {
122-
return '/'.ltrim($pattern, '/');
123-
});
124-
125-
$files = $patterns->map(function (string $pattern): string {
126-
return '/'.ltrim($pattern, '/');
127-
})->filter(function (string $pattern): bool {
128-
return is_file($this->projectDirectory.$pattern);
129-
})->map(function (string $pattern): \SplFileInfo {
130-
return $this->getSplFileInfo($pattern);
131-
});
132-
133-
$paths = $patterns->filter(function (string $pattern): bool {
134-
return !is_file($this->projectDirectory.$pattern);
135-
});
136-
137-
return $this->getBaseFinder()
138-
->path($paths->all())
139-
->append($files->all())
140-
->followLinks();
141-
}
142-
143105
/**
144106
* Get a SplFileInfo object for a project file.
145107
*/

src/Project/EnvironmentConfiguration.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ public function getBuildCommands(): array
6464
return $commands;
6565
}
6666

67+
/**
68+
* Get the build exclude paths for the environment.
69+
*/
70+
public function getBuildExcludePaths(): array
71+
{
72+
return $this->getPaths('build.exclude');
73+
}
74+
6775
/**
6876
* Get the build include paths for the environment.
6977
*/
7078
public function getBuildIncludePaths(): array
7179
{
72-
return (array) Arr::get($this->configuration, 'build.include', []);
80+
return $this->getPaths('build.include');
7381
}
7482

7583
/**
@@ -185,4 +193,16 @@ public function without(string ...$keys): self
185193
{
186194
return new self($this->name, array_diff_key($this->configuration, array_flip($keys)));
187195
}
196+
197+
/**
198+
* Get the paths for the given configuration key.
199+
*/
200+
private function getPaths(string $key): array
201+
{
202+
return collect(Arr::get($this->configuration, $key, []))
203+
->map(function (string $path): string {
204+
return ltrim($path, '/');
205+
})
206+
->all();
207+
}
188208
}

src/Project/Type/AbstractProjectType.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public function generateEnvironmentConfiguration(string $environment, array $bas
4848
/**
4949
* {@inheritDoc}
5050
*/
51+
public function getExcludedFiles(string $directory): Finder
52+
{
53+
$baseFinder = $this->getBaseFinder($directory)
54+
->ignoreDotFiles(false)
55+
->ignoreVCS(false);
56+
57+
return Finder::create()
58+
->append((clone $baseFinder)->path(['.git', 'node_modules']))
59+
->append((clone $baseFinder)->name(['*.ts', '*.tsx', '*.scss']));
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function getIncludedFiles(string $directory, array $paths): Finder
66+
{
67+
return $this->getBaseFinder($directory)
68+
->path($paths)
69+
->followLinks();
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
5175
public function getInitializationSteps(): array
5276
{
5377
return [
@@ -106,8 +130,7 @@ protected function generateEnvironmentConfigurationArray(string $environment, ar
106130
protected function getBaseFinder(string $directory): Finder
107131
{
108132
return Finder::create()
109-
->in($directory)
110-
->files();
133+
->in($directory);
111134
}
112135

113136
/**

src/Project/Type/BedrockProjectType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function getBuildSteps(): array
6767
Build\ExecuteBuildCommandsStep::class,
6868
Build\EnsureIntegrationIsInstalledStep::class,
6969
Build\WordPress\CopyMustUsePluginStep::class,
70+
Build\CleanupBuildStep::class,
7071
Build\ExtractAssetFilesStep::class,
7172
];
7273
}

src/Project/Type/ProjectTypeInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public function getAssetFiles(string $directory): Finder;
3838
*/
3939
public function getBuildSteps(): array;
4040

41+
/**
42+
* Get the Finder object for finding all the files that we want to exclude from the final build.
43+
*/
44+
public function getExcludedFiles(string $directory): Finder;
45+
46+
/**
47+
* Get the Finder object for finding all the files that we want to include in the final build.
48+
*/
49+
public function getIncludedFiles(string $directory, array $paths): Finder;
50+
4151
/**
4252
* Get the initialization steps when initializing the project type.
4353
*/

src/Project/Type/RadicleProjectType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function getBuildSteps(): array
6767
Build\ExecuteBuildCommandsStep::class,
6868
Build\EnsureIntegrationIsInstalledStep::class,
6969
Build\WordPress\CopyMustUsePluginStep::class,
70+
Build\CleanupBuildStep::class,
7071
Build\ExtractAssetFilesStep::class,
7172
];
7273
}
@@ -120,7 +121,7 @@ protected function generateEnvironmentConfigurationArray(string $environment, ar
120121
{
121122
return Arr::add(parent::generateEnvironmentConfigurationArray($environment, $baseConfiguration), 'build', [
122123
'COMPOSER_MIRROR_PATH_REPOS=1 composer install'.('production' === $environment ? ' --no-dev' : ''),
123-
'yarn install && yarn build && rm -rf node_modules',
124+
'yarn install && yarn build',
124125
]);
125126
}
126127

src/Project/Type/WordPressProjectType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getBuildSteps(): array
5959
Build\EnsureIntegrationIsInstalledStep::class,
6060
Build\WordPress\CopyMustUsePluginStep::class,
6161
Build\WordPress\ModifyWordPressConfigurationStep::class,
62+
Build\CleanupBuildStep::class,
6263
Build\ExtractAssetFilesStep::class,
6364
];
6465
}

0 commit comments

Comments
 (0)