Skip to content

Commit

Permalink
- directory fix (#3)
Browse files Browse the repository at this point in the history
* - fix

* - fixed directories

* Apply suggestions from code review

Co-authored-by: Jacek Sawoszczuk <jacek.sawoszczuk@gmail.com>

---------

Co-authored-by: Jacek Sawoszczuk <jacek.sawoszczuk@gmail.com>
  • Loading branch information
kamilpiech97 and jsawo authored Sep 18, 2024
1 parent 3a5eaa3 commit 84a7596
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"scripts": {
"cs": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config codestyle.php",
"csf": "./vendor/bin/php-cs-fixer fix --diff --config codestyle.php",
"test": "./vendor/bin/phpunit tests --colors=always"
"test": "./vendor/bin/phpunit tests --colors=always",
"post-install-cmd": [
"chmod +x src/scripts/version.sh",
"chmod +x src/scripts/check.sh"
]
}
}
12 changes: 8 additions & 4 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

class Version
{
public const string SCRIPTS_DIRECTORY = "src/scripts/";
public const string SCRIPTS_DIRECTORY = __DIR__ . "/scripts/";
public const string PATH_TO_VERSION_SCRIPT = "cd ../../../ && ./version/src/scripts/version.sh";
public const string PATH_TO_CHECK_SCRIPT = "cd ../../../ && ./version/src/scripts/check.sh";

public function __construct(
public string $pathToVersionScript = self::PATH_TO_VERSION_SCRIPT,
public string $pathToCheckScript = self::PATH_TO_CHECK_SCRIPT,
public bool $long = false,
) {}

Expand All @@ -21,17 +25,17 @@ public function setLong(bool $long): void

public function generate(): string
{
return (new Process(["./check.sh"], self::SCRIPTS_DIRECTORY))->run()
return (new Process(["sh", "-c", $this->pathToCheckScript], self::SCRIPTS_DIRECTORY))->run()
? $this->getVersionBasedOnGit()
: $this->getVersionBasedOnTimestamp();
}

private function getVersionBasedOnGit(): string
{
$process = new Process(["./version.sh"], self::SCRIPTS_DIRECTORY);
$process = new Process(["sh", "-c", $this->pathToVersionScript], self::SCRIPTS_DIRECTORY);

if ($this->long) {
$process = new Process(["./version.sh", "--long"], self::SCRIPTS_DIRECTORY);
$process = new Process(["sh", "-c", "$this->pathToVersionScript --long"], self::SCRIPTS_DIRECTORY);
}

$process->mustRun();
Expand Down
16 changes: 10 additions & 6 deletions src/VersionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

class VersionHelper
{
public static function generateShortVersion(): string
{
return (new Version())->generate();
public static function generateShortVersion(
string $pathToVersionScript = Version::PATH_TO_VERSION_SCRIPT,
string $pathToCheckScript = Version::PATH_TO_CHECK_SCRIPT,
): string {
return (new Version($pathToVersionScript, $pathToCheckScript))->generate();
}

public static function generateLongVersion(): string
{
return (new Version(long: true))->generate();
public static function generateLongVersion(
string $pathToVersionScript = Version::PATH_TO_VERSION_SCRIPT,
string $pathToCheckScript = Version::PATH_TO_CHECK_SCRIPT,
): string {
return (new Version($pathToVersionScript, $pathToCheckScript, long: true))->generate();
}
}
10 changes: 8 additions & 2 deletions tests/VersionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ class VersionHelperTest extends TestCase
{
public function testGeneratingVersionBasedOnGit(): void
{
$version = VersionHelper::generateShortVersion();
$version = VersionHelper::generateShortVersion(
pathToVersionScript: "./version.sh",
pathToCheckScript: "./check.sh",
);

$this->assertIsString($version);
$this->assertStringNotContainsString("|", $version);
}

public function testGeneratingLongVersionBasedOnGit(): void
{
$version = VersionHelper::generateLongVersion();
$version = VersionHelper::generateLongVersion(
pathToVersionScript: "./version.sh",
pathToCheckScript: "./check.sh",
);
$versionHash = trim(shell_exec("git log --format='%h' -n 1"));

$this->assertIsString($version);
Expand Down
11 changes: 9 additions & 2 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ class VersionTest extends TestCase
{
public function testGeneratingVersionBasedOnGit(): void
{
$version = (new Version())->generate();
$version = (new Version(
pathToVersionScript: "./version.sh",
pathToCheckScript: "./check.sh",
))->generate();

$this->assertIsString($version);
$this->assertStringNotContainsString("|", $version);
}

public function testGeneratingLongVersionBasedOnGit(): void
{
$version = (new Version(true))->generate();
$version = (new Version(
pathToVersionScript: "./version.sh",
pathToCheckScript: "./check.sh",
long: true,
))->generate();
$versionHash = trim(shell_exec("git log --format='%h' -n 1"));

$this->assertIsString($version);
Expand Down

0 comments on commit 84a7596

Please sign in to comment.