-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathentity_scaffolder.drush.inc
215 lines (197 loc) · 6.68 KB
/
entity_scaffolder.drush.inc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* @file
* Provide Drush integration for entity schaffolding.
*/
require_once __DIR__ . '/vendor/autoload.php';
spl_autoload_register('drush_entity_scaffolder_autoload');
use Drush\EntityScaffolder\ScaffolderBase;
use Drush\EntityScaffolder\BoilerPlate;
use Drush\EntityScaffolder\TranslationExtractor;
use Drush\EntityScaffolder\d7_1\Scaffolder as Scaffolder_d7_1;
use Drush\EntityScaffolder\Logger;
ini_set('memory_limit', -1);
define('DRUPAL_ROOT', drush_entity_scaffolder_define_drupal_root());
chdir(DRUPAL_ROOT);
define('MAINTENANCE_MODE', 'update');
/**
* Implements hook_drush_help().
*/
function entity_scaffolder_drush_help($section) {
switch ($section) {
case 'drush:entity-scaffold':
return dt('Runs Schaffolder to create entity and various preprocessing.');
}
}
/**
* Implements hook_drush_command().
*/
function entity_scaffolder_drush_command() {
$items = array();
$items['entity-scaffold'] = array(
'description' => 'Helps create entity and fields from config files.',
'callback' => 'drush_entity_scaffolder',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'options' => array(
'config-dir' => 'Directory to read the config file/s from.',
'debug' => 'Runs entity_scaffolder in debug mode.',
'info' => 'Interactive information system about supported plugins.',
'force' => 'Do not ask questions, scaffold it',
),
'examples' => array(
'drush es --config-dir=".tools/es"' => 'Looks for config file/s in .tools/es and runs scaffolding job.',
),
'aliases' => array('es'),
);
$items['entity-scaffold-boilerplate'] = array(
'description' => 'Generate Boilderplate code.',
'callback' => 'drush_entity_scaffolder_boilerplate',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'arguments' => array(
'type' => 'Boilerplate Type',
),
'options' => array(
'debug' => 'Runs entity_scaffolder in debug mode.',
),
'examples' => array(
'drush esb --debug' => 'This will provide an extensive details about boilderplate that would be generated.',
),
'aliases' => array('esb'),
);
$items['entity-scaffold-translation-extractor'] = [
'description' => 'Extract translation.',
'callback' => 'drush_entity_scaffolder_translation_extractor',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'arguments' => [],
'options' => [
'debug' => 'Runs entity_scaffolder in debug mode.',
],
'examples' => [
'drush este --debug' => 'This will provide an extensive details about translation extraction that would be done.',
],
'aliases' => ['este'],
];
return $items;
}
/**
* Run entity scaffolder.
*/
function drush_entity_scaffolder() {
Logger::log("DRUPAL_ROOT is " . DRUPAL_ROOT, 'status');
$scaffolder = drush_entity_scaffolder_get_scaffolder();
if (!$scaffolder) {
if (!($config_dir = drush_get_option('config-dir'))) {
$config_dir = '.tools/es';
}
Logger::log("Make sure `" . $config_dir . "/config.yaml` exists", 'warning');
Logger::log("Scaffolder quits without scaffolding.", 'error');
return;
}
$config = $scaffolder->getConfig();
if (isset($config['locked']) && $config['locked']) {
Logger::log("Configuration is locked.", 'warning');
if ($config['lock message']) {
Logger::log("LOCK MESSAGE : " . $config['lock message'], 'warning');
}
Logger::log("Scaffolder quits without scaffolding.", 'error');
return;
}
// Check if current scaffolder version is outdated.
$log = $scaffolder->getLoggedScaffolderInfo();
if (isset($log['version'])) {
$version_compare = version_compare($scaffolder::VERSION, $log['version']);
if ($version_compare < 0) {
Logger::log("Entity Scaffolder found in your system is outdated. Scaffolding will exit.", 'error');
Logger::log("
You can upgrade the Entity Scaffolder version by going to the folder where
it is installed and pulling the latest version from git. You may also have
to run composer install to install any new dependencies if needed.
Eg.
>> cd ~/.drush/entity_scaffolder/
>> git pull
>> composer install
", 'status');
Logger::log("If you choose to upgrade Entity Scaffolder, the code generated henceforth could be different. Exercise caution while committing scaffolded code.", 'warning');
return;
}
elseif ($version_compare > 0) {
Logger::log("Entity Scaffolder found in your system is of newer version than the one that was used to scaffold earlier.", 'warning');
if (drush_get_option('force', FALSE) !== TRUE) {
$response = drush_prompt("Do you wish to continue? (y/n)", "n", TRUE);
$response = strtolower($response);
switch ($response) {
case 'y':
case 'yes':
break;
default:
Logger::log("Scaffolding aborted.", 'error');
return;
}
}
}
}
// @todo process help here instead of scaffolding.
if (drush_get_option('info')) {
list($type, $name) = func_get_args();
$scaffolder->help($type, $name);
}
else {
$scaffolder->scaffold();
$scaffolder->exportCode();
$scaffolder->logScaffolderInfo();
Logger::log("Scaffolding finished.", 'success');
}
}
/**
* Run entity scaffolder for boilderplate.
*/
function drush_entity_scaffolder_boilerplate() {
Logger::log("Boilerplate generation has moved to phabalicious project.", 'error');
}
/**
* Extract Translation.
*/
function drush_entity_scaffolder_translation_extractor() {
spl_autoload_register('drush_entity_scaffolder_autoload');
$translation_extractor = new TranslationExtractor();
$translation_extractor->extract();
}
/**
* Helper function to load the correct scaffolder based on context.
*/
function drush_entity_scaffolder_get_scaffolder() {
// We dropped support for multiple scaffolder versions start 7.1.2 for D7.
return new Scaffolder_d7_1();
}
/**
* Implements the autoloader.
*/
function drush_entity_scaffolder_autoload($class) {
if (0 !== strpos($class, 'Drush\EntityScaffolder')) {
return;
}
if (is_file($file = dirname(__FILE__) . '/' . str_replace('\\', '/', $class) . '.php')) {
require $file;
}
else {
Logger::log("FILE NOT FOUND: $file", 'error');
}
}
/**
* Find the drupal root directory by looking in parent directories.
*
* If unable to discover it, fail and exit.
*/
function drush_entity_scaffolder_define_drupal_root() {
$parent_count = 0;
// 8 seems reasonably far to go looking up.
while ($parent_count < 8) {
$dir = realpath(getcwd() . str_repeat('/..', $parent_count));
if (file_exists($dir . '/index.php')) {
return $dir;
}
$parent_count++;
}
Logger::log("Failure: Unable to discover DRUPAL_ROOT.", 'error');
exit(1);
}