From 50a6ff57ecadfaec77b7d8233b89e1a19a48c815 Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Tue, 8 Dec 2020 12:28:02 +0200 Subject: [PATCH] Added support for theme view in admin, removed CLI --- composer.json | 4 +- src/Console/Command/Bootstrap.php | 352 ------------------ .../Command/ScandiPWABootstrapException.php | 16 - src/Console/Command/ThemeBootstrapCommand.php | 144 ------- src/Console/CommandList.php | 50 --- src/Theme/Plugin/ThemePlugin.php | 21 ++ src/Theme/ThemeInterface.php | 1 - src/cli_commands.php | 5 - src/etc/di.xml | 3 + 9 files changed, 25 insertions(+), 571 deletions(-) delete mode 100644 src/Console/Command/Bootstrap.php delete mode 100644 src/Console/Command/ScandiPWABootstrapException.php delete mode 100755 src/Console/Command/ThemeBootstrapCommand.php delete mode 100644 src/Console/CommandList.php create mode 100644 src/Theme/Plugin/ThemePlugin.php delete mode 100644 src/cli_commands.php diff --git a/composer.json b/composer.json index bdc560c..469c046 100755 --- a/composer.json +++ b/composer.json @@ -7,12 +7,10 @@ ], "require": { "magento/framework": "*", - "magento/module-theme": "*", - "scandipwa/source": "^3" + "magento/module-theme": "*" }, "autoload": { "files": [ - "src/cli_commands.php", "src/registration.php" ], "psr-4": { diff --git a/src/Console/Command/Bootstrap.php b/src/Console/Command/Bootstrap.php deleted file mode 100644 index 76c9610..0000000 --- a/src/Console/Command/Bootstrap.php +++ /dev/null @@ -1,352 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - -namespace ScandiPWA\Installer\Console\Command; - - -use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Component\ComponentRegistrar; -use Magento\Framework\Component\ComponentRegistrarInterface; -use Magento\Framework\Exception\FileSystemException; -use Magento\Framework\Filesystem; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class Copy - * - * @package ScandiPWA\Installer\Console\Command - */ -class Bootstrap -{ - /** - * @var Filesystem\Directory\ReadInterface - */ - private $appRead; - - /** - * @var Filesystem\Directory\WriteInterface - */ - private $appWrite; - - /** - * @var Filesystem\Directory\ReadInterface - */ - private $baseReader; - - /** - * @var Filesystem\Directory\WriteInterface - */ - private $baseWriter; - - /** - * @var array - */ - private $copyQueue; - - /** - * @var OutputInterface - */ - private $output; - - /** - * @var string|null - */ - private $sourcePath; - - /** - * @var string - */ - private $themeName; - - const THEME_REGISTRATION_TEMPLATE = << - - - {{THEME_NAME}} - Magento/blank - true - - -EOT; - - - /** - * Bootstrap constructor. - * @param Filesystem $fs - * @param ComponentRegistrarInterface $registrar - * @throws FileSystemException - */ - public function __construct( - Filesystem $fs, - ComponentRegistrarInterface $registrar - ) - { - $this->copyQueue = $this->getCopyQueue(); - $this->sourcePath = $registrar->getPath(ComponentRegistrar::MODULE, ThemeBootstrapCommand::SOURCE_THEME_NAME); - $this->appRead = $fs->getDirectoryRead(DirectoryList::APP); - $this->appWrite = $fs->getDirectoryWrite(DirectoryList::APP); - $this->baseReader = $fs->getDirectoryRead(DirectoryList::ROOT); - $this->baseWriter = $fs->getDirectoryWrite(DirectoryList::ROOT); - } - - /** - * @return string[] - */ - private function getCopyQueue(): array - { - return [ - "package.json", - "package-lock.json", - "scandipwa.json", - "src/config/", - "src/public/", - "i18n/", - [ - "source" => ".eslintrc", - "destination" => ".eslintrc.sample" - ], - "process.yml", - "jsconfig.json", - "etc/view.xml", - "media/", - ".npmrc" - ]; - } - - - /** - * @param string $themeName - * @param OutputInterface $output - * @return int|mixed - * @throws ScandiPWABootstrapException - * @throws FileSystemException - */ - public function copy(string $themeName, OutputInterface $output) - { - $this->output = $output; - $this->themeName = $themeName; - - $output->write('Checking prerequisite...'); - try { - $this->validate($themeName); - } catch (ScandiPWABootstrapException $e) { - $output->writeln(' Failed'); - $output->writeln(sprintf('%s', $e->getMessage())); - - return $e->getCode(); - } - - $output->writeln(' Done'); - $output->writeln('Theme not set up. Setting up...'); - $output->writeln('Copying files...'); - - if ($this->copyFiles($this->copyQueue)) { - return 0; - } - - return 1; - } - - /** - * @param string $themeName - * @return int - * @throws FileSystemException - */ - public function generateRegistration(string $themeName): int - { - $destinationPath = $this->getThemePath($themeName); - return $this->appWrite->writeFile( - $destinationPath . DIRECTORY_SEPARATOR . 'registration.php', - str_replace( - '{{THEME_NAME}}', - ThemeBootstrapCommand::SECTION . DIRECTORY_SEPARATOR . $themeName, - self::THEME_REGISTRATION_TEMPLATE) - ); - } - - /** - * @param string $themeName - * @return int - * @throws FileSystemException - */ - public function generateThemeXml(string $themeName): int - { - $destinationPath = $this->getThemePath($themeName); - return $this->appWrite->writeFile( - $destinationPath . DIRECTORY_SEPARATOR . 'theme.xml', - str_replace( - '{{THEME_NAME}}', - str_replace('/', ' ', $themeName . ' theme'), - self::THEME_XML - ) - ); - } - - /** - * @param $themeName - * @return string - */ - protected function getThemePath($themeName): string - { - return $destinationPath = $this->appRead->getAbsolutePath( - ThemeBootstrapCommand::THEME_DIR . DIRECTORY_SEPARATOR . $themeName); - } - - /** - * @param string $path - * @param string $sourceFilePath - * @return bool - * @throws FileSystemException - * @throws ScandiPWABootstrapException - */ - protected function copyDirectory(string $path, string $sourceFilePath): bool - { - if (substr($path, -1) !== '/') { - $path .= '/'; - } - $directoryQueue = $this->getSubDirFiles($sourceFilePath); - $subDirQueue = array_map(function ($subDirItem) use ($path) { - return $path . $subDirItem; - }, $directoryQueue); - - return $this->copyFiles($subDirQueue); - } - - /** - * @param array $copyQueue - * @return bool - * @throws ScandiPWABootstrapException - * @throws FileSystemException - */ - protected function copyFiles(array $copyQueue): bool - { - if (count($copyQueue) < 1) { - return false; - } - - $output = $this->output; - $sourceFolder = $this->baseReader->getRelativePath($this->sourcePath) . DIRECTORY_SEPARATOR; - $destinationFolder = $this->appRead->getAbsolutePath( - ThemeBootstrapCommand::THEME_DIR . DIRECTORY_SEPARATOR . $this->themeName); - $destinationFolder = $this->baseWriter->getRelativePath($destinationFolder) . DIRECTORY_SEPARATOR; - - foreach ($copyQueue as $key => $item) { - if (is_array($item)) { - $sourceFilePath = $sourceFolder . $item['source']; - $destinationFilePath = $destinationFolder . $item['destination']; - } else { - $sourceFilePath = $sourceFolder . $item; - $destinationFilePath = $destinationFolder . $item; - } - - if ($this->baseReader->isDirectory($sourceFilePath)) { - unset($copyQueue[$key]); - $output->writeln(sprintf('Copying DIR: %s', $destinationFilePath)); - if (is_array($item)) { - throw new ScandiPWABootstrapException('Directory definition can not be array'); - } - if ($this->copyDirectory($item, $sourceFilePath)) { - $output->writeln(sprintf('Finished DIR: %s', $destinationFilePath)); - continue; - } - $output->writeln('Error copying dir: ' . $destinationFilePath); - } - - $output->write('Copying ' . $destinationFilePath . ''); - $copyingResult = $this->baseWriter->copyFile( - $sourceFilePath, - $destinationFilePath - ); - - $output->writeln($copyingResult ? ' Done' : ' Failed'); - } - - return true; - } - - /** - * @param string $directory - * @return array - */ - protected function getSubDirFiles($directory): array - { - $dirList = scandir($this->baseReader->getAbsolutePath($directory), 0); - if (!count($dirList)) { - return []; - } - $dirFiles = array_filter($dirList, function ($item) { - return ($item !== '.' && $item !== '..'); - }); - - return $dirFiles; - } - - /** - * @param $themeName - * @return bool - * @throws ScandiPWABootstrapException - */ - protected function isDestDirPresent($themeName): bool - { - if ($this->appRead->isDirectory(ThemeBootstrapCommand::THEME_DIR . DIRECTORY_SEPARATOR . $themeName)) { - throw new ScandiPWABootstrapException( - 'Theme already present. Please choose another name or remove manually', - 97); - } - - return true; - } - - /** - * @return bool - * @throws ScandiPWABootstrapException - */ - protected function isSourcePathAvailable(): bool - { - if ($this->sourcePath === null) { - throw new ScandiPWABootstrapException( - 'Sources are missing, have you installed the source package?', - 98); - } - - return true; - } - - /** - * @param $themeName - * @return bool - * @throws ScandiPWABootstrapException - */ - protected function validate($themeName): bool - { - return $this->isSourcePathAvailable() && $this->isDestDirPresent($themeName); - } -} diff --git a/src/Console/Command/ScandiPWABootstrapException.php b/src/Console/Command/ScandiPWABootstrapException.php deleted file mode 100644 index ded4091..0000000 --- a/src/Console/Command/ScandiPWABootstrapException.php +++ /dev/null @@ -1,16 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - -namespace ScandiPWA\Installer\Console\Command; - - -class ScandiPWABootstrapException extends \Exception -{ - -} diff --git a/src/Console/Command/ThemeBootstrapCommand.php b/src/Console/Command/ThemeBootstrapCommand.php deleted file mode 100755 index 4ebb878..0000000 --- a/src/Console/Command/ThemeBootstrapCommand.php +++ /dev/null @@ -1,144 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - - -namespace ScandiPWA\Installer\Console\Command; - -use Magento\Framework\Filesystem; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Formatter\OutputFormatterStyle; -use Symfony\Component\Console\Helper\QuestionHelper; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\QuestionFactory; - - -/** - * Class ThemeBootstrap - * - * @package ScandiPWA\Installer\Console\Command - */ -class ThemeBootstrapCommand extends Command -{ - const THEME_DIR = 'design/frontend'; - - const SECTION = 'frontend'; - - const SOURCE_THEME_NAME = 'ScandiPWA_Source'; - - /** - * @var Bootstrap - */ - private $bootstrap; - - /** - * @var QuestionFactory - */ - private $question; - - /** - * ThemeBootstrap constructor. - * - * @param Filesystem $fs - * @param null $name - */ - public function __construct(Bootstrap $bootstrap, QuestionFactory $question, $name = null) - { - parent::__construct($name); - $this->bootstrap = $bootstrap; - $this->question = $question; - } - - /** - * Define Symfony\Console compatible command - */ - protected function configure() - { - $this->setName('scandipwa:theme:bootstrap') - ->setDescription('Bootstraps ScandiPWA theme') - ->addArgument('name', InputArgument::REQUIRED, 'Put the theme name you want to create'); - - parent::configure(); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $this->prepareOutput($output); - $themeName = $input->getArgument('name'); - - // On success return 0, as these are EXIT codes - $copyCode = $this->bootstrap->copy($themeName, $output); - if ($copyCode) { - return $copyCode; - } - $noInteraction = $input->getOption('no-interaction'); - if (!$noInteraction) { - $question = $this->question->create([ - 'question' => 'Do you want to automatically generate registration.php? [y/N]', - 'default' => false, - ]); - $questionHelper = new QuestionHelper(); - $generateRegistarion = $questionHelper->ask($input, $output, $question); - if (!$generateRegistarion) { - $output->writeln(' You\'re done! '); - - return 0; - } - } - - - return $this->generateThemeFiles($themeName, $output); - } - - /** - * @param string $themeName - * @param OutputInterface $output - * @return int - */ - protected function generateThemeFiles(string $themeName, OutputInterface $output) - { - $registration = $this->bootstrap->generateRegistration($themeName); - $themeXml = $this->bootstrap->generateThemeXml($themeName); - if ($registration < 1 || $themeXml < 1) { - $output->writeln('Failed to generate files'); - return 9; - } - - $output->writeln('ScandiPWA new theme bootstrap done! Happy coding!'); - $output->writeln(sprintf('Now you can build the theme: cd app/design/frontend/%s && npm ci && npm run build', - $themeName)); - $output->writeln('Read the docs: https://docs.scandipwa.com'); - - return 0; - } - - /** - * @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/src/Console/CommandList.php b/src/Console/CommandList.php deleted file mode 100644 index a8ae554..0000000 --- a/src/Console/CommandList.php +++ /dev/null @@ -1,50 +0,0 @@ -objectManager = $objectManager; - } - - protected function getCommandClasses(): array - { - return [ - ThemeBootstrapCommand::class, - ]; - } - - /** - * @return array|\Symfony\Component\Console\Command\Command[] - * @throws \Exception - */ - public function getCommands() - { - $commands = []; - foreach ($this->getCommandClasses() as $class) { - if (!\class_exists($class)) { - throw new \Exception(sprintf('Class %s does not exist', $class)); - } - $commands[] = $this->objectManager->get($class); - } - - return $commands; - } -} \ No newline at end of file diff --git a/src/Theme/Plugin/ThemePlugin.php b/src/Theme/Plugin/ThemePlugin.php new file mode 100644 index 0000000..c64f973 --- /dev/null +++ b/src/Theme/Plugin/ThemePlugin.php @@ -0,0 +1,21 @@ + + * @copyright Copyright (c) 2020 Scandiweb, Ltd (https://scandiweb.com) + * @license OSL-3.0 + */ + +namespace ScandiPWA\Installer\Theme\Plugin; + +use Magento\Theme\Model\Theme; +use ScandiPWA\Installer\Theme\ThemeInterface; + +class ThemePlugin { + public function afterIsVisible(Theme $subject, $result) { + return $result ? $result : (int) $subject->getType() === ThemeInterface::TYPE_PWA; + } +} diff --git a/src/Theme/ThemeInterface.php b/src/Theme/ThemeInterface.php index 76026d6..e6ada48 100644 --- a/src/Theme/ThemeInterface.php +++ b/src/Theme/ThemeInterface.php @@ -11,7 +11,6 @@ namespace ScandiPWA\Installer\Theme; - use Magento\Framework\View\Design\ThemeInterface as MagentoThemeInterface; interface ThemeInterface extends MagentoThemeInterface diff --git a/src/cli_commands.php b/src/cli_commands.php deleted file mode 100644 index 7fe40ec..0000000 --- a/src/cli_commands.php +++ /dev/null @@ -1,5 +0,0 @@ - + + + theme