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');

    // 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.
    $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);
  }

  /**
   * 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->write('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->write('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.');
  }

}