From 8ed03cefc05f0a5f20079ac24016711469061fa3 Mon Sep 17 00:00:00 2001 From: Brian Dillingham Date: Sat, 8 Jul 2017 14:57:43 -0400 Subject: [PATCH 1/2] add composer create-project / closes #3 --- .env.example | 2 ++ .env.example-windows | 2 ++ .env.example-windows-d-drive | 2 ++ app/Commands/Host.php | 57 +++++++++++++++++++++++++----------- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index de61bda..c4dcccf 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ +PROJECTS_FOLDER=/Users/*USER*/Projects/ +COMPOSER_PROJECT=laravel/laravel DEFAULT_FOLDER_SUFFIX=/public DEFAULT_DOMAIN_EXTENSION=.app HOSTS_FILE_PATH=/etc/hosts diff --git a/.env.example-windows b/.env.example-windows index ec9bbdf..95e0a88 100644 --- a/.env.example-windows +++ b/.env.example-windows @@ -1,3 +1,5 @@ +PROJECTS_FOLDER=C:\Users\*USER*\Projects +DEFAULT_COMPOSER_PROJECT=laravel/laravel DEFAULT_FOLDER_SUFFIX=/public DEFAULT_DOMAIN_EXTENSION=.app HOSTS_FILE_PATH=C:\Windows\System32\drivers\etc\hosts diff --git a/.env.example-windows-d-drive b/.env.example-windows-d-drive index 8bd158b..b44c2aa 100644 --- a/.env.example-windows-d-drive +++ b/.env.example-windows-d-drive @@ -1,3 +1,5 @@ +PROJECTS_FOLDER=D:\Projects +DEFAULT_COMPOSER_PROJECT=laravel/laravel DEFAULT_FOLDER_SUFFIX=/public DEFAULT_DOMAIN_EXTENSION=.app HOSTS_FILE_PATH=C:\Windows\System32\drivers\etc\hosts diff --git a/app/Commands/Host.php b/app/Commands/Host.php index 5572e4a..4f57792 100644 --- a/app/Commands/Host.php +++ b/app/Commands/Host.php @@ -15,6 +15,8 @@ class Host extends Command private $inputInterface; private $outputInterface; + private $name; + private $composerProject; private $folder; private $folderSuffix; private $database; @@ -43,7 +45,9 @@ private function init(InputInterface $input, OutputInterface $output){ } private function updateFromConfig(){ + $this->folder = getenv('PROJECTS_FOLDER'); $this->folderSuffix = getenv('DEFAULT_FOLDER_SUFFIX'); + $this->composerProject = getenv('DEFAULT_COMPOSER_PROJECT'); $this->hostPath = getenv('HOSTS_FILE_PATH'); $this->hostIP = getenv('HOMESTEAD_HOST_IP'); $this->homesteadPath = getenv('HOMESTEAD_FILE_PATH'); @@ -57,18 +61,24 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->init($input, $output); + $this->name = $this->getNameFromQuestion(); + + $this->composerProject = $this->getComposerProjectFromQuestion(); + $this->folder = $this->getFolderFromQuestion(); - if(empty($this->folder)){ - $output->writeln('Host creation failed. No folder set'); - return; - } - $this->folderSuffix = $this->getFolderSuffixFromQuestion(); + if($this->composerProject != 'laravel/laravel') + { + $this->folderSuffix = $this->getFolderSuffixFromQuestion(); + } $this->database = $this->getDatabaseFromQuestion(); $this->domain = $this->getDomainFromQuestion(); + $output->writeln('Creating project..'); + $this->createProject(); + $output->writeln('Create host ('.$this->domain.')...'); $this->updateHostsFile(); @@ -83,33 +93,41 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('Complete!'); + $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 is your project's folder? ($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); } @@ -128,6 +146,11 @@ private function defaultDomainNameFromKey($key){ return $key; } + private function createProject() + { + $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); @@ -137,7 +160,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); From 4c4c343b2d52ec2861481b39ca0419c63947213f Mon Sep 17 00:00:00 2001 From: Jeff Kilroy Date: Sat, 8 Jul 2017 19:50:02 -0400 Subject: [PATCH 2/2] - Disable composer creation by default (instead set USE_COMPOSER=true) - rename PROJECTS_FOLDER to LOCAL_SITES_PATH for consistency with HOMESTEAD_SITES_PATH --- .env.example | 7 ++++--- .env.example-windows | 5 +++-- .env.example-windows-d-drive | 5 +++-- app/Commands/Host.php | 23 +++++++++++++++-------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index c4dcccf..80df257 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,10 @@ -PROJECTS_FOLDER=/Users/*USER*/Projects/ -COMPOSER_PROJECT=laravel/laravel +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 \ No newline at end of file +HOMESTEAD_BOX_PATH=/Users/*USER*/Homestead +LOCAL_SITES_PATH=/Users/*USER*/Code/ \ No newline at end of file diff --git a/.env.example-windows b/.env.example-windows index 95e0a88..75ff5fb 100644 --- a/.env.example-windows +++ b/.env.example-windows @@ -1,9 +1,10 @@ -PROJECTS_FOLDER=C:\Users\*USER*\Projects 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 \ No newline at end of file +HOMESTEAD_BOX_PATH=C:\Homestead +LOCAL_SITES_PATH=/Users/*USER*/Code/ \ No newline at end of file diff --git a/.env.example-windows-d-drive b/.env.example-windows-d-drive index b44c2aa..db6a04d 100644 --- a/.env.example-windows-d-drive +++ b/.env.example-windows-d-drive @@ -1,5 +1,5 @@ -PROJECTS_FOLDER=D:\Projects 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 @@ -7,4 +7,5 @@ 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" \ No newline at end of file +HOMESTEAD_PROVISION_COMMAND="cd /d D: && cd /Homestead && vagrant provision" +LOCAL_SITES_PATH=D:\Projects \ No newline at end of file diff --git a/app/Commands/Host.php b/app/Commands/Host.php index 4f57792..7709b82 100644 --- a/app/Commands/Host.php +++ b/app/Commands/Host.php @@ -17,6 +17,7 @@ class Host extends Command private $name; private $composerProject; + private $useComposer; private $folder; private $folderSuffix; private $database; @@ -38,15 +39,16 @@ 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('PROJECTS_FOLDER'); + $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'); @@ -63,7 +65,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->name = $this->getNameFromQuestion(); - $this->composerProject = $this->getComposerProjectFromQuestion(); + if($this->useComposer){ + $this->composerProject = $this->getComposerProjectFromQuestion(); + } $this->folder = $this->getFolderFromQuestion(); @@ -76,13 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->domain = $this->getDomainFromQuestion(); - $output->writeln('Creating project..'); - $this->createProject(); + if($this->useComposer && !empty($this->composerProject)){ + $output->writeln('Creating project..'); + $this->createProject(); + } $output->writeln('Create host ('.$this->domain.')...'); $this->updateHostsFile(); - $output->writeln('Update Vagrant site mapper (/'.$this->folder.')'); + $output->writeln('Update Vagrant site mapper ('.$this->folder.')'); $this->updateHomesteadSites(); $output->writeln('Update Vagrant database ('.$this->database.')'); @@ -110,7 +116,7 @@ private function getComposerProjectFromQuestion(){ } private function getFolderFromQuestion(){ - $question = new Question("What is your project's folder? ($this->folder): ", $this->folder); + $question = new Question("What local directory will store your project? ($this->folder): ", $this->folder); return $this->questionHelper->ask($this->inputInterface, $this->outputInterface, $question); } @@ -148,6 +154,7 @@ private function defaultDomainNameFromKey($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}"); } @@ -177,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');