From 9231062cebcdebb17bc27be4c87c1702f2afdd0e Mon Sep 17 00:00:00 2001 From: Sander van der Burg Date: Wed, 12 Jul 2017 21:45:13 +0200 Subject: [PATCH] Add conversion routine for the path type --- README.md | 3 ++- src/Composer2Nix/Generator.php | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8681c35..2a9cdec 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,8 @@ Limitations =========== Currently, the state of this tool is that it is just a proof on concept implementation. As a result, it is lacking many features and probably buggy. -Most importantly, only the `zip`, `git`, and `hg` dependencies are supported. +Most importantly, only the `path`, `zip`, `git`, and `hg` dependencies are +supported. License ======= diff --git a/src/Composer2Nix/Generator.php b/src/Composer2Nix/Generator.php index b97d9a1..6d78f78 100644 --- a/src/Composer2Nix/Generator.php +++ b/src/Composer2Nix/Generator.php @@ -6,10 +6,16 @@ class Generator { private static function composeNixFilePath($path) { - if((strlen($path) > 0 && substr($path, 0, 1) === "/") || (strlen($path) > 1 && substr($path, 0, 2) === "./")) - return $path; + // Prefix certain relative paths so that they are of the path type in the Nix expression language + if((strlen($path) > 0 && substr($path, 0, 1) === "/") || (strlen($path) > 1 && substr($path, 0, 2) === "./") || (strlen($path) > 2 && substr($path, 0, 3) === "../")) + $normalizedPath = $path; else - return "./".$path; + $normalizedPath = "./".$path; + + if(strpos($normalizedPath, ' ') === false) + return $normalizedPath; // Path names without spaces don't require any escaping + else + return '/. + "'.$normalizedPath.'"'; // Compose a path containing spaces } private static function selectSourceObject($preferredInstall, $package) @@ -50,12 +56,17 @@ class Generator switch($sourceObj["type"]) { + case "path": + fwrite($handle, ' "'.$package["name"].'" = '.Generator::composeNixFilePath($sourceObj['url']).';'."\n"); + break; + case "zip": $hash = shell_exec('nix-prefetch-url "'.$sourceObj['url'].'"'); fwrite($handle, ' "'.$package["name"].'" = composerEnv.buildZipPackage {'."\n"); fwrite($handle, ' name = "'.strtr($package["name"], "/", "-").'-'.$sourceObj["reference"].'";'."\n"); fwrite($handle, ' url = "'.$sourceObj["url"].'";'."\n"); fwrite($handle, ' sha256 = "'.substr($hash, 0, -1).'";'."\n"); + fwrite($handle, " };\n"); break; case "git": $outputStr = shell_exec('nix-prefetch-git "'.$sourceObj['url'].'" '.$sourceObj["reference"]); @@ -68,6 +79,7 @@ class Generator fwrite($handle, ' url = "'.$sourceObj["url"].'";'."\n"); fwrite($handle, ' rev = "'.$sourceObj["reference"].'";'."\n"); fwrite($handle, ' sha256 = "'.$hash.'";'."\n"); + fwrite($handle, " };\n"); break; case "hg": $outputStr = shell_exec('nix-prefetch-hg "'.$sourceObj['url'].'" '.$sourceObj["reference"]); @@ -80,11 +92,10 @@ class Generator fwrite($handle, ' url = "'.$sourceObj["url"].'";'."\n"); fwrite($handle, ' rev = "'.$sourceObj["reference"].'";'."\n"); fwrite($handle, ' sha256 = "'.$hash.'";'."\n"); + fwrite($handle, " };\n"); default: throw new Exception("Cannot convert dependency of type: ".$sourceObj["type"]); } - - fwrite($handle, " };\n"); } fwrite($handle, " };\n");