From fce451da04078fed432c0a53a78376728aa758a9 Mon Sep 17 00:00:00 2001 From: Robbin Ploeger Date: Tue, 21 Dec 2021 13:16:49 +0100 Subject: [PATCH] Complete laravel setup --- autopilot | 8 ++++---- src/Drivers/LaravelDriver.php | 31 +++++++++++++++++++++++++------ src/Tasks/Task.php | 9 +++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/Tasks/Task.php diff --git a/autopilot b/autopilot index db280f4..0770c36 100755 --- a/autopilot +++ b/autopilot @@ -29,17 +29,17 @@ use Symfony\Component\Console\Style\SymfonyStyle; $io->title("Bit Academy Autopilot"); $io->writeln("Cloning repository..."); - $repository = new Repository($input->getFirstArgument()); - $repository->clone(); + $repo = new Repository($input->getFirstArgument()); + $repo->clone(); - $driver = (new ProjectClassifier($repository))->selectDriver(); + $driver = (new ProjectClassifier($repo))->selectDriver(); $type = (new ReflectionClass($driver))->getShortName(); $io->writeln("Identified project type: {$type}"); // @todo allow user to select program $io->writeln('Opening text editor'); - (new TextEditor("code"))->open($repository->getPath()); + (new TextEditor("code"))->open($repo->dir()->getPath()); $driver->setUp(); diff --git a/src/Drivers/LaravelDriver.php b/src/Drivers/LaravelDriver.php index 00c37f8..3d77c65 100644 --- a/src/Drivers/LaravelDriver.php +++ b/src/Drivers/LaravelDriver.php @@ -11,10 +11,12 @@ public function matches(): bool public function setUp(): Driver { + $this->installDependencies(); $this->createEnvFile(); + $this->generateAppKey(); $this->createDatabase(); $this->setEnvDatabase(); - $this->installDependencies(); + $this->migrateAndSeed(); return $this; } @@ -30,19 +32,22 @@ public function serve(): Driver public function getDatabaseName(): string { - return basename($this->repository->dir()->getPath()); + return "autopilot"; } private function createDatabase(): void { - // @todo this seems really unsafe :) maybe replace with db drivers - exec(sprintf('mysql -q --user=bit_academy --password=bit_academy -e "create database %s" > /dev/null 2>&1', $this->getDatabaseName())); + // @todo this seems really unsafe :) maybe replace with proper sql client + exec(sprintf( + 'mysql -q --user=bit_academy --password=bit_academy -e "drop database if exists %s; create database %s" > /dev/null 2>&1', + $this->getDatabaseName(), + $this->getDatabaseName() + )); } private function createEnvFile(): void { copy($this->repository->dir()->getPath('.env.example'), $this->repository->dir()->getPath('.env')); - exec('php artisan key:generate'); } private function setEnvDatabase(): void @@ -50,7 +55,7 @@ private function setEnvDatabase(): void $path = $this->repository->dir()->getPath('.env'); $envFile = file_get_contents($path); - $envFile = str_replace('DB_DATABASE=', 'DB_DATABASE=' . $this->getDatabaseName(), $envFile); + $envFile = preg_replace("/DB_DATABASE=.+/", 'DB_DATABASE=' . $this->getDatabaseName(), $envFile); file_put_contents($path, $envFile); } @@ -59,4 +64,18 @@ private function installDependencies(): void $dir = $this->repository->dir()->getPath(); exec("composer install -q --working-dir=$dir"); } + + private function migrateAndSeed(): void + { + $artisan = $this->repository->dir()->getPath('artisan'); + + exec("php {$artisan} migrate --seed"); + } + + private function generateAppKey(): void + { + $artisan = $this->repository->dir()->getPath('artisan'); + + passthru("php {$artisan} key:generate"); + } } diff --git a/src/Tasks/Task.php b/src/Tasks/Task.php new file mode 100644 index 0000000..4c0a00d --- /dev/null +++ b/src/Tasks/Task.php @@ -0,0 +1,9 @@ +