Skip to content

Commit

Permalink
Merge pull request #89 from clue-labs/cleanup
Browse files Browse the repository at this point in the history
Minor internal refactoring to clean up some unneeded code duplication and unneeded references
  • Loading branch information
clue authored Nov 10, 2019
2 parents e882a05 + b637493 commit a6c4b73
Show file tree
Hide file tree
Showing 25 changed files with 290 additions and 179 deletions.
2 changes: 1 addition & 1 deletion src/Clue/PharComposer/Command/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$pharer = $this->packager->getPharer($input->getArgument('project'));

$path = $this->packager->getSystemBin($pharer, $input->getArgument('target'));
$path = $this->packager->getSystemBin($pharer->getPackageRoot(), $input->getArgument('target'));

if (is_file($path)) {
$helper = $this->getHelper('question');
Expand Down
2 changes: 1 addition & 1 deletion src/Clue/PharComposer/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$pharer = $this->packager->getPharer($project, $version);

if ($action === 'install') {
$path = $this->packager->getSystemBin($pharer);
$path = $this->packager->getSystemBin($pharer->getPackageRoot());
$this->packager->install($pharer, $path);
} else {
$pharer->build();
Expand Down
13 changes: 0 additions & 13 deletions src/Clue/PharComposer/Package/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Clue\PharComposer\Package;

use Clue\PharComposer\Logger;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -18,18 +17,6 @@ class Bundle implements \IteratorAggregate
*/
private $resources = array();

/**
* create bundle from given package
*
* @param Package $package
* @param Logger $logger
* @return self
*/
public static function from(Package $package, Logger $logger)
{
return $package->getBundler($logger)->bundle();
}

/**
* add given file to bundle
*
Expand Down
7 changes: 4 additions & 3 deletions src/Clue/PharComposer/Package/Bundler/Complete.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public function bundle()
$iterator = Finder::create()
->files()
->ignoreVCS(true)
->filter($this->package->getBlacklistFilter())
->exclude($this->package->getPathVendorRelative())
->exclude(rtrim($this->package->getPathVendor(), '/'))
->notPath('/^composer\.phar/')
->notPath('/^phar-composer\.phar/')
->in($this->package->getDirectory());
$this->logger->log(' Adding whole project directory "' . $this->package->getDirectory() . '"');
$this->logger->log(' Adding whole project directory');
return $bundle->addDir($iterator);
}
}
10 changes: 4 additions & 6 deletions src/Clue/PharComposer/Package/Bundler/Explicit.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ private function bundleBins(Bundle $bundle)
{
foreach ($this->package->getBins() as $bin) {
$this->logger->log(' adding "' . $bin . '"');
$bundle->addFile($bin);
$bundle->addFile($this->package->getDirectory() . $bin);
}
}

private function bundlePsr0(Bundle $bundle, Autoload $autoload)
{
foreach ($autoload->getPsr0() as $path) {
$this->addDir($bundle, $path);
$this->addDir($bundle, rtrim($path, '/') . '/');
}
}

Expand All @@ -81,17 +81,15 @@ private function bundleFiles(Bundle $bundle, Autoload $autoload)
private function addFile(Bundle $bundle, $file)
{
$this->logger->log(' adding "' . $file . '"');
$bundle->addFile($this->package->getAbsolutePath($file));
$bundle->addFile($this->package->getDirectory() . $file);
}

private function addDir(Bundle $bundle, $dir)
{
$dir = $this->package->getAbsolutePath(rtrim($dir, '/') . '/');
$this->logger->log(' adding "' . $dir . '"');
$bundle->addDir(Finder::create()
->files()
//->filter($this->getBlacklistFilter())
->ignoreVCS(true)
->in($dir));
->in($this->package->getDirectory() . $dir));
}
}
89 changes: 21 additions & 68 deletions src/Clue/PharComposer/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Clue\PharComposer\Package\Bundler\Complete as CompleteBundler;
use Clue\PharComposer\Package\Bundler\Explicit as ExplicitBundler;
use Clue\PharComposer\Logger;
use Symfony\Component\Finder\SplFileInfo;

/**
* The package represents either the main/root package or one of the vendor packages.
Expand All @@ -25,45 +24,51 @@ class Package
public function __construct(array $package, $directory)
{
$this->package = $package;
$this->directory = $directory;
$this->directory = rtrim($directory, '/') . '/';
}

/**
* get package name as defined in composer.json
*
* @return string
* @return ?string
*/
public function getName()
{
return isset($this->package['name']) ? $this->package['name'] : 'unknown';
return isset($this->package['name']) ? $this->package['name'] : null;
}

/**
* Get path to vendor directory (relative to package directory)
*
* @return string
*/
public function getPathVendorRelative()
public function getShortName()
{
$vendor = 'vendor';
if (isset($this->package['config']['vendor-dir'])) {
$vendor = $this->package['config']['vendor-dir'];
// skip vendor name from package name or default to last directory component
$name = $this->getName();
if ($name === null) {
$name = realpath($this->directory);
if ($name === false) {
$name = $this->directory;
}
}
return $vendor;
return basename($name);
}

/**
* Get absolute path to vendor directory
* Get path to vendor directory (relative to package directory, always ends with slash)
*
* @return string
*/
public function getPathVendor()
{
return $this->getAbsolutePath($this->getPathVendorRelative() . '/');
$vendor = 'vendor';
if (isset($this->package['config']['vendor-dir'])) {
$vendor = $this->package['config']['vendor-dir'];
}
return $vendor . '/';
}

/**
* Get package directory (the directory containing its composer.json)
* Get package directory (the directory containing its composer.json, always ends with slash)
*
* @return string
*/
Expand Down Expand Up @@ -108,64 +113,12 @@ public function getAutoload()
}

/**
* Get list of files defined as "bin" (absolute paths)
* Get list of files defined as "bin" (relative to package directory)
*
* @return string[]
*/
public function getBins()
{
if (!isset($this->package['bin'])) {
return array();
}

$bins = array();
foreach ($this->package['bin'] as $bin) {
$bins []= $this->getAbsolutePath($bin);
}

return $bins;
}

/**
* Get blacklisted files which are not to be included
*
* Hardcoded to exclude composer.phar and phar-composer.phar at the moment.
*
* @return string[]
*/
public function getBlacklist()
{
return array(
$this->getAbsolutePath('composer.phar'),
$this->getAbsolutePath('phar-composer.phar')
);
}

/**
* Gets a filter function to exclude blacklisted files
*
* Only used for CompleteBundler at the moment
*
* @return \Closure
* @uses self::getBlacklist()
*/
public function getBlacklistFilter()
{
$blacklist = $this->getBlacklist();

return function (SplFileInfo $file) use ($blacklist) {
return in_array($file->getPathname(), $blacklist) ? false : null;
};
}

/**
* Get absolute path for the given package-relative path
*
* @param string $path
* @return string
*/
public function getAbsolutePath($path)
{
return $this->directory . ltrim($path, '/');
return isset($this->package['bin']) ? $this->package['bin'] : array();
}
}
14 changes: 9 additions & 5 deletions src/Clue/PharComposer/Phar/Packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
use Clue\PharComposer\Package\Package;

class Packager
{
Expand Down Expand Up @@ -210,7 +211,7 @@ function () use ($that, $command) {
$pharer->setOutput($this->output);
$pharer->setStep($step);

$pathVendor = $pharer->getPathVendor();
$pathVendor = $pharer->getPackageRoot()->getDirectory() . $pharer->getPackageRoot()->getPathVendor();
if (!is_dir($pathVendor)) {
throw new RuntimeException('Project is not installed via composer. Run "composer install" manually');
}
Expand Down Expand Up @@ -294,7 +295,12 @@ public function install(PharComposer $pharer, $path)
$this->log(' <info>OK</info> - Moved to <info>' . $path . '</info>');
}

public function getSystemBin(PharComposer $pharer, $path = null)
/**
* @param Package $package
* @param ?string $path
* @return string
*/
public function getSystemBin(Package $package, $path = null)
{
// no path given => place in system bin path
if ($path === null) {
Expand All @@ -306,11 +312,9 @@ public function getSystemBin(PharComposer $pharer, $path = null)
$path = self::PATH_BIN . '/' . $path;
}

// TODO: check path is in $PATH environment

// path is actually a directory => append package name
if (is_dir($path)) {
$path = rtrim($path, '/') . '/' . basename($pharer->getTarget(), '.phar');
$path = rtrim($path, '/') . '/' . $package->getShortName();
}

return $path;
Expand Down
Loading

0 comments on commit a6c4b73

Please sign in to comment.