Skip to content

Commit

Permalink
Improve version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
soebbing committed Nov 12, 2018
1 parent 79438a8 commit df591b1
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -9,7 +9,7 @@ engine/*
/vendor/*

# PHPUnit
/tests
/tests/shopware

# Themes
/themes/Backend/ExtJs
Expand Down Expand Up @@ -37,6 +37,7 @@ engine/*
!/var/cache/.htaccess
!/var/cache/clear_cache.sh
/web/cache/*
/web/sitemap/*

# Log files
/var/log/*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@ one created for you using an interactive installer.
To use the installer, simply run:

```bash
$ ./app/install.sh
$ ./app/bin/install.sh
```

## Configuration
Expand Down
60 changes: 16 additions & 44 deletions app/AppKernel.php
@@ -1,6 +1,5 @@
<?php

use PackageVersions\Versions;
use Shopware\Kernel;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -61,51 +60,24 @@ protected function prepareContainer(ContainerBuilder $container)

private function loadRelease()
{
$this->loadReleaseFromEnv();
$this->loadReleaseFromComposer();
}

/**
* Setting the environment variables, either directly, by the webserver or using the .env-file
* allows you to define a custom Shopware version IF NECESSARY.
*
* It should match the version being installed by composer. This way plugins still are able to check
* for the Shopware version.
*
* YOU SHOULDN'T NORMALLY HAVE TO DO THIS! (See below)
*/
private function loadReleaseFromEnv()
{
$this->release['version'] = getenv('SHOPWARE_VERSION') === false ? self::VERSION : getenv('SHOPWARE_VERSION');
$this->release['revision'] = getenv('SHOPWARE_REVISION') === false ? self::REVISION : getenv('SHOPWARE_REVISION');
$this->release['version_text'] = getenv('SHOPWARE_VERSION_TEXT') === false ? self::VERSION_TEXT : getenv('SHOPWARE_VERSION_TEXT');
}

/**
* We try to determine the installed version of Shopware automatically.
*/
private function loadReleaseFromComposer()
{
// If something was defined in the ENV, we respect that setting
if ($this->release['version'] !== self::VERSION) {
return;
}

try {
$version = Versions::getVersion('shopware/shopware');

if (!preg_match('/^v?(?<plainVersion>[\d]+\.[\d]+\.[\d]+)(\-(?<stability>[a-z\d]{0,4}))?(@(?<hash>[a-z\d]+)?)?$/i', $version, $versionMatches)) {
throw new OutOfBoundsException(sprintf('Version "%s" not in expected format', $version));
}

$this->release['version'] = $versionMatches['plainVersion'];
$this->release['revision'] = isset($versionMatches['hash']) ? substr($versionMatches['hash'], 0, 10) : '';
$this->release['version_text'] = $versionMatches['stability'] ?? '';
$release = ShopwareVersion::parseVersion(
\PackageVersions\Versions::getVersion('shopware/shopware')
);
} catch (\OutOfBoundsException $ex) {
// Silent catch
$this->release['version'] = 'unknown';
$this->release['revision'] = 'unknown';
$this->release['version_text'] = '';
try {
$release = ShopwareVersion::parseVersion(
sprintf('%s-%s@%s', getenv('SHOPWARE_VERSION'), getenv('SHOPWARE_REVISION'), getenv('SHOPWARE_VERSION_TEXT'))
);
} catch (\OutOfBoundsException $ex) {
$release = [
'version' => '___VERSION___',
'version_text' => '___VERSION_TEXT___',
'revision' => '___REVISION___'
];
}
}

$this->release = $release;
}
}
17 changes: 17 additions & 0 deletions app/ShopwareVersion.php
@@ -0,0 +1,17 @@
<?php

class ShopwareVersion
{
public static function parseVersion(string $version): array
{
if (!preg_match('/^v?(?<version>[\d]+\.[\d]+\.[\d]+)(\-(?<version_text>[a-z\d]{0,4}))?(@(?<revision>[a-z\d]+)?)?$/i', $version, $versionMatches)) {
throw new OutOfBoundsException(sprintf('Version "%s" not in expected format', $version));
}

return [
'version' => $versionMatches['version'],
'version_text' => $versionMatches['version_text'] ?? '',
'revision' => isset($versionMatches['revision']) ? substr($versionMatches['revision'], 0, 10) : ''
];
}
}
7 changes: 0 additions & 7 deletions app/banner.txt

This file was deleted.

7 changes: 7 additions & 0 deletions app/bin/banner.txt
@@ -0,0 +1,7 @@
____ _
/ ___|| |__ ___ _ ____ ____ _ _ __ ___
\___ \| '_ \ / _ \| '_ \ \ /\ / / _` | '__/ _ \
___) | | | | (_) | |_) \ V V / (_| | | | __/
|____/|_| |_|\___/| .__/ \_/\_/ \__,_|_| \___|
|_|

58 changes: 28 additions & 30 deletions app/functions.sh → app/bin/functions.sh
Expand Up @@ -16,6 +16,7 @@ function get_realpath(){
# Set magic variables for current FILE & DIR
declare -r __FILE__=$(get_realpath ${BASH_SOURCE[0]})
declare -r __DIR__=$(dirname $__FILE__)
declare -r __ENV_FILE__=$__DIR__/../../.env

# Coloring/Styling helpers
esc=$(printf '\033')
Expand Down Expand Up @@ -58,7 +59,7 @@ function promptYesOrNo(){
}

function swCommand(){
${__DIR__}/../bin/console "$@"
${__DIR__}/../../bin/console "$@"
}

function banner(){
Expand All @@ -68,7 +69,7 @@ function banner(){
}

function envFileDoesNotExists(){
[ ! -f ${__DIR__}/../.env ]
[ ! -f ${__ENV_FILE__} ]
return $?
}

Expand Down Expand Up @@ -119,33 +120,33 @@ function createEnvFile(){

IMPORT_DEMODATA=$(promptYesOrNo "Would you like to install demo data? (Y/n) " 'y')

echo -e "# This file was generated by the shopware composer shell installer\n" > ${__DIR__}/../.env
echo -e "# Shop environment and database connection" >> ${__DIR__}/../.env
echo -e "SHOPWARE_ENV=\"dev\"" >> ${__DIR__}/../.env

echo -e "\n# The URL has priority over the other values, so only one parameter needs to be set in production environments" >> ${__DIR__}/../.env
echo -e "DATABASE_URL=\"mysql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}\"\n" >> ${__DIR__}/../.env

echo -e "# If e.g. the password contains special chars not allowed in a URL, you can define each parameter by itself instead" >> ${__DIR__}/../.env
echo -e "DB_HOST=\"${DB_HOST}\"" >> ${__DIR__}/../.env
echo -e "DB_DATABASE=\"${DB_DATABASE}\"" >> ${__DIR__}/../.env
echo -e "DB_USERNAME=\"${DB_USERNAME}\"" >> ${__DIR__}/../.env
echo -e "DB_PASSWORD=\"${DB_PASSWORD}\"" >> ${__DIR__}/../.env
echo -e "DB_PORT=\"${DB_PORT}\"" >> ${__DIR__}/../.env

echo -e "\n# Installation configuration (can be removed after installation)" >> ${__DIR__}/../.env
echo -e "ADMIN_EMAIL=\"$ADMIN_EMAIL\"" >> ${__DIR__}/../.env
echo -e "ADMIN_NAME=\"$ADMIN_NAME\"" >> ${__DIR__}/../.env
echo -e "ADMIN_USERNAME=\"$ADMIN_USERNAME\"" >> ${__DIR__}/../.env
echo -e "ADMIN_PASSWORD=\"$ADMIN_PASSWORD\"" >> ${__DIR__}/../.env
echo -e "SHOP_URL=\"$SHOP_URL\"\n" >> ${__DIR__}/../.env
echo -e "IMPORT_DEMODATA=$IMPORT_DEMODATA" >> ${__DIR__}/../.env
echo -e "# This file was generated by the shopware composer shell installer\n" > ${__ENV_FILE__}
echo -e "# Shop environment and database connection" >> ${__ENV_FILE__}
echo -e "SHOPWARE_ENV=\"dev\"" >> ${__ENV_FILE__}

echo -e "\n# The URL has priority over the other values, so only one parameter needs to be set in production environments" >> ${__ENV_FILE__}
echo -e "DATABASE_URL=\"mysql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}\"\n" >> ${__ENV_FILE__}

echo -e "# If e.g. the password contains special chars not allowed in a URL, you can define each parameter by itself instead" >> ${__ENV_FILE__}
echo -e "DB_HOST=\"${DB_HOST}\"" >> ${__ENV_FILE__}
echo -e "DB_DATABASE=\"${DB_DATABASE}\"" >> ${__ENV_FILE__}
echo -e "DB_USERNAME=\"${DB_USERNAME}\"" >> ${__ENV_FILE__}
echo -e "DB_PASSWORD=\"${DB_PASSWORD}\"" >> ${__ENV_FILE__}
echo -e "DB_PORT=\"${DB_PORT}\"" >> ${__ENV_FILE__}

echo -e "\n# Installation configuration (can be removed after installation)" >> ${__ENV_FILE__}
echo -e "ADMIN_EMAIL=\"$ADMIN_EMAIL\"" >> ${__ENV_FILE__}
echo -e "ADMIN_NAME=\"$ADMIN_NAME\"" >> ${__ENV_FILE__}
echo -e "ADMIN_USERNAME=\"$ADMIN_USERNAME\"" >> ${__ENV_FILE__}
echo -e "ADMIN_PASSWORD=\"$ADMIN_PASSWORD\"" >> ${__ENV_FILE__}
echo -e "SHOP_URL=\"$SHOP_URL\"\n" >> ${__ENV_FILE__}
echo -e "IMPORT_DEMODATA=$IMPORT_DEMODATA" >> ${__ENV_FILE__}
}

function loadEnvFile(){
if [ -f $__DIR__/../.env ]; then
if [ -f ${__ENV_FILE__} ]; then
echo "${green}Loading configuration settings from .env file${reset}"
source $__DIR__/../.env
source ${__ENV_FILE__}
return
fi
echo "Could not load .env file"
Expand All @@ -161,23 +162,20 @@ function createEnvFileInteractive(){
echo -e "\n----------------------------------------------------------"
echo -e "The following settings have been written to the .env file:"
echo -e "----------------------------------------------------------\n"
cat ${__DIR__}/../.env
cat ${__ENV_FILE__}
echo -e "----------------------------------------------------------------\n"
correct=$(promptYesOrNo "Is this information correct? (Y/n) " 'y')
done
}

function createSymLinks(){
echo "Creating symlinks in $__DIR__"
cd $__DIR__/..
cd $__DIR__/../..
rm -rf engine/Library
mkdir -p engine/Library
ln -s ../../vendor/shopware/shopware/engine/Library/CodeMirror engine/Library/CodeMirror
ln -s ../../vendor/shopware/shopware/engine/Library/ExtJs engine/Library/ExtJs
ln -s ../../vendor/shopware/shopware/engine/Library/TinyMce engine/Library/TinyMce

rm -rf tests
ln -s vendor/shopware/shopware/tests tests

rm -rf themes/Frontend/{Bare,Responsive}
rm -rf themes/Backend/ExtJs
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions app/post-install.sh → app/bin/post-install.sh
Expand Up @@ -2,6 +2,8 @@

source $(dirname "$0")/functions.sh

createSymLinks

if envFileDoesNotExists
then
banner
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions app/config/config.php
Expand Up @@ -56,8 +56,16 @@
],
],

'web' => [
'webDir' => $projectDir . 'web',
'cacheDir' => $projectDir . 'web/cache',
],

'app' => [
'rootDir' => $projectDir,
/**
* These parameters were necessary in Shopware 5.4.x and are replaced by the filesystem below in 5.5.x
*/
/**
* Since Shopware 5.5 the configuration for 'downloadsDir' and 'documentsDir'
* have become obsolete and are now handled by the defined filesystem adapters below.
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Expand Up @@ -10,12 +10,15 @@
}
],
"autoload": {
"classmap": [ "app/AppKernel.php" ]
"classmap": [
"app/AppKernel.php",
"app/ShopwareVersion.php"
]
},
"require": {
"php": "^7.0",
"composer/installers": "^1.0",
"shopware/shopware": "^5.4",
"shopware/shopware": "^5.5",
"ocramius/package-versions": "1.2.0",
"vlucas/phpdotenv": "~2.0"
},
Expand All @@ -35,13 +38,13 @@
},
"scripts": {
"post-root-package-install": [
"./app/post-install.sh"
"./app/bin/post-install.sh"
],
"post-install-cmd": [
"./app/post-install.sh"
"./app/bin/post-install.sh"
],
"post-update-cmd":[
"./app/post-update.sh"
"./app/bin/post-update.sh"
]
}
}

0 comments on commit df591b1

Please sign in to comment.