<commit_before><?php

/**
 * 模块操作命令行
 * usage: app command [args...]
 *    app new app_path [Name]
 *
 * @package default
 * @author <NAME>
**/

namespace Gini\Controller\CLI;

if (!function_exists('mb_str_pad')) {

    function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
    {
        $diff = mb_strwidth( $input ) - mb_strlen( $input );

        return str_pad( $input, $pad_length + $diff, $pad_string, $pad_type );
    }
}

class App extends \Gini\Controller\CLI
{

    private function _init_phpunit()
    {
        $gini = \Gini\Core::moduleInfo('gini');

        echo "Generating PHPUnit files...";

        $xml = APP_PATH.'/phpunit.xml';
        if (!file_exists($xml)) {
            copy($gini->path . '/raw/templates/phpunit/phpunit.xml', $xml);
        }

        $dir = APP_PATH . '/tests';
        if (!file_exists($dir)) {
            mkdir($dir);
        }

        $base = APP_PATH . '/tests/gini.php';
        if (!file_exists($base)) {
            copy($gini->path . '/raw/templates/phpunit/gini.php', $base);
        }

        echo "\e[1mDONE.\e[0m\n";
    }

    /**
     * 初始化模块
     *
     * @return void
     **/
    public function actionInit($args)
    {
        if (count($args) > 0) {
            if (in_array('phpunit', $args)) {
                return $this->_init_phpunit();
            }

            return;
        }

        $path = $_SERVER['PWD'];

        $prompt = array(
            'id' => 'Id',
            'name' => 'Name',
            'description' => 'Description',
            'version' => 'Version',
            'dependencies' => 'Dependencies',
            );

        $default = array(
            'name' => ucwords(basename($path)),
            'id' => strtolower(basename($path)),
            'path' => $path,
            'description' => 'App description...',
            'version' => '0.1.0',
            'dependencies' => '[]',
            );

        foreach ($prompt as $k => $v) {
            $data[$k] = readline($v . " [\e[31m" . ($default[$k]?:'N/A') . "\e[0m]: ");
            if (!$data[$k]) $data[$k] = $default[$k];
        }

        $data['dependencies'] = (array) @json_decode($data['dependencies']);

        $gini_json = J($data, JSON_PRETTY_PRINT);
        file_put_contents($path . '/gini.json', $gini_json);
    }

    public function actionHelp($args)
    {
        echo "gini init\n";
        echo "gini modules\n";
        echo "gini cache [class|view|config]\n";
        echo "gini update [modules|orm|web]\n";
        echo "gini server <host:port>\n";
        echo "gini install\n";
        echo "gini build\n";
    }

    public function __index($args)
    {
        $this->actionHelp($args);
    }

    public function actionInfo($args)
    {
        $path = $args[0] ?: APP_ID;

        $info = \Gini\Core::moduleInfo($path);
        if ($info) {
            foreach ($info as $k => $v) {
                if (is_array($v)) $v = J($v);
                printf("%s = %s\n", $k, $v);
            }
        }

    }

    public function actionModules($args)
    {
        foreach (\Gini\Core::$MODULE_INFO as $name => $info) {
            printf("%s %s %s %s %s\e[0m\n",
                $info->error ? "\e[31m":'',
                mb_str_pad($name, 20, ' '),
                mb_str_pad($info->version, 10, ' '),
                mb_str_pad($info->name, 30, ' '),
                $info->error ? "($info->error)":''
            );
        }
    }

    public function actionDoctor()
    {
        $errors = $this->_diagnose();
        if (!count($errors)) {
            echo "\e[32mWell Done! There is no error.\e[0m\n";<commit_msg>gini doctor<commit_after><?php

/**
 * 模块操作命令行
 * usage: app command [args...]
 *    app new app_path [Name]
 *
 * @package default
 * @author <NAME>
**/

namespace Gini\Controller\CLI;

if (!function_exists('mb_str_pad')) {

    function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
    {
        $diff = mb_strwidth( $input ) - mb_strlen( $input );

        return str_pad( $input, $pad_length + $diff, $pad_string, $pad_type );
    }
}

class App extends \Gini\Controller\CLI
{

    private function _init_phpunit()
    {
        $gini = \Gini\Core::moduleInfo('gini');

        echo "Generating PHPUnit files...";

        $xml = APP_PATH.'/phpunit.xml';
        if (!file_exists($xml)) {
            copy($gini->path . '/raw/templates/phpunit/phpunit.xml', $xml);
        }

        $dir = APP_PATH . '/tests';
        if (!file_exists($dir)) {
            mkdir($dir);
        }

        $base = APP_PATH . '/tests/gini.php';
        if (!file_exists($base)) {
            copy($gini->path . '/raw/templates/phpunit/gini.php', $base);
        }

        echo "\e[1mDONE.\e[0m\n";
    }

    /**
     * 初始化模块
     *
     * @return void
     **/
    public function actionInit($args)
    {
        if (count($args) > 0) {
            if (in_array('phpunit', $args)) {
                return $this->_init_phpunit();
            }

            return;
        }

        $path = $_SERVER['PWD'];

        $prompt = array(
            'id' => 'Id',
            'name' => 'Name',
            'description' => 'Description',
            'version' => 'Version',
            'dependencies' => 'Dependencies',
            );

        $default = array(
            'name' => ucwords(basename($path)),
            'id' => strtolower(basename($path)),
            'path' => $path,
            'description' => 'App description...',
            'version' => '0.1.0',
            'dependencies' => '[]',
            );

        foreach ($prompt as $k => $v) {
            $data[$k] = readline($v . " [\e[31m" . ($default[$k]?:'N/A') . "\e[0m]: ");
            if (!$data[$k]) $data[$k] = $default[$k];
        }

        $data['dependencies'] = (array) @json_decode($data['dependencies']);

        $gini_json = J($data, JSON_PRETTY_PRINT);
        file_put_contents($path . '/gini.json', $gini_json);
    }

    public function actionHelp($args)
    {
        echo "gini init\n";
        echo "gini modules\n";
        echo "gini cache [class|view|config]\n";
        echo "gini update [modules|orm|web]\n";
        echo "gini server <host:port>\n";
        echo "gini install\n";
        echo "gini build\n";
    }

    public function __index($args)
    {
        $this->actionHelp($args);
    }

    public function actionInfo($args)
    {
        $path = $args[0] ?: APP_ID;

        $info = \Gini\Core::moduleInfo($path);
        if ($info) {
            foreach ($info as $k => $v) {
                if (is_array($v)) $v = J($v);
                printf("%s = %s\n", $k, $v);
            }
        }

    }

    public function actionModules($args)
    {
        foreach (\Gini\Core::$MODULE_INFO as $name => $info) {
            printf("%s %s %s %s %s\e[0m\n",
                $info->error ? "\e[31m":'',
                mb_str_pad($name, 20, ' '),
                mb_str_pad($info->version, 10, ' '),
                mb_str_pad($info->name, 30, ' '),
                $info->error ? "($info->error)":''
            );
        }
    }

    public function actionDoctor()
    {
        $errors = $this->_diagnose();
        if (!count($errors)) {
            echo "\e[32mDoctor: Well Done! There is no error.\e[0m\n";
{'fraction_non_alphanumeric': 0.18377710678012626, 'fraction_numerical': 0.0060389788635739775, 'mean_word_length': 1.715989563920984, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3497105', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2426, 'n_words': 459}