Skip to content

Commit

Permalink
플러그인 생성기능 개선 및 설치 기능 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
jhyeon1010 committed Apr 24, 2019
1 parent b635c6c commit fc46386
Show file tree
Hide file tree
Showing 31 changed files with 370 additions and 149 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@ composer.user.json
*.iml
.DS_Store
/plugins
/privates
node_modules
/resources/jsdoc
*.map
Expand Down
39 changes: 35 additions & 4 deletions app/Console/Commands/ComponentMakeCommand.php
Expand Up @@ -17,7 +17,9 @@
use Illuminate\Filesystem\Filesystem;
use ReflectionClass;
use Symfony\Component\Console\Input\InputOption;
use Xpressengine\Plugin\Composer\ComposerFileWriter;
use Xpressengine\Plugin\PluginEntity;
use Xpressengine\Plugin\PluginHandler;

/**
* Abstract Class ComponentMakeCommand
Expand All @@ -41,14 +43,22 @@ abstract class ComponentMakeCommand extends MakeCommand
*/
protected $componentType;

/**
* @var PluginHandler instance
*/
protected $handler;

/**
* Create a new component creator command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files Filesystem instance
* @param \Illuminate\Filesystem\Filesystem $files Filesystem instance
* @param PluginHandler $handler PluginHandler instance
*/
public function __construct(Filesystem $files)
public function __construct(Filesystem $files, ComposerFileWriter $writer, PluginHandler $handler)
{
parent::__construct($files);
parent::__construct($files, $writer);

$this->handler = $handler;

$this->addOption('--title', '', InputOption::VALUE_OPTIONAL, 'The title of component');
$this->addOption('--description', '', InputOption::VALUE_OPTIONAL, 'The description of component');
Expand All @@ -62,7 +72,7 @@ public function __construct(Filesystem $files)
*/
protected function getPlugin()
{
if(!$plugin = app('xe.plugin')->getPlugin($name = $this->getPluginName())) {
if(!$plugin = $this->handler->getPlugin($name = $this->getPluginName())) {
throw new \Exception("Unable to find a plugin to locate the skin file. plugin[$name] is not found.");
}

Expand Down Expand Up @@ -265,6 +275,27 @@ protected function existsAutoload($class, $file)
return in_array($file, (array)($autoload->classmap ?? []));
}

/**
* Refresh the given plugin
*
* @param PluginEntity $plugin plugin entity
* @return int
* @throws \Exception
*/
protected function refresh(PluginEntity $plugin)
{
$this->writer->reset()->cleanOperation();
$this->writer->write();

$result = $this->composerDump($plugin->hasVendor() ? $plugin->getPath() : base_path());

if (0 !== $result) {
throw new \Exception('Fail to run composer dump');
}

return $result;
}

/**
* Clean
*
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Commands/ComposerRunTrait.php
Expand Up @@ -127,6 +127,8 @@ protected function checkComposerHome()
*/
protected function runComposer($inputs, $skipPlugin = false, $output = null)
{
define('__RUN_IN_ARTISAN__', true);

ini_set('memory_limit', '-1');

if ($skipPlugin) {
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/DynamicFieldMake.php
Expand Up @@ -96,6 +96,7 @@ public function handle()

try {
$this->makeUsable($attr);
$this->info('Generate the dynamic field');

$class = $namespace.'\\'.$className;

Expand All @@ -106,7 +107,7 @@ public function handle()
throw new \Exception('Writing to composer.json file was failed.');
}

$this->runComposerDump($plugin->getPath());
$this->refresh($plugin);
} catch (\Exception $e) {
$this->clean($path);
throw $e;
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/DynamicFieldSkinMake.php
Expand Up @@ -99,13 +99,14 @@ public function handle()

try {
$this->makeUsable($attr);
$this->info('Generate the dynamic skin');

// composer.json 파일 수정
if ($this->registerComponent($plugin, $id, $namespace.'\\'.$className, $file, ['name' => $title, 'description' => $description]) === false) {
throw new \Exception('Writing to composer.json file was failed.');
}

$this->runComposerDump($plugin->getPath());
$this->refresh($plugin);
} catch (\Exception $e) {
$this->clean($path);
throw $e;
Expand Down
20 changes: 7 additions & 13 deletions app/Console/Commands/MakeCommand.php
Expand Up @@ -14,8 +14,8 @@

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Xpressengine\Plugin\Composer\ComposerFileWriter;

/**
* Abstract Class MakeCommand
Expand All @@ -27,10 +27,8 @@
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link http://www.xpressengine.com
*/
abstract class MakeCommand extends Command
abstract class MakeCommand extends ShouldOperation
{
use ComposerRunTrait;

/**
* The filesystem instance.
*
Expand All @@ -41,11 +39,12 @@ abstract class MakeCommand extends Command
/**
* Create a new component creator command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files instance
* @param Filesystem $files Filesystem instance
* @param ComposerFileWriter $writer ComposerFileWriter instance
*/
public function __construct(Filesystem $files)
public function __construct(Filesystem $files, ComposerFileWriter $writer)
{
parent::__construct();
parent::__construct($writer);

$this->files = $files;
}
Expand Down Expand Up @@ -101,11 +100,6 @@ protected function buildFile($file, array $search, array $replace, $to = null)
*/
protected function runComposerDump($path)
{
$inputs = [
'command' => 'dump-autoload',
'--working-dir' => $path,
];

return $this->runComposer($inputs, false);
return $this->composerDump($path);
}
}
46 changes: 0 additions & 46 deletions app/Console/Commands/PluginCommand.php
Expand Up @@ -101,52 +101,6 @@ protected function init(
//
}

/**
* Execute composer update.
*
* @param array $packages specific package name. no need version
* @return int
* @throws \Exception
* @throws \Throwable
*/
protected function composerUpdate(array $packages)
{
if ($this->isLocked()) {
throw new \Exception('The command is locked. Make sure that another process is running.');
}

$this->lock();

try {
if (0 !== $this->clearCache(true)) {
throw new \Exception('cache clear fail.. check your system.');
}

$this->prepareComposer();

$inputs = [
'command' => 'update',
"--with-dependencies" => true,
//"--quiet" => true,
'--working-dir' => base_path(),
/*'--verbose' => '3',*/
'packages' => $packages
];

$this->writeResult($result = $this->runComposer($inputs, false, $this->output));

return $result;
} catch (\Exception $e) {
$this->setFailed($e->getCode());
throw $e;
} catch (\Throwable $e) {
$this->setFailed($e->getCode());
throw $e;
} finally {
$this->unlock();
}
}

/**
* Run cache clear.
*
Expand Down
57 changes: 41 additions & 16 deletions app/Console/Commands/PluginMake.php
Expand Up @@ -14,6 +14,8 @@

namespace App\Console\Commands;

use Illuminate\Filesystem\Filesystem;
use Xpressengine\Plugin\Composer\ComposerFileWriter;
use Xpressengine\Plugin\PluginHandler;

/**
Expand Down Expand Up @@ -47,6 +49,27 @@ class PluginMake extends MakeCommand
*/
protected $description = 'Create a new plugin of XpressEngine';

/**
* PluginHandler instance.
*
* @var PluginHandler
*/
protected $handler;

/**
* PluginMake constructor.
*
* @param Filesystem $files Filesystem instance
* @param ComposerFileWriter $writer ComposerFileWriter instance
* @param PluginHandler $handler PluginHandler instance
*/
public function __construct(Filesystem $files, ComposerFileWriter $writer, PluginHandler $handler)
{
parent::__construct($files, $writer);

$this->handler = $handler;
}

/**
* Execute the console command.
*
Expand All @@ -60,14 +83,23 @@ public function handle()

$title = $this->getTitleInput($name);

$this->copyStubDirectory($path = plugins_path($name));

try {

$this->copyStubDirectory($path = app('path.privates').DIRECTORY_SEPARATOR.$name);
$this->makeUsable($path, $name, $namespace, $title);
$this->info('Generate the plugin');

$this->writer->reset()->cleanOperation();
$this->writer->install($packageName = 'xpressengine-plugin/'.$name, '*');
$this->writer->write();

$this->info('Run composer update command');
$this->line('> composer update');

// composer update
$this->runComposerDump($path);
$result = $this->composerUpdate([$packageName]);

if (0 !== $result) {
throw new \Exception('Fail to run composer update');
}

// plugin activate
$this->activatePlugin($name);
Expand All @@ -77,13 +109,8 @@ public function handle()
throw $e;
}

// print info
$url = trim(config('app.url'), '/').'/'.config('xe.routing.fixedPrefix').'/'.$name;

$this->info("Plugin is created and activated successfully.");

$this->info("See ./plugins/$name directory. And open $url in your browser.");

$this->info("See ./plugins/$name directory.");
$this->info("Input and modify your plugin information in ./plugins/$name/composer.json file.");
}

Expand Down Expand Up @@ -217,12 +244,10 @@ protected function makeControllerClass($path, $name, $namespace, $title)
*/
protected function activatePlugin($name)
{
/** @var PluginHandler $handler */
$handler = app('xe.plugin');
$handler->getAllPlugins(true);
$this->handler->getAllPlugins(true);

if ($handler->isActivated($name) === false) {
$handler->activatePlugin($name);
if (!$this->handler->isActivated($name)) {
$this->handler->activatePlugin($name);
}
}

Expand Down

0 comments on commit fc46386

Please sign in to comment.