Skip to content

Commit

Permalink
Merge pull request #8 from kilroyweb/briandillingham-master
Browse files Browse the repository at this point in the history
Briandillingham master
  • Loading branch information
kilroyweb authored Jul 8, 2017
2 parents 25f2f2a + 4c4c343 commit 4e117e7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
DEFAULT_COMPOSER_PROJECT=laravel/laravel
USE_COMPOSER=true
DEFAULT_FOLDER_SUFFIX=/public
DEFAULT_DOMAIN_EXTENSION=.app
HOSTS_FILE_PATH=/etc/hosts
HOMESTEAD_HOST_IP=192.168.10.10
HOMESTEAD_FILE_PATH=/Users/*USER*/.homestead/Homestead.yaml
HOMESTEAD_SITES_PATH=/home/vagrant/Code/
HOMESTEAD_BOX_PATH=/Users/*USER*/Homestead
HOMESTEAD_BOX_PATH=/Users/*USER*/Homestead
LOCAL_SITES_PATH=/Users/*USER*/Code/
5 changes: 4 additions & 1 deletion .env.example-windows
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
DEFAULT_COMPOSER_PROJECT=laravel/laravel
USE_COMPOSER=true
DEFAULT_FOLDER_SUFFIX=/public
DEFAULT_DOMAIN_EXTENSION=.app
HOSTS_FILE_PATH=C:\Windows\System32\drivers\etc\hosts
HOMESTEAD_HOST_IP=192.168.10.10
HOMESTEAD_FILE_PATH=C:\Homestead\Homestead.yaml
HOMESTEAD_SITES_PATH=/home/vagrant/Code/
HOMESTEAD_BOX_PATH=C:\Homestead
HOMESTEAD_BOX_PATH=C:\Homestead
LOCAL_SITES_PATH=/Users/*USER*/Code/
5 changes: 4 additions & 1 deletion .env.example-windows-d-drive
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
DEFAULT_COMPOSER_PROJECT=laravel/laravel
USE_COMPOSER=true
DEFAULT_FOLDER_SUFFIX=/public
DEFAULT_DOMAIN_EXTENSION=.app
HOSTS_FILE_PATH=C:\Windows\System32\drivers\etc\hosts
HOMESTEAD_HOST_IP=192.168.10.10
HOMESTEAD_FILE_PATH=D:\Homestead\Homestead.yaml
HOMESTEAD_SITES_PATH=/home/vagrant/Code/
HOMESTEAD_BOX_PATH=D:\Homestead
HOMESTEAD_PROVISION_COMMAND="cd /d D: && cd /Homestead && vagrant provision"
HOMESTEAD_PROVISION_COMMAND="cd /d D: && cd /Homestead && vagrant provision"
LOCAL_SITES_PATH=D:\Projects
70 changes: 50 additions & 20 deletions app/Commands/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Host extends Command
private $inputInterface;
private $outputInterface;

private $name;
private $composerProject;
private $useComposer;
private $folder;
private $folderSuffix;
private $database;
Expand All @@ -36,14 +39,17 @@ protected function configure()
}

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

private function updateFromConfig(){
$this->folder = getenv('LOCAL_SITES_PATH');
$this->folderSuffix = getenv('DEFAULT_FOLDER_SUFFIX');
$this->useComposer = boolval(getenv('USE_COMPOSER'));
$this->composerProject = getenv('DEFAULT_COMPOSER_PROJECT');
$this->hostPath = getenv('HOSTS_FILE_PATH');
$this->hostIP = getenv('HOMESTEAD_HOST_IP');
$this->homesteadPath = getenv('HOMESTEAD_FILE_PATH');
Expand All @@ -57,22 +63,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($input, $output);

$this->folder = $this->getFolderFromQuestion();
if(empty($this->folder)){
$output->writeln('<error>Host creation failed. No folder set</error>');
return;
$this->name = $this->getNameFromQuestion();

if($this->useComposer){
$this->composerProject = $this->getComposerProjectFromQuestion();
}

$this->folderSuffix = $this->getFolderSuffixFromQuestion();
$this->folder = $this->getFolderFromQuestion();

if($this->composerProject != 'laravel/laravel')
{
$this->folderSuffix = $this->getFolderSuffixFromQuestion();
}

$this->database = $this->getDatabaseFromQuestion();

$this->domain = $this->getDomainFromQuestion();

if($this->useComposer && !empty($this->composerProject)){
$output->writeln('<info>Creating project..</info>');
$this->createProject();
}

$output->writeln('<info>Create host ('.$this->domain.')...</info>');
$this->updateHostsFile();

$output->writeln('<info>Update Vagrant site mapper (/'.$this->folder.')</info>');
$output->writeln('<info>Update Vagrant site mapper ('.$this->folder.')</info>');
$this->updateHomesteadSites();

$output->writeln('<info>Update Vagrant database ('.$this->database.')</info>');
Expand All @@ -83,33 +99,41 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln('<success>Complete!</success>');

$output->writeln('Visit: http://' . $this->domain);

return;

}

private function getNameFromQuestion(){
$question = new Question('What is your project\'s name? ', 'project-' . time());
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

private function getComposerProjectFromQuestion(){
$question = new Question("What composer project? ({$this->composerProject}): ", $this->composerProject);
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

private function getFolderFromQuestion(){
$question = new Question('Folder Name (Leave blank to cancel): ', false);
$question = new Question("What local directory will store your project? ($this->folder): ", $this->folder);
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

private function getFolderSuffixFromQuestion(){
$question = new ConfirmationQuestion('Point site to '.$this->folderSuffix.' suffix? (yes)', true);
$response = $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
if(!$response){
return '';
}
return $this->folderSuffix;
private function getFolderSuffixFromQuestion(){
$question = new Question("Point site to? ({$this->folderSuffix}): ", $this->folderSuffix);
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

private function getDatabaseFromQuestion(){
$default = $this->defaultDatabaseNameFromKey($this->folder);
$question = new Question('Database Name: ('.$default.') ', $default);
$default = $this->defaultDatabaseNameFromKey($this->name);
$question = new Question('Database Name? ('.$default.'): ', $default);
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

private function getDomainFromQuestion(){
$default = $this->defaultDomainNameFromKey($this->database);
$question = new Question('Development Domain: ('.$default.')', $default);
$question = new Question('Development Domain? ('.$default.'): ', $default);
return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question);
}

Expand All @@ -128,6 +152,12 @@ private function defaultDomainNameFromKey($key){
return $key;
}

private function createProject()
{
$this->outputInterface->writeln("cd {$this->folder} && composer create-project {$this->composerProject} {$this->name}");
$shellOutput = shell_exec("cd {$this->folder} && composer create-project {$this->composerProject} {$this->name}");
}

private function updateHostsFile(){
$hostAppendLine = $this->hostIP.' '.$this->domain;
file_put_contents($this->hostPath, PHP_EOL.$hostAppendLine, FILE_APPEND | LOCK_EX);
Expand All @@ -137,7 +167,7 @@ private function updateHomesteadSites(){
$homesteadContents = file_get_contents($this->homesteadPath);
$tabSpacing = " ";
$mapLine = $tabSpacing."- map: ".$this->domain;
$toLine = $tabSpacing." to: ".$this->homesteadSitesPath.$this->folder.$this->folderSuffix;
$toLine = $tabSpacing." to: ".$this->homesteadSitesPath.$this->name.$this->folderSuffix;
$newLines = $mapLine.PHP_EOL.$toLine;
$search = "sites:";
$homesteadContents = str_replace($search,$search.PHP_EOL.$newLines,$homesteadContents);
Expand All @@ -154,7 +184,7 @@ private function updateHomesteadDatabases(){
}

private function provisionHomestead(){
if(!is_null($this->homesteadProvisionCommand)){
if(!empty($this->homesteadProvisionCommand)){
$shellOutput = shell_exec($this->homesteadProvisionCommand);
}else{
$shellOutput = shell_exec('cd '.$this->homesteadBoxPath.' && vagrant provision');
Expand Down

0 comments on commit 4e117e7

Please sign in to comment.