<commit_before><?php

/**
 * This file is part of the DeployBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sokil\DeployBundle\Task;

use Sokil\DeployBundle\Exception\TaskConfigurationValidateException;
use Sokil\DeployBundle\TaskManager\ProcessRunner;
use Symfony\Component\Console\Output\OutputInterface;

class WebpackTask extends AbstractTask implements ProcessRunnerAwareTaskInterface
{
    /**
     * @var array
     */
    private $options;

    /**
     * @var ProcessRunner
     */
    private $processRunner;

    /**
     * Get console command description
     * @return mixed
     */
    public function getDescription()
    {
        return 'Sync files between servers';
    }

    /**
     * Prepare task options configured in bundle`s config: check values and set default values
     *
     * @param array $options configuration
     *
     * @throws TaskConfigurationValidateException
     */
    protected function configure(array $options)
    {
        $this->options = $options;
    }

    /**
     * @param ProcessRunner $runner
     */
    public function setProcessRunner(ProcessRunner $runner)
    {
        $this->processRunner = $runner;
    }

    /**
     * Run task
     *
     * @param array $commandOptions
     * @param $environment
     * @param $verbosity
     * @param OutputInterface $output
     *
     * @throws TaskConfigurationValidateException
     */
    public function run(
        array $commandOptions,
        $environment,
        $verbosity,
        OutputInterface $output
    ) {
        $webpackOptions = $this->options;

        // get path to webpack
        $webpackPath = !empty($webpackOptions['webpackPath'])
            ? $webpackOptions['webpackPath']
            : 'webpack';

        unset($webpackOptions['webpackPath']);

        // build command
        $command = [$webpackPath];
        foreach ($webpackOptions as $webpackOptionName => $webpackOptionValue) {
            if (is_bool($webpackOptionValue)) {
                // flag
                if ($webpackOptionValue === true) {
                    if (strlen($webpackOptionName) === 1) {
                        $command[] = '-' . $webpackOptionName;
                    } else {
                        $command[] = '--' . $webpackOptionName;
                    }
                }
            } elseif (is_array($webpackOptionValue)) {
                // array argument
                foreach ($webpackOptionValue as $commandOptionValueElement) {
                    $command[] = '--' . $webpackOptionName . ' ' . $commandOptionValueElement;
                }
            } else {
                // scalar argument<commit_msg>allow specify dir and force production flag<commit_after><?php

/**
 * This file is part of the DeployBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sokil\DeployBundle\Task;

use Sokil\DeployBundle\Exception\TaskConfigurationValidateException;
use Sokil\DeployBundle\TaskManager\ProcessRunner;
use Symfony\Component\Console\Output\OutputInterface;

class WebpackTask extends AbstractTask implements ProcessRunnerAwareTaskInterface
{
    /**
     * @var array
     */
    private $options;

    /**
     * @var ProcessRunner
     */
    private $processRunner;

    /**
     * Get console command description
     * @return mixed
     */
    public function getDescription()
    {
        return 'Sync files between servers';
    }

    /**
     * Prepare task options configured in bundle`s config: check values and set default values
     *
     * @param array $options configuration
     *
     * @throws TaskConfigurationValidateException
     */
    protected function configure(array $options)
    {
        $this->options = $options;
    }

    /**
     * @param ProcessRunner $runner
     */
    public function setProcessRunner(ProcessRunner $runner)
    {
        $this->processRunner = $runner;
    }

    /**
     * Run task
     *
     * @param array $commandOptions
     * @param $environment
     * @param $verbosity
     * @param OutputInterface $output
     *
     * @throws TaskConfigurationValidateException
     */
    public function run(
        array $commandOptions,
        $environment,
        $verbosity,
        OutputInterface $output
    ) {
        $webpackOptions = $this->options;

        $command = [];
        
        // get working dir
        $workingDir = !empty($webpackOptions['workingDir'])
            ? $webpackOptions['workingDir']
            : null;
        
        if ($workingDir) {
            $command[] = 'cd ' . $workingDir . ';';
        }
        
        // get path to webpack
        $webpackPath = !empty($webpackOptions['webpackPath'])
            ? $webpackOptions['webpackPath']
            : 'webpack';

        unset($webpackOptions['webpackPath']);
        
        $command[] = $webpackPath;
        
        // force production flag in production env
        if ($environment === 'prod' && empty($webpackOptions['p'])) {
            $webpackOptions['p'] = true;
        }

        // build command
        foreach ($webpackOptions as $webpackOptionName => $webpackOptionValue) {
            if (is_bool($webpackOptionValue)) {
                // flag
                if ($webpackOptionValue === true) {
                    if (strlen($webpackOptionName) === 1) {
                        $command[] = '-' . $webpackOptionName;
                    } else {
                        $command[] = '--' . $webpackOptionName;
                    }
                }
            } elseif (is_array($webpackOptionValue)) {
                // array argument
                foreach ($webpackOptionValue as $commandOptionValueElement) {
                    $command[] = '--' . $webpackOptionName . ' ' . $commandOptionValueElement;
                }
            } else {
                // scalar argument
{'fraction_non_alphanumeric': 0.10675631354542472, 'fraction_numerical': 0.00032797638570022957, 'mean_word_length': 1.9181818181818182, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518450', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1512, 'n_words': 374}