Skip to content

Commit

Permalink
Fixed phar building issues on Windows paths (backslashes), closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Apr 13, 2018
1 parent f4b8d77 commit f4d7fc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
15 changes: 7 additions & 8 deletions src/DevTools/ConsoleScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -48,10 +48,10 @@
echo "You must specify a relative path with --relative <path> 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
Expand Down Expand Up @@ -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('<?php require("phar://" . __FILE__ . "/' . $entry . '"); __HALT_COMPILER();');
$realEntry = addslashes(str_replace([$basePath, "\\"], ["", "/"], $realEntry));
echo "Setting entry point to " . $realEntry . PHP_EOL;
$phar->setStub('<?php require("phar://" . __FILE__ . "/' . $realEntry . '"); __HALT_COMPILER();');
}else{
if(file_exists($basePath . "plugin.yml")){
$metadata = yaml_parse_file($basePath . "plugin.yml");
Expand Down Expand Up @@ -145,7 +144,7 @@ function preg_quote_array(array $strings, string $delim = null) : array{

//If paths contain any of these, they will be excluded
$excludedSubstrings = [
"/.", //"Hidden" files, git information etc
DIRECTORY_SEPARATOR . ".", //"Hidden" files, git information etc
$pharName //don't add the phar to itself
];

Expand Down
17 changes: 12 additions & 5 deletions src/DevTools/DevTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ private function makePluginCommand(CommandSender $sender, Command $command, stri
$reflection = new \ReflectionClass("pocketmine\\plugin\\PluginBase");
$file = $reflection->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);

Expand Down Expand Up @@ -231,8 +233,8 @@ private function makeServerCommand(CommandSender $sender, Command $command, stri

$stub = '<?php require_once("phar://". __FILE__ ."/src/pocketmine/PocketMine.php"); __HALT_COMPILER();';

$filePath = realpath(\pocketmine\PATH) . "/";
$filePath = rtrim(str_replace("\\", "/", $filePath), "/") . "/";
$filePath = realpath(\pocketmine\PATH) . DIRECTORY_SEPARATOR;
$filePath = rtrim($filePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

$this->buildPhar($sender, $pharPath, $filePath, ['src', 'vendor'], $metadata, $stub, \Phar::SHA1);

Expand All @@ -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...");
Expand All @@ -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
];

Expand Down

0 comments on commit f4d7fc0

Please sign in to comment.