Skip to content

Commit 0e3b716

Browse files
authored
Add support for INI configuration files (#288)
* feature: ini config closes #270 * test: improve coverage
1 parent 27ae83f commit 0e3b716

15 files changed

Lines changed: 558 additions & 44 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/.idea
22
/vendor
33
/*.phar
4-
.phpunit*
4+
.phpunit*
5+
# Local wiki symlink
6+
docs

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,29 @@
2929
"Gt\\Build\\Test\\": "./test/phpunit"
3030
}
3131
},
32+
"config": {
33+
"platform": {
34+
"php": "8.2.0"
35+
}
36+
},
3237

3338
"bin": [
3439
"bin/build"
3540
],
3641

42+
"scripts": {
43+
"phpunit": "vendor/bin/phpunit --configuration phpunit.xml",
44+
"phpstan": "vendor/bin/phpstan analyse --level 6 src",
45+
"phpcs": "vendor/bin/phpcs src --standard=phpcs.xml",
46+
"phpmd": "vendor/bin/phpmd src/ text phpmd.xml",
47+
"test": [
48+
"@phpunit",
49+
"@phpstan",
50+
"@phpcs",
51+
"@phpmd"
52+
]
53+
},
54+
3755
"funding": [
3856
{
3957
"type": "github",

composer.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
class Build {
77
protected TaskList $taskList;
8+
protected string $workingDirectory;
89

910
public function __construct(
1011
string $jsonFilePath,
1112
string $workingDirectory,
1213
?string $mode = null
1314
) {
15+
$this->workingDirectory = $workingDirectory;
1416
$this->taskList = new TaskList(
1517
$jsonFilePath,
1618
$workingDirectory,
@@ -25,18 +27,25 @@ public function __construct(
2527
*/
2628
public function check(?array &$errors = null):int {
2729
$count = 0;
30+
$previousCwd = getcwd();
31+
chdir($this->workingDirectory);
2832

29-
foreach($this->taskList as $pathMatch => $task) {
30-
$absolutePathMatch = implode(DIRECTORY_SEPARATOR, [
31-
getcwd(),
32-
$pathMatch,
33-
]);
34-
$fileList = Glob::glob($absolutePathMatch);
35-
if(!empty($fileList)) {
36-
$task->check($errors);
37-
}
33+
try {
34+
foreach($this->taskList as $pathMatch => $task) {
35+
$absolutePathMatch = implode(DIRECTORY_SEPARATOR, [
36+
$this->workingDirectory,
37+
$pathMatch,
38+
]);
39+
$fileList = Glob::glob($absolutePathMatch);
40+
if(!empty($fileList)) {
41+
$task->check($errors);
42+
}
3843

39-
$count++;
44+
$count++;
45+
}
46+
}
47+
finally {
48+
chdir($previousCwd);
4049
}
4150

4251
return $count;

src/BuildRunner.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(?string $path = null, ?Stream $stream = null) {
2222
}
2323
$this->defaultPath = implode(DIRECTORY_SEPARATOR, [
2424
getcwd(),
25-
"build.json",
25+
"build.ini",
2626
]);
2727
$this->workingDirectory = $path;
2828
$this->stream = $stream;
@@ -68,32 +68,49 @@ protected function formatWorkingDirectory():string {
6868

6969
/** @SuppressWarnings(PHPMD.ExitExpression) */
7070
protected function getJsonPath(string $workingDirectory):string {
71-
$jsonPath = $workingDirectory;
72-
if(is_dir($jsonPath)) {
73-
$jsonPath .= DIRECTORY_SEPARATOR;
74-
$jsonPath .= "build.json";
75-
}
71+
$jsonPath = $this->resolveConfigPath($workingDirectory)
72+
?? $this->resolveConfigPath($this->defaultPath);
7673

77-
if(!is_file($jsonPath)) {
78-
$jsonPath = $this->defaultPath;
79-
}
80-
if(!is_file($jsonPath)) {
81-
$whichPath = $jsonPath === $this->defaultPath
82-
? "default"
83-
: "user";
74+
if(is_null($jsonPath)) {
75+
$whichPath = $this->defaultPath === $workingDirectory
76+
? "user"
77+
: "default";
8478

79+
$errorString = "No build config found. Trying $whichPath path: $this->defaultPath";
8580
$this->stream->writeLine(
86-
"No build config found. Trying $whichPath path: $jsonPath",
81+
$errorString,
8782
Stream::ERROR
8883
);
8984
// TODO: Dynamic exit code https://github.com/PhpGt/Cli/issues/13
9085
// phpcs:ignore
91-
exit(1);
86+
throw new BuildException($errorString);
9287
}
9388

9489
return $jsonPath;
9590
}
9691

92+
protected function resolveConfigPath(string $path):?string {
93+
if(is_dir($path)) {
94+
$iniPath = $path . DIRECTORY_SEPARATOR . "build.ini";
95+
if(is_file($iniPath)) {
96+
return $iniPath;
97+
}
98+
99+
$jsonPath = $path . DIRECTORY_SEPARATOR . "build.json";
100+
if(is_file($jsonPath)) {
101+
return $jsonPath;
102+
}
103+
104+
return null;
105+
}
106+
107+
if(is_file($path)) {
108+
return $path;
109+
}
110+
111+
return null;
112+
}
113+
97114
/**
98115
* @SuppressWarnings(PHPMD.ExitExpression)
99116
* @param array<int, string> $errors
@@ -110,7 +127,7 @@ protected function checkRequirements(
110127
$workingDirectory,
111128
$mode,
112129
);
113-
} catch(JsonParseException $exception) {
130+
} catch(ConfigurationParseException $exception) {
114131
$this->stream->writeLine("Syntax error in $jsonPath", Stream::ERROR);
115132
// TODO: Dynamic exit code https://github.com/PhpGt/Cli/issues/13
116133
// phpcs:ignore

src/Cli/RunCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
class RunCommand extends Command {
1111
public function run(?ArgumentValueList $arguments = null):int {
12-
$buildRunner = new BuildRunner(getcwd(), $this->stream);
12+
$path = getcwd();
13+
if($arguments->contains("config")) {
14+
$path = (string)$arguments->get("config");
15+
}
16+
$buildRunner = new BuildRunner($path, $this->stream);
1317
if($arguments->contains("default")) {
1418
$buildRunner->setDefaultPath($arguments->get("default"));
1519
}

0 commit comments

Comments
 (0)