Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/additional enhancements #7

Merged
merged 17 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ First, download the Skeletor installer using Composer:
composer global require "pixelfusion/skeletor"
```

Make sure to place the `$HOME/.composer/vendor/bin` directory (or the equivalent directory for your OS) in your $PATH so
Make sure to place the `~/.composer/vendor/bin` directory (or the equivalent directory for your OS) in your $PATH so
the `Skeletor` executable can be located by you system.


Expand Down
7 changes: 4 additions & 3 deletions skeletor
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (file_exists(__DIR__.'/../../autoload.php')) {
."To do so, execute the following command:\n\n"
.'$curl -s http://getcomposer.org/installer | php'
."\n\n"
.'Make sure to place the $HOME/.composer/vendor/bin directory (or the equivalent directory for'
.'Make sure to place the ~/.composer/vendor/bin directory (or the equivalent directory for'
."\n"
.'your OS) in your $PATH so the skeletor executable can be located by your system.'
."\n\n"
Expand All @@ -35,8 +35,9 @@ $config = new SkeletorConfigurator();
$app = new App($config, $container);

$app->addCommands(array(
new Skeletor\Console\CreateProjectCommand,
new Skeletor\Console\GetPackageVersions,
new Skeletor\Console\CreateProjectCommand(),
new Skeletor\Console\GetPackageVersions(),
new Skeletor\Console\AddNewPackage(),
));

$app->run();
28 changes: 26 additions & 2 deletions src/Skeletor/Api/PackagistApi.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Skeletor\Api;

use Skeletor\Exceptions\FailedToLoadPackageException;
use Skeletor\Exceptions\FailedToLoadPackageVersion;

class PackagistApi
Expand All @@ -14,7 +15,7 @@ public function getAvailablePackasgeVersions(array $packages)
$packageVersions = [];
foreach($packages as $key => $package)
{
$packageVersions[$package->getName()] = $this->getVersionsPackage($package->getInstallSlug());
$packageVersions[$package->getInstallSlug()] = $this->getVersionsPackage($package->getInstallSlug());
}
return $packageVersions;
}
Expand All @@ -32,12 +33,26 @@ public function getVersionsPackage(string $packageSlug)
}

$packageData = json_decode($data, true);
$versions = array_keys($packageData['packages'][$packageSlug]);
$versions = json_last_error() === JSON_ERROR_NONE ? array_keys($packageData['packages'][$packageSlug]) : [];

// Flip the array and return versions
return array_reverse($versions);
}

/**
* @param string $package
* @return array
*/
public function searchPackage(string $package)
{
$data = file_get_contents($this->buildSearchUrl($package));
$data = json_decode($data, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check $data is valid? something like json_last_error() === JSON_ERROR_NONE ? array_map(...) : []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, implement this and for the getVersionsPackage as well


return json_last_error() === JSON_ERROR_NONE ? array_map(function($result) {
return $result['name'];
}, $data['results']) : [];
}

/**
* @param string $packageSlug
* @return string
Expand All @@ -46,4 +61,13 @@ public function buildUrl(string $packageSlug)
{
return sprintf('https://packagist.org/p/%s.json', $packageSlug);
}

/**
* @param string $packageSlug
* @return string
*/
public function buildSearchUrl(string $packageSlug)
{
return sprintf('https://packagist.org/search.json?q=%s', $packageSlug);
}
}
14 changes: 14 additions & 0 deletions src/Skeletor/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,27 @@ public function getCli()
return $this->container->get('Cli');
}

/**
* @return object
*/
public function getPackagistApi()
{
return $this->container->get('PackagistApi');
}

/**
* @return object
*/
public function getSkeletorFilesystem()
{
return $this->container->get('skeletorFilesystem');
}

/**
* @return SkeletorConfigurator
*/
public function getConfigurator()
{
return $this->configurator;
}
}
13 changes: 12 additions & 1 deletion src/Skeletor/App/Config/SkeletorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
class SkeletorConfigurator
{
protected $options;
protected $optionFile;

/**
* SkeletorConfigurator constructor.
*/
public function __construct()
{
$this->options = Yaml::parse(file_get_contents(__DIR__.'/skeletor.yml'));
$this->optionFile = __DIR__.'/skeletor.yml';
$this->options = Yaml::parse(file_get_contents($this->optionFile));
}

/**
Expand Down Expand Up @@ -62,4 +64,13 @@ public function getConfig()
{
return $this->options;
}

/**
* @param array $options
*/
public function storeConfig(array $options)
{
$yaml = Yaml::dump($options);
file_put_contents($this->optionFile, $yaml);
}
}
6 changes: 3 additions & 3 deletions src/Skeletor/App/Config/skeletor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ frameworks:
- Laravel54Framework
- LaravelLumen54Framework
packages:
- BehatPackage
- JsonBehatExtensionPackage
- BehatBehatPackage
- KielabokkieJsonapiBehatExtensionPackage
defaultPackages:
- GitHooksPackage
- PixelfusionGitHooksPackage
86 changes: 86 additions & 0 deletions src/Skeletor/Console/AddNewPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace Skeletor\Console;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AddNewPackage extends SkeletorCommand
{
protected function configure()
{
$this->setName('package:add')
->setDescription('Add new package')
->addArgument('name', InputArgument::REQUIRED, 'Package name');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setupCommand();

$package = $input->getArgument('name');
$this->cli->br()->yellow(sprintf('Skeletor - add new package, searching for %s', $package))->br();

$packageOptions = $this->packagistApi->searchPackage($package);
if (empty($packageOptions)) {
$this->cli->br()->red('package not found');
return;
}

$packageInfo['slug'] = $this->packageManager->specifyPackage($packageOptions);
$packageInfo = $this->buildPackageInfo($packageInfo);
if (in_array($packageInfo['name'], $this->packageManager->getAllPackageNames())) {
$this->cli->br()->red('package already installed');
return;
}

$this->makePackageClass($packageInfo);
$this->addPackageToConfig($packageInfo);
}

/**
* @param array $packageInfo
* @return array
*/
protected function buildPackageInfo(array $packageInfo)
{
$packageName = preg_replace('/\/|-/', ' ', $packageInfo['slug']);
$packageName = ucwords($packageName);
$packageInfo['name'] = $packageName;

$packageName = preg_replace('/\s+/', '', $packageName);
$packageInfo['class'] = $packageName.'Package';

return $packageInfo;
}

/**
* @param array $packageInfo
*/
protected function addPackageToConfig(array $packageInfo)
{
$config = $this->configurator->getConfig();
$config['packages'][] = $packageInfo['class'];
$this->configurator->storeConfig($config);
}

/**
* @param array $packageInfo
*/
protected function makePackageClass(array $packageInfo)
{
//Read stub
$stub = $this->skeletorFilesystem->read('Templates/stubs/package.stub');

//Replace stub dummy data
foreach ($packageInfo as $key => $info) {
$stub = str_replace($key.'Dummy', $info, $stub);
}

//Put final class in Skeletor
$this->skeletorFilesystem->put(
'Packages/'.$packageInfo['class'].'.php',
$stub
);
}
}
29 changes: 6 additions & 23 deletions src/Skeletor/Console/CreateProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
namespace Skeletor\Console;

use Skeletor\Frameworks\Framework;
use Symfony\Component\Process\Process;
use Skeletor\Exceptions\FailedFilesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class CreateProjectCommand extends Command
{
protected $frameworkManager;
protected $packageManager;
protected $cli;

class CreateProjectCommand extends SkeletorCommand
{
protected function configure()
{
$this->setName('project:create')
Expand All @@ -38,11 +33,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->setupFolder($name);
}

$this->getApplication()->registerServices($dryRun);
$this->setupCommand();
$this->setupCommand($dryRun);

//Start process in the background
$process = new Process('php skeletor package:show --no-ansi');
$process = new Process('skeletor package:show --no-ansi');
$process->setTimeout(7200);
$process->start();

Expand All @@ -66,22 +60,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
return false;
}

$activePackages = $this->packageManager->mergeSelectedAndDefaultPackages($activePackages);
$activePackages = $this->packageManager->mergePackagesWithDefault($activePackages);
$this->buildProject($activeFramework, $activePackages);
$this->cli->br()->green('Yhea, success')->br();
}

private function setupCommand()
{
$this->frameworkManager = $this->getApplication()->getFrameworkManager();
$this->packageManager = $this->getApplication()->getPackageManager();
$this->cli = $this->getApplication()->getCli();

$this->frameworkManager->setFrameworks($this->getApplication()->getFrameworks());
$this->packageManager->setPackages($this->getApplication()->getPackages());
$this->packageManager->setDefaultPackages($this->getApplication()->getDefaultPackages());
}

/**
* @param string $name
*/
Expand Down
16 changes: 4 additions & 12 deletions src/Skeletor/Console/GetPackageVersions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
namespace Skeletor\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetPackageVersions extends Command
class GetPackageVersions extends SkeletorCommand
{
protected function configure()
{
Expand All @@ -22,17 +21,10 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$ansi = $input->getOption('no-ansi');
$this->getApplication()->registerServices();
$packagistApi = $this->getApplication()->getPackagistApi();
$this->setupCommand();

$packageManager = $this->getApplication()->getPackageManager();
$packageManager->setPackages($this->getApplication()->getPackages());

$packages = $packageManager->getPackages();
$packageVersions = $packagistApi->getAvailablePackasgeVersions($packages);

$skeletorFilesystem = $this->getApplication()->getSkeletorFilesystem();
$skeletorFilesystem->put('Tmp/PackageVersions.json', json_encode($packageVersions));
$packageVersions = $this->packagistApi->getAvailablePackasgeVersions($this->packageManager->getPackages());
$this->skeletorFilesystem->put('Tmp/PackageVersions.json', json_encode($packageVersions));

if(!$ansi) {
$cli = $this->getApplication()->getCli();
Expand Down
31 changes: 31 additions & 0 deletions src/Skeletor/Console/SkeletorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Skeletor\Console;

use Symfony\Component\Console\Command\Command;

abstract class SkeletorCommand extends Command
{
protected $cli;
protected $packagistApi;
protected $configurator;
protected $packageManager;
protected $frameworkManager;
protected $skeletorFilesystem;

protected function setupCommand($dryRun = false)
{
$this->getApplication()->registerServices($dryRun);

$this->cli = $this->getApplication()->getCli();
$this->packagistApi = $this->getApplication()->getPackagistApi();
$this->configurator = $this->getApplication()->getConfigurator();
$this->skeletorFilesystem = $this->getApplication()->getSkeletorFilesystem();

$this->frameworkManager = $this->getApplication()->getFrameworkManager();
$this->packageManager = $this->getApplication()->getPackageManager();

$this->frameworkManager->setFrameworks($this->getApplication()->getFrameworks());
$this->packageManager->setPackages($this->getApplication()->getPackages());
$this->packageManager->setDefaultPackages($this->getApplication()->getDefaultPackages());
}
}
2 changes: 1 addition & 1 deletion src/Skeletor/Frameworks/Laravel54Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function configure()

$this->mountManager->copy(
'skeletor://'.$this->options['templatePath'].'/JsonBehatExtensionPackage/FeatureContext.php',
'project://namespace/pixelfusion/bootstrap/FeatureContext.php'
'project://app/namespace/pixelfusion/bootstrap/FeatureContext.php'
);
}
}