forked from factorial-io/entity_scaffolder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_scaffolder.drush.inc
More file actions
180 lines (165 loc) · 5.29 KB
/
entity_scaffolder.drush.inc
File metadata and controls
180 lines (165 loc) · 5.29 KB
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
<?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\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.',
),
'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'),
);
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 syste is outdated. Scaffolding will exit.", 'error');
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');
$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() {
spl_autoload_register('drush_entity_scaffolder_autoload');
list($type) = func_get_args();
$boilerPlate = new BoilerPlate();
$boilerPlate->generate($type);
}
/**
* 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);
}