This repository has been archived by the owner on Mar 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
291 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
include __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Khepin\Medusa\Compiler; | ||
|
||
$compiler = new Compiler(); | ||
$compiler->compile(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Khepin\Medusa\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Command\Command; | ||
use Khepin\Medusa\DependencyResolver; | ||
use Guzzle\Service\Client; | ||
use Khepin\Medusa\Downloader; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
|
||
class MirrorCommand extends Command | ||
{ | ||
protected $guzzle; | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('mirror') | ||
->setDescription('Mirrors all repositories given a config file') | ||
->setDefinition(array( | ||
new InputArgument('config', InputArgument::OPTIONAL, 'A config file', null), | ||
)) | ||
; | ||
} | ||
|
||
/** | ||
* @param InputInterface $input The input instance | ||
* @param OutputInterface $output The output instance | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeln('<info>First getting all dependencies</info>'); | ||
$this->guzzle = new Client('http://packagist.org'); | ||
$config = json_decode(file_get_contents($input->getArgument('config'))); | ||
$repos = []; | ||
foreach($config->require as $dependency){ | ||
$output->writeln(' - Getting dependencies for <info>'.$dependency.'</info>'); | ||
$resolver = new DependencyResolver($dependency); | ||
$deps = $resolver->resolve(); | ||
$repos = array_merge($repos, $deps); | ||
} | ||
|
||
foreach($repos as $repo){ | ||
$command = $this->getApplication()->find('add'); | ||
|
||
$arguments = array( | ||
'command' => 'add', | ||
'package' => $repo, | ||
'repos-dir' => $config->repodir, | ||
); | ||
if(!is_null($config->satisconfig)){ | ||
$arguments['--config-file'] = $config->satisconfig; | ||
} | ||
|
||
$input = new ArrayInput($arguments); | ||
$returnCode = $command->run($input, $output); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace Khepin\Medusa\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Process\Process; | ||
|
||
class UpdateReposCommand extends Command | ||
{ | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('update') | ||
->setDescription('Fetch last updates from each package') | ||
->setDefinition(array( | ||
new InputArgument('repos-dir', InputArgument::OPTIONAL, 'Location where to output built files', null), | ||
)) | ||
->setHelp(<<<EOT | ||
The <info>mirror</info> command reads the given composer.lock file and mirrors | ||
each git repository so they can be used locally. | ||
<warning>This will only work for repos hosted on github.</warning> | ||
EOT | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* @param InputInterface $input The input instance | ||
* @param OutputInterface $output The output instance | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$dir = $input->getArgument('repos-dir'); | ||
$repos = glob($dir.'/*/*.git'); | ||
$cmd = 'cd %s && git fetch'; | ||
foreach($repos as $repo){ | ||
$output->writeln(' - Fetching latest changes in <info>'.$repo.'</info>'); | ||
$process = new Process(sprintf($cmd, $repo)); | ||
$process->run(); | ||
if (!$process->isSuccessful()) { | ||
throw new \Exception($process->getErrorOutput()); | ||
} | ||
$output->writeln($process->getOutput()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
namespace Khepin\Medusa; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
class Compiler | ||
{ | ||
public function compile($pharFile = 'medusa.phar') | ||
{ | ||
if (file_exists($pharFile)) { | ||
unlink($pharFile); | ||
} | ||
|
||
$phar = new \Phar($pharFile, 0, 'medusa.phar'); | ||
$phar->setSignatureAlgorithm(\Phar::SHA1); | ||
|
||
$phar->startBuffering(); | ||
|
||
$finders = array(); | ||
|
||
$finder = new Finder(); | ||
$finder->files() | ||
->ignoreVCS(true) | ||
->name('*.php') | ||
->notName('Compiler.php') | ||
->in(__DIR__.'/../../') | ||
; | ||
$finders[] = $finder; | ||
|
||
$finder = new Finder(); | ||
$finder->files() | ||
->ignoreVCS(true) | ||
->name('*.php') | ||
->in([ | ||
__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Component/Console', | ||
__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Component/EventDispatcher', | ||
__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Component/Process', | ||
]) | ||
; | ||
$finders[] = $finder; | ||
|
||
$finder = new Finder(); | ||
$finder->files() | ||
->ignoreVCS(true) | ||
->name('autoload.php') | ||
->in(__DIR__.'/../../../vendor') | ||
; | ||
$finders[] = $finder; | ||
|
||
$finder = new Finder(); | ||
$finder->files() | ||
->ignoreVCS(true) | ||
->name('*.php') | ||
->in(__DIR__.'/../../../vendor/composer') | ||
->depth(0) | ||
; | ||
$finders[] = $finder; | ||
|
||
$finder = new Finder(); | ||
$finder->files() | ||
->ignoreVCS(true) | ||
->name('*.php') | ||
->in(__DIR__.'/../../../vendor/guzzle') | ||
; | ||
$finders[] = $finder; | ||
|
||
foreach ($finders as $finder) { | ||
foreach ($finder as $file) { | ||
$this->addFile($phar, $file); | ||
} | ||
} | ||
|
||
$this->addBin($phar); | ||
|
||
// Stubs | ||
$phar->setStub($this->getStub()); | ||
|
||
$phar->stopBuffering(); | ||
|
||
$phar->compressFiles(\Phar::GZ); | ||
|
||
unset($phar); | ||
} | ||
|
||
private function addFile($phar, $file, $strip = true) | ||
{ | ||
$path = str_replace(dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR, '', $file->getRealPath()); | ||
|
||
$content = file_get_contents($file); | ||
if ($strip) { | ||
$content = $this->stripWhitespace($content); | ||
} elseif ('LICENSE' === basename($file)) { | ||
$content = "\n".$content."\n"; | ||
} | ||
|
||
$phar->addFromString($path, $content); | ||
} | ||
|
||
private function addBin($phar) | ||
{ | ||
$content = file_get_contents(__DIR__.'/../../../bin/medusa'); | ||
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content); | ||
$phar->addFromString('bin/medusa', $content); | ||
} | ||
|
||
private function stripWhitespace($source) | ||
{ | ||
if (!function_exists('token_get_all')) { | ||
return $source; | ||
} | ||
|
||
$output = ''; | ||
foreach (token_get_all($source) as $token) { | ||
if (is_string($token)) { | ||
$output .= $token; | ||
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { | ||
$output .= str_repeat("\n", substr_count($token[1], "\n")); | ||
} elseif (T_WHITESPACE === $token[0]) { | ||
// reduce wide spaces | ||
$whitespace = preg_replace('{[ \t]+}', ' ', $token[1]); | ||
// normalize newlines to \n | ||
$whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace); | ||
// trim leading spaces | ||
$whitespace = preg_replace('{\n +}', "\n", $whitespace); | ||
$output .= $whitespace; | ||
} else { | ||
$output .= $token[1]; | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
private function getStub() | ||
{ | ||
return <<<'EOF' | ||
#!/usr/bin/env php | ||
<?php | ||
Phar::mapPhar('medusa.phar'); | ||
require 'phar://medusa.phar/bin/medusa'; | ||
__HALT_COMPILER(); | ||
EOF; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters