From f4d7fc001e942e97048c5775cc2fd259637c8b66 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 13 Apr 2018 17:28:33 +0100 Subject: [PATCH] Fixed phar building issues on Windows paths (backslashes), closes #29 --- src/DevTools/ConsoleScript.php | 15 +++++++-------- src/DevTools/DevTools.php | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/DevTools/ConsoleScript.php b/src/DevTools/ConsoleScript.php index 26b13b7..3de3218 100644 --- a/src/DevTools/ConsoleScript.php +++ b/src/DevTools/ConsoleScript.php @@ -39,7 +39,7 @@ } //Convert to absolute path for base path detection - $path = rtrim(str_replace("\\", "/", $realPath), "/") . "/"; + $path = rtrim($realPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; }); $basePath = ""; @@ -48,10 +48,10 @@ echo "You must specify a relative path with --relative to be able to include multiple directories" . PHP_EOL; exit(1); }else{ - $basePath = rtrim(str_replace("\\", "/", realpath(array_shift($includedPaths))), "/") . "/"; + $basePath = rtrim(realpath(array_shift($includedPaths)), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } }else{ - $basePath = rtrim(str_replace("\\", "/", realpath($opts["relative"])), "/") . "/"; + $basePath = rtrim(realpath($opts["relative"]), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } //Convert included paths back to relative after we decide what the base path is @@ -89,10 +89,9 @@ die("Entry point not found"); } - $entry = addslashes(str_replace("\\", "/", $realEntry)); - $entry = str_replace($basePath, "", $entry); - echo "Setting entry point to " . $entry . PHP_EOL; - $phar->setStub('setStub('getProperty("file"); $file->setAccessible(true); - $filePath = rtrim(str_replace("\\", "/", $file->getValue($plugin)), "/") . "/"; + $filePath = realpath($file->getValue($plugin)); + assert(is_string($filePath)); + $filePath = rtrim($filePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $this->buildPhar($sender, $pharPath, $filePath, [], $metadata, $stub, \Phar::SHA1); @@ -231,8 +233,8 @@ private function makeServerCommand(CommandSender $sender, Command $command, stri $stub = 'buildPhar($sender, $pharPath, $filePath, ['src', 'vendor'], $metadata, $stub, \Phar::SHA1); @@ -248,7 +250,12 @@ private function preg_quote_array(array $strings, string $delim = null) : array{ private function buildPhar(CommandSender $sender, string $pharPath, string $basePath, array $includedPaths, array $metadata, string $stub, int $signatureAlgo = \Phar::SHA1){ if(file_exists($pharPath)){ $sender->sendMessage("Phar file already exists, overwriting..."); - \Phar::unlinkArchive($pharPath); + try{ + \Phar::unlinkArchive($pharPath); + }catch(\PharException $e){ + //unlinkArchive() doesn't like dodgy phars + unlink($pharPath); + } } $sender->sendMessage("[DevTools] Adding files..."); @@ -262,7 +269,7 @@ private function buildPhar(CommandSender $sender, string $pharPath, string $base //If paths contain any of these, they will be excluded $excludedSubstrings = [ - "/.", //"Hidden" files, git information etc + DIRECTORY_SEPARATOR . ".", //"Hidden" files, git information etc realpath($pharPath) //don't add the phar to itself ];