composer2nix/bin/composer2nix
Sander van der Burg f9829e8b9e - Add option to deploy third party package CLI tools
- Fix bug in generation of tarball downloads
- Added some documentation
2017-09-18 22:47:28 +02:00

172 lines
5.2 KiB
PHP
Executable file

#!/usr/bin/env php
<?php
require_once(dirname(__FILE__)."/../vendor/autoload.php");
use Composer2Nix\Composer;
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)
--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
--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.
--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).
-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(
"config-file:",
"lock-file:",
"package:",
"package-version:",
"output:",
"composition:",
"composer-env:",
"prefer-source",
"prefer-dist",
"no-dev",
"name:",
"executable",
"no-copy-composer-env",
"symlink-deps",
"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;
$noCopyComposerEnv = array_key_exists("no-copy-composer-env", $options);
$preferredInstall = "dist"; // TODO: consult composer.json's preferred-install property. defaults to: auto
if(array_key_exists("prefer-source", $options))
$preferredInstall = "source";
if(array_key_exists("prefer-dist", $options))
$preferredInstall = "dist";
$noDev = array_key_exists("no-dev", $options);
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);
/* 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);
}
}
catch(Exception $ex)
{
fwrite(STDERR, $ex->getMessage()."\n");
exit(1);
}
?>