Skip to content

Commit

Permalink
Add composer:create-project command
Browse files Browse the repository at this point in the history
  • Loading branch information
kilrizzy committed Jul 29, 2017
1 parent dd1a2b8 commit b2d8f2e
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 36 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Tool for automating sites using Laravel Homestead. With one command, Homeboy wil
- [File](#file)
- [Vagrant](#vagrant)
- [Domain](#domain)
- [Composer](#composer)
- [Troubleshooting](#troubleshooting)

## Requirements
Expand Down Expand Up @@ -258,7 +259,12 @@ The domain command lets you quickly add a new domain record to your hosts file.
homeboy domain
```

Homeboy will prompt the domain as well as the ip address
### Composer

The composer:create-project command prompts you details on running a "composer create-project [composer-project] [project-name]" command
```
homeboy composer:create-project
```

## Troubleshooting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use App\Actions\Interfaces\ActionInterface;

class ComposerCreateProject extends BaseAction implements ActionInterface {
class ComposerCreateProjectAction extends BaseAction implements ActionInterface {

private $commandExecutor;
private $accessCommand;
private $project;
private $name;

public function __construct($accessCommand, $project, $name)
public function __construct($commandExecutor, $accessCommand, $project, $name)
{
$this->commandExecutor = $commandExecutor;
$this->accessCommand = $accessCommand;
$this->project = $project;
$this->name = $name;
Expand All @@ -30,7 +32,7 @@ public function actionMessage(){
}

public function run(){
$shellOutput = shell_exec($this->command());
$this->commandExecutor->run($this->command());
}

}
126 changes: 126 additions & 0 deletions app/Commands/ComposerCreateProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Commands;

use App\Actions\ComposerCreateProjectAction;
use App\Configuration\Config;
use App\Input\Interrogator;
use App\Support\Traits\HasCommandExecutor;
use App\Support\Traits\HasCommandOptions;
use App\Support\Traits\RequireEnvFile;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ComposerCreateProject extends Command
{

use RequireEnvFile;
use HasCommandOptions;
use HasCommandExecutor;

private $questionHelper;
private $inputInterface;
private $outputInterface;

private $config;

private $projectDirectory;
private $composerProject;
private $projectName;

private $interrogator;

private $commandExecutor;

public function __construct($name = null, Config $config)
{
$this->config = $config;
parent::__construct($name);
}

protected function configure()
{
$this
->setName('composer:create-project')
->setDescription('Create a new composer project')
->setHelp("");
}

private function init(InputInterface $input, OutputInterface $output){
$this->inputInterface = $input;
$this->outputInterface = $output;
$this->hasDotEnvFile();
$this->questionHelper = $this->getHelper('question');
$this->interrogator = new Interrogator($input, $output, $this->getHelper('question'));
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($input, $output);
$this->interrogate();

$taskConfirmation = $this->getTaskConfirmationFromQuestion();

if($taskConfirmation){
$this->runTasks();
}else{
$output->writeln('<error>Tasks cancelled</error>');
}

return;

}

private function interrogate(){

$this->projectDirectory = $this->interrogator->ask(
'Directory to create project?',
$this->config->getFolder()
);

$this->composerProject = $this->interrogator->ask(
'Composer project',
$this->config->getComposerProject()
);

$this->projectName = $this->interrogator->ask(
'Project directory name',
'project-'.time()
);

}

private function composerCreateProjectAction(){
if(!empty($this->config->getAccessLocalSitesDirectoryCommand())){
$accessCommand = $this->config->getAccessLocalSitesDirectoryCommand();
}else{
$accessCommand = 'cd '.$this->projectDirectory;
}
return new ComposerCreateProjectAction($this->commandExecutor,$accessCommand, $this->composerProject, $this->projectName);
}

private function getTaskConfirmationFromQuestion(){
$this->outputInterface->writeln('<info>The following tasks will be executed:</info>');

$this->outputInterface->writeln('- '.$this->composerCreateProjectAction()->confirmationMessage());

$response = $this->interrogator->ask(
'Run tasks?',
'Y'
);
if(strtoupper($response) == 'Y'){
return true;
}
return false;
}

private function runTasks(){

$this->outputInterface->writeln('<info>'.$this->composerCreateProjectAction()->actionMessage().'...</info>');
$this->composerCreateProjectAction()->run();
$this->outputInterface->writeln('');
$this->outputInterface->writeln('<info>Complete!</info>');
}

}
20 changes: 0 additions & 20 deletions app/Commands/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@

namespace App\Commands;

use App\Actions\ComposerCreateProject;
use App\Actions\HomesteadAddDatabase;
use App\Actions\HomesteadMapSite;
use App\Actions\HostsAddLine;
use App\Actions\ProvisionHomestead;
use App\Commands\Options\DatabaseOption;
use App\Commands\Options\DomainOption;
use App\Commands\Options\ProjectNameOption;
use App\Commands\Options\SkipConfirmationOption;
use App\Commands\Options\UseComposerOption;
use App\Commands\Options\UseDefaultsOption;
use App\Configuration\Config;
use App\Formatters\DatabaseNameFormatter;
use App\Formatters\DomainFormatter;
use App\Input\Interrogator;
use App\Support\Traits\HasCommandOptions;
use App\Support\Traits\RequireEnvFile;
use App\Support\Vagrant\Vagrant;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -41,8 +28,6 @@ class Domain extends Command

private $interrogator;

private $vagrant;

public function __construct($name = null, Config $config)
{
$this->config = $config;
Expand All @@ -63,11 +48,6 @@ private function init(InputInterface $input, OutputInterface $output){
$this->hasDotEnvFile();
$this->questionHelper = $this->getHelper('question');
$this->interrogator = new Interrogator($input, $output, $this->getHelper('question'));
$vagrantAccessDirectoryCommand = 'cd '.$this->config->getHomesteadBoxPath();
if(!empty($this->config->getHomesteadAccessDirectoryCommand())){
$vagrantAccessDirectoryCommand = $this->config->getHomesteadAccessDirectoryCommand();
}
$this->vagrant = new Vagrant($vagrantAccessDirectoryCommand);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
8 changes: 6 additions & 2 deletions app/Commands/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Commands;

use App\Actions\ComposerCreateProject;
use App\Actions\ComposerCreateProjectAction;
use App\Actions\HomesteadAddDatabase;
use App\Actions\HomesteadMapSite;
use App\Actions\HostsAddLine;
Expand All @@ -17,6 +17,7 @@
use App\Formatters\DatabaseNameFormatter;
use App\Formatters\DomainFormatter;
use App\Input\Interrogator;
use App\Support\Traits\HasCommandExecutor;
use App\Support\Traits\HasCommandOptions;
use App\Support\Traits\RequireEnvFile;
use App\Support\Vagrant\Vagrant;
Expand All @@ -29,6 +30,7 @@ class Host extends Command

use RequireEnvFile;
use HasCommandOptions;
use HasCommandExecutor;

private $questionHelper;
private $inputInterface;
Expand All @@ -50,6 +52,8 @@ class Host extends Command

private $vagrant;

private $commandExecutor;

public function __construct($name = null, Config $config)
{
$this->config = $config;
Expand Down Expand Up @@ -222,7 +226,7 @@ private function composerCreateProjectAction(){
}else{
$accessCommand = 'cd '.$this->folder;
}
return new ComposerCreateProject($accessCommand, $this->composerProject, $this->name);
return new ComposerCreateProjectAction($this->commandExecutor, $accessCommand, $this->composerProject, $this->name);
}

private function hostsAddLineAction(){
Expand Down
35 changes: 35 additions & 0 deletions app/Support/Shell/CommandExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Support\Shell;

class CommandExecutor{

protected $command;
protected $execute=true;

public function setExecute($execute){
$this->execute = $execute;
}

public function getExecute(){
return $this->execute;
}

public function setCommand($command){
$this->command = $command;
}

public function getCommand(){
return $this->command;
}

public function run($command=null){
if($command){
$this->setCommand($command);
}
if($this->execute){
shell_exec($this->getCommand());
}
}

}
18 changes: 18 additions & 0 deletions app/Support/Traits/HasCommandExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Support\Traits;

trait HasCommandExecutor{

public function setCommandExecutor($commandExecutor){
$this->commandExecutor = $commandExecutor;
}

public function getCommandExecutor(){
if(!$this->commandExecutor){
$this->commandExecutor = new \App\Support\Shell\CommandExecutor();
}
return $this->commandExecutor;
}

}
22 changes: 12 additions & 10 deletions homeboy
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ if($config->hasDotEnvFile()){

$application = new Application();

$setupCommand = new \App\Commands\Setup(null, $config);
$hostCommand = new \App\Commands\Host(null, $config);
$vagrantCommand = new \App\Commands\Vagrant(null, $config);
$fileCommand = new \App\Commands\File(null, $config);
$domainCommand = new \App\Commands\Domain(null, $config);

$application->add($setupCommand);
$application->add($hostCommand);
$application->add($vagrantCommand);
$application->add($fileCommand);
$application->add($domainCommand);

$commands = [
new \App\Commands\Setup(null, $config),
new \App\Commands\Vagrant(null, $config),
new \App\Commands\File(null, $config),
new \App\Commands\Domain(null, $config),
new \App\Commands\ComposerCreateProject(null, $config),
];

foreach($commands as $command){
$application->add($command);
}

$application->setDefaultCommand($hostCommand->getName());

Expand Down
42 changes: 42 additions & 0 deletions tests/Commands/ComposerCreateProjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class ComposerCreateProjectTest extends \Tests\AppTestCase\AppTestCase
{

public function test_create_project()
{

$projectDirectory = '/Code';
$composerProject = 'laravel/laravel';
$projectName = 'new-project';

$config = new \App\Configuration\Config();

$commandExecutor = new \App\Support\Shell\CommandExecutor();
$commandExecutor->setExecute(false);

$composerCreateProjectCommand = new \App\Commands\ComposerCreateProject(null, $config);
$composerCreateProjectCommand->setCommandExecutor($commandExecutor);

$application = new \Symfony\Component\Console\Application();

$application->add($composerCreateProjectCommand);

$command = $application->find($composerCreateProjectCommand->getName());
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
$commandTester->setInputs([
$projectDirectory,
$composerProject,
$projectName,
'y',
]);
$commandTester->execute(array(
'command' => $command->getName(),
));

$expectedCommand = 'cd '.$projectDirectory.' && composer create-project '.$composerProject.' '.$projectName;

$this->assertEquals($expectedCommand,$commandExecutor->getCommand());
}

}

0 comments on commit b2d8f2e

Please sign in to comment.