Commit 7ae90d51 authored by Jon Pugh's avatar Jon Pugh

Add a "contexts_path" property and ask the user if they want to create it.

parent cda23e10
......@@ -36,7 +36,7 @@ try {
$io = new DrupalStyle($input, $output);
// Create a config object.
$config = new Config();
$config = new Config($io);
// Create the app.
$app = new \Aegir\Provision\Provision($config, $input, $output);
......
......@@ -2,8 +2,13 @@
namespace Aegir\Provision\Console;
use Aegir\Provision\Provision;
use Drupal\Console\Core\Style\DrupalStyle;
use Robo\Common\IO;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
/**
* Class Config
......@@ -14,14 +19,23 @@ use Symfony\Component\Console\Exception\InvalidOptionException;
class Config extends ProvisionConfig
{
const CONFIG_FILENAME = '.provision.yml';
use IO;
/**
* @var Filesystem
*/
private $fs;
/**
* DefaultsConfig constructor.
*/
public function __construct()
public function __construct(DrupalStyle $io = null)
{
parent::__construct();
$this->io = $io;
$this->fs = new Filesystem();
$this->set('root', $this->getProvisionRoot());
$this->set('php', $this->getPhpBinary());
$this->set('php_version', PHP_VERSION);
......@@ -42,6 +56,8 @@ class Config extends ProvisionConfig
$this->set('config_path', $this->getHomeDir() . '/config');
}
$this->set('contexts_path', $this->get('config_path') . DIRECTORY_SEPARATOR . Provision::CONTEXTS_PATH);
$file = $this->get('user_home') . DIRECTORY_SEPARATOR . Config::CONFIG_FILENAME;
$this->set('console_config_file', $file);
......@@ -59,27 +75,29 @@ class Config extends ProvisionConfig
*/
protected function validateConfig() {
// Check that aegir_root is writable.
// @TODO: Create some kind of Setup functionality.
if (!is_writable($this->get('aegir_root'))) {
throw new InvalidOptionException(
"The folder set to 'aegir_root' ({$this->get('aegir_root')}) is not writable. Fix this or change the aegir_root value in the file {$this->get('console_config_file')}"
);
}
// If config_path does not exist and we cannot create it...
if (!file_exists($this->get('config_path')) && !mkdir($this->get('config_path'))) {
throw new InvalidOptionException(
"The folder set to 'config_path' ({$this->get('config_path')}) does not exist, and cannot be created. Create it manually or change the 'config_path' value in the file {$this->get('console_config_file')}."
);
if (!file_exists($this->get('contexts_path'))) {
// START: New User!
$this->io->title("Welcome to Provision!");
$this->io->block("It looks like this is your first time running Provision. You are missing the {$this->get('contexts_path')} folder.");
if ($this->io->confirm('Would you like to create the folder ' . $this->get('contexts_path') . '?')) {
$this->fs->mkdir($this->get('contexts_path'), 0700);
}
}
elseif (!is_writable($this->get('config_path'))) {
if (!is_writable($this->get('contexts_path'))) {
throw new InvalidOptionException(
"The folder set to 'config_path' ({$this->get('config_path')}) is not writable. Fix this or change the config_path value in the file {$this->get('console_config_file')}."
"The folder set to 'contexts_path' ({$this->get('contexts_path')}) is not writable. Fix this or change the contexts_path value in the file {$this->get('console_config_file')}."
);
}
elseif (!file_exists($this->get('config_path') . '/provision')) {
mkdir($this->get('config_path') . '/provision');
}
// Ensure that script_user is the user.
$real_script_user = $this->getScriptUser();
if ($this->get('script_user') != $real_script_user) {
......
......@@ -109,11 +109,11 @@ class Context implements BuilderAwareInterface
private function loadContextConfig($options = []) {
if ($this->getProvision()) {
$this->config_path = $this->getProvision()->getConfig()->get('config_path') . '/provision/' . $this->type . '.' . $this->name . '.yml';
$this->config_path = $this->getProvision()->getConfig()->get('contexts_path') . DIRECTORY_SEPARATOR . $this->type . '.' . $this->name . '.yml';
}
else {
$config = new Config();
$this->config_path = $config->get('config_path') . '/provision/' . $this->type . '.' . $this->name . '.yml';
$this->config_path = $config->get('contexts_path') . DIRECTORY_SEPARATOR . $this->type . '.' . $this->name . '.yml';
}
$configs = [];
......
......@@ -26,6 +26,7 @@ use Robo\Contract\IOAwareInterface;
use Robo\LoadAllTasks;
use Robo\Robo;
use Robo\Runner as RoboRunner;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
......@@ -47,6 +48,11 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
const APPLICATION_FUN_NAME = '𝙋𝙍𝙊𝙑𝙄𝙎𝙄𝙊𝙉';
const VERSION = '4.x-dev';
const REPOSITORY = 'aegir-project/provision';
/**
* The path within config_path to write contexts to.
*/
const CONTEXTS_PATH = 'contexts';
use BuilderAwareTrait;
use ConfigAwareTrait;
......@@ -147,7 +153,7 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
*/
private function loadAllContexts()
{
$folder = $this->getConfig()->get('config_path') . '/provision';
$folder = $this->getConfig()->get('contexts_path');
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->name("*.yml")->in($folder);
foreach ($finder as $file) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment