composer2nix/bin/composer2nix

173 lines
5.2 KiB
Text
Raw Normal View History

2017-07-07 20:51:59 +00:00
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__)."/../vendor/autoload.php");
use Composer2Nix\Composer;
2017-07-07 20:51:59 +00:00
use Composer2Nix\Generator;
function displayHelp($command)
{
print("Usage: ".$command." [OPTION]\n\n");
echo <<<EOT
This executable can be used to generate Nix expressions from a composer.lock
(and a composer.json) file so that a package and all its dependencies can be
deployed by the Nix package manager.
Options:
--config-file=FILE Path to the composer.json file (defaults to:
composer.json)
--lock-file=FILE Path to the composer.lock file (defaults to:
composer.lock)
-p, --package Package to deploy as a command-line utility
--package-version Preferred version of the package to deploy (defaults
to the latest version)
2017-07-07 20:51:59 +00:00
--output=FILE Path to the Nix expression containing the generated
packages (defaults to: php-packages.nix)
--composition=FILE Path to the Nix expression that composes the package
(defaults to: default.nix)
--composer-env=FILE Path to the Nix expression deploying the composer
packages (defaults to: composer-env.nix)
--prefer-source Forces installation from package sources when possible
--prefer-dist Forces installation from package dist
--no-dev Do not install the development packages
2017-07-07 20:51:59 +00:00
--name Name of the generated package (defaults to the name
provided in the composer.json file)
--executable Generate a Nix package for an executable as opposed to
a library or web application.
2017-07-07 20:51:59 +00:00
--no-copy-composer-env
Do not create a copy of the Nix expression that builds
composer packages
--symlink-deps Symlink the dependencies as opposed to copying them.
This is more space efficient, but not all packages may
be able to load their dependencies if they depend on
symlink resolving (defaults to: false).
2017-07-07 20:51:59 +00:00
-h, --help Shows the usage of this command
-v, --version Shows the version of this command
EOT;
}
function displayVersion($command)
{
print($command." (composer2nix 0.0.1)\n\nCopyright (C) 2017 Sander van der Burg\n");
}
/* Parse command line options */
$options = getopt("p:hv", array(
2017-07-07 20:51:59 +00:00
"config-file:",
"lock-file:",
"package:",
"package-version:",
2017-07-07 20:51:59 +00:00
"output:",
"composition:",
"composer-env:",
"prefer-source",
"prefer-dist",
"no-dev",
2017-07-07 20:51:59 +00:00
"name:",
"executable",
2017-07-07 20:51:59 +00:00
"no-copy-composer-env",
"symlink-deps",
2017-07-07 20:51:59 +00:00
"help",
"version"
));
if($options === false)
{
fwrite(STDERR, "Cannot parse the command-line options!\n");
exit(1);
}
/* Parse the options themselves */
if(array_key_exists("h", $options) || array_key_exists("help", $options))
{
displayHelp($argv[0]);
exit();
}
if(array_key_exists("v", $options) || array_key_exists("version", $options))
{
displayVersion($argv[0]);
exit();
}
if(array_key_exists("config-file", $options))
$configFile = $options["config-file"];
else
$configFile = "composer.json";
if(array_key_exists("lock-file", $options))
$lockFile = $options["lock-file"];
else
$lockFile = "composer.lock";
if(array_key_exists("output", $options))
$outputFile = $options["output"];
else
$outputFile = "php-packages.nix";
if(array_key_exists("composition", $options))
$compositionFile = $options["composition"];
else
$compositionFile = "default.nix";
if(array_key_exists("composer-env", $options))
$composerEnvFile = $options["composer-env"];
else
$composerEnvFile = "composer-env.nix";
if(array_key_exists("p", $options))
$package = $options["p"];
else if(array_key_exists("package", $options))
$package = $options["package"];
else
$package = null;
if(array_key_exists("package-version", $options))
$versionSpec = $options["package-version"];
else
$versionSpec = null;
2017-07-07 20:51:59 +00:00
$noCopyComposerEnv = array_key_exists("no-copy-composer-env", $options);
$preferredInstall = "dist"; // TODO: consult composer.json's preferred-install property. defaults to: auto
2017-07-07 20:51:59 +00:00
if(array_key_exists("prefer-source", $options))
$preferredInstall = "source";
2017-07-07 20:51:59 +00:00
if(array_key_exists("prefer-dist", $options))
$preferredInstall = "dist";
$noDev = array_key_exists("no-dev", $options);
2017-07-07 20:51:59 +00:00
if(array_key_exists("name", $options))
$name = $options["name"];
else
$name = null;
$symlinkDependencies = array_key_exists("symlink-deps", $options);
$executable = array_key_exists("executable", $options);
2017-07-07 20:51:59 +00:00
/* Execute the generator */
try
{
if($package === null)
Generator::generateNixExpressions($name, $executable, $preferredInstall, $noDev, $configFile, $lockFile, $outputFile, $compositionFile, $composerEnvFile, $noCopyComposerEnv, $symlinkDependencies);
else
{
Composer::composePackageFromDependency($package, $versionSpec, $preferredInstall, $noDev);
Generator::generateNixExpressions($package, true, $preferredInstall, $noDev, "composer.json", "composer.lock", $outputFile, $compositionFile, $composerEnvFile, $noCopyComposerEnv, $symlinkDependencies);
}
2017-07-07 20:51:59 +00:00
}
catch(Exception $ex)
{
fwrite(STDERR, $ex->getMessage()."\n");
exit(1);
}
?>