Skip to content

Commit

Permalink
Merge pull request #93 from shopwareLabs/next-minor-1.5
Browse files Browse the repository at this point in the history
Next minor 1.5
  • Loading branch information
JanPietrzyk authored Oct 14, 2020
2 parents 482c5fe + 464c37e commit 79d98c7
Show file tree
Hide file tree
Showing 27 changed files with 459 additions and 189 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ In order to use psh as a script executor, you need to define the locations in wh

PSH will then search in all these locations for `*.sh` files. These scripts can then be executed through PSH.

> Notice: If the script name starts with a dot (`.`) it will be excluded from the listing, but is callable like any other script. `> psh.phar .hidden-action`
#### Placeholders

Placeholders in your scripts looks like this:
Expand Down Expand Up @@ -276,6 +278,15 @@ A environment called `foo` may look like this:
This environment loads all scripts from `foo/sh/scripts` and `bar/sh/scripts`, adds a constant `TEST` and a variable `ID`.
If you want to call a script in this environment you have to prefix your call with `foo:`.

In order to exclude a whole environment from the listing add the `hidden` attribute to the environment tag and set it to `true`, like this:

```xml
<environment name="internal" hidden="true">
<path>internal/only/scripts</path>
</environment>
```

These scripts can be executed like any regular script, they will just not be shown in the listing.

#### Headers

Expand Down
1 change: 1 addition & 0 deletions resource/config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<xs:element name="template" type="template"/>
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="hidden" type="xs:boolean" default="false" />
</xs:complexType>

<xs:complexType name="template">
Expand Down
4 changes: 2 additions & 2 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function run(array $inputArgs): int
return self::RESULT_ERROR;
}

$this->showListing($scriptFinder->getAllScripts());
$this->showListing($scriptFinder->getAllVisibleScripts());

return self::RESULT_SUCCESS;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private function showAutocompleteListing(Config $config)
$scriptFinder = $this->applicationFactory
->createScriptFinder($config);

$scripts = $scriptFinder->getAllScripts();
$scripts = $scriptFinder->getAllVisibleScripts();

$commands = array_map(function (Script $script) {
return $script->getName();
Expand Down
42 changes: 2 additions & 40 deletions src/Application/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ApplicationFactory
*/
public function createConfig(string $rootDirectory, array $params): Config
{
$overwrittenConsts = (new ParameterParser())->parseParams($params);
$configFinder = new ConfigFileFinder();
$configFiles = $configFinder->discoverFiles($rootDirectory);

Expand All @@ -51,7 +52,7 @@ public function createConfig(string $rootDirectory, array $params): Config
continue;
}

$configs[] = $configLoader->load($configFile, $this->reformatParams($params));
$configs[] = $configLoader->load($configFile, $overwrittenConsts);
}
}

Expand Down Expand Up @@ -114,45 +115,6 @@ public function createCommands(Script $script, ScriptFinder $scriptFinder): arra
return $scriptLoader->loadScript($script);
}

/**
* @param array $params
* @return array
*/
private function reformatParams(array $params): array
{
if (count($params) < 2) {
return [];
}

$reformattedParams = [];
$paramsCount = count($params);
for ($i = 2; $i < $paramsCount; $i++) {
$key = $params[$i];

if (strpos($key, '--') !== 0) {
throw new InvalidParameterException(
sprintf('Unable to parse parameter %s. Use -- for correct usage', $key)
);
}

if (strpos($key, '=')) {
list($key, $value) = explode('=', $key, 2);

if (strpos($value, '"') === 0) {
$value = substr($value, 1, -1);
}
} else {
$i++;
$value = $params[$i];
}

$key = str_replace('--', '', $key);
$reformattedParams[strtoupper($key)] = $value;
}

return $reformattedParams;
}

/**
* @param $directory
* @return array
Expand Down
73 changes: 73 additions & 0 deletions src/Application/ParameterParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace Shopware\Psh\Application;

class ParameterParser
{
/**
* @param array $params
* @return array
*/
public function parseParams(array $params): array
{
if (count($params) < 2) {
return [];
}

$params = array_slice($params, 2);

$reformattedParams = [];
$paramsCount = count($params);
for ($i = 0; $i < $paramsCount; $i++) {
$key = $params[$i];

$this->testParameterFormat($key);

if ($this->isKeyValuePair($key)) {
list($key, $value) = explode('=', $key, 2);

if ($this->isEnclosedInAmpersand($value)) {
$value = substr($value, 1, -1);
}
} else {
$i++;
$value = $params[$i];
}

$key = str_replace('--', '', $key);
$reformattedParams[strtoupper($key)] = $value;
}

return $reformattedParams;
}

/**
* @param $key
*/
private function testParameterFormat(string $key)
{
if (strpos($key, '--') !== 0) {
throw new InvalidParameterException(
sprintf('Unable to parse parameter %s. Use -- for correct usage', $key)
);
}
}

/**
* @param $key
* @return bool|int
*/
private function isKeyValuePair($key)
{
return strpos($key, '=');
}

/**
* @param $value
* @return bool
*/
private function isEnclosedInAmpersand($value): bool
{
return strpos($value, '"') === 0;
}
}
6 changes: 3 additions & 3 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function getAllScriptsPaths(): array
foreach ($this->environments as $name => $environmentConfig) {
foreach ($environmentConfig->getAllScriptsPaths() as $path) {
if ($name !== $this->defaultEnvironment) {
$paths[] = new ScriptsPath($path, $name);
$paths[] = new ScriptsPath($path, $environmentConfig->isHidden(), $name);
} else {
$paths[] = new ScriptsPath($path);
$paths[] = new ScriptsPath($path, false);
}
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ private function createResult(callable ...$valueProviders): array
*/
private function getEnvironment(string $name = null): ConfigEnvironment
{
if (!$name) {
if ($name === null) {
return $this->environments[$this->defaultEnvironment];
}

Expand Down
16 changes: 15 additions & 1 deletion src/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ConfigBuilder

private $currentConstants;

private $hidden;

/**
* @param string|null $header
* @return ConfigBuilder
Expand All @@ -44,14 +46,24 @@ public function setHeader(string $header = null): ConfigBuilder
public function start(string $environment = null): ConfigBuilder
{
$this->reset();
if (!$environment) {
if ($environment === null) {
$environment = self::DEFAULT_ENV;
}

$this->currentEnvironment = $environment;
return $this;
}

/**
* @param bool $set
* @return ConfigBuilder
*/
public function setHidden(bool $set): ConfigBuilder
{
$this->hidden = $set;
return $this;
}

/**
* @param array $commandPaths
* @return ConfigBuilder
Expand Down Expand Up @@ -146,6 +158,7 @@ private function reset()
{
if ($this->currentEnvironment) {
$this->environments[$this->currentEnvironment] = new ConfigEnvironment(
$this->hidden,
$this->currentCommandPaths,
$this->currentDynamicVariables,
$this->currentConstants,
Expand All @@ -159,5 +172,6 @@ private function reset()
$this->currentDynamicVariables = [];
$this->currentConstants = [];
$this->templates = [];
$this->hidden = false;
}
}
16 changes: 16 additions & 0 deletions src/Config/ConfigEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/
class ConfigEnvironment
{
/**
* @var bool
*/
private $hidden;

/**
* @var array
*/
Expand Down Expand Up @@ -39,21 +44,32 @@ class ConfigEnvironment
* @param array $constants
* @param array $templates
* @param array $dotenvPaths
* @param bool $hidden
*/
public function __construct(
bool $hidden,
array $commandPaths = [],
array $dynamicVariables = [],
array $constants = [],
array $templates = [],
array $dotenvPaths = []
) {
$this->hidden = $hidden;
$this->commandPaths = $commandPaths;
$this->dynamicVariables = $dynamicVariables;
$this->constants = $constants;
$this->templates = $templates;
$this->dotenvPaths = $dotenvPaths;
}

/**
* @return bool
*/
public function isHidden(): bool
{
return $this->hidden;
}

/**
* @return array
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Config/ConfigFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function findFirstDirectoryWithConfigFile(string $fromDirectory): array
do {
$globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB);

if ($globResult) {
if (is_array($globResult) && count($globResult) > 0) {
return $globResult;
}

Expand Down Expand Up @@ -66,7 +66,7 @@ public function determineResultInDirectory(array $globResult): array
return $extension !== 'override' && $extension !== 'dist';
});

if ($configFiles) {
if (count($configFiles) > 0) {
return array_merge($configFiles, $overrideFiles);
}

Expand Down
49 changes: 39 additions & 10 deletions src/Config/ConfigMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,41 @@ public function merge(Config $config, Config $override = null): Config
private function mergeConfigEnvironments(Config $config, Config $override): array
{
$environments = [];
foreach ($override->getEnvironments() as $name => $overrideEnv) {
$originalConfigEnv = $config->getEnvironments()[$name];

$environments[$name] = new ConfigEnvironment(
$this->overrideScriptsPaths($originalConfigEnv, $overrideEnv),
$this->mergeDynamicVariables($config->getEnvironments()[$name], $overrideEnv),
$this->mergeConstants($config->getEnvironments()[$name], $overrideEnv),
$this->overrideTemplates($config->getEnvironments()[$name], $overrideEnv),
$this->mergeDotenvPaths($originalConfigEnv, $overrideEnv)
);

$foundEnvironments = array_keys(array_merge($config->getEnvironments(), $override->getEnvironments()));

foreach ($foundEnvironments as $name) {
if (!isset($override->getEnvironments()[$name])) {
$environments[$name] = $config->getEnvironments()[$name];

continue;
}

if (!isset($config->getEnvironments()[$name])) {
$environments[$name] = $override->getEnvironments()[$name];

continue;
}

$environments[$name] = $this
->mergeEnvironments($config->getEnvironments()[$name], $override->getEnvironments()[$name]);
}

return $environments;
}

private function mergeEnvironments(ConfigEnvironment $original, ConfigEnvironment $override): ConfigEnvironment
{
return new ConfigEnvironment(
$this->overrideHidden($original, $override),
$this->overrideScriptsPaths($original, $override),
$this->mergeDynamicVariables($original, $override),
$this->mergeConstants($original, $override),
$this->overrideTemplates($original, $override),
$this->mergeDotenvPaths($original, $override)
);
}

/**
* @param ConfigEnvironment $configEnvironment
* @param ConfigEnvironment $overrideEnv
Expand Down Expand Up @@ -114,4 +134,13 @@ private function overrideTemplates(ConfigEnvironment $configEnvironment, ConfigE

return $configEnvironment->getTemplates();
}

private function overrideHidden(ConfigEnvironment $originalConfigEnv, ConfigEnvironment $overrideEnv): bool
{
if ($overrideEnv->isHidden()) {
return true;
}

return $originalConfigEnv->isHidden();
}
}
Loading

0 comments on commit 79d98c7

Please sign in to comment.