add support for tarballs

This commit is contained in:
Charlotte 🦝 Delenk 2023-04-24 09:13:58 +01:00
parent 299caca4aa
commit d49d559980
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122
5 changed files with 96 additions and 4 deletions

View file

@ -4,7 +4,7 @@
let let
composerEnv = import ./src/Composer2Nix/composer-env.nix { composerEnv = import ./src/Composer2Nix/composer-env.nix {
inherit (pkgs) stdenv lib writeTextFile fetchurl unzip; inherit (pkgs) stdenv lib writeTextFile fetchurl unzip libarchive;
inherit php phpPackages; inherit php phpPackages;
}; };
in in

View file

@ -85,6 +85,7 @@ class CompositionExpression extends NixASTNode
"writeTextFile" => new NixInherit("pkgs"), "writeTextFile" => new NixInherit("pkgs"),
"fetchurl" => new NixInherit("pkgs"), "fetchurl" => new NixInherit("pkgs"),
"unzip" => new NixInherit("pkgs"), "unzip" => new NixInherit("pkgs"),
"libarchive" => new NixInherit("pkgs"),
"php" => new NixInherit(), "php" => new NixInherit(),
"phpPackages" => new NixInherit() "phpPackages" => new NixInherit()
)) ))

View file

@ -68,6 +68,8 @@ abstract class Source extends NixASTNode
return new PathSource($package, $sourceObj); return new PathSource($package, $sourceObj);
case "zip": case "zip":
return new ZipSource($package, $sourceObj); return new ZipSource($package, $sourceObj);
case "tar":
return new TarSource($package, $sourceObj);
case "git": case "git":
return new GitSource($package, $sourceObj); return new GitSource($package, $sourceObj);
case "hg": case "hg":

View file

@ -0,0 +1,74 @@
<?php
namespace Composer2Nix\Sources;
use Exception;
use PNDP\NixGenerator;
use PNDP\AST\NixExpression;
use PNDP\AST\NixFile;
use PNDP\AST\NixFunInvocation;
/**
* Represents a Tar package source.
*/
class TarSource extends Source
{
/* Contains a SHA1 reference to the corresponding Git revision */
public string $reference;
/** Stores the output hash of the download */
public string $hash;
/**
* Constructs a new Tar dependency instance.
*
* @param $package An array of package configuration properties
* @param $sourceObj An array of download properties
*/
public function __construct(array $package, array $sourceObj)
{
parent::__construct($package, $sourceObj);
}
/**
* @see Source::fetch()
*/
public function fetch(): void
{
$this->reference = "";
if(substr($this->sourceObj["url"], 0, 7) === "http://" || substr($this->sourceObj["url"], 0, 8) === "https://")
{
$this->hash = shell_exec('nix-prefetch-url "'.$this->sourceObj['url'].'"');
if($this->hash === false)
throw new Exception("Error while invoking nix-prefetch-url");
}
else
$this->hash = null;
}
/**
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{
$ast = parent::toNixAST();
if($this->hash === null)
$src = new NixFile($this->sourceObj['url']);
else
{
$src = new NixFunInvocation(new NixFile("fetchurl"), array(
"url" => $this->sourceObj["url"],
"sha256" => substr($this->hash, 0, -1)
));
}
$ast["src"] = new NixFunInvocation(new NixExpression("composerEnv.buildTarPackage"), array(
"name" => strtr($this->package["name"], "/", "-").$this->reference,
"src" => $src
));
return $ast;
}
}
?>

View file

@ -1,6 +1,6 @@
# This file originates from composer2nix # This file originates from composer2nix
{ stdenv, lib, writeTextFile, fetchurl, php, unzip, phpPackages }: { stdenv, lib, writeTextFile, fetchurl, php, unzip, phpPackages, libarchive }:
let let
inherit (phpPackages) composer; inherit (phpPackages) composer;
@ -22,6 +22,20 @@ let
''; '';
}; };
buildTarPackage = { name, src }:
stdenv.mkDerivation {
inherit name src;
nativeBuildInputs = [ libarchive ];
buildCommand = ''
shopt -s dotglob
bsdtar -xf $src
baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
cd $baseDir
mkdir -p $out
mv * $out
'';
};
buildPackage = buildPackage =
{ name { name
, src , src
@ -240,5 +254,6 @@ in
inherit filterSrc; inherit filterSrc;
composer = lib.makeOverridable composer; composer = lib.makeOverridable composer;
buildZipPackage = lib.makeOverridable buildZipPackage; buildZipPackage = lib.makeOverridable buildZipPackage;
buildTarPackage = lib.makeOverridable buildTarPackage;
buildPackage = lib.makeOverridable buildPackage; buildPackage = lib.makeOverridable buildPackage;
} }