From c205febd606fafee2a5926e7a8ece6d5da72edae Mon Sep 17 00:00:00 2001 From: Sander van der Burg Date: Thu, 10 May 2018 16:40:39 +0200 Subject: [PATCH] Correctly compose relative paths to the composition expression --- .../Expressions/CompositionExpression.php | 19 ++++++++++++++++--- src/Composer2Nix/Generator.php | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Composer2Nix/Expressions/CompositionExpression.php b/src/Composer2Nix/Expressions/CompositionExpression.php index 74f474e..ea18390 100644 --- a/src/Composer2Nix/Expressions/CompositionExpression.php +++ b/src/Composer2Nix/Expressions/CompositionExpression.php @@ -24,16 +24,29 @@ class CompositionExpression extends NixASTNode /** Path to the composer environment expression containing the build functionality */ public $composerEnvFile; + private function relativePath($from, $to, $ps = DIRECTORY_SEPARATOR) + { + $arFrom = explode($ps, rtrim($from, $ps)); + $arTo = explode($ps, rtrim($to, $ps)); + + while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0])) + { + array_shift($arFrom); + array_shift($arTo); + } + return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo); + } + /** * Composes a new composition expression object. * * @param string $outputFile Path to the packages Nix expression * @param string $composerEnvFile Path to the composer environment expression containing the build functionality */ - public function __construct($outputFile, $composerEnvFile) + public function __construct($baseDir, $outputFile, $composerEnvFile) { - $this->outputFile = $outputFile; - $this->composerEnvFile = $composerEnvFile; + $this->outputFile = CompositionExpression::relativePath($baseDir, realpath($outputFile)); + $this->composerEnvFile = CompositionExpression::relativePath($baseDir, realpath($composerEnvFile)); } /** diff --git a/src/Composer2Nix/Generator.php b/src/Composer2Nix/Generator.php index 59ecdb6..b3aaf27 100644 --- a/src/Composer2Nix/Generator.php +++ b/src/Composer2Nix/Generator.php @@ -27,7 +27,8 @@ class Generator private static function generateCompositionExpression($compositionFile, $outputFile, $composerEnvFile) { - Generator::writeExprToFile($compositionFile, new CompositionExpression($outputFile, $composerEnvFile)); + $baseDir = realpath(dirname($compositionFile)); + Generator::writeExprToFile($compositionFile, new CompositionExpression($baseDir, $outputFile, $composerEnvFile)); } private static function copyComposerEnv($composerEnvFile, $noCopyComposerEnv)