Divide the package expression into sub classes. Separate fetching from the expression generation.

This commit is contained in:
Sander van der Burg 2017-10-24 23:24:59 +02:00
parent aa74ee8cb5
commit db4c6eefdf
11 changed files with 185 additions and 90 deletions

View file

@ -52,7 +52,7 @@ class CompositionExpression extends NixASTNode
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -1,19 +1,14 @@
<?php
namespace Composer2Nix\Expressions;
use Composer2Nix\ComposerConfig;
use Composer2Nix\Package;
use Composer2Nix\SourcesCache;
use Composer2Nix\Sources\Source;
use PNDP\NixGenerator;
use PNDP\AST\NixAttrSet;
use PNDP\AST\NixASTNode;
use PNDP\AST\NixExpression;
use PNDP\AST\NixFile;
use PNDP\AST\NixFunction;
use PNDP\AST\NixFunInvocation;
use PNDP\AST\NixInherit;
use PNDP\AST\NixLet;
use PNDP\AST\NixNoDefault;
use PNDP\AST\NixObject;
use PNDP\AST\NixURL;
/**
* A representation of a packages expression specifying all dependencies
@ -21,88 +16,37 @@ use PNDP\AST\NixURL;
*/
class PackagesExpression extends NixASTNode
{
/** Specifies whether the package to be deployed is an executable project */
public $executable;
/** Contains a cache with all the dependencies' sources */
public $sourcesCache;
/** Specifies whether the dependencies should be symlinked */
public $symlinkDependencies;
/** The composer package configuration */
public $config;
/** Specifies the preferred installation source ('dist' or 'source') */
public $preferredInstall;
/** Contains all properties of the composer package to deploy */
public $package;
/**
* Creates a new packages expression instance.
*
* @param ComposerConfig $config The composer package configuration
* @param ComposerConfig $composerConfig The composer package configuration
* @param bool $executable Specifies whether the package to be deployed is an executable project
* @param bool $symlinkDependencies Specifies whether the dependencies should be symlinked
* @param string $preferredInstall Specifies the preferred installation source ('dist' or 'source')
*/
public function __construct(ComposerConfig $config, $executable, $symlinkDependencies, $preferredInstall)
public function __construct(ComposerConfig $composerConfig, $executable, $symlinkDependencies, $preferredInstall)
{
$this->executable = $executable;
$this->symlinkDependencies = $symlinkDependencies;
$this->config = $config;
$this->preferredInstall = $preferredInstall;
}
private function generateDependenciesExpr(array $packages)
{
$sources = array();
foreach($packages as $package)
{
$source = Source::constructSource($package, $this->preferredInstall);
$source->fetch();
$sources[$package["name"]] = $source;
}
return new NixAttrSet($sources);
}
private function generatePackageMetaDataExpr()
{
$meta = array();
if(array_key_exists("homepage", $this->config->values))
$meta["homepage"] = new NixURL($this->config->values["homepage"]);
if(array_key_exists("license", $this->config->values))
{
if(is_string($this->config->values["license"])) // If the license is a string, just take it
$meta["license"] = $this->config->values["license"];
else if(is_array($this->config->values["license"])) // If the license is an array, compose a comma , separated string from it
{
if(count($this->config->values["license"]) > 0)
$meta["license"] = $this->config->values["license"][0];
for($i = 1; $i < count($this->config->values["license"]); $i++)
$meta["license"] .= ", ".$this->config->values["license"][$i];
}
}
return new NixAttrSet($meta);
}
private function generatePackageBuild()
{
return new NixFunInvocation(new NixExpression("composerEnv.buildPackage"), array(
"name" => $this->config->packageName,
"src" => new NixFile("./."),
"executable" => $this->executable,
"packages" => new NixInherit(),
"devPackages" => new NixInherit(),
"noDev" => new NixInherit(),
"symlinkDependencies" => $this->symlinkDependencies,
"meta" => $this->generatePackageMetaDataExpr()
));
$this->sourcesCache = new SourcesCache($composerConfig, $preferredInstall);
$this->package = new Package($composerConfig, $executable, $symlinkDependencies);
}
/**
* @see NixAST::toNixAST()
* Fetches the package metadata of all sources.
*/
public function fetchSources()
{
$this->sourcesCache->fetchSources();
}
/**
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{
@ -113,10 +57,7 @@ class PackagesExpression extends NixASTNode
"fetchhg" => null,
"fetchsvn" => null,
"noDev" => false
), new NixLet(array(
"packages" => $this->generateDependenciesExpr($this->config->packages),
"devPackages" => $this->generateDependenciesExpr($this->config->devPackages)
), $this->generatePackageBuild()));
), new NixLet($this->sourcesCache->toNixAST(), $this->package));
}
}
?>

View file

@ -20,7 +20,9 @@ class Generator
private static function generatePackagesExpression($outputFile, ComposerConfig $config, $executable, $symlinkDependencies, $preferredInstall)
{
Generator::writeExprToFile($outputFile, new PackagesExpression($config, $executable, $symlinkDependencies, $preferredInstall));
$expr = new PackagesExpression($config, $executable, $symlinkDependencies, $preferredInstall);
$expr->fetchSources();
Generator::writeExprToFile($outputFile, $expr);
}
private static function generateCompositionExpression($compositionFile, $outputFile, $composerEnvFile)
@ -51,8 +53,8 @@ class Generator
*/
public static function generateNixExpressions($name, $executable, $preferredInstall, $noDev, $configFile, $lockFile, $outputFile, $compositionFile, $composerEnvFile, $noCopyComposerEnv, $symlinkDependencies)
{
$config = new ComposerConfig($configFile, $lockFile, $name, $noDev);
Generator::generatePackagesExpression($outputFile, $config, $executable, $symlinkDependencies, $preferredInstall);
$composerConfig = new ComposerConfig($configFile, $lockFile, $name, $noDev);
Generator::generatePackagesExpression($outputFile, $composerConfig, $executable, $symlinkDependencies, $preferredInstall);
Generator::generateCompositionExpression($compositionFile, $outputFile, $composerEnvFile);
Generator::copyComposerEnv($composerEnvFile, $noCopyComposerEnv);
}

View file

@ -0,0 +1,81 @@
<?php
namespace Composer2Nix;
use PNDP\AST\NixAttrSet;
use PNDP\AST\NixASTNode;
use PNDP\AST\NixExpression;
use PNDP\AST\NixFile;
use PNDP\AST\NixFunInvocation;
use PNDP\AST\NixInherit;
use PNDP\AST\NixURL;
/**
* A representation of an composer package that is obtained from an external
* source and that may bundle dependencies.
*/
class Package extends NixASTNode
{
/** The composer package configuration */
public $composerConfig;
/** Specifies whether the package to be deployed is an executable project */
public $executable;
/** Specifies whether the dependencies should be symlinked */
public $symlinkDependencies;
/**
* Constructs a new package instance.
*
* @param ComposerConfig $composerConfig The composer package configuration
* @param bool $executable Specifies whether the package to be deployed is an executable project
* @param bool $symlinkDependencies Specifies whether the dependencies should be symlinked
*/
public function __construct(ComposerConfig $composerConfig, $executable, $symlinkDependencies)
{
$this->composerConfig = $composerConfig;
$this->executable = $executable;
$this->symlinkDependencies = $symlinkDependencies;
}
private function generatePackageMetaDataAST()
{
$meta = array();
if(array_key_exists("homepage", $this->composerConfig->values))
$meta["homepage"] = new NixURL($this->composerConfig->values["homepage"]);
if(array_key_exists("license", $this->composerConfig->values))
{
if(is_string($this->composerConfig->values["license"])) // If the license is a string, just take it
$meta["license"] = $this->composerConfig->values["license"];
else if(is_array($this->composerConfig->values["license"])) // If the license is an array, compose a comma , separated string from it
{
if(count($this->composerConfig->values["license"]) > 0)
$meta["license"] = $this->composerConfig->values["license"][0];
for($i = 1; $i < count($this->composerConfig->values["license"]); $i++)
$meta["license"] .= ", ".$this->composerConfig->values["license"][$i];
}
}
return new NixAttrSet($meta);
}
/**
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{
return new NixFunInvocation(new NixExpression("composerEnv.buildPackage"), array(
"name" => $this->composerConfig->packageName,
"src" => new NixFile("./."),
"executable" => $this->executable,
"packages" => new NixInherit(),
"devPackages" => new NixInherit(),
"noDev" => new NixInherit(),
"symlinkDependencies" => $this->symlinkDependencies,
"meta" => $this->generatePackageMetaDataAST()
));
}
}
?>

View file

@ -39,7 +39,7 @@ class GitSource extends Source
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -34,7 +34,7 @@ class HgSource extends Source
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -30,7 +30,7 @@ class PathSource extends Source
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -32,7 +32,7 @@ class SVNSource extends Source
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -14,7 +14,8 @@ abstract class Source extends NixASTNode
protected $sourceObj;
/**
* Creates a new source instance.
* Creates a new source instance. This constructor should never be used
* directly. Instead, use Source::constructSource()
*
* @param array $package An array of package configuration properties
* @param array $sourceObj An array of download properties
@ -82,7 +83,7 @@ abstract class Source extends NixASTNode
abstract public function fetch();
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -46,7 +46,7 @@ class ZipSource extends Source
}
/**
* @see NixAST::toNixAST()
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{

View file

@ -0,0 +1,70 @@
<?php
namespace Composer2Nix;
use Composer2Nix\Sources\Source;
use PNDP\AST\NixASTNode;
use PNDP\AST\NixAttrSet;
/**
* A cache store that memorizes all packages to obtain from external sources.
*/
class SourcesCache extends NixASTNode
{
/** The composer package configuration */
public $composerConfig;
/** Specifies the preferred installation source ('dist' or 'source') */
public $preferredInstall;
/** Contains all relevant metadata for the runtime dependencies */
public $packages;
/** Contains all relevant metadata for the development dependencies */
public $devPackages;
/**
* Creates a new sources cache instance.
*
* @param ComposerConfig $composerConfig The composer package configuration
* @param string $preferredInstall Specifies the preferred installation source ('dist' or 'source')
*/
public function __construct(ComposerConfig $composerConfig, $preferredInstall)
{
$this->composerConfig = $composerConfig;
$this->preferredInstall = $preferredInstall;
}
private function fetchSourcesOfPackages(array $packages)
{
$sources = array();
foreach($packages as $package)
{
$source = Source::constructSource($package, $this->preferredInstall);
$source->fetch();
$sources[$package["name"]] = $source;
}
return $sources;
}
/**
* Fetches the package metadata of all sources.
*/
public function fetchSources()
{
$this->packages = $this->fetchSourcesOfPackages($this->composerConfig->packages);
$this->devPackages = $this->fetchSourcesOfPackages($this->composerConfig->devPackages);
}
/**
* @see NixASTNode::toNixAST()
*/
public function toNixAST()
{
return array(
"packages" => new NixAttrSet($this->packages),
"devPackages" => new NixAttrSet($this->devPackages)
);
}
}
?>