-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |