Skip to content

Commit

Permalink
Match laravel repos within a subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
robbplo committed Dec 21, 2021
1 parent fce451d commit cbcd7ac
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
}
],
"require": {
"php": "^7.1|^8.0",
"php": "^7.3|^8.0",
"symfony/console": "^3.0|^4.0|^5.0",
"symfony/filesystem": "^3.0|^4.0|^5.0",
"symfony/finder": "^3.0|^4.0|^5.0",
"czproject/git-php": "^4.0"
},
"require-dev": {
Expand Down
65 changes: 63 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;

class Directory
{
Expand Down Expand Up @@ -31,15 +32,9 @@ public function getPath(string $file = null): string
return $this->path;
}

public function find(string $file): ?string
public function find(): Finder
{
$files = glob($this->getPath($file));

if (count($files) > 0 && $this->fs->exists($files[0])) {
return $files[0];
}

return null;
return (new Finder())->in($this->getPath());
}

public function contains(string $file): bool
Expand Down
34 changes: 33 additions & 1 deletion src/Drivers/LaravelDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Autopilot\Drivers;

use Symfony\Component\Filesystem\Filesystem;

class LaravelDriver extends Driver
{
public function matches(): bool
{
return $this->repository->dir()->contains('artisan');
if ($this->repository->dir()->contains('artisan')) {
return true;
}

// @todo maybe all drivers should attempt subdirectories
return $this->attemptMatchInSubdirectory();
}

public function setUp(): Driver
Expand Down Expand Up @@ -78,4 +85,29 @@ private function generateAppKey(): void

passthru("php {$artisan} key:generate");
}

private function attemptMatchInSubdirectory(): bool
{
$finder = $this->repository->dir()->find()
->depth(1)
->name('artisan');

if ($finder->count() === 0) {
return false;
}

$file = array_values(iterator_to_array($finder))[0];
$this->moveSubdirectoryToRoot($file->getPath());

return true;
}

private function moveSubdirectoryToRoot(string $subdirectory)
{
$fs = new Filesystem();

$dir = $this->repository->dir();
$fs->rename($subdirectory, $dir->getPath('../temp'), true);
$fs->rename($dir->getPath('../temp'), $dir->getPath('../repository'), true);
}
}
18 changes: 9 additions & 9 deletions src/Drivers/PhpWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PhpWebDriver extends Driver
{
public function matches(): bool
{
return $this->repository->dir()->find('*.php') !== null;
return $this->repository->dir()->find()->name('*.php')->count() > 0;
}

public function setUp(): Driver
Expand All @@ -21,12 +21,12 @@ public function serve(): Driver
return $this;
}

private function getPrimaryFile(): string
{
if ($index = $this->repository->dir()->find('index.php')) {
return $index;
}

return $this->repository->dir()->find('*.php');
}
//private function getPrimaryFile(): string
//{
// if ($index = $this->repository->dir()->find('index.php')) {
// return $index;
// }
//
// return $this->repository->dir()->find('*.php');
//}
}
1 change: 0 additions & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Autopilot;

use CzProject\GitPhp\Git;
use Symfony\Component\Filesystem\Filesystem;

class Repository
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/LaravelDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public function it_matches_default_laravel_project()
$this->assertTrue($driver->matches());
}

/** @test */
public function it_matches_laravel_project_in_subdirectory()
{
$repository = new Repository("[email protected]:221b9169-0a37-11ec-a943-4213e7ee7fac/d2a8bcaa-0a37-11ec-a943-4213e7ee7fac/Modelling-Is-Paramount-25a726e6-4a9f49dc.git");
$repository->clone();

$driver = new LaravelDriver($repository);

$this->assertTrue($driver->matches());
}

/** @test */
public function it_sets_up_laravel_application()
{
Expand Down

0 comments on commit cbcd7ac

Please sign in to comment.