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)