-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'swissup:theme:bootstrap' console command
- Loading branch information
Showing
3 changed files
with
294 additions
and
0 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,181 @@ | ||
<?php | ||
namespace Swissup\Core\Console\Command; | ||
|
||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\FileSystemException; | ||
use Magento\Framework\Filesystem; | ||
use Swissup\Core\Console\Command\ThemeBootstrapCommand; | ||
|
||
class ThemeBootstrap | ||
{ | ||
const THEME_COMPOSER_TEMPLATE = <<<EOT | ||
{ | ||
"name": "{{THEME_PACKAGE_NAME}}", | ||
"type": "magento2-theme", | ||
"version": "1.0.0", | ||
"require": { | ||
"{{PARENT_THEME_PACKAGE_NAME}}": "*" | ||
}, | ||
"autoload": { | ||
"files": [ "registration.php" ] | ||
} | ||
} | ||
EOT; | ||
|
||
const THEME_REGISTRATION_TEMPLATE = <<<EOT | ||
<?php | ||
use \Magento\Framework\Component\ComponentRegistrar; | ||
ComponentRegistrar::register( | ||
ComponentRegistrar::THEME, | ||
'{{THEME_NAME}}', | ||
__DIR__ | ||
); | ||
EOT; | ||
|
||
const THEME_XML = <<<EOT | ||
<?xml version="1.0"?> | ||
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd"> | ||
<title>{{THEME_NAME}}</title> | ||
<parent>{{PARENT_THEME_NAME}}</parent> | ||
</theme> | ||
EOT; | ||
|
||
/** | ||
* @var Filesystem\Directory\ReadInterface | ||
*/ | ||
private $appRead; | ||
|
||
/** | ||
* @var Filesystem\Directory\WriteInterface | ||
*/ | ||
private $appWrite; | ||
|
||
/** | ||
* Bootstrap constructor. | ||
* | ||
* @param Filesystem $fs | ||
* @throws FileSystemException | ||
*/ | ||
public function __construct(Filesystem $fs) | ||
{ | ||
$this->appRead = $fs->getDirectoryRead(DirectoryList::APP); | ||
$this->appWrite = $fs->getDirectoryWrite(DirectoryList::APP); | ||
} | ||
|
||
/** | ||
* @param string $themeName | ||
* @return int | ||
* @throws FileSystemException | ||
*/ | ||
public function generateRegistration(string $themeName): int | ||
{ | ||
$destinationPath = $this->getThemePath($themeName); | ||
|
||
$content = self::THEME_REGISTRATION_TEMPLATE; | ||
$content = str_replace( | ||
'{{THEME_NAME}}', | ||
ThemeBootstrapCommand::SECTION . DIRECTORY_SEPARATOR . $themeName, | ||
$content | ||
); | ||
|
||
return $this->appWrite->writeFile( | ||
$destinationPath . DIRECTORY_SEPARATOR . 'registration.php', | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $themeName | ||
* @return boolean | ||
*/ | ||
public function isExist(string $themeName) | ||
{ | ||
$path = $this->getThemePath($themeName); | ||
return $this->appWrite->isExist($path); | ||
} | ||
|
||
/** | ||
* @param string $themeName | ||
* @param string $parentThemeName | ||
* @return int | ||
* @throws FileSystemException | ||
*/ | ||
public function generateThemeXml(string $themeName, string $parentThemeName): int | ||
{ | ||
$content = self::THEME_XML; | ||
$content = str_replace( | ||
'{{THEME_NAME}}', | ||
str_replace('/', ' ', $themeName . ' theme'), | ||
$content | ||
); | ||
$content = str_replace( | ||
'{{PARENT_THEME_NAME}}', | ||
$parentThemeName, | ||
$content | ||
); | ||
$destinationPath = $this->getThemePath($themeName); | ||
|
||
return $this->appWrite->writeFile( | ||
$destinationPath . DIRECTORY_SEPARATOR . 'theme.xml', | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* @param string $themeName | ||
* @param string $parentThemePackageName | ||
* @return int | ||
* @throws FileSystemException | ||
*/ | ||
public function generateComposerJson(string $themeName, string $parentThemePackageName): int | ||
{ | ||
// local/argento-stripes-custom | ||
// swissup/theme-frontend-argento-stripe | ||
$content = self::THEME_COMPOSER_TEMPLATE; | ||
$content = str_replace( | ||
'{{THEME_PACKAGE_NAME}}', | ||
strtolower($themeName), | ||
// strtolower($themeName) . '-custom', | ||
$content | ||
); | ||
$content = str_replace( | ||
'{{PARENT_THEME_PACKAGE_NAME}}', | ||
$parentThemePackageName, | ||
$content | ||
); | ||
$destinationPath = $this->getThemePath($themeName); | ||
|
||
return $this->appWrite->writeFile( | ||
$destinationPath . DIRECTORY_SEPARATOR . 'composer.json', | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* @param string $themeName | ||
* @return int | ||
* @throws FileSystemException | ||
*/ | ||
public function generateCustomCss(string $themeName): int | ||
{ | ||
$content = '/* Autogenerated */'; | ||
$destinationPath = $this->getThemePath($themeName); | ||
|
||
return $this->appWrite->writeFile( | ||
$destinationPath . DIRECTORY_SEPARATOR . 'web/css/source/_argento_custom.less', | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* @param $themeName | ||
* @return string | ||
*/ | ||
protected function getThemePath($themeName): string | ||
{ | ||
return $destinationPath = $this->appRead->getAbsolutePath( | ||
ThemeBootstrapCommand::THEME_DIR . DIRECTORY_SEPARATOR . $themeName | ||
); | ||
} | ||
} |
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,112 @@ | ||
<?php | ||
namespace Swissup\Core\Console\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Formatter\OutputFormatterStyle; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Swissup\Core\Console\Command\ThemeBootstrap; | ||
|
||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class ThemeBootstrapCommand extends Command | ||
{ | ||
const THEME_DIR = 'design/frontend'; | ||
|
||
const SECTION = 'frontend'; | ||
|
||
/** | ||
* | ||
* @var ThemeBootstrap | ||
*/ | ||
private $bootstrap; | ||
|
||
/** | ||
* Inject dependencies | ||
* | ||
* @param \Swissup\Core\Model\ComponentList\Loader $bootstrap | ||
*/ | ||
public function __construct(ThemeBootstrap $bootstrap, $name = null) | ||
{ | ||
$this->bootstrap = $bootstrap; | ||
parent::__construct($name); | ||
} | ||
|
||
/** | ||
* Define Symfony\Console compatible command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('swissup:theme:bootstrap') | ||
->setDescription('Bootstrap Local Swissup theme') | ||
->addArgument('name', InputArgument::REQUIRED, 'Put the theme name you want to create (Local/argento-stripes)') | ||
->addArgument('parent', InputArgument::REQUIRED, 'Put the parent short theme name (stripes)'); | ||
|
||
$this->addOption( | ||
'css', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Should I create custom css?', | ||
false | ||
); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->prepareOutput($output); | ||
|
||
$themeName = $input->getArgument('name'); | ||
if (strpos($themeName, '/') === false) { | ||
$themeName = 'Local/' . $themeName; | ||
} | ||
$parent = $input->getArgument('parent'); | ||
$parentThemeName = 'Swissup/argento-' . $parent; | ||
$parentThemePackageName = 'swissup/theme-frontend-argento-' . $parent; | ||
|
||
if ($this->bootstrap->isExist($themeName)) { | ||
$output->writeln('<error>Theme dir already exist</error>'); | ||
return 9; | ||
} | ||
$registration = $this->bootstrap->generateRegistration($themeName); | ||
$themeXml = $this->bootstrap->generateThemeXml($themeName, $parentThemeName); | ||
$composerjson = $this->bootstrap->generateComposerJson($themeName, $parentThemePackageName); | ||
|
||
$withCss = $input->getOption('css'); | ||
$withCss = ($withCss !== false); | ||
if ($withCss) { | ||
$this->bootstrap->generateCustomCss($themeName); | ||
} | ||
|
||
if ($registration < 1 || $themeXml < 1 || $composerjson < 1) { | ||
$output->writeln('<error>Failed to generate files</error>'); | ||
return 9; | ||
} | ||
|
||
$output->writeln('<success>New Local Swissup theme bootstrap done! Happy coding!</success>'); | ||
$output->writeln('<warn>Please run setup:upgrade from Magento CLI</warn>'); | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* @return OutputInterface | ||
*/ | ||
protected function prepareOutput(OutputInterface $output) | ||
{ | ||
$error = new OutputFormatterStyle('red', 'black', ['bold', 'blink']); | ||
$warn = new OutputFormatterStyle('yellow', 'black', ['bold', 'blink']); | ||
$success = new OutputFormatterStyle('green', 'black', ['bold', 'blink']); | ||
$special = new OutputFormatterStyle('blue', 'black', ['bold', 'blink']); | ||
$output->getFormatter()->setStyle('error', $error); | ||
$output->getFormatter()->setStyle('warn', $warn); | ||
$output->getFormatter()->setStyle('success', $success); | ||
$output->getFormatter()->setStyle('special', $special); | ||
|
||
return $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