<commit_before><?php

namespace Yab\Laracogs\Console;

use Artisan;
use Illuminate\Console\AppNamespaceDetectorTrait;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Yab\Laracogs\Utilities\FormMaker;

class TableCrud extends Command
{
    use AppNamespaceDetectorTrait;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'laracogs:table-crud {table} {--api} {--migration} {--bootstrap} {--semantic}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate a basic CRUD from an existing table';

    /**
     * Generate a CRUD stack.
     *
     * @return mixed
     */
    public function handle()
    {
        $filesystem = new Filesystem();
        $table = $this->argument('table');
        $tableDefintion = $this->tableDefintion($table);

        Artisan::call('laracogs:crud', [
            'table'       => $table,
            '--api'       => $this->option('api'),
            '--migration' => $this->option('migration'),
            '--bootstrap' => $this->option('bootstrap'),
            '--semantic'  => $this->option('semantic'),
            '--schema'    => $tableDefintion,
        ]);

        $migrationName = 'create_'.$table.'_table';
        $migrationFiles = $filesystem->allFiles(base_path('database/migrations'));

        foreach ($migrationFiles as $file) {
            if (stristr($file->getBasename(), $migrationName)) {
                $migrationData = file_get_contents($file->getPathname());
                if (stristr($migrationData, 'updated_at')) {
                    $migrationData = str_replace('$table->timestamps();', '', $migrationData);
                }
                file_put_contents($file->getPathname(), $migrationData);
            }
        }

        $this->line("\nYou've generated a CRUD for the table: ".$table);
        $this->line("\n\nYou may wish to add this as your testing database");
        $this->line("'testing' => [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '' ],");
        $this->info("\n\nCRUD for $table is done.");
    }

    /**
     * Table definitions.
     *
     * @param string $table
     *
     * @return string
     */
    private function tableDefintion($table)
    {
        $columnStringArray = [];
        $formMaker = new FormMaker();
        $columns = $formMaker->getTableColumns($table, true);

        foreach ($columns as $key => $column) {
            if ($key === 'id') {
                $column['type'] = 'increments';
            }

            $columnStringArray[] = $key.':'.$column['type'];
        }

        $columnString = implode(',', $columnStringArray);

        return $columnString;
    }
}
<commit_msg>More cleanup and validation<commit_after><?php

namespace Yab\Laracogs\Console;

use Artisan;
use Exception;
use Illuminate\Console\AppNamespaceDetectorTrait;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Yab\Laracogs\Utilities\FormMaker;

class TableCrud extends Command
{
    use AppNamespaceDetectorTrait;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'laracogs:table-crud {table}
        {--api : Creates an API Controller and Routes}
        {--ui= : Select one of bootstrap|semantic for the UI}
        {--serviceOnly : Does not generate a Controller or Routes}
        {--withFacade : Creates a facade that can be bound in your app to access the CRUD service}
    ';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate a basic CRUD from an existing table';

    /**
     * Generate a CRUD stack.
     *
     * @return mixed
     */
    public function handle()
    {
        $filesystem = new Filesystem();
        $table = $this->argument('table');
        $tableDefintion = $this->tableDefintion($table);

        if (empty($tableDefintion)) {
            throw new Exception("There is no table definition for $table. Are you sure you spelled it correctly? Table names are case sensitive.", 1);
        }

        Artisan::call('laracogs:crud', [
            'table'       => $table,
            '--api'       => $this->option('api'),
            '--ui'        => $this->option('ui'),
            '--serviceOnly' => $this->option('serviceOnly'),
            '--withFacade' => $this->option('withFacade'),
            '--migration' => true,
            '--schema'    => $tableDefintion,
        ]);

        $migrationName = 'create_'.$table.'_table';
        $migrationFiles = $filesystem->allFiles(base_path('database/migrations'));

        foreach ($migrationFiles as $file) {
            if (stristr($file->getBasename(), $migrationName)) {
                $migrationData = file_get_contents($file->getPathname());
                if (stristr($migrationData, 'updated_at')) {
                    $migrationData = str_replace('$table->timestamps();', '', $migrationData);
                }
                file_put_contents($file->getPathname(), $migrationData);
            }
        }

        $this->line("\nYou've generated a CRUD for the table: ".$table);
        $this->line("\n\nYou may wish to add this as your testing database");
        $this->line("'testing' => [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '' ],");
        $this->info("\n\nCRUD for $table is done.");
    }

    /**
     * Table definitions.
     *
     * @param string $table
     *
     * @return string
     */
    private function tableDefintion($table)
    {
        $columnStringArray = [];
        $formMaker = new FormMaker();
        $columns = $formMaker->getTableColumns($table, true);

        foreach ($columns as $key => $column) {
            if ($key === 'id') {
                $column['type'] = 'increments';
            }

            $columnStringArray[] = $key.':'.$this->columnNameCheck($column['type']);
        }

        $columnString = implode(',', $columnStringArray);

        return $columnString;
    }

    /**
     * Corrects a column type for Schema building.
     *
     * @param  string $column
     * @return string
     */
    private function columnNameCheck($column)
    {
        $columnsToAdjust = [
            'datetime' => 'dateTime',
            'smallint' => 'smallInteger',
            'bigint' => 'bigInteger',
            'datetimetz' => 'timestamp',
        ];

        if (isset($columnsToAdjust[$column])) {
            return $columnsToAdjust[$column];
        }

        return $column;
    }
}

{'fraction_non_alphanumeric': 0.1528178641956555, 'fraction_numerical': 0.00015190642564180464, 'mean_word_length': 2.1412213740458017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '891575', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1896, 'n_words': 420}