diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b268967..59f4749 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.1, 8.2] # + php: [8.1, 8.2, 8.3] # os: [ubuntu-latest, macOS-latest] # windows-latest, # include: # - os: 'ubuntu-latest' diff --git a/phpunit.xml b/phpunit.xml index a314068..af51bd9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,24 +1,14 @@ - - - - - test - - - - - - src - - + + + + + test + + + + + src + + diff --git a/src/Extra/FileTreeBuilder.php b/src/Extra/FileTreeBuilder.php index a812b29..55dbcf3 100644 --- a/src/Extra/FileTreeBuilder.php +++ b/src/Extra/FileTreeBuilder.php @@ -153,7 +153,7 @@ public function copy(string $srcFile, string $dstFile, ?callable $afterFn = null * @param array $options = [ * 'include' => [], // limit copy files or dirs * 'exclude' => [], // exclude files or dirs - * 'renderOn' => ['*.java', ], // patterns to render + * 'renderOn' => ['*.java', ], // patterns to render file * 'afterFn' => function(string $newFile) {}, * ] * @@ -450,6 +450,18 @@ public function renderFile(string $tplFile, array $tplVars = []): static return $this->tplFile($tplFile, '', $tplVars); } + /** + * Render template vars in the give files, will update file contents to rendered. + * + * @param string ...$tplFiles + * + * @return $this + */ + public function renderFiles(string ...$tplFiles): static + { + return $this->tplFiles($tplFiles); + } + /** * Create files from template files * @@ -640,6 +652,7 @@ protected function renderPathVars(string $path): string 'workdir' => $this->workdir, ]); + $vars['projectDir'] = $this->baseDir; return Str::renderVars($path, $vars, '{%s}', true); } @@ -756,6 +769,40 @@ public function setTplVars(array $tplVars): self return $this; } + /** + * @param array $tplVars + * + * @return $this + */ + public function addTplVars(array $tplVars): self + { + $this->tplVars = array_merge($this->tplVars, $tplVars); + return $this; + } + + /** + * @param string $key + * @param mixed $value + * + * @return $this + */ + public function setTplVar(string $key, mixed $value): self + { + $this->tplVars[$key] = $value; + return $this; + } + + /** + * @param string $key + * @param mixed|null $default + * + * @return mixed + */ + public function getTplVar(string $key, mixed $default = null): mixed + { + return $this->tplVars[$key] ?? $default; + } + /** * @param callable(string $newFile): void $afterCopy * diff --git a/src/FileSystem.php b/src/FileSystem.php index d3b8e3b..9515101 100644 --- a/src/FileSystem.php +++ b/src/FileSystem.php @@ -106,17 +106,7 @@ public static function isExclude(string $path, array $patterns): bool if (!$patterns) { return false; } - - foreach ($patterns as $pattern) { - if ($pattern === '*' || $pattern === '**/*') { - return true; - } - - if (fnmatch($pattern, $path)) { - return true; - } - } - return false; + return self::isMatch($path, $patterns); } /** @@ -130,13 +120,23 @@ public static function isInclude(string $path, array $patterns): bool if (!$patterns) { return true; } + return self::isMatch($path, $patterns); + } + /** + * @param string $path + * @param array $patterns + * + * @return bool + */ + public static function isMatch(string $path, array $patterns): bool + { foreach ($patterns as $pattern) { if ($pattern === '*' || $pattern === '**/*') { return true; } - if (fnmatch($pattern, $path)) { + if ($pattern === $path || fnmatch($pattern, $path)) { return true; } } diff --git a/test/FsTest.php b/test/FsTest.php index 71df3fa..543fed6 100644 --- a/test/FsTest.php +++ b/test/FsTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Toolkit\FsUtil\FS; +use Toolkit\Stdlib\OS; use function strlen; use function vdump; @@ -37,32 +38,42 @@ public function testBasicFsMethods(): void { // join $this->assertEquals('/ab', FS::join('/ab')); - $this->assertEquals('/ab/d', FS::join('/ab', '', 'd')); - $this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e')); $this->assertEquals('/ab', FS::join('/ab', '.')); $this->assertEquals('/ab', FS::join('/ab', './')); - $this->assertEquals('/ab/cd', FS::join('/ab', './cd')); + if (OS::isWindows()) { + $this->assertEquals('/ab\\d', FS::join('/ab', '', 'd')); + $this->assertEquals('/ab\\d\\e', FS::join('/ab', 'd', 'e')); + $this->assertEquals('/ab\\cd', FS::join('/ab', './cd')); + } else { + $this->assertEquals('/ab/d', FS::join('/ab', 'd')); + $this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e')); + $this->assertEquals('/ab/cd', FS::join('/ab', './cd')); + } + } public function testIsExclude_isInclude(): void { - $this->assertTrue(FS::isInclude('./abc.php', [])); - $this->assertTrue(FS::isInclude('./abc.php', ['*'])); - $this->assertTrue(FS::isInclude('./abc.php', ['*.php'])); - $this->assertTrue(FS::isInclude('path/to/abc.php', ['*.php'])); - $this->assertFalse(FS::isInclude('./abc.php', ['*.xml'])); + $tests = [ + ['./abc.php', '*', true], + ['./abc.php', '*.php', true], + ['./abc.php', '*.yml', false], + ['path/to/abc.php', '*.php', true], + ]; + foreach ($tests as $item) { + $this->assertEquals($item[2], FS::isInclude($item[0], [$item[1]])); + $this->assertEquals($item[2], FS::isExclude($item[0], [$item[1]])); + $this->assertEquals($item[2], FS::isMatch($item[0], [$item[1]])); + } + $this->assertTrue(FS::isInclude('./abc.php', [])); $this->assertFalse(FS::isExclude('./abc.php', [])); - $this->assertTrue(FS::isExclude('./abc.php', ['*'])); - $this->assertTrue(FS::isExclude('./abc.php', ['*.php'])); - $this->assertTrue(FS::isExclude('path/to/abc.php', ['*.php'])); - $this->assertFalse(FS::isExclude('./abc.php', ['*.yml'])); } public function testRealpath(): void { $rPaths = []; - $tests = [ + $tests = [ '~', '~/.kite', ];