From 476c37e3b1099bbe0876e891e2c4d391034269d2 Mon Sep 17 00:00:00 2001 From: Prageeth Silva Date: Sat, 29 Jun 2019 00:35:36 +0200 Subject: [PATCH 1/4] Update tests to run on windows --- tests/TestCase.php | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 9e94e0b..a345182 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,14 +19,14 @@ public function setUp() { parent::setUp(); - exec('rm -rf '.__DIR__.'/temp/*'); + $this->removeTempFiles(); } public function tearDown() { parent::tearDown(); - exec('rm -rf '.__DIR__.'/temp/*'); + $this->removeTempFiles(); $this->consoleOutput = ''; } @@ -34,19 +34,24 @@ public function tearDown() public function createTempFiles($files = []) { foreach ($files as $dir => $dirFiles) { - mkdir(__DIR__.'/temp/'.$dir); + mkdir(__DIR__.'/temp/'.$dir, 0777, true); foreach ($dirFiles as $file => $content) { if (is_array($content)) { - mkdir(__DIR__.'/temp/'.$dir.'/'.$file); + mkdir(__DIR__.'/temp/'.$dir.'/'.$file, 0777, true); foreach ($content as $subDir => $subContent) { - mkdir(__DIR__.'/temp/vendor/'.$file.'/'.$subDir); + mkdir(__DIR__.'/temp/vendor/'.$file.'/'.$subDir, 0777, true); foreach ($subContent as $subFile => $subsubContent) { file_put_contents(__DIR__.'/temp/'.$dir.'/'.$file.'/'.$subDir.'/'.$subFile.'.php', $subsubContent); } } } else { + $fileParts = explode('/', $file); + if (count($fileParts) > 1) { + $fileParts = array_slice($fileParts, 0, count($fileParts) - 1); + } + mkdir(__DIR__.'/temp/'.$dir.'/'.implode('/', $fileParts), 0777, true); file_put_contents(__DIR__.'/temp/'.$dir.'/'.$file.'.php', $content); } } @@ -71,4 +76,26 @@ public function consoleOutput() { return $this->consoleOutput ?: $this->consoleOutput = $this->app[Kernel::class]->output(); } + + private function removeTempFiles() { + $this->rrmdir(__DIR__.'/temp', '/\.gitignore$/i', true); + } + + private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { + $this->rrmdir($dir.DIRECTORY_SEPARATOR.$object, $ignoreRegex); + } else if (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { + unlink($dir.DIRECTORY_SEPARATOR.$object); + } + } + } + if (empty($skipTopLevel)) { + rmdir($dir); + } + } + } } From 11c104970f0a9a0da49a39a4ee84a50f0dc19fde Mon Sep 17 00:00:00 2001 From: Prageeth Silva Date: Sat, 29 Jun 2019 00:35:52 +0200 Subject: [PATCH 2/4] Allow nested langauge files --- src/Manager.php | 25 +++++++++++++++++-------- tests/ShowCommandTest.php | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Manager.php b/src/Manager.php index 4ff5824..306df59 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -57,21 +57,30 @@ public function files() return $this->disk->extension($file) == 'php'; }); - $filesByFile = $files->groupBy(function ($file) { - $fileName = $file->getBasename('.'.$file->getExtension()); + $languageDirectoryIndex = count(explode(DIRECTORY_SEPARATOR, $this->path)); + $filesByFile = $files->groupBy(function ($file) use ($languageDirectoryIndex) { + $fileName = $file->getBasename('.'.$file->getExtension()); if (Str::contains($file->getPath(), 'vendor')) { $fileName = str_replace('.php', '', $file->getFileName()); - $packageName = basename(dirname($file->getPath())); - return "{$packageName}::{$fileName}"; } else { - return $fileName; + $directories = explode(DIRECTORY_SEPARATOR, $file->getPathname()); + // remove lang path + $directories = array_slice($directories, $languageDirectoryIndex + 1); + // remove file name (inc. extension) + $directories = array_slice($directories, 0, count($directories) - 1); + // add file name without extension + $directories[] = $fileName; + // use the laravel SEPARATOR for lang, not the platform specific one + return implode('/', $directories); } - })->map(function ($files) { - return $files->keyBy(function ($file) { - return basename($file->getPath()); + })->map(function ($files) use($languageDirectoryIndex) { + return $files->keyBy(function ($file) use($languageDirectoryIndex) { + $directories = explode(DIRECTORY_SEPARATOR, $file->getPath()); + // ignoring the path directories, the very next one is the language code + return $directories[$languageDirectoryIndex]; })->map(function ($file) { return $file->getRealPath(); }); diff --git a/tests/ShowCommandTest.php b/tests/ShowCommandTest.php index 904c7c7..6da0b48 100644 --- a/tests/ShowCommandTest.php +++ b/tests/ShowCommandTest.php @@ -69,6 +69,24 @@ public function testCommandOutputForFileWithNestedKeys() $this->assertRegExp('/name.last(?:.*)last/', $this->consoleOutput()); } + public function testCommandOutputForFileWithNestedFiles() + { + + $separator = '/'; + $nestedFile = "nested{$separator}user"; // nested/user + + $this->createTempFiles([ + 'en' => [$nestedFile => " ['first' => 'first', 'last' => 'last']];"], + 'sp' => [$nestedFile => " ['first' => 'firstsp']];"], + ]); + + $this->artisan('langman:show', ['key' => 'nested/user']); + + $this->assertRegExp('/key(?:.*)en(?:.*)sp/', $this->consoleOutput()); + $this->assertRegExp('/name.first(?:.*)first(?:.*)firstsp/', $this->consoleOutput()); + $this->assertRegExp('/name.last(?:.*)last/', $this->consoleOutput()); + } + public function testCommandOutputForKey() { $this->createTempFiles([ From 07448b1b6dff354d02a2e09d9f41cfd35b422dc2 Mon Sep 17 00:00:00 2001 From: Prageeth Silva Date: Sat, 29 Jun 2019 00:45:57 +0200 Subject: [PATCH 3/4] Fix styles --- src/Manager.php | 4 ++-- tests/ShowCommandTest.php | 1 - tests/TestCase.php | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Manager.php b/src/Manager.php index 306df59..7b68424 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -76,8 +76,8 @@ public function files() // use the laravel SEPARATOR for lang, not the platform specific one return implode('/', $directories); } - })->map(function ($files) use($languageDirectoryIndex) { - return $files->keyBy(function ($file) use($languageDirectoryIndex) { + })->map(function ($files) use ($languageDirectoryIndex) { + return $files->keyBy(function ($file) use ($languageDirectoryIndex) { $directories = explode(DIRECTORY_SEPARATOR, $file->getPath()); // ignoring the path directories, the very next one is the language code return $directories[$languageDirectoryIndex]; diff --git a/tests/ShowCommandTest.php b/tests/ShowCommandTest.php index 6da0b48..eb62945 100644 --- a/tests/ShowCommandTest.php +++ b/tests/ShowCommandTest.php @@ -71,7 +71,6 @@ public function testCommandOutputForFileWithNestedKeys() public function testCommandOutputForFileWithNestedFiles() { - $separator = '/'; $nestedFile = "nested{$separator}user"; // nested/user diff --git a/tests/TestCase.php b/tests/TestCase.php index a345182..c9cfed2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -77,25 +77,27 @@ public function consoleOutput() return $this->consoleOutput ?: $this->consoleOutput = $this->app[Kernel::class]->output(); } - private function removeTempFiles() { + private function removeTempFiles() + { $this->rrmdir(__DIR__.'/temp', '/\.gitignore$/i', true); } - private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) { + private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) + { if (is_dir($dir)) { - $objects = scandir($dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { - $this->rrmdir($dir.DIRECTORY_SEPARATOR.$object, $ignoreRegex); - } else if (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { - unlink($dir.DIRECTORY_SEPARATOR.$object); - } + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { + $this->rrmdir($dir.DIRECTORY_SEPARATOR.$object, $ignoreRegex); + } else if (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { + unlink($dir.DIRECTORY_SEPARATOR.$object); + } + } + } + if (empty($skipTopLevel)) { + rmdir($dir); } - } - if (empty($skipTopLevel)) { - rmdir($dir); - } } } } From aeab90370d8566c503849e455c190f1162a23adc Mon Sep 17 00:00:00 2001 From: Prageeth Silva Date: Sat, 29 Jun 2019 00:47:14 +0200 Subject: [PATCH 4/4] Clean up whitespace --- tests/TestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index c9cfed2..a5a742c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -79,7 +79,7 @@ public function consoleOutput() private function removeTempFiles() { - $this->rrmdir(__DIR__.'/temp', '/\.gitignore$/i', true); + $this->rrmdir(__DIR__.'/temp', '/\.gitignore$/i', true); } private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) @@ -90,7 +90,7 @@ private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) if ($object != "." && $object != "..") { if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { $this->rrmdir($dir.DIRECTORY_SEPARATOR.$object, $ignoreRegex); - } else if (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { + } elseif (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { unlink($dir.DIRECTORY_SEPARATOR.$object); } }