Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
meltmedia/meltconsole / src / App / Commands / SetupLocalCommand.php
Size: Mime:
<?php

namespace MeltConsole\App\Commands;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Exception\RuntimeException;

/**
 * Handles the functionality to create new environments on your local.
 *
 * @package MeltConsole\App\Commands
 */
class SetupLocalCommand extends MeltCommand {

  private $gitUrl;
  private $sshUser;
  private $sshHost;
  private $sshRoot;
  private $sshUri = '<PROJECT_NAME-ENV>.drupal.meltdemo.com';

  /**
   * {@inheritdoc}
   */
  protected function configure() {

    // Initial config for command.
    $this
      ->setName('melt:setup-local')
      ->setAliases(['melt:sl'])
      ->setDescription('Prepares local environment prior to lando intialization.');
  }

  /**
   * {@inheritdoc}
   */
  protected function interact(InputInterface $input, OutputInterface $output) {
    parent::interact($input, $output);

    $helper = $this->getHelper('question');

    $output->writeln([
      '----------------------',
      'READ FIRST!!!!',
      'This command will create a <info>.lando.local.yml</info> file which should',
      'be added to your <info>.gitignore</info> file as it may contain credentials',
      'that you may not want committed to git.',
      '----------------------',
    ]);

    // Ask for git url.
    $gitQuestion = new Question('Enter the git url for project: ');
    $this->gitUrl = $helper->ask($input, $output, $gitQuestion);

    // Ask questions related to ssh credentials.
    // Useful output.
    $sshQuestion1 = new Question('Enter the ssh user: ');
    $sshQuestion2 = new Question('Enter the ssh host: ');
    $sshQuestion3 = new Question('Enter the ssh root. Defaults to <info>/var/www/apps</info>: ', '/var/www/apps');

    $sshQuestion3->setValidator(function ($answer) {
      if ($answer[0] !== '/') {
        throw new RuntimeException('Should begin with "/"');
      }
      return $answer;
    });

    $sshQuestion3->setMaxAttempts(2);

    $this->sshUser = $helper->ask($input, $output, $sshQuestion1);
    $this->sshHost = $helper->ask($input, $output, $sshQuestion2);
    $this->sshRoot = $helper->ask($input, $output, $sshQuestion3);

  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {
    $this->createBaseLandoFile($input, $output);
    $this->createLocalLandoFile($input, $output);
    $this->createLocalSettingsFile($input, $output);
    $this->createDrushAliasFile($input, $output);
  }

  /**
   * Creates local.settings.php file.
   */
  protected function createLocalSettingsFile(InputInterface $input, OutputInterface $output) {
    $fs = new Filesystem();
    $yaml = $this->parseYamlFile('.lando.yml');
    $settings_path = 'docroot/sites/default/settings';

    if (!$fs->exists("{$settings_path}/local.settings.php")) {
      $output->writeln('Creating <info>local.settings.php</info> file');
      $this->renderFile(
        'local.settings.php.twig',
        'docroot/sites/default/settings/local.settings.php',
        [ 'db_name' => $yaml['name'] ]
      );
      $output->writeln("Created <info>{$settings_path}/local.settings.php</info> file.");
    } else {
      $output->writeln("Looks like the <info>{$settings_path}/local.settings.php</info> file already exists. Skipping...");
    }

  }

  /**
   * Creates .lando.base.yml file.
   */
  protected function createBaseLandoFile(InputInterface $input, OutputInterface $output) {
    $fs = new Filesystem();
    $baseData = $this->parseYamlFile($this->templatesDir . '/.lando.base.yml');

    if ($fs->exists('.lando.base.yml')) {
      $output->writeln('Looks like a <info>.lando.base.yml</info> file already exists. ');
      $helper = $this->getHelper('question');
      $question = new ChoiceQuestion('Would you like to: ', [
        'merge and override',
        'create new',
        'do nothing',
      ]);

      $answer = $helper->ask($input, $output, $question);

      if ($answer == 'merge and override') {
        $existingYaml = $this->parseYamlFile('.lando.base.yml');
        $baseData = array_merge($existingYaml, $baseData);
      }

      if ($answer == 'do nothing') {
        return $output->writeln('Ok bye!');
      }
    }

    $this->writeToYamlFile($baseData, '.lando.base.yml');
    $output->writeln('Created <info>.lando.base.yml</info> file.');

  }

  /**
   * Creates `.lando.local.yml` file for local environment.
   */
  protected function createLocalLandoFile(InputInterface $input, OutputInterface $output) {

    $fs = new Filesystem();
    $yaml = [];
    $yaml['melt']['git'] = $this->gitUrl;
    $yaml['melt']['ssh']['user'] = $this->sshUser;
    $yaml['melt']['ssh']['host'] = $this->sshHost;
    $yaml['melt']['ssh']['root'] = "{$this->sshRoot}";
    $yaml['melt']['ssh']['uri'] = $this->sshUri;

    // If the file already exists, ask the user if they would like to merge the
    // new data in with the existing.
    if ($fs->exists('.lando.local.yml')) {

      $output->writeln('Looks like a <info>.lando.local.yml</info> file already exists. ');
      $helper = $this->getHelper('question');
      $question = new ChoiceQuestion('Would you like to: ', [
        'merge and override',
        'create new',
        'do nothing',
      ]);

      $answer = $helper->ask($input, $output, $question);

      if ($answer == 'merge and override') {
        $existingYaml = $this->parseYamlFile('.lando.local.yml');
        $yaml = array_merge($existingYaml, $yaml);
      }

      if ($answer == 'do nothing') {
        return $output->writeln('Ok bye!');
      }
    }

    $this->writeToYamlFile($yaml, '.lando.local.yml');
    $output->writeln('Created <info>.lando.local.yml</info> file.');
  }


  /**
   * Creates drush alias file.
   */
  protected function createDrushAliasFile(InputInterface $input, OutputInterface $output) {
    $fs = new Filesystem();
    $yaml = $this->parseYamlFile('.lando.local.yml');

    // Throw error if .lando.local.yml file doesn't exist because that is the file we
    // pass the SSH credentials for drush aliases to.
    if(!$fs->exists('.lando.local.yml')) {
      $output->writeln("<error>UH OHH!!</error> {$this->pathToDrushAlias} can not be created because the .lando.local.yml doesn't exist");
      return false;
    }

    $ssh = $yaml['melt']['ssh'];

    if (!$fs->exists($this->pathToDrushAlias)) {
      $output->writeln('Creating drush alias...');
      $this->renderFile(
        'melt.site.yml.twig',
        $this->pathToDrushAlias,
        [
          'ssh_host' => $ssh['host'],
          'ssh_user' => $ssh['user'],
          'ssh_root' => $ssh['root'] . "/{$this->projectName}-dev",
          'ssh_uri' => "{$this->projectName}-dev.drupal.meltdemo.com", // Add `dev` by default
        ]
      );
      $output->writeln("Created <info>{$this->pathToDrushAlias}</info> file.");
    } else {
      $output->writeln("Looks like the <info>{$this->pathToDrushAlias}</info> file already exists. Skipping...");
    }

  }

}