Skip to content

Commit

Permalink
feature: build mode
Browse files Browse the repository at this point in the history
closes #262
  • Loading branch information
g105b committed Oct 19, 2023
1 parent 1315a21 commit f6866fa
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class Build {

public function __construct(
string $jsonFilePath,
string $workingDirectory
string $workingDirectory,
?string $mode = null
) {
$this->taskList = new TaskList(
$jsonFilePath,
$workingDirectory
$workingDirectory,
$mode,
);
}

Expand Down
17 changes: 13 additions & 4 deletions src/BuildRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function __construct(?string $path = null, ?Stream $stream = null) {
}

/** @SuppressWarnings(PHPMD.ExitExpression) */
public function run(bool $continue = true):void {
public function run(
bool $continue = true,
?string $mode = null
):void {
$workingDirectory = $this->formatWorkingDirectory();
$jsonPath = $this->getJsonPath($workingDirectory);

Expand All @@ -40,7 +43,7 @@ public function run(bool $continue = true):void {
// reference will suppress exceptions, instead filling the array with error
// strings for output back to the terminal.
$errors = [];
$build = $this->checkRequirements($jsonPath, $workingDirectory, $errors);
$build = $this->checkRequirements($jsonPath, $workingDirectory, $errors, $mode);

if(!empty($errors)) {
$this->showErrors($errors);
Expand Down Expand Up @@ -92,11 +95,17 @@ protected function getJsonPath(string $workingDirectory):string {
}

/** @SuppressWarnings(PHPMD.ExitExpression) */
protected function checkRequirements(string $jsonPath, string $workingDirectory, array $errors):Build {
protected function checkRequirements(
string $jsonPath,
string $workingDirectory,
array $errors,
?string $mode
):Build {
try {
$build = new Build(
$jsonPath,
$workingDirectory
$workingDirectory,
$mode,
);
} catch(JsonParseException $exception) {
$this->stream->writeLine("Syntax error in $jsonPath", Stream::ERROR);
Expand Down
11 changes: 10 additions & 1 deletion src/Cli/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public function run(ArgumentValueList $arguments = null):void {
if($arguments->contains("default")) {
$buildRunner->setDefaultPath($arguments->get("default"));
}
$buildRunner->run($arguments->contains("watch"));
$buildRunner->run(
$arguments->contains("watch"),
$arguments->get("mode"),
);
}

public function getName():string {
Expand Down Expand Up @@ -60,6 +63,12 @@ public function getOptionalParameterList():array {
"d",
"Path to a default build.json to use if there is no build.json in the project root."
),
new Parameter(
true,
"mode",
"m",
"Which version of build.json to build (e.g. production/development)."
),
];
}
}
19 changes: 18 additions & 1 deletion src/Configuration/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Manifest implements Iterator {
/** @var int Numerical index to use in iteration */
protected $iteratorIndex;

public function __construct(string $jsonFilePath) {
public function __construct(string $jsonFilePath, ?string $mode = null) {
if(!is_file($jsonFilePath)) {
throw new MissingBuildFileException($jsonFilePath);
}
Expand All @@ -27,6 +27,23 @@ public function __construct(string $jsonFilePath) {
throw new JsonParseException(json_last_error_msg());
}

if($mode) {
$modeJsonFilePath = substr(
$jsonFilePath,
0,
-strlen(".json"),
);
$modeJsonFilePath .= ".$mode.json";
if(!is_file($modeJsonFilePath)) {
throw new MissingBuildFileException($modeJsonFilePath);
}
$modeJson = json_decode(file_get_contents($modeJsonFilePath));
// For legacy reasons, stdClass is used to represent the block details.
// This code might look weird, but it remains backwards compatible until an OOP
// refactoring is made.
$json = (object)array_merge((array)$json, (array)$modeJson);
}

$this->taskBlockList = [];
foreach($json as $glob => $details) {
$this->taskBlockList []= new TaskBlock(
Expand Down
3 changes: 3 additions & 0 deletions src/Configuration/TaskBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(string $glob, object $details) {
$this->require = null;
}

if(is_null($details->execute)) {
throw new MissingConfigurationKeyException("execute");
}
$this->execute = new ExecuteBlock($details->execute);
$this->name = $details->name ?? $this->execute->command;
}
Expand Down
8 changes: 6 additions & 2 deletions src/TaskList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class TaskList implements Iterator {
/** @var int Numerical index to use in iteration */
protected $iteratorIndex;

public function __construct(string $jsonFilePath, string $baseDir = null) {
public function __construct(
string $jsonFilePath,
?string $baseDir = null,
?string $mode = null
) {
if(is_null($baseDir)) {
$baseDir = dirname($jsonFilePath);
}

$specification = new Manifest($jsonFilePath);
$specification = new Manifest($jsonFilePath, $mode);
foreach($specification as $glob => $taskBlock) {
$this->taskList[$glob] = new Task($taskBlock);
}
Expand Down
18 changes: 18 additions & 0 deletions test/phpunit/Helper/Json/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"/tmp/phpgt/build/*.txt": {
"name": "Example dev TXT",
"require": {},
"execute": {
"command": "echo",
"arguments": ["hello", "text"]
}
},
"/tmp/phpgt/build/*.md": {
"name": "Example dev MD",
"require": {},
"execute": {
"command": "echo",
"arguments": ["hello", "markdown"]
}
}
}
10 changes: 10 additions & 0 deletions test/phpunit/Helper/Json/build.other-mode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"/tmp/phpgt/build/*.txt": {
"name": "Example other mode TXT",
"require": {},
"execute": {
"command": "echo",
"arguments": ["hello", "text", "other"]
}
}
}
38 changes: 38 additions & 0 deletions test/phpunit/ManifestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Gt\Build\Test;

use Gt\Build\Configuration\Manifest;
use Gt\Build\Configuration\TaskBlock;
use PHPUnit\Framework\TestCase;

class ManifestTest extends TestCase {
public function testIterator():void {
$jsonFile = "test/phpunit/Helper/Json/build.json";
$jsonObj = json_decode(file_get_contents($jsonFile), true);
$sut = new Manifest($jsonFile);
/** @var TaskBlock $taskBlock */
foreach($sut as $taskBlock) {
$currentJsonObj = current($jsonObj);
self::assertSame($currentJsonObj["name"], $taskBlock->getName());
self::assertSame($currentJsonObj["execute"]["arguments"], $taskBlock->getExecuteBlock()->arguments);
next($jsonObj);
}
}

public function testIterator_mode():void {
$jsonFile = "test/phpunit/Helper/Json/build.json";
$jsonFileOther = "test/phpunit/Helper/Json/build.other-mode.json";
$jsonObjOther = json_decode(file_get_contents($jsonFileOther), true);
$jsonObj = json_decode(file_get_contents($jsonFile), true);
$jsonObj = array_merge($jsonObj, $jsonObjOther);

$sut = new Manifest($jsonFile, "other-mode");
/** @var TaskBlock $taskBlock */
foreach($sut as $taskBlock) {
$currentJsonObj = current($jsonObj);
self::assertSame($currentJsonObj["name"], $taskBlock->getName());
self::assertSame($currentJsonObj["execute"]["arguments"], $taskBlock->getExecuteBlock()->arguments);
next($jsonObj);
}
}
}

0 comments on commit f6866fa

Please sign in to comment.