|
| 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 <[email protected]> |
| 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\Build; |
| 15 | + |
| 16 | +use Symfony\Component\Filesystem\Filesystem; |
| 17 | +use Symfony\Component\Process\Exception\RuntimeException; |
| 18 | +use Tightenco\Collect\Support\Arr; |
| 19 | +use Ymir\Cli\Docker; |
| 20 | +use Ymir\Cli\ProjectConfiguration; |
| 21 | + |
| 22 | +class BuildContainerImageStep implements BuildStepInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * The build directory where the project files are copied to. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $buildDirectory; |
| 30 | + |
| 31 | + /** |
| 32 | + * The file system. |
| 33 | + * |
| 34 | + * @var Filesystem |
| 35 | + */ |
| 36 | + private $filesystem; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor. |
| 40 | + */ |
| 41 | + public function __construct(string $buildDirectory, Filesystem $filesystem) |
| 42 | + { |
| 43 | + $this->buildDirectory = rtrim($buildDirectory, '/'); |
| 44 | + $this->filesystem = $filesystem; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public function getDescription(): string |
| 51 | + { |
| 52 | + return 'Building container image'; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function isNeeded(string $environment, ProjectConfiguration $projectConfiguration): bool |
| 59 | + { |
| 60 | + return 'image' === Arr::get((array) $projectConfiguration->getEnvironment($environment), 'deployment'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function perform(string $environment, ProjectConfiguration $projectConfiguration) |
| 67 | + { |
| 68 | + $file = 'Dockerfile'; |
| 69 | + |
| 70 | + if ($this->filesystem->exists($this->buildDirectory.sprintf('/.%s.Dockerfile', $environment))) { |
| 71 | + $file = sprintf('.%s.Dockerfile', $environment); |
| 72 | + } elseif ($this->filesystem->exists($this->buildDirectory.sprintf('/%s.Dockerfile', $environment))) { |
| 73 | + $file = sprintf('%s.Dockerfile', $environment); |
| 74 | + } |
| 75 | + |
| 76 | + if (!$this->filesystem->exists($this->buildDirectory.'/'.$file)) { |
| 77 | + throw new RuntimeException('Unable to find a Dockerfile to build the container image'); |
| 78 | + } |
| 79 | + |
| 80 | + Docker::build($file, sprintf('%s:%s', $projectConfiguration->getProjectName(), $environment), $this->buildDirectory); |
| 81 | + } |
| 82 | +} |
0 commit comments