Skip to content

Commit

Permalink
feat: Add musl() function
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 14, 2024
1 parent 8901b37 commit a687d09
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ Suppose your PHP package `foo/bar` relies on an external archive file (`examplel
"extra": {
"downloads": {
"examplelib": {
"url": "https://example.com/examplelib-{$version}-{$os}-{$architecture}.{$extension}",
"url": "https://example.com/examplelib-{$version}-{$os}-{$architecture}${$musl}.{$extension}",
"path": "extern/{$id}",
"version": "1.1.0",
"variables": {
"{$musl}": "PHP_OS === 'Linux' && musl() === true ? '-musl' : ''",
"{$os}": "strtolower(PHP_OS_FAMILY)",
"{$architecture}": "strtolower(php_uname('m'))",
"{$extension}": "PHP_OS_FAMILY === 'Windows' ? 'zip' : 'tar.gz'",
Expand Down Expand Up @@ -108,6 +109,7 @@ Custom variable support these methods:
* `str_starts_with`
* `str_ends_with`
* `matches`
* `musl`

#### Constants

Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/Validator/VariablesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Composer\Package\PackageInterface;
use LastCall\DownloadsPlugin\Attribute\AttributeManagerInterface;
use LastCall\DownloadsPlugin\Enum\Attribute;
use LastCall\DownloadsPlugin\Helper\MuslDetector;
use Le\SMPLang\Exception;
use Le\SMPLang\SMPLang;

Expand All @@ -27,6 +28,7 @@ public function __construct(
'str_contains' => str_contains(...),
'str_starts_with' => str_starts_with(...),
'str_ends_with' => str_ends_with(...),
'musl' => new MuslDetector(),
'matches' => fn (string $pattern, string $subject) => 1 === preg_match($pattern, $subject),
'PHP_OS' => \PHP_OS,
'PHP_OS_FAMILY' => \PHP_OS_FAMILY,
Expand Down
17 changes: 17 additions & 0 deletions src/Helper/MuslDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace LastCall\DownloadsPlugin\Helper;

class MuslDetector
{
public function __invoke(): bool
{
exec('ldd /bin/sh 2>&1', $output, $returnCode);
if (0 !== $returnCode) {
return false;
}
$ldd = trim(implode("\n", $output));

return str_contains($ldd, 'musl');
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Helper/MuslDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LastCall\DownloadsPlugin\Tests\Unit\Parser;

use LastCall\DownloadsPlugin\Helper\MuslDetector;
use PHPUnit\Framework\TestCase;

class MuslDetectorTest extends TestCase
{
public function testInvoke(): void
{
$detector = new MuslDetector();
// @todo Test when the result is true
$this->assertFalse($detector());
}
}

0 comments on commit a687d09

Please sign in to comment.