Skip to content

Commit

Permalink
Merge pull request #10 from fortrabbit/remove-project-config-after-apply
Browse files Browse the repository at this point in the history
Remove project.yaml after running project-config/apply
  • Loading branch information
Oliver Stark authored Nov 8, 2022
2 parents f87d5e4 + e261a6a commit 4d0d1a8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor/
.idea/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ By setting the ENV var `DISABLE_CRAFT_AUTOMIGRATE=1` you disable the plugin.

By setting the ENV var `PROJECT_CONFIG_FORCE_APPLY=1` the `project-config/apply` command is executed with the `--force` flag.

The file `config/project/project.yaml` will be removed after applying, unless you set the ENV var `KEEP_PROJECT_CONFIG=1` or composer install is running locally (interactive mode).
This behaviour was added in `2.5.0` to prevent re-applying the Project Config in the Craft CP.

40 changes: 33 additions & 7 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function runCommands(): bool
}


if ($this->hasProjectConfigFile()) {
if ($this->hasProjectConfig()) {
$args = ["project-config/apply"];

if (getenv('PROJECT_CONFIG_FORCE_APPLY') == 1) {
Expand All @@ -87,6 +87,16 @@ public function runCommands(): bool
if ($cmd->run()) {
$this->io->write(PHP_EOL . "▶ <info>Craft auto migrate</info> [project-config/apply]");
$this->io->write(PHP_EOL . $cmd->getOutput());

// Remove project.yaml during deployment (non-interactive mode)
if ($this->shouldRemoveProjectConfigAfterApply()) {
$projectConfigFile = $this->getProjectConfigFile();
if (is_string($projectConfigFile) && unlink($projectConfigFile)) {
$this->io->write(PHP_EOL . "$projectConfigFile removed");
}
}


} else {
$this->io->writeError(PHP_EOL . "▶ <info>Craft auto migrate</info> [project-config/apply ERROR]");
$this->io->writeError(PHP_EOL . $cmd->getErrorOutput());
Expand All @@ -99,6 +109,14 @@ public function runCommands(): bool
return true;
}

protected function shouldRemoveProjectConfigAfterApply(): bool
{
if (getenv('KEEP_PROJECT_CONFIG') == 1 || $this->io->isInteractive() === true) {
return false;
}

return true;
}

/**
* Checks if Craft is installed by showing a help command
Expand All @@ -119,18 +137,26 @@ protected function isCraftInstalled(): bool
}


/**
* @return bool
*/
protected function hasProjectConfigFile(): bool
protected function hasProjectConfig(): bool
{
$projectRoot = realpath(dirname(Factory::getComposerFile()));
$pathToConfigFile = $projectRoot . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'project.yaml';
$pathToConfigDir = $projectRoot . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'project';
$pathToConfigFile = implode(DIRECTORY_SEPARATOR, [$projectRoot, 'config', 'project', 'project.yaml']);
$pathToConfigDir = implode(DIRECTORY_SEPARATOR, [$projectRoot, 'config', 'project']);

return file_exists($pathToConfigFile) || is_dir($pathToConfigDir);
}

protected function getProjectConfigFile(): ?string
{
$projectRoot = realpath(dirname(Factory::getComposerFile()));
$projectConfigFile = implode(DIRECTORY_SEPARATOR, [$projectRoot, 'config', 'project', 'project.yaml']);

return file_exists($projectConfigFile)
? $projectConfigFile
: null;

}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit 4d0d1a8

Please sign in to comment.