Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xrow committed Jan 26, 2017
1 parent 3930044 commit 0ac260c
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 25 deletions.
156 changes: 135 additions & 21 deletions Command/BundleGeneratorCommand.php
@@ -1,16 +1,20 @@
<?php
namespace xrow\bootstrapBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
use Sensio\Bundle\GeneratorBundle\Generator\BundleGenerator;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Composer\Json\JsonFile;
use Symfony\Component\Filesystem\Filesystem;
use xrow\bootstrapBundle\Model\Bundle;


class BundleGeneratorCommand extends ContainerAwareCommand
class BundleGeneratorCommand extends GeneratorCommand
{
/**
* @see Command
Expand All @@ -21,23 +25,27 @@ protected function configure()
->setName('xrow:generate:bundle')
->setDescription('Create a new bundle')
->addArgument('name', InputArgument::REQUIRED, 'Name of bundle')
->setHelp( "app/console xrow:generate:bundle --name=MostViewed");
->setHelp( "app/console xrow:generate:bundle MostViewed");

}
//@TODO Bundle generator requires symfony generator 3.x, but conflicts https://github.com/ezsystems/ezstudio/blob/master/composer.json#L30
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');

$helper = $this->getHelper('question');
$question = new ConfirmationQuestion("Have you created git@gitlab.com:xrow-shared/". strtolower($name) . "-bundle.git? Continue with this action?", false, '/^(y|j)/i');

if (!$helper->ask($input, $output, $question)) {
return;
}

$namespace = "Xrow/" . $name;
$bundleName = $name . "Bundle";
$projectRootDirectory = $this->getContainer()->getParameter('kernel.root_dir').'/..';

$dir = $projectRootDirectory."/vendor/xrow/" . strtolower($name) . "-bundle";
$process = new Process("app/console generate:bundle --namespace=" . $namespace . " --bundle-name=" . $name . "Bundle --no-interaction --format=annotation --dir=./vendor");
$process->run();
return; // Better code below...
$dir = $projectRootDirectory."/src";

$bundle = new \Sensio\Bundle\GeneratorBundle\Model\Bundle(
$bundle = new Bundle(
$namespace,
$bundleName,
$dir,
Expand All @@ -49,29 +57,135 @@ protected function execute(InputInterface $input, OutputInterface $output)
$generator = $this->getGenerator();

$generator->generateBundle($bundle);

$bundlebasename = strtolower($name)."-bundle";
$package = array();
$package["name"] = "xrow/".strtolower($name)."-bundle";
$package["name"] = "xrow/". $bundlebasename;
$package["type"] = "symfony-bundle";
$package["homepage"] = "symfony-bundle";
$package["license"] = "Apache License 2.0";
$package["authors"]["name"] = "xrow GmbH";
$package["authors"]["homepage"] = "http://www.xrow.com";
$package["require"]["php"] = "~5.5|~7.0";
$package["autoload"]["psr-4"]["Xrow\\".$name."\\"] = "http://www.xrow.com";
file_put_contents($file, $dir. "/composer.json", json_encode($package, JSON_PRETTY_PRINT));
$package["autoload"]["psr-4"][$bundle->getNamespace() . "\\"] = "";
file_put_contents( $bundle->getTargetDirectory() . "/composer.json", json_encode($package, JSON_PRETTY_PRINT));
$repo = "git@gitlab.com:xrow-shared/". $bundlebasename . ".git";


$json = new JsonFile( $projectRootDirectory . "/composer.json");
$composer = $json->read();
$composer["repositories"][] = array(
"type" => "vcs",
"url" => $repo
);
$composer["require"]["xrow/" . $bundlebasename] = "dev-master";
$json->write($composer);


$process = new Process("git init" , $dir );
$process = new Process("git init" , $bundle->getTargetDirectory() );
$process->run();
$process = new Process("git remote add origin git@gitlab.com:xrow-shared/". strtolower($name)."-bundle.git" , $dir );
$process = new Process("git remote add origin " . $repo , $bundle->getTargetDirectory() );
$process->run();
$process = new Process("add ." , $dir );
$process = new Process("git add ." , $bundle->getTargetDirectory() );
$process->run();
$process = new Process("git commit" , $dir );
$process = new Process("git commit -a -m\"Initial Create\"" , $bundle->getTargetDirectory() );
$process->run();
$process = new Process("git push -u origin master" , $dir );
$process = new Process("git push -u origin master" , $bundle->getTargetDirectory() );
$process->run();

$output->writeln($name . ' created!');
$fs = new Filesystem();
$fs->remove($bundle->getTargetDirectory());

#$output->writeln( 'Composer run' );
#$process = new Process("composer update" , $projectRootDirectory );
#$process->run();
$output->writeln( $name . ' created');
}
protected function updateKernel(OutputInterface $output, KernelInterface $kernel, Bundle $bundle)
{
$kernelManipulator = new KernelManipulator($kernel);

$output->writeln(sprintf(
'> Enabling the bundle inside <info>%s</info>',
$this->makePathRelative($kernelManipulator->getFilename())
));

try {
$ret = $kernelManipulator->addBundle($bundle->getBundleClassName());

if (!$ret) {
$reflected = new \ReflectionObject($kernel);

return array(
sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()),
' and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:',
'',
sprintf(' <comment>new %s(),</comment>', $bundle->getBundleClassName()),
'',
);
}
} catch (\RuntimeException $e) {
return array(
sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $bundle->getBundleClassName()),
'',
);
}
}
// Copied functoins from Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand;
protected function updateRouting(OutputInterface $output, Bundle $bundle)
{
$targetRoutingPath = $this->getContainer()->getParameter('kernel.root_dir').'/config/routing.yml';
$output->writeln(sprintf(
'> Importing the bundle\'s routes from the <info>%s</info> file',
$this->makePathRelative($targetRoutingPath)
));
$routing = new RoutingManipulator($targetRoutingPath);
try {
$ret = $routing->addResource($bundle->getName(), $bundle->getConfigurationFormat());
if (!$ret) {
if ('annotation' === $bundle->getConfigurationFormat()) {
$help = sprintf(" <comment>resource: \"@%s/Controller/\"</comment>\n <comment>type: annotation</comment>\n", $bundle->getName());
} else {
$help = sprintf(" <comment>resource: \"@%s/Resources/config/routing.%s\"</comment>\n", $bundle->getName(), $bundle->getConfigurationFormat());
}
$help .= " <comment>prefix: /</comment>\n";

return array(
'- Import the bundle\'s routing resource in the app\'s main routing file:',
'',
sprintf(' <comment>%s:</comment>', $bundle->getName()),
$help,
'',
);
}
} catch (\RuntimeException $e) {
return array(
sprintf('Bundle <comment>%s</comment> is already imported.', $bundle->getName()),
'',
);
}
}

protected function updateConfiguration(OutputInterface $output, Bundle $bundle)
{
$targetConfigurationPath = $this->getContainer()->getParameter('kernel.root_dir').'/config/config.yml';
$output->writeln(sprintf(
'> Importing the bundle\'s %s from the <info>%s</info> file',
$bundle->getServicesConfigurationFilename(),
$this->makePathRelative($targetConfigurationPath)
));
$manipulator = new ConfigurationManipulator($targetConfigurationPath);
try {
$manipulator->addResource($bundle);
} catch (\RuntimeException $e) {
return array(
sprintf('- Import the bundle\'s "%s" resource in the app\'s main configuration file:', $bundle->getServicesConfigurationFilename()),
'',
$manipulator->getImportCode($bundle),
'',
);
}
}
protected function createGenerator()
{
return new BundleGenerator($this->getContainer()->get('filesystem'));
}
}
2 changes: 1 addition & 1 deletion Command/ContentTypeCommand.php
Expand Up @@ -31,7 +31,7 @@ class ContentTypeCommand extends ContainerAwareCommand
protected function configure()
{
$this
->setName('xrowmigration:class_attribute')
->setName('xrow:class_attribute')
->setDescription('Migrate field definition/class attribute')
->addArgument('contenttype_identifier', InputArgument::REQUIRED, 'a content type/classname identifier')
->addArgument('field_identifier', InputArgument::REQUIRED, 'a field/class_attribute identifier')
Expand Down
2 changes: 1 addition & 1 deletion Command/LocationServiceCommand.php
Expand Up @@ -18,7 +18,7 @@ class LocationServiceCommand extends ContainerAwareCommand
protected function configure()
{
$this
->setName('xrowmigration:location')
->setName('xrow:location')
->setDescription('Manage Subtree Location')
->addArgument('operation', InputArgument::REQUIRED, 'Operation to execute, either copy or move')
->addArgument('srcLocationId', InputArgument::REQUIRED, 'A subtree\'s root Location')
Expand Down
2 changes: 1 addition & 1 deletion Command/updateCommand.php
Expand Up @@ -16,7 +16,7 @@ class updateCommand extends ContainerAwareCommand
protected function configure()
{
$this
->setName('style:update')
->setName('xrow:style:update')
->setDescription('Updates the CSS Styles from YML');
}

Expand Down
2 changes: 1 addition & 1 deletion Command/viewCacheCommand.php
Expand Up @@ -25,7 +25,7 @@ public function configure()
EOT
)
->setName('xrowcache:view:clear');
->setName('xrow:view:clear');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
40 changes: 40 additions & 0 deletions Model/Bundle.php
@@ -0,0 +1,40 @@
<?php
namespace xrow\bootstrapBundle\Model;

use Symfony\Component\DependencyInjection\Container;
use Sensio\Bundle\GeneratorBundle\Model\Bundle as GeneratorBundle;
/**
* Represents a bundle being built.
*/
class Bundle extends GeneratorBundle
{
private $namespace;

private $name;

private $targetDirectory;

private $configurationFormat;

private $isShared;

private $testsDirectory;
public function __construct($namespace, $name, $targetDirectory, $configurationFormat, $isShared)
{
$this->namespace = $namespace;
$this->name = $name;
$this->targetDirectory = $targetDirectory;
$this->configurationFormat = $configurationFormat;
$this->isShared = $isShared;
$this->testsDirectory = $this->getTargetDirectory().'/Tests';
}
/**
* Returns the directory where the bundle will be generated.
*
* @return string
*/
public function getTargetDirectory()
{
return rtrim($this->targetDirectory, '/').'/'.trim( strtolower(strtr($this->namespace, '\\', '/') ), '/');
}
}
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -6,6 +6,7 @@
"require": {
"braincrafted/bootstrap-bundle": "2.1.2",
"knplabs/knp-menu-bundle": "1.*",
"sensio/generator-bundle": "~3.1",
"twbs/bootstrap-sass": "v3.3.4"
},
"autoload": {
Expand Down

0 comments on commit 0ac260c

Please sign in to comment.