diff --git a/Console/Command/ThemeBootstrap.php b/Console/Command/ThemeBootstrap.php new file mode 100644 index 0000000..f168a7f --- /dev/null +++ b/Console/Command/ThemeBootstrap.php @@ -0,0 +1,181 @@ + + + {{THEME_NAME}} + {{PARENT_THEME_NAME}} + +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 + ); + } +} diff --git a/Console/Command/ThemeBootstrapCommand.php b/Console/Command/ThemeBootstrapCommand.php new file mode 100644 index 0000000..4b1d7af --- /dev/null +++ b/Console/Command/ThemeBootstrapCommand.php @@ -0,0 +1,112 @@ +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('Theme dir already exist'); + 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('Failed to generate files'); + return 9; + } + + $output->writeln('New Local Swissup theme bootstrap done! Happy coding!'); + $output->writeln('Please run setup:upgrade from Magento CLI'); + } + + /** + * @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; + } +} diff --git a/etc/di.xml b/etc/di.xml index 0051423..4c9b0c7 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -21,6 +21,7 @@ Swissup\Core\Console\Command\ModuleCommand Swissup\Core\Console\Command\ModuleListCommand Swissup\Core\Console\Command\ModuleInstallCommand + Swissup\Core\Console\Command\ThemeBootstrapCommand