diff --git a/src/ProjectFinder.php b/src/ProjectFinder.php index dfe89c1..ad48ef7 100644 --- a/src/ProjectFinder.php +++ b/src/ProjectFinder.php @@ -6,21 +6,60 @@ class ProjectFinder { - public function find($taskfile = './Taskfile') + /** + * Directory to search for Taskfiles + * + * @var string + */ + private $cwd; + + /** + * Taskfile basename variants, in priority order. + * + * @var array + */ + private $variants = ['Taskfile', 'taskfile', 'taskfile.php']; + + public function __construct($cwd = null) { - if (!file_exists($taskfile)) { - throw new \InvalidArgumentException("$taskfile not found"); - } + $this->cwd = $cwd ?: getcwd(); + } + + public function getCwd() + { + return $this->cwd; + } - if (filesize($taskfile) === 0) { - throw new \LogicException("Taskfile is empty"); + /** + * @return string|false + */ + public function findTaskfile() + { + foreach ($this->variants as $variant) { + $file = $this->getCwd() . DIRECTORY_SEPARATOR . $variant; + + if (is_file($file)) { + return $file; + } + } + + return false; + } + + /** + * @return Task\Project + * @throws RuntimeException + */ + public function find() + { + if (!$taskfile = $this->findTaskfile()) { + throw new \RuntimeException("No Taskfile found"); } $project = require $taskfile; - - $cls = 'Task\Project'; - if (!($project instanceof $cls)) { - throw new \LogicException("Taskfile must return a Project"); + + if (!($project instanceof Project)) { + throw new \UnexpectedValueException("Taskfile must return an instance of 'Task\Project'"); } return $project; diff --git a/tests/fixtures/Taskfile b/tests/fixtures/Taskfile deleted file mode 100644 index cfd5822..0000000 --- a/tests/fixtures/Taskfile +++ /dev/null @@ -1,5 +0,0 @@ -find($taskfile); - $this->assertEquals(require $taskfile, $project); + $dir = dirname(__DIR__) . '/fixtures/valid-taskfile'; + + $taskfile = $dir . '/Taskfile'; + + $expected = require $taskfile; + + $actual = (new ProjectFinder($dir))->find(); + + $this->assertEquals($expected, $actual); } /** - * @expectedException InvalidArgumentException + * @expectedException RuntimeException */ public function testFindThrowsOnNoTaskFile() { - (new ProjectFinder)->find('nope'); + $dir = dirname(__DIR__) . '/fixtures/no-taskfile'; + + (new ProjectFinder($dir))->find(); } /** - * @expectedException LogicException - */ - public function testFindThrowsOnEmptyTaskFile() - { - $taskfile = tempnam(sys_get_temp_dir(), 'taskfile'); - (new ProjectFinder)->find($taskfile); - unlink($taskfile); - } - - /** - * @expectedException LogicException + * @expectedException UnexpectedValueException */ public function testFindThrowsOnNotProject() { - $taskfile = tempnam(sys_get_temp_dir(), 'taskfile'); - file_put_contents($taskfile, 'find($taskfile); - unlink($taskfile); + $dir = dirname(__DIR__) . '/fixtures/invalid-taskfile'; + + (new ProjectFinder($dir))->find(); } }