From ebd638d80866bdcb1dd7a94fe6e9e1d064dfb9a7 Mon Sep 17 00:00:00 2001 From: Daniel Schipper Date: Thu, 28 Jul 2022 11:56:24 +0200 Subject: [PATCH] Update make and composer require --- makefile | 33 ++++++++- update-composer-require.php | 131 ++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 update-composer-require.php diff --git a/makefile b/makefile index 5d8578e..9342245 100644 --- a/makefile +++ b/makefile @@ -14,16 +14,45 @@ help: install: ## Installs all production dependencies @composer install --no-dev + @cd src/Resources/app/administration && npm install --production + @cd src/Resources/app/storefront && npm install --production dev: ## Installs all dev dependencies @composer install + @cd src/Resources/app/administration && npm install + @cd src/Resources/app/storefront && npm install clean: ## Cleans all dependencies rm -rf vendor rm -rf .reports | true + @make clean-node + +clean-node: ## Removes node_modules + rm -rf src/Resources/app/administration/node_modules + rm -rf src/Resources/app/storefront/node_modules # ------------------------------------------------------------------------------------------------------------ -release: ## Creates a new ZIP package +build: ## Builds the package + @rm -rf src/Resources/app/storefront/dist + @cd ../../.. && php bin/console plugin:refresh + @cd ../../.. && php bin/console plugin:install MyPaShopware --activate --clearCache | true + @cd ../../.. && php bin/console plugin:refresh + @cd ../../.. && php bin/console theme:dump + @cd ../../.. && PUPPETEER_SKIP_DOWNLOAD=1 ./bin/build-js.sh + @cd ../../.. && php bin/console theme:refresh + @cd ../../.. && php bin/console theme:compile + @cd ../../.. && php bin/console theme:refresh + +release: ## Create a new release + @make clean + @make install + @make build + @make zip + +zip: ## Creates a new ZIP package + @php update-composer-require.php --shopware=^6.3.0 --env=prod + @cd .. && echo "\nCreating Zip file MyPaShopware-$(PLUGIN_VERSION).zip\n" @cd .. && rm -rf MyPaShopware-$(PLUGIN_VERSION).zip - @cd .. && zip -qq -r -0 MyPaShopware-$(PLUGIN_VERSION).zip MyPaShopware/ -x '.editorconfig' '*.git*' '*.reports*' '*.travis.yml*' '*/tests*' '*/makefile' '*.DS_Store' '*/phpunit.xml' '*/.phpstan.neon' '*/.php_cs.php' '*/phpinsights.php' + @cd .. && zip -qq -r -0 MyPaShopware-$(PLUGIN_VERSION).zip MyPaShopware/ -x '*.editorconfig' '*.git*' '*.reports*' '*.travis.yml*' '*/tests*' '*/makefile' '*.DS_Store' '*/phpunit.xml' '*/.phpstan.neon' '*/.php_cs.php' '*/phpinsights.php' '*node_modules*' '*administration/build*' '*storefront/build*' '*/update-composer-require.php' + @php update-composer-require.php --shopware=^6.3.0 --env=dev diff --git a/update-composer-require.php b/update-composer-require.php new file mode 100644 index 0000000..2d67d54 --- /dev/null +++ b/update-composer-require.php @@ -0,0 +1,131 @@ +error('No options set. Option "env" is required.'); + return; +} + +switch ($opts['env']) { + case 'dev': + case 'develop': + case 'development': + $env = 'development'; + $require = 'require-dev'; + break; + case 'prod': + case 'production': + $env = 'production'; + $require = 'require'; + break; +} + +if(!isset($require)) { + $output->error('Env needs to be one of: dev, develop, development, prod, production'); + return; +} + +$shopware = '*'; + +if(isset($opts['shopware'])) { + $shopware = (string)$opts['shopware']; +} + +try { + $composerContent = file_get_contents(__DIR__ . '/composer.json'); + $composerContent = json_decode($composerContent, true); + + unset($composerContent['require']['shopware/core']); + unset($composerContent['require']['shopware/administration']); + unset($composerContent['require']['shopware/storefront']); + unset($composerContent['require-dev']['shopware/core']); + unset($composerContent['require-dev']['shopware/administration']); + unset($composerContent['require-dev']['shopware/storefront']); + + if(empty($composerContent['require'])) { + unset($composerContent['require']); + } + + if(empty($composerContent['require-dev'])) { + unset($composerContent['require-dev']); + } + + $composerContent[$require]['shopware/core'] = $shopware; + $composerContent[$require]['shopware/administration'] = $shopware; + $composerContent[$require]['shopware/storefront'] = $shopware; + + file_put_contents(__DIR__ . '/composer.json', json_encode($composerContent, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + + $output->info(sprintf('Switched composer.json to %s requiring Shopware version %s', $env, $shopware)); +} catch (\Exception $e) { + $output->error($e->getMessage()); +} + +class Output +{ + const COLORS = [ + 'black' => 0, + 'red' => 1, + 'green' => 2, + 'yellow' => 3, + 'blue' => 4, + 'magenta' => 5, + 'cyan' => 6, + 'white' => 7, + 'default' => 9, + ]; + + public function info($text) + { + $this->writeLn($this->createBlock([$text]), self::COLORS['black'], self::COLORS['green']); + } + + public function error($text) + { + $this->writeLn($this->createBlock([$text]), self::COLORS['white'], self::COLORS['red']); + } + + public function writeLn($messages, $fg = self::COLORS['default'], $bg = self::COLORS['default']) + { + if (!is_iterable($messages)) { + $messages = [$messages]; + } + + $fg = '3' . $fg; + $bg = '4' . $bg; + + foreach ($messages as $message) { + echo sprintf("\033[%s;%sm%s\033[0m%s", $fg, $bg, $message, PHP_EOL); + } + } + + public function createBlock(iterable $messages, int $indentLength = 2) + { + $lines = []; + $lineLength = 80; + + $lineIndentation = str_repeat(' ', $indentLength); + + foreach ($messages as $message) { + $messageLineLength = $lineLength - ($indentLength * 2); + $messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true)); + + foreach ($messageLines as $messageLine) { + $lines[] = $messageLine; + } + } + + array_unshift($lines, ''); + $lines[] = ''; + + foreach ($lines as &$line) { + $line = $lineIndentation . $line; + $line .= str_repeat(' ', max($lineLength - strlen($line), 0)); + } + + return $lines; + } +}