-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwizard
176 lines (140 loc) · 4.49 KB
/
wizard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
use Illuminate\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Ferreira\AutoCrud\Generators\MigrationSetGenerator;
require __DIR__ . '/vendor/autoload.php';
class SetupEmptyLaravel
{
public function run()
{
exec('composer create-project laravel/laravel --prefer-dist . "8.*"');
$this->initialize();
exec('git init');
exec('git add .');
exec('git config user.email "[email protected]"');
exec('git config user.name "No Name"');
exec('git commit -m "Empty laravel project prepared for autocrud tests"');
}
private function initialize()
{
$this->useSqliteDatabase();
$this->requireAutocrudPackage();
}
private function useSqliteDatabase()
{
$this->changeEnvFile();
$this->changePhpunitXml();
exec('touch database/db');
}
private function changeEnvFile()
{
$contents = file_get_contents('.env');
$contents = preg_replace('/^DB_.*\n*/m', '', $contents);
$contents .= implode("\n", [
'',
'DB_CONNECTION=sqlite',
'DB_DATABASE=database/db',
'DB_FOREIGN_KEYS=true',
]);
file_put_contents('.env', $contents);
}
private function changePhpunitXml()
{
$contents = file_get_contents('phpunit.xml');
$contents = preg_replace(
'/<!-- (<server name="DB_.*".*\/>) -->/',
'$1',
$contents
);
file_put_contents('phpunit.xml', $contents);
}
private function requireAutocrudPackage()
{
$composer = json_decode(file_get_contents('composer.json'), true);
$composer['require-dev']['jdferreira/autocrud'] = '@dev';
$composer['repositories'][] = ['type' => 'path', 'url' => '/var/www'];
file_put_contents('composer.json', json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
exec('composer update');
}
public function revert()
{
error('You cannot revert setting up an empty laravel project');
}
}
class GenerateMigrations
{
public function run()
{
$dir = implode(DIRECTORY_SEPARATOR, [
'database',
'migrations',
]);
(new Filesystem())->cleanDirectory($dir);
(new MigrationSetGenerator($dir))->save();
}
public function revert()
{
exec('git clean -fd');
exec('git reset --hard');
}
}
class AutocrudMaker
{
public function run()
{
// Run the migrations and then the autocrud:make command
exec('php artisan migrate');
passthru('php artisan autocrud:make');
}
public function revert()
{
// Stage the changes to the migration files (removing the default
// laravel migrations and generating the random ones) so that we can
// easily clean the repository but keep these migration files
exec('git add database/migrations');
// Remove all changes to the repository except the migrations
exec('git clean -fd');
exec('git checkout -- .');
// Unstage the migration changes back into the index
exec('git reset');
// Reset the database
exec('php artisan migrate:reset');
}
}
function error($message)
{
echo "\033[31m";
echo $message;
echo "\033[m";
echo PHP_EOL;
die;
}
// The MigrationGenerator inherits from PhpGenerator which uses the `config`
// helper function. As such, we need to establish an application that has
// access to a configuration repository. Let's use an empty repository
// and rely on default configuration values on the code used below.
$app = app();
$app->instance('config', new Repository([]));
// Change directory to the actual laravel applications; this is essential for
// the commands that we will execute below.
chdir('/empty-laravel');
if (! array_key_exists(1, $argv)) {
error('Missing command: expecting one of { setup | migrations | autocrud }');
} elseif ($argv[1] === 'setup') {
$obj = new SetupEmptyLaravel();
} elseif ($argv[1] === 'migrations') {
$obj = new GenerateMigrations();
} elseif ($argv[1] === 'autocrud') {
$obj = new AutocrudMaker();
} else {
error('Cannot understand command ' . $argv[1]);
}
if (! array_key_exists(2, $argv)) {
error('Missing action: expecting one of { run | revert }');
} elseif ($argv[2] === 'run') {
$obj->run();
} elseif ($argv[2] === 'revert') {
$obj->revert();
} else {
error('Cannot understand action ' . $argv[2]);
}