Skip to content

Commit

Permalink
Save WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Aug 17, 2022
1 parent a2059b1 commit 5be2b86
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
16 changes: 8 additions & 8 deletions src/Configuration/Storage/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ class Node implements \IteratorAggregate, \ArrayAccess

public function __construct($value, $source)
{
if (is_array($value)) {
$this->value = array_map(function ($elem) use ($source) {
return new Node($elem, $source);
}, $value);
} else {
$this->value = $value;
}
$this->source = Sources::getSource($source);
$this->setValue($value);
}

public function __clone()
Expand Down Expand Up @@ -60,7 +54,13 @@ public function getSource():Source
*/
public function setValue($value): Node
{
$this->value = $value;
if (is_array($value)) {
$this->value = array_map(function ($elem) {
return new Node($elem, $this->getSource());
}, $value);
} else {
$this->value = $value;
}
return $this;
}

Expand Down
81 changes: 47 additions & 34 deletions src/Method/RunCommandBaseMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ abstract class RunCommandBaseMethod extends BaseMethod implements MethodInterfac


const HOST_CONTEXT = 'host';
const DOCKER_HOST_CONTEXT = 'dockerHost';
const DOCKER_IMAGE = ScriptExecutionContext::DOCKER_IMAGE;
const DOCKER_HOST_CONTEXT = 'docker-host';
const DOCKER_IMAGE_CONTEXT = ScriptExecutionContext::DOCKER_IMAGE;
const DOCKER_IMAGE_ON_DOCKER_HOST_CONTEXT = 'docker-image-on-docker-host';

const RUN_CONTEXT_KEY = 'runContext';
const ROOT_FOLDER_KEY = 'rootFolder';
Expand Down Expand Up @@ -96,7 +97,10 @@ public function validateConfig(Node $config, ValidationErrorBagInterface $errors
$run_context_key = $this->getConfigKey(self::RUN_CONTEXT_KEY);
$validation->isOneOf(
$run_context_key,
[self::HOST_CONTEXT, self::DOCKER_HOST_CONTEXT, self::DOCKER_IMAGE]
[
self::HOST_CONTEXT,
self::DOCKER_HOST_CONTEXT,
self::DOCKER_IMAGE_CONTEXT]
);

if ($config->getProperty($run_context_key) == self::DOCKER_HOST_CONTEXT
Expand Down Expand Up @@ -130,57 +134,66 @@ protected function runCommand(
$command = $this->prepareCommand($host_config, $context, $command);
$run_context = $host_config->getProperty($this->getConfigKey(self::RUN_CONTEXT_KEY));

// Lets construct a script and set the execution context there.
/** @var \Phabalicious\Method\ScriptMethod $script_method */
$script_method = $context->getConfigurationService()->getMethodFactory()->getMethod('script');
$script_context = clone $context;
$script_context->set(
ScriptMethod::SCRIPT_CONTEXT_DATA,
$host_config->getProperty($this->getConfigPrefix())
);

switch ($run_context) {
case self::DOCKER_IMAGE:
// Lets construct a script and set the execution context there.
/** @var \Phabalicious\Method\ScriptMethod $script_method */
$script_method = $context->getConfigurationService()->getMethodFactory()->getMethod('script');
$script_context = clone $context;
case self::DOCKER_IMAGE_CONTEXT:
$script_context->set(ScriptMethod::SCRIPT_CONTEXT, $run_context);
$script_context->set(
ScriptMethod::SCRIPT_CONTEXT_DATA,
$host_config->getProperty($this->getConfigPrefix())
);
$script_context->setShell($this->getShell($host_config, $context));

$bag = new ScriptDataBag();
$bag->setContext($script_context)
->setCommands([
sprintf('#!%s %s', $this->getExecutableName(), $command)
])
->setRootFolder($this->getConfig($host_config, self::ROOT_FOLDER_KEY));

$result = $script_method->runScriptImpl($bag);
$context->mergeResults($script_context);

$context->setResult('exitCode', $result->getExitCode());
$context->setCommandResult($result);

$shell = $this->getShell($host_config, $context);
$shell->pushWorkingDir($this->getConfig($host_config, self::ROOT_FOLDER_KEY));

break;

case self::DOCKER_HOST_CONTEXT:
case self::DOCKER_IMAGE_ON_DOCKER_HOST_CONTEXT:
/** @var DockerMethod $docker_method */
$docker_method = $context->getConfigurationService()->getMethodFactory()->getMethod('docker');
$docker_config = $docker_method->getDockerConfig($host_config, $context);
$shell = $docker_config->shell();
$shell->pushWorkingDir($docker_method->getProjectFolder($docker_config, $host_config));
$shell->cd($this->getConfig($host_config, self::ROOT_FOLDER_KEY));
$result = $shell->run('#!' . $this->getExecutableName(). ' ' . $command);
$context->setResult('exitCode', $result->getExitCode());
$context->setCommandResult($result);
$shell->popWorkingDir();

if ($run_context == self::DOCKER_IMAGE_ON_DOCKER_HOST_CONTEXT) {
$script_context->set(ScriptMethod::SCRIPT_CONTEXT, ScriptExecutionContext::DOCKER_IMAGE);
}

break;

default:
$shell = $this->getShell($host_config, $context);
$shell->pushWorkingDir($this->getConfig($host_config, self::ROOT_FOLDER_KEY));
$result = $shell->run('#!' . $this->getExecutableName(). ' ' . $command);
$context->setResult('exitCode', $result->getExitCode());
$context->setCommandResult($result);
$shell->popWorkingDir();

break;
}

$commands = is_array($command)
? $command
: [
sprintf('#!%s %s', $this->getExecutableName(), $command),
];

$script_context->setShell($shell);

$bag = new ScriptDataBag();
$bag->setContext($script_context)
->setCommands($commands)
->setRootFolder($this->getConfig($host_config, self::ROOT_FOLDER_KEY));

$result = $script_method->runScriptImpl($bag);
$context->mergeResults($script_context);

$context->setResult('exitCode', $result->getExitCode());
$context->setCommandResult($result);

$shell->popWorkingDir();
}

protected function prepareCommand(HostConfig $host_config, TaskContextInterface $context, string $command): string
Expand Down
5 changes: 5 additions & 0 deletions tests/RunCommandBaseMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,18 @@ public function testYarnRunCommand($config)
$result = $this->context->getCommandResult();

$this->assertEquals(0, $result->getExitCode());

$payload = implode("\n", $result->getOutput());
$this->assertStringContainsString("name: 'react'", $payload);
}


public function hostConfigDataProvider(): array
{
return [
['inside-docker-image-on-docker-host'],
['on-host'],
['on-docker-host'],
['inside-docker-image'],
];
}
Expand Down
24 changes: 24 additions & 0 deletions tests/assets/run-command-tests/fabfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: run-command-tests
needs:
- yarn

dockerHosts:
dockerHost:
runLocally: true
rootFolder: .

hosts:
yarn-deprecated:
Expand All @@ -11,6 +15,7 @@ hosts:
yarnBuildCommand: build:prod
yarnRunContext: docker-image
image: node:16

npm-deprecated:
needs:
- npm
Expand All @@ -19,13 +24,15 @@ hosts:
npmBuildCommand: build:prod
npmRunContext: docker-image
image: node:16

composer-deprecated:
needs:
- composer
rootFolder: .
composerRootFolder: /foo/bar
composerRunContext: docker-image
image: composer

laravel-deprecated:
needs:
- laravel
Expand All @@ -39,10 +46,27 @@ hosts:
yarnBuildCommand: info react
yarnRunContext: host

on-docker-host:
rootFolder: .
yarnBuildCommand:
- pwd
- yarn info react
yarnRunContext: docker-host
docker:
configuration: docker-host

inside-docker-image:
inheritsFrom:
- on-host
yarn:
buildCommand: info react
context: docker-image
image: node:14

inside-docker-image-on-docker-host:
inheritsFrom:
- on-docker-host
yarn:
buildCommand: info react
context: docker-image-on-docker-host
image: node:14

0 comments on commit 5be2b86

Please sign in to comment.