Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
Allow to choose host TLD interactively
Browse files Browse the repository at this point in the history
  • Loading branch information
ogizanagi committed Dec 15, 2017
1 parent b3e35d3 commit 1f107c0
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 42 deletions.
27 changes: 26 additions & 1 deletion src/Command/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Dependency\Dependency;
use Manala\Manalize\Env\Config\Variable\Dependency\VersionBounded;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Defaults\Defaults;
use Manala\Manalize\Env\Defaults\DefaultsParser;
use Manala\Manalize\Env\Dumper;
Expand Down Expand Up @@ -70,6 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->comment(sprintf('Start composing your <info>%s</info> environment', (string) $envName));

$appName = $this->askForAppName($io, strtolower(basename($cwd)));
$tld = $this->askForTld($io);
$envDefaults = DefaultsParser::parse($envName);
$options = ['dumper_flags' => $input->getOption('no-update') ? Dumper::DUMP_METADATA : Dumper::DUMP_ALL];

Expand All @@ -79,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dependencies = SetupHandler::createDefaultDependencySet($envDefaults);
}

$handler = new SetupHandler($cwd, new AppName($appName), $envName, $dependencies, $options);
$handler = new SetupHandler($cwd, new AppName($appName), $envName, new Tld($tld), $dependencies, $options);

try {
$handler->handle(function (string $target) use ($io) {
Expand Down Expand Up @@ -184,6 +186,29 @@ private function shouldConfigureDependencies(SymfonyStyle $io, Defaults $default
return $io->confirm('Do you want to customize your dependencies?', false);
}

private function askForTld(SymfonyStyle $io): string
{
$choices = [
'vm' => '.vm',
'test' => '.test',
'custom' => 'Choose a custom TLD',
];

$tld = array_search($io->choice('Which TLD would you like to use?', array_values($choices), '.vm'), $choices);

if ('custom' === $tld) {
$tld = $io->ask('Please enter the TLD you\'d like to use', null, function ($value) {
if (empty($value)) {
throw new \InvalidArgumentException('TLD cannot be empty.');
}

return Tld::validate($value);
});
}

return $tld;
}

private function askStrategyForExistingFile(SymfonyStyle $io, string $cwd, string $target, array $strategies): string
{
$strategy = $io->choice(
Expand Down
2 changes: 2 additions & 0 deletions src/Env/Config/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Dependency\Dependency;
use Manala\Manalize\Env\Config\Variable\Dependency\VersionBounded;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Config\Variable\VagrantBoxVersion;
use Manala\Manalize\Env\Config\Variable\Variable;

Expand All @@ -37,6 +38,7 @@ public function getClassesByAliases(): array
'vagrant' => Vagrant::class,
// Variable
'app_name' => AppName::class,
'tld' => Tld::class,
'box_version' => VagrantBoxVersion::class,
'dependency' => Dependency::class,
'dependency_with_version' => VersionBounded::class,
Expand Down
43 changes: 43 additions & 0 deletions src/Env/Config/Variable/Tld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Manalize project.
*
* (c) Manala <contact@manala.io>
*
* For the full copyright and license information, please refer to the LICENSE
* file that was distributed with this source code.
*/

namespace Manala\Manalize\Env\Config\Variable;

/**
* The "tld" var.
*
* @author Maxime STEINHAUSSER <maxime.steinhausser@gmail.com>
*/
final class Tld extends SingleValue
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'tld';
}

/**
* {@inheritdoc}
*/
public static function validate(string $value)
{
if (!preg_match('/^([-A-Z0-9])*$/i', $value)) {
throw new \InvalidArgumentException(sprintf(
'This value must contain only alphanumeric characters and hyphens.',
$value
));
}

return strtolower($value);
}
}
5 changes: 3 additions & 2 deletions src/Env/EnvFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Manala\Manalize\Env\Config\Registry;
use Manala\Manalize\Env\Config\Vagrant;
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Config\Variable\VagrantBoxVersionResolver;
use Manala\Manalize\Env\Config\Variable\VariableHydrator;

Expand All @@ -26,11 +27,11 @@
*/
class EnvFactory
{
public static function createEnv(EnvName $name, AppName $appName, \Traversable $dependencies): Env
public static function createEnv(EnvName $name, AppName $appName, Tld $tld, \Traversable $dependencies): Env
{
return new Env(
$name->getValue(),
new Vagrant($name, $appName, VagrantBoxVersionResolver::resolve($dependencies)),
new Vagrant($name, $appName, $tld, VagrantBoxVersionResolver::resolve($dependencies)),
new Ansible($name, ...$dependencies),
new Make($name)
);
Expand Down
7 changes: 5 additions & 2 deletions src/Handler/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Dependency\Dependency;
use Manala\Manalize\Env\Config\Variable\Dependency\VersionBounded;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Defaults\Defaults;
use Manala\Manalize\Env\Dumper;
use Manala\Manalize\Env\EnvFactory;
Expand All @@ -30,25 +31,27 @@ class Setup
private $envName;
private $dependencies;
private $options;
private $patch;
private $tld;

public function __construct(
string $cwd,
AppName $appName,
EnvName $envName,
Tld $tld,
\Traversable $dependencies,
array $options = []
) {
$this->cwd = $cwd;
$this->appName = $appName;
$this->envName = $envName;
$this->tld = $tld;
$this->dependencies = $dependencies;
$this->options = $this->normalizeOptions($options);
}

public function handle(callable $notifier, callable $existingFileCallback = null)
{
$env = EnvFactory::createEnv($this->envName, $this->appName, $this->dependencies);
$env = EnvFactory::createEnv($this->envName, $this->appName, $this->tld, $this->dependencies);
$dumper = new Dumper($this->cwd);

try {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/common/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Vagrant.configure(2) do |config|
# Vm
config.vm.box = app[:box]
config.vm.box_version = app[:box_version]
config.vm.hostname = app[:name] + '.vm'
config.vm.hostname = app[:name] + '.{# tld #}'
config.vm.network 'private_network', type: 'dhcp'
config.vm.define 'localhost' do |localhost| end
config.vm.synced_folder '.', '/srv/app',
Expand Down
2 changes: 2 additions & 0 deletions tests/Env/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Manala\Manalize\Env\Config\Config;
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Dumper;
use Manala\Manalize\Env\EnvExporter;
use Manala\Manalize\Env\EnvFactory;
Expand Down Expand Up @@ -89,6 +90,7 @@ private function createEnv()
$env = EnvFactory::createEnv(
EnvName::SYMFONY(),
new AppName('dummy'),
new Tld('vm'),
$this->prophesize(\Iterator::class)->reveal()
);

Expand Down
6 changes: 4 additions & 2 deletions tests/Env/EnvFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Manala\Manalize\Env\Config\Make;
use Manala\Manalize\Env\Config\Vagrant;
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Config\Variable\VagrantBoxVersion;
use Manala\Manalize\Env\Env;
use Manala\Manalize\Env\EnvFactory;
Expand All @@ -27,9 +28,10 @@ public function testCreateEnv()
{
$envType = EnvName::SYMFONY();
$appName = new AppName('rch');
$tld = new Tld('vm');
$boxVersion = new VagrantBoxVersion('~> 3.0.0');
$env = EnvFactory::createEnv($envType, $appName, $this->prophesize(\Iterator::class)->reveal());
$expectedConfigs = [new Vagrant($envType, $appName, $boxVersion), new Ansible($envType), new Make($envType)];
$env = EnvFactory::createEnv($envType, $appName, $tld, $this->prophesize(\Iterator::class)->reveal());
$expectedConfigs = [new Vagrant($envType, $appName, $tld, $boxVersion), new Ansible($envType), new Make($envType)];

$this->assertInstanceOf(Env::class, $env);
$this->assertEquals($expectedConfigs, $env->getConfigs());
Expand Down
68 changes: 36 additions & 32 deletions tests/Functional/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function tearDown()
/**
* @dataProvider provideEnvs()
*/
public function testExecute(array $inputs, $expectedBoxName, $expectedBoxVersion, $expectedDeps, $expectedMetadataFilename)
public function testExecute(array $inputs, $expectedBoxName, $expectedBoxVersion, $expectedTld, $expectedDeps, $expectedMetadataFilename)
{
self::createSymfonyStandardProject(self::$cwd);
$tester = new CommandTester(new Setup());
Expand All @@ -63,6 +63,7 @@ public function testExecute(array $inputs, $expectedBoxName, $expectedBoxVersion

$this->assertContains(":name => '$expectedBoxName'", $vagrantFile);
$this->assertContains(":box_version => '$expectedBoxVersion'", $vagrantFile);
$this->assertContains("config.vm.hostname = app[:name] + '.$expectedTld'", $vagrantFile);

if (UPDATE_FIXTURES) {
file_put_contents("$fixturesDir/$expectedDeps", file_get_contents(self::$cwd.'/ansible/group_vars/app.yml'));
Expand All @@ -73,9 +74,39 @@ public function testExecute(array $inputs, $expectedBoxName, $expectedBoxVersion
$this->assertFileEquals("$fixturesDir/$expectedMetadataFilename", self::$cwd.'/ansible/.manalize.yml');
}

public function provideEnvs()
{
return [
[
["\n", '.vm', "\n"],
'manalized-app',
'~> 3.0.0',
'vm',
'app_1.yml',
'metadata_1.yml',
],
[
['foo-bar.manala', '.vm', 'yes', '5.6', "\n", "\n", "\n", "\n", "\n", "\n"],
'foo-bar.manala',
'~> 2.0.0',
'vm',
'app_2.yml',
'metadata_2.yml',
],
[
['foo-bar.manala', 'Choose a custom TLD', 'my-tld', "\n"],
'foo-bar.manala',
'~> 3.0.0',
'my-tld',
'app_3.yml',
'metadata_3.yml',
],
];
}

public function testExecuteEnvNameChoice()
{
$inputs = ['0', "\n", "\n", "\n"];
$inputs = ['0', "\n", "\n", "\n", "\n"];
$tester = new CommandTester(new Setup());
$tester
->setInputs($inputs)
Expand All @@ -92,7 +123,7 @@ public function testExecuteEnvNameChoice()
public function testExecuteEnvNameWithIncorrectChoice()
{
$countEnvs = count(EnvName::values());
$inputs = [(string) $countEnvs, '0', "\n", "\n", "\n", "\n"];
$inputs = [(string) $countEnvs, '0', "\n", "\n", "\n", "\n", "\n"];
$tester = new CommandTester(new Setup());
$tester
->setInputs($inputs)
Expand All @@ -107,38 +138,11 @@ public function testExecuteEnvNameWithIncorrectChoice()
$this->assertContains('Environment successfully configured', $consoleDisplay);
}

public function provideEnvs()
{
return [
[
["\n", "\n"],
'manalized-app',
'~> 3.0.0',
'app_1.yml',
'metadata_1.yml',
],
[
['foo-bar.manala', 'yes', '5.6', "\n", "\n", "\n", "\n", "\n", "\n"],
'foo-bar.manala',
'~> 2.0.0',
'app_2.yml',
'metadata_2.yml',
],
[
['foo-bar.manala', "\n"],
'foo-bar.manala',
'~> 3.0.0',
'app_3.yml',
'metadata_3.yml',
],
];
}

public function testExecuteNoUpdate()
{
$tester = new CommandTester(new Setup());
$tester
->setInputs(["\n", "\n"])
->setInputs(["\n", "\n", "\n"])
->execute(['cwd' => static::$cwd, '--no-update' => true, '--env' => 'symfony']);

if (0 !== $tester->getStatusCode()) {
Expand Down Expand Up @@ -167,7 +171,7 @@ public function testExecuteHandleConflicts()

$tester = new CommandTester(new Setup());
$tester
->setInputs(["\n", "\n", '0']) // patch strategy
->setInputs(["\n", "\n", "\n", '0']) // patch strategy
->execute(['cwd' => self::$cwd, '--env' => 'symfony']);

if (0 !== $tester->getStatusCode()) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Dependency\Dependency;
use Manala\Manalize\Env\Config\Variable\Dependency\VersionBounded;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Config\Variable\VariableHydrator;
use Manala\Manalize\Env\Defaults\DefaultsParser;
use Manala\Manalize\Env\EnvName;
Expand Down Expand Up @@ -113,8 +114,7 @@ protected static function manalizeProject($cwd, $appName, EnvName $envType, \Tra
$dependencies = Setup::createDefaultDependencySet(DefaultsParser::parse($envType));
}

(new Setup($cwd, new AppName($appName), $envType, $dependencies))->handle(function () {
});
(new Setup($cwd, new AppName($appName), $envType, new Tld('vm'), $dependencies))->handle(function () {});
}

protected static function createManalizedProject($cwd, $appName = 'dummy.manala', EnvName $envType = null, \Iterator $dependencies = null)
Expand Down
2 changes: 2 additions & 0 deletions tests/Handler/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Manala\Manalize\Tests\Handler;

use Manala\Manalize\Env\Config\Variable\AppName;
use Manala\Manalize\Env\Config\Variable\Tld;
use Manala\Manalize\Env\Dumper;
use Manala\Manalize\Env\EnvName;
use Manala\Manalize\Handler\Setup;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function testHandle($options, $expectedFiles)
self::$cwd,
new AppName('setup_test'),
EnvName::SYMFONY(),
new Tld('vm'),
$this->prophesize(\Iterator::class)->reveal(),
$options
);
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/Command/SetupTest/execute_no_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ configs:
vagrant:
app_name:
- { value: manalized-app }
tld:
- { value: vm }
box_version:
- { value: '~> 3.0.0' }
ansible:
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/Command/SetupTest/metadata_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ configs:
vagrant:
app_name:
- { value: manalized-app }
tld:
- { value: vm }
box_version:
- { value: '~> 3.0.0' }
ansible:
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/Command/SetupTest/metadata_2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ configs:
vagrant:
app_name:
- { value: foo-bar.manala }
tld:
- { value: vm }
box_version:
- { value: '~> 2.0.0' }
ansible:
Expand Down
Loading

0 comments on commit 1f107c0

Please sign in to comment.