Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.idea
/vendor
/*.phar
.phpunit*
.phpunit*
# Local wiki symlink
docs
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,29 @@
"Gt\\Build\\Test\\": "./test/phpunit"
}
},
"config": {
"platform": {
"php": "8.2.0"
}
},

"bin": [
"bin/build"
],

"scripts": {
"phpunit": "vendor/bin/phpunit --configuration phpunit.xml",
"phpstan": "vendor/bin/phpstan analyse --level 6 src",
"phpcs": "vendor/bin/phpcs src --standard=phpcs.xml",
"phpmd": "vendor/bin/phpmd src/ text phpmd.xml",
"test": [
"@phpunit",
"@phpstan",
"@phpcs",
"@phpmd"
]
},

"funding": [
{
"type": "github",
Expand Down
5 changes: 4 additions & 1 deletion composer.lock

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

29 changes: 19 additions & 10 deletions src/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

class Build {
protected TaskList $taskList;
protected string $workingDirectory;

public function __construct(
string $jsonFilePath,
string $workingDirectory,
?string $mode = null
) {
$this->workingDirectory = $workingDirectory;
$this->taskList = new TaskList(
$jsonFilePath,
$workingDirectory,
Expand All @@ -25,18 +27,25 @@ public function __construct(
*/
public function check(?array &$errors = null):int {
$count = 0;
$previousCwd = getcwd();
chdir($this->workingDirectory);

foreach($this->taskList as $pathMatch => $task) {
$absolutePathMatch = implode(DIRECTORY_SEPARATOR, [
getcwd(),
$pathMatch,
]);
$fileList = Glob::glob($absolutePathMatch);
if(!empty($fileList)) {
$task->check($errors);
}
try {
foreach($this->taskList as $pathMatch => $task) {
$absolutePathMatch = implode(DIRECTORY_SEPARATOR, [
$this->workingDirectory,
$pathMatch,
]);
$fileList = Glob::glob($absolutePathMatch);
if(!empty($fileList)) {
$task->check($errors);
}

$count++;
$count++;
}
}
finally {
chdir($previousCwd);
}

return $count;
Expand Down
49 changes: 33 additions & 16 deletions src/BuildRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(?string $path = null, ?Stream $stream = null) {
}
$this->defaultPath = implode(DIRECTORY_SEPARATOR, [
getcwd(),
"build.json",
"build.ini",
]);
$this->workingDirectory = $path;
$this->stream = $stream;
Expand Down Expand Up @@ -68,32 +68,49 @@ protected function formatWorkingDirectory():string {

/** @SuppressWarnings(PHPMD.ExitExpression) */
protected function getJsonPath(string $workingDirectory):string {
$jsonPath = $workingDirectory;
if(is_dir($jsonPath)) {
$jsonPath .= DIRECTORY_SEPARATOR;
$jsonPath .= "build.json";
}
$jsonPath = $this->resolveConfigPath($workingDirectory)
?? $this->resolveConfigPath($this->defaultPath);

if(!is_file($jsonPath)) {
$jsonPath = $this->defaultPath;
}
if(!is_file($jsonPath)) {
$whichPath = $jsonPath === $this->defaultPath
? "default"
: "user";
if(is_null($jsonPath)) {
$whichPath = $this->defaultPath === $workingDirectory
? "user"
: "default";

$errorString = "No build config found. Trying $whichPath path: $this->defaultPath";
$this->stream->writeLine(
"No build config found. Trying $whichPath path: $jsonPath",
$errorString,
Stream::ERROR
);
// TODO: Dynamic exit code https://github.com/PhpGt/Cli/issues/13
// phpcs:ignore
exit(1);
throw new BuildException($errorString);
}

return $jsonPath;
}

protected function resolveConfigPath(string $path):?string {
if(is_dir($path)) {
$iniPath = $path . DIRECTORY_SEPARATOR . "build.ini";
if(is_file($iniPath)) {
return $iniPath;
}

$jsonPath = $path . DIRECTORY_SEPARATOR . "build.json";
if(is_file($jsonPath)) {
return $jsonPath;
}

return null;
}

if(is_file($path)) {
return $path;
}

return null;
}

/**
* @SuppressWarnings(PHPMD.ExitExpression)
* @param array<int, string> $errors
Expand All @@ -110,7 +127,7 @@ protected function checkRequirements(
$workingDirectory,
$mode,
);
} catch(JsonParseException $exception) {
} catch(ConfigurationParseException $exception) {
$this->stream->writeLine("Syntax error in $jsonPath", Stream::ERROR);
// TODO: Dynamic exit code https://github.com/PhpGt/Cli/issues/13
// phpcs:ignore
Expand Down
6 changes: 5 additions & 1 deletion src/Cli/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

class RunCommand extends Command {
public function run(?ArgumentValueList $arguments = null):int {
$buildRunner = new BuildRunner(getcwd(), $this->stream);
$path = getcwd();
if($arguments->contains("config")) {
$path = (string)$arguments->get("config");
}
$buildRunner = new BuildRunner($path, $this->stream);
if($arguments->contains("default")) {
$buildRunner->setDefaultPath($arguments->get("default"));
}
Expand Down
Loading
Loading