Skip to content

Commit

Permalink
Standardize yaml writing and allow for it to be configurable (#215)
Browse files Browse the repository at this point in the history
* Standardize yaml writing and allow for it to be configurable
Resolves #214
  • Loading branch information
apotek authored Aug 12, 2024
1 parent a9433c0 commit 8a64c42
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Robo/Plugin/Commands/DevelopmentModeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function frontendDevEnable(string $siteDir = 'default', array $opts = ['y
'auto_reload' => true,
'cache' => false,
];
file_put_contents($this->devServicesPath, Yaml::dump($devServices));
$this->writeYaml($this->devServicesPath, $devServices);
$this->say("disabling render and dynamic_page_cache in settings.local.php.");
// https://github.com/consolidation/robo/issues/1059#issuecomment-967732068
$result = $this->collectionBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/Robo/Plugin/Commands/ToolingCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function updateRoboConfig(string $key, string $value): void
$roboConfigPath = "$this->cwd/robo.yml";
$roboConfig = Yaml::parse((string) file_get_contents($roboConfigPath));
$roboConfig[$key] = $value;
file_put_contents($roboConfigPath, Yaml::dump($roboConfig));
$this->writeYaml($roboConfigPath, $roboConfig);
}

/**
Expand Down
65 changes: 65 additions & 0 deletions src/Robo/Plugin/Traits/RoboConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,55 @@

use Robo\Robo;
use Robo\Exception\TaskException;
use Symfony\Component\Yaml\Yaml;
use Usher\Robo\Plugin\Enums\ConfigTypes;

/**
* Trait to provide access to Robo configuration.
*/
trait RoboConfigTrait
{
protected array $defaultYamlConf = [
'inline_level' => 10,
'indent' => 2,
'dump_bits' => Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE |
Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE |
Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK |
Yaml::DUMP_NUMERIC_KEY_AS_STRING |
Yaml::DUMP_OBJECT_AS_MAP,
];

/**
* Get the default YAML configuration.
*/
protected function getDefaultYamlConf(): array
{
return $this->defaultYamlConf;
}

/**
* Get an optional Robo configuration array.
*
* @param string $key
* The key of the configuration to load.
*
* @throws \Robo\Exception\TaskException
*/
protected function getOptionalRoboConfigArrayFor(string $key): mixed
{
$configValue = Robo::config()->get($key);
if (is_null($configValue)) {
$configValue = [];
} else {
$this->validateRoboConfigValueMatchesType(
configValue: $configValue,
expectedType: ConfigTypes::array,
key: $key,
);
}
return $configValue;
}

/**
* Get Robo configuration array value.
*
Expand Down Expand Up @@ -98,4 +140,27 @@ private function validateRoboConfigValueMatchesType(
}
return true;
}

/**
* Write data as yaml to a file.
*
* @param string $filepath
* The file to write the data to.
* @param string[] $data
* An array of data to be written out as yaml.
*/
protected function writeYaml(string $filepath, array $data): void
{
$yaml_conf = $this->getOptionalRoboConfigArrayFor('yaml_conf');
$yaml_conf = [...$this->getDefaultYamlConf(), ...$yaml_conf];
file_put_contents(
$filepath,
"---\n" . Yaml::dump(
$data,
$yaml_conf['inline_level'],
$yaml_conf['indent'],
$yaml_conf['dump_bits'],
)
);
}
}
4 changes: 3 additions & 1 deletion src/Robo/Plugin/Traits/SitesConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
trait SitesConfigTrait
{
use RoboConfigTrait;

/**
* Filename for a site's configuration file.
*
Expand Down Expand Up @@ -102,7 +104,7 @@ public function getSiteConfigItem(string $key, string $siteName = 'default', boo
protected function writeSitesConfig(array $sitesConfig): void
{
ksort($sitesConfig);
file_put_contents($this->sitesConfigFile, Yaml::dump($sitesConfig));
$this->writeYaml($this->sitesConfigFile, $sitesConfig);
}

/**
Expand Down

0 comments on commit 8a64c42

Please sign in to comment.