Skip to content

Commit

Permalink
Merge pull request #80 from shopwareLabs/add-defered-commands
Browse files Browse the repository at this point in the history
add defered commands to psh
  • Loading branch information
JanPietrzyk authored Jan 31, 2019
2 parents be53baa + 4095347 commit d8ac27f
Show file tree
Hide file tree
Showing 19 changed files with 705 additions and 190 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ TEMPLATE: ../templates/template.ini.tpl:../destination/template.ini

Notice that all paths here must be **relative** to the script location.

#### Defer execution to the background

Execute the script in the background, so the following command gets executed right away

```sh
D: php generate_some_things.php
```

#### Wait for all deferred commands to execute

If you then want to wait for all results, just add a `WAIT` in there.

```sh
WAIT:
```


#### Open a ssh connection to another machine

Many dev-ops script open a SSH channel to a locally running virtual machine / container or a remote staging / test system. If you do this
Expand Down
7 changes: 5 additions & 2 deletions actions/unit.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
# DESCRIPTION: Execute unit tests

bin/php-cs-fixer fix
bin/phpunit --debug --verbose --coverage-clover=./build/coverage.xml --coverage-html=./build/html-coverage
D: bin/php-cs-fixer fix
D: phpdbg -qrr bin/phpunit --debug --verbose --coverage-clover=./build/coverage.xml --coverage-html=./build/html-coverage

WAIT:

./humbug.phar --no-interaction
60 changes: 38 additions & 22 deletions src/Application/ClimateLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use League\CLImate\CLImate;
use Shopware\Psh\Listing\Script;
use Shopware\Psh\ScriptRuntime\Logger;
use Shopware\Psh\ScriptRuntime\LogMessage;

/**
* A CLImate implementation of the runtime logger
Expand Down Expand Up @@ -61,53 +62,68 @@ public function finishScript(Script $script)
}

/**
* @param string $shellCommand
* @param int $line
* @param bool $isIgnoreError
* @param int $index
* @param int $max
* @param string $response
* @return string
*/
public function logCommandStart(string $shellCommand, int $line, bool $isIgnoreError, int $index, int $max)
private function formatOutput(string $response) :string
{
$index++;
$this->cliMate->yellow()->inline("\n({$index}/{$max}) Starting\n<bold>> {$shellCommand}</bold>\n\t");
return str_replace(PHP_EOL, PHP_EOL . "\t", $response);
}


/**
* @param string $destination
* @param int $line
* @param int $index
* @param int $max
* @return void
*/
public function logTemplate(string $destination, int $line, int $index, int $max)
public function logWait()
{
$index++;
$this->cliMate->yellow()->inline("\n({$index}/{$max}) Rendering\n<bold>> {$destination}</bold>\n\t");
$this->cliMate->green()->bold()->inline("\nWAITING...\n\t");
}

public function log(LogMessage $logMessage)
{
if ($logMessage->isError()) {
$this->err($logMessage->getMessage());
} else {
$this->out($logMessage->getMessage());
}
}

/**
* @param string $response
*/
public function err(string $response)
private function err(string $response)
{
$this->cliMate->red()->inline($this->formatOutput($response));
}

/**
* @param string $response
*/
public function out(string $response)
private function out(string $response)
{
$this->cliMate->green()->inline($this->formatOutput($response));
}

/**
* @param string $response
* @return string
* @param string $headline
* @param string $subject
* @param int $line
* @param bool $isIgnoreError
* @param int $index
* @param int $max
*/
private function formatOutput(string $response) :string
public function logStart(string $headline, string $subject, int $line, bool $isIgnoreError, int $index, int $max)
{
return str_replace(PHP_EOL, PHP_EOL . "\t", $response);
$index++;
$this->cliMate->yellow()->inline("\n({$index}/{$max}) $headline\n<bold>> {$subject}</bold>\n\t");
}

public function logSuccess()
{
$this->cliMate->green()->bold()->out('Executed Successfully');
}

public function LogFailure()
{
$this->cliMate->green()->red()->out('Executed with failure');
}
}
81 changes: 48 additions & 33 deletions src/ScriptRuntime/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ class CommandBuilder
*/
private $allCommands = [];

/**
* @var string
*/
private $currentShellCommand;

/**
* @var int
*/
private $startLine;

/**
* @var bool
*/
Expand All @@ -38,38 +28,40 @@ class CommandBuilder
*/
private $template;

/**
* @var bool
*/
private $deferred;

public function __construct()
{
$this->reset();
}

private function reset()
{
if ($this->currentShellCommand) {
$this->allCommands[] = new ProcessCommand(
$this->currentShellCommand,
$this->startLine,
$this->ignoreError,
$this->tty
);
}

$this->currentShellCommand = null;
$this->startLine = null;
$this->ignoreError = false;
$this->tty = false;
$this->template = false;
$this->deferred = false;
}

/**
* @param string $shellCommand
* @param int $startLine
* @param bool $ignoreError
* @return CommandBuilder
*/
public function next(string $shellCommand, int $startLine, bool $ignoreError, bool $tty): CommandBuilder
public function addProcessCommand(string $shellCommand, int $startLine): CommandBuilder
{
$this->reset();
$this->allCommands[] = new ProcessCommand(
$shellCommand,
$startLine,
$this->ignoreError,
$this->tty,
$this->deferred
);

$this->currentShellCommand = $shellCommand;
$this->startLine = $startLine;
$this->ignoreError = $ignoreError;
$this->tty = $tty;
$this->reset();

return $this;
}
Expand All @@ -80,7 +72,7 @@ public function next(string $shellCommand, int $startLine, bool $ignoreError, bo
* @param int $lineNumber
* @return $this
*/
public function addTemplateCommand(string $source, string $destination, int $lineNumber)
public function addTemplateCommand(string $source, string $destination, int $lineNumber): CommandBuilder
{
$this->reset();

Expand All @@ -93,6 +85,18 @@ public function addTemplateCommand(string $source, string $destination, int $lin
return $this;
}

/**
* @param int $lineNumber
* @return CommandBuilder
*/
public function addWaitCommand(int $lineNumber): CommandBuilder
{
$this->reset();
$this->allCommands[] = new WaitCommand($lineNumber);

return $this;
}

/**
* @param bool $set
* @return CommandBuilder
Expand All @@ -105,12 +109,23 @@ public function setIgnoreError(bool $set = true): CommandBuilder
}

/**
* @param string $shellCommand
* @param bool $set
* @return CommandBuilder
*/
public function setTty(bool $set = true): CommandBuilder
{
$this->tty = $set;

return $this;
}

/**
* @param bool $set
* @return CommandBuilder
*/
public function add(string $shellCommand): CommandBuilder
public function setDeferredExecution(bool $set = true): CommandBuilder
{
$this->currentShellCommand .= ' ' . trim($shellCommand);
$this->deferred = $set;

return $this;
}
Expand All @@ -119,7 +134,7 @@ public function add(string $shellCommand): CommandBuilder
* @param array $commands
* @return CommandBuilder
*/
public function setCommands(array $commands): CommandBuilder
public function replaceCommands(array $commands): CommandBuilder
{
$this->allCommands = $commands;

Expand Down
80 changes: 80 additions & 0 deletions src/ScriptRuntime/DeferredProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);

namespace Shopware\Psh\ScriptRuntime;

use Symfony\Component\Process\Process;

class DeferredProcess
{
/**
* @var string
*/
private $parsedCommand;

/**
* @var ProcessCommand
*/
private $command;

/**
* @var Process
*/
private $process;

/**
* @var LogMessage[]
*/
private $log = [];

/**
* @param string $parsedCommand
* @param ProcessCommand $command
* @param Process $process
*/
public function __construct(string $parsedCommand, ProcessCommand $command, Process $process)
{
$this->command = $command;
$this->process = $process;
$this->parsedCommand = $parsedCommand;
}

/**
* @return string
*/
public function getParsedCommand(): string
{
return $this->parsedCommand;
}

/**
* @return ProcessCommand
*/
public function getCommand(): ProcessCommand
{
return $this->command;
}

/**
* @return Process
*/
public function getProcess(): Process
{
return $this->process;
}

/**
* @param LogMessage $logMessage
*/
public function log(LogMessage $logMessage)
{
$this->log[] = $logMessage;
}

/**
* @return LogMessage[]
*/
public function getLog(): array
{
return $this->log;
}
}
42 changes: 42 additions & 0 deletions src/ScriptRuntime/LogMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Shopware\Psh\ScriptRuntime;

class LogMessage
{
/**
* @var string
*/
private $message;

/**
* @var bool
*/
private $error;

/**
* @param string $message
* @param bool $error
*/
public function __construct(string $message, bool $error)
{
$this->message = $message;
$this->error = $error;
}

/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}

/**
* @return bool
*/
public function isError(): bool
{
return $this->error;
}
}
Loading

0 comments on commit d8ac27f

Please sign in to comment.