<commit_before><?php

namespace BootstrapUI\Shell\Task;

use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;

/**
 * Task for installing Bootstrap assets and layouts to app.
 */
class TwbsAssetsTask extends Shell
{
    protected $_assetDir;
    protected $_nodeDir;
    protected $_cssDir;
    protected $_jsDir;

    /**
     * TwbsAssetsTask constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->_assetDir = new Folder(Plugin::path('BootstrapUI') . 'webroot');
        $this->_nodeDir = new Folder(Plugin::path('BootstrapUI') . 'node_modules');
        $this->_cssDir = new Folder($this->_assetDir->path . DS . 'css');
        $this->_jsDir = new Folder($this->_assetDir->path . DS . 'js');
    }

    /**
     * Installs Bootstrap assets using npm
     *
     * @return void
     */
    public function installAssets()
    {
        $this->out('<info>Checking npm...</info>');
        if (!`which npm`) {
            $this->abort(__('NPM (https://www.npmjs.com/) is required, but not installed. Aborting.'));
        }

        chdir(Plugin::path('BootstrapUI'));
        $node_mod = new Folder('node_modules');
        if ($node_mod->delete()) {
            $this->out(__('<success>Cleared node_modules...</success>'));
        }

        exec('npm install --verbose', $output, $return);
        $this->out($output);
        if ($return === 0) {
            $this->out('<success>Bootstrap assets installed successfully.</success>');
        } else {
            $this->abort('Bootstrap assets could not be installed.');
        }
    }

    /**
     * Copy assets from node_modules folder to plugin's webroot
     * If in production mode, just copies min. required and minified assets
     *
     * @return void
     */
    public function copyAssets()
    {
        $this->out('<info>Clearing webroot and copying assets...</info>');
        if ($this->_clear($this->_assetDir, '^(?!cover)(?!dashboard)(?!signin).*$')) {
            $this->out('<success>All files cleared...</success>');
        }

        if (Configure::read('debug') === true) {
            $folders[] = new Folder($this->_nodeDir->path . DS . 'bootstrap/dist');
            $folders[] = new Folder($this->_nodeDir->path . DS . 'jquery/dist');
            $folders[] = new Folder($this->_nodeDir->path . DS . 'popper.js/dist');

            foreach ($folders as $folder) {
                foreach ($folder->findRecursive() as $file) {
                    $files[] = new File($file);
                }
            }
        } else {
            $files[] = new File($this->_nodeDir->path . DS . 'bootstrap' . DS . 'dist' . DS . 'css' . DS . 'bootstrap.min.css');
            $files[] = new File($this->_nodeDir->path . DS . 'bootstrap' . DS . 'dist' . DS . 'js' . DS . 'bootstrap.min.js');
            $files[] = new File($this->_nodeDir->path . DS . 'jquery' . DS . 'dist' . DS . 'jquery.min.js');
            $files[] = new File($this->_nodeDir->path . DS . 'popper.js' . DS . 'dist' . DS . 'popper.min.js');
        }
        $this->_copy($files);
    }

    /**
     * Copy sample layouts to app's layout dir
     *
     * @param string $target The destination path
     * @return void
     */
    public function copyLayouts($target = null)
    {
        $this->out(__('<info>Copying sample layouts...</info>'));
        $layoutDir = new Folder(Plugin::path('BootstrapUI') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'examples');

        if ($target == null) {
            $target = APP . 'Template' . DS . 'Layout' . DS . 'TwitterBootstrap';
        }

        if (!$layoutDir->copy($target)) {
            $this->abort('Sample layouts could not be copied.');
        }
        $this->out('<success>Sample layouts copied successfully.</success>');
    }

    /**
     * Copy files to assetdir's css/js path
     *
     * @param array $files Assetfiles to copy
     * @return void
     */
    protected function _copy(array $files)
    {
        foreach ($files as $file) {
            if ($file->ext() == 'css') {
                $dir = $this->_cssDir;
            } elseif ($file->ext() == 'js') {
                $dir = $this->_jsDir;
            }

            if ($file->copy($dir->path . DS . $file->name)) {
                $this->out('<success>' . $file->name . ' successfully copied.</success>');
            } else {
                $this->out('<warning>' . $file->name . ' could not be copied.</warning>');
            }
        }
    }

    /**
     * Clear folder of assets<commit_msg>Modified used correct output tags<commit_after><?php

namespace BootstrapUI\Shell\Task;

use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;

/**
 * Task for installing Bootstrap assets and layouts to app.
 */
class TwbsAssetsTask extends Shell
{
    protected $_assetDir;
    protected $_nodeDir;
    protected $_cssDir;
    protected $_jsDir;

    /**
     * TwbsAssetsTask constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->_assetDir = new Folder(Plugin::path('BootstrapUI') . 'webroot');
        $this->_nodeDir = new Folder(Plugin::path('BootstrapUI') . 'node_modules');
        $this->_cssDir = new Folder($this->_assetDir->path . DS . 'css');
        $this->_jsDir = new Folder($this->_assetDir->path . DS . 'js');
    }

    /**
     * Installs Bootstrap assets using npm
     *
     * @return void
     */
    public function installAssets()
    {
        $this->info('Checking npm...');
        if (!`which npm`) {
            $this->abort('NPM (https://www.npmjs.com/) is required, but not installed. Aborting.');
        }

        chdir(Plugin::path('BootstrapUI'));
        $node_mod = new Folder('node_modules');
        if ($node_mod->delete()) {
            $this->success('Cleared node_modules...');
        }

        exec('npm install --verbose', $output, $return);
        $this->out($output);
        if ($return === 0) {
            $this->success('Bootstrap assets installed successfully.');
        } else {
            $this->abort('Bootstrap assets could not be installed.');
        }
    }

    /**
     * Copy assets from node_modules folder to plugin's webroot
     * If in production mode, just copies min. required and minified assets
     *
     * @return void
     */
    public function copyAssets()
    {
        $this->info('Clearing webroot and copying assets...');
        if ($this->_clear($this->_assetDir, '^(?!cover)(?!dashboard)(?!signin).*$')) {
            $this->success('All files cleared...');
        }

        if (Configure::read('debug') === true) {
            $folders[] = new Folder($this->_nodeDir->path . DS . 'bootstrap/dist');
            $folders[] = new Folder($this->_nodeDir->path . DS . 'jquery/dist');
            $folders[] = new Folder($this->_nodeDir->path . DS . 'popper.js/dist');

            foreach ($folders as $folder) {
                foreach ($folder->findRecursive() as $file) {
                    $files[] = new File($file);
                }
            }
        } else {
            $files[] = new File($this->_nodeDir->path . DS . 'bootstrap' . DS . 'dist' . DS . 'css' . DS . 'bootstrap.min.css');
            $files[] = new File($this->_nodeDir->path . DS . 'bootstrap' . DS . 'dist' . DS . 'js' . DS . 'bootstrap.min.js');
            $files[] = new File($this->_nodeDir->path . DS . 'jquery' . DS . 'dist' . DS . 'jquery.min.js');
            $files[] = new File($this->_nodeDir->path . DS . 'popper.js' . DS . 'dist' . DS . 'popper.min.js');
        }
        $this->_copy($files);
    }

    /**
     * Copy sample layouts to app's layout dir
     *
     * @param string $target The destination path
     * @return void
     */
    public function copyLayouts($target = null)
    {
        $this->info(__('Copying sample layouts...'));
        $layoutDir = new Folder(Plugin::path('BootstrapUI') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'examples');

        if ($target == null) {
            $target = APP . 'Template' . DS . 'Layout' . DS . 'TwitterBootstrap';
        }

        if (!$layoutDir->copy($target)) {
            $this->abort('Sample layouts could not be copied.');
        }
        $this->success('Sample layouts copied successfully.');
    }

    /**
     * Copy files to assetdir's css/js path
     *
     * @param array $files Assetfiles to copy
     * @return void
     */
    protected function _copy(array $files)
    {
        foreach ($files as $file) {
            if ($file->ext() == 'css') {
                $dir = $this->_cssDir;
            } elseif ($file->ext() == 'js') {
                $dir = $this->_jsDir;
            }

            if ($file->copy($dir->path . DS . $file->name)) {
                $this->success($file->name . ' successfully copied.');
            } else {
                $this->warning($file->name . ' could not be copied.');
            }
        }
    }

    /**
     * Clear folder of assets
{'fraction_non_alphanumeric': 0.1715448943661972, 'fraction_numerical': 0.00022007042253521127, 'mean_word_length': 1.9644487932159165, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 121, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1765029', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2651, 'n_words': 641}