Skip to content

Commit 01f7b65

Browse files
committed
feat: add --ymir-file option so you can select a configuration file
1 parent ae9bc3c commit 01f7b65

4 files changed

Lines changed: 122 additions & 42 deletions

File tree

config/services.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ parameters:
44
build_artifact_path: '%hidden_directory%/build.zip'
55
cli_configuration_filepath: '%home_directory%/.ymir/config.json'
66
hidden_directory: '%working_directory%/.ymir'
7-
project_configuration_filepath: '%working_directory%/ymir.yml'
87
stub_directory: '%application_directory%/stubs'
98
uploads_directory: '%hidden_directory%/uploads'
109
version: '1.32.4'
@@ -57,14 +56,6 @@ services:
5756
arguments:
5857
$configurationFilePath: '%cli_configuration_filepath%'
5958

60-
Ymir\Cli\EventDispatcher\AutowiredEventDispatcher:
61-
arguments:
62-
- !tagged subscriber
63-
64-
Ymir\Cli\ProjectConfiguration\ProjectConfiguration:
65-
arguments:
66-
$configurationFilePath: '%project_configuration_filepath%'
67-
6859
Ymir\Cli\Command\Project\BuildProjectCommand:
6960
arguments:
7061
$buildSteps:
@@ -99,3 +90,7 @@ services:
9990
arguments:
10091
$deploymentSteps:
10192
- '@Ymir\Cli\Deployment\StartAndMonitorDeploymentStep'
93+
94+
Ymir\Cli\EventDispatcher\AutowiredEventDispatcher:
95+
arguments:
96+
- !tagged subscriber

src/Application.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace Ymir\Cli;
1515

1616
use Symfony\Component\Console\Application as BaseApplication;
17+
use Symfony\Component\Console\Input\InputDefinition;
18+
use Symfony\Component\Console\Input\InputOption;
1719
use Symfony\Component\Console\Output\OutputInterface;
1820
use Ymir\Cli\Exception\CommandCancelledException;
1921

@@ -42,4 +44,16 @@ public function renderThrowable(\Throwable $exception, OutputInterface $output):
4244

4345
parent::renderThrowable($exception, $output);
4446
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function getDefaultInputDefinition(): InputDefinition
52+
{
53+
$definition = parent::getDefaultInputDefinition();
54+
55+
$definition->addOption(new InputOption('ymir-file', null, InputOption::VALUE_OPTIONAL, 'Path to Ymir project configuration file', 'ymir.yml'));
56+
57+
return $definition;
58+
}
4559
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\EventListener;
15+
16+
use Symfony\Component\Console\ConsoleEvents;
17+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
18+
use Symfony\Component\Console\Exception\RuntimeException;
19+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20+
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
21+
22+
class LoadProjectConfigurationSubscriber implements EventSubscriberInterface
23+
{
24+
/**
25+
* The Ymir project configuration.
26+
*
27+
* @var ProjectConfiguration
28+
*/
29+
private $projectConfiguration;
30+
31+
/**
32+
* Constructor.
33+
*/
34+
public function __construct(ProjectConfiguration $projectConfiguration)
35+
{
36+
$this->projectConfiguration = $projectConfiguration;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public static function getSubscribedEvents()
43+
{
44+
return [
45+
ConsoleEvents::COMMAND => 'onConsoleCommand',
46+
];
47+
}
48+
49+
/**
50+
* Load the Ymir project configuration.
51+
*/
52+
public function onConsoleCommand(ConsoleCommandEvent $event)
53+
{
54+
$configurationFilePath = $event->getInput()->getOption('ymir-file');
55+
56+
if (!is_string($configurationFilePath)) {
57+
throw new RuntimeException('The "--ymir-file" option must be a string value');
58+
}
59+
60+
$this->projectConfiguration->loadConfiguration($configurationFilePath);
61+
}
62+
}

src/ProjectConfiguration/ProjectConfiguration.php

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ProjectConfiguration implements Arrayable
3131
private $configuration;
3232

3333
/**
34-
* The path to the configuration file.
34+
* The path to the Ymir project configuration file.
3535
*
3636
* @var string
3737
*/
@@ -47,11 +47,11 @@ class ProjectConfiguration implements Arrayable
4747
/**
4848
* Constructor.
4949
*/
50-
public function __construct(string $configurationFilePath, Filesystem $filesystem)
50+
public function __construct(Filesystem $filesystem, string $configurationFilePath = '')
5151
{
52-
$this->configurationFilePath = $configurationFilePath;
5352
$this->filesystem = $filesystem;
54-
$this->configuration = $this->load($configurationFilePath);
53+
54+
$this->loadConfiguration($configurationFilePath);
5555
}
5656

5757
/**
@@ -115,8 +115,12 @@ public function createNew(Collection $project, array $environments, string $type
115115
*/
116116
public function delete()
117117
{
118+
if ($this->exists()) {
119+
$this->filesystem->remove($this->configurationFilePath);
120+
}
121+
118122
$this->configuration = [];
119-
$this->filesystem->remove($this->configurationFilePath);
123+
$this->configurationFilePath = '';
120124
}
121125

122126
/**
@@ -130,11 +134,11 @@ public function deleteEnvironment(string $environment)
130134
}
131135

132136
/**
133-
* Checks if the project configuration file exists.
137+
* Checks if the Ymir project configuration file exists.
134138
*/
135139
public function exists(): bool
136140
{
137-
return $this->filesystem->exists($this->configurationFilePath);
141+
return !empty($this->configurationFilePath) && $this->filesystem->exists($this->configurationFilePath);
138142
}
139143

140144
/**
@@ -143,7 +147,7 @@ public function exists(): bool
143147
public function getEnvironment(string $environment): array
144148
{
145149
if (!$this->hasEnvironment($environment)) {
146-
throw new InvalidArgumentException(sprintf('Environment "%s" not found in ymir.yml file', $environment));
150+
throw new InvalidArgumentException(sprintf('Environment "%s" not found in Ymir project configuration file', $environment));
147151
}
148152

149153
return (array) $this->configuration['environments'][$environment];
@@ -163,7 +167,7 @@ public function getEnvironments(): array
163167
public function getProjectId(): int
164168
{
165169
if (empty($this->configuration['id'])) {
166-
throw new RuntimeException('No "id" found in ymir.yml file');
170+
throw new RuntimeException('No "id" found in Ymir project configuration file');
167171
}
168172

169173
return (int) $this->configuration['id'];
@@ -175,7 +179,7 @@ public function getProjectId(): int
175179
public function getProjectName(): string
176180
{
177181
if (empty($this->configuration['name'])) {
178-
throw new RuntimeException('No "name" found in ymir.yml file');
182+
throw new RuntimeException('No "name" found in Ymir project configuration file');
179183
}
180184

181185
return (string) $this->configuration['name'];
@@ -187,7 +191,7 @@ public function getProjectName(): string
187191
public function getProjectType(): string
188192
{
189193
if (empty($this->configuration['type'])) {
190-
throw new RuntimeException('No "type" found in ymir.yml file');
194+
throw new RuntimeException('No "type" found in Ymir project configuration file');
191195
}
192196

193197
return (string) $this->configuration['type'];
@@ -201,6 +205,25 @@ public function hasEnvironment(string $environment): bool
201205
return array_key_exists($environment, $this->configuration['environments']);
202206
}
203207

208+
/**
209+
* Load the given Ymir project configuration file.
210+
*/
211+
public function loadConfiguration(string $configurationFilePath)
212+
{
213+
$configuration = [];
214+
215+
if ($this->filesystem->exists($configurationFilePath)) {
216+
$configuration = Yaml::parse((string) file_get_contents($configurationFilePath));
217+
}
218+
219+
if (!empty($configuration) && !is_array($configuration)) {
220+
throw new RuntimeException('Error parsing Ymir project configuration file');
221+
}
222+
223+
$this->configuration = $configuration;
224+
$this->configurationFilePath = $configurationFilePath;
225+
}
226+
204227
/**
205228
* {@inheritdoc}
206229
*/
@@ -215,41 +238,27 @@ public function toArray()
215238
public function validate()
216239
{
217240
if (!$this->exists()) {
218-
throw new RuntimeException(sprintf('No Ymir project found in the current directory. You can initialize one with the "%s" command.', InitializeProjectCommand::ALIAS));
241+
throw new RuntimeException(sprintf('No Ymir project configuration file found. You can create one by initializing a project with the "%s" command.', InitializeProjectCommand::ALIAS));
219242
} elseif (empty($this->configuration['id'])) {
220-
throw new RuntimeException('The ymir.yml file must have an "id"');
243+
throw new RuntimeException('The Ymir project configuration file must have an "id"');
221244
} elseif (empty($this->configuration['environments'])) {
222-
throw new RuntimeException('The ymir.yml file must have at least one environment');
245+
throw new RuntimeException('The Ymir project configuration file must have at least one environment');
223246
} elseif (empty($this->configuration['type'])) {
224-
throw new RuntimeException('The ymir.yml file must have a "type"');
247+
throw new RuntimeException('The Ymir project configuration file must have a "type"');
225248
} elseif (!in_array($this->configuration['type'], ['bedrock', 'wordpress'])) {
226249
throw new RuntimeException('The allowed project "type" are "bedrock" or "wordpress"');
227250
}
228251
}
229252

230-
/**
231-
* Load the options from the configuration file.
232-
*/
233-
private function load(string $configurationFilePath): array
234-
{
235-
$configuration = [];
236-
237-
if ($this->filesystem->exists($configurationFilePath)) {
238-
$configuration = Yaml::parse((string) file_get_contents($configurationFilePath));
239-
}
240-
241-
if (!empty($configuration) && !is_array($configuration)) {
242-
throw new RuntimeException('Error parsing ymir.yml file');
243-
}
244-
245-
return $configuration;
246-
}
247-
248253
/**
249254
* Save the configuration options to the configuration file.
250255
*/
251256
private function save()
252257
{
258+
if (empty($this->configurationFilePath)) {
259+
throw new RuntimeException('No Ymir project configuration file path set');
260+
}
261+
253262
$this->filesystem->dumpFile($this->configurationFilePath, str_replace('!!float 8', '8.0', Yaml::dump($this->configuration, 20, 2, Yaml::DUMP_NULL_AS_TILDE)));
254263
}
255264
}

0 commit comments

Comments
 (0)