-
Notifications
You must be signed in to change notification settings - Fork 1
/
textmate.drush.inc
392 lines (343 loc) · 10.8 KB
/
textmate.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* @file
* TextMate Template Generator for Drush
*/
/**
* Implementation of hook_drush_command().
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing each command.
*/
function textmate_drush_command() {
$items = array();
$items['tm-generate'] = array(
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'description' => "Generate TextMate Snippets.",
'options' => array(
'write' => 'File directory',
'type' => 'hook or theme',
),
'examples' => array(
'drush tm-generate --write=/tmp/d7 --type=hook',
'drush tm-generate --write=/tmp/d6 --type=theme',
),
);
$items['tm-format'] = array(
'description' => "Generate TextMate completion.",
'options' => array(
'plist' => 'plist completion',
'txt' => 'txt hook list',
'textmate' => 'Path to textmate bundle',
'filter' => 'all, new, existing. all is default',
),
'examples' => array(
'drush tm-format --format=plist --textmate=\'/Users/florian/Library/Application Support/TextMate/Bundles/php_drupal\'',
'drush tm-format --format=txt --filter=new --textmate=\'/Users/florian/Library/Application Support/TextMate/Bundles/php_drupal\''
)
);
return $items;
}
/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help <name-of-your-command>'
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function textmate_drush_help($section) {
switch ($section) {
case 'drush:textmate':
return dt("Generate TextMate Snippets from current drupal installation.");
}
}
/**
* Drush TextMate-generate command callback.
*/
function drush_textmate_tm_generate() {
$version = drush_drupal_major_version();
$type = drush_get_option('type');
if (is_string(drush_get_option('write')) && $dir = realpath(drush_get_option('write'))) {
if (is_writable($dir)) {
$write_files = TRUE;
}
}
$functions = drush_textmate_find_functions($type, $version);
foreach($functions as $function_name) {
if (strpos($function_name, '::') === FALSE) {
if (!function_exists($function_name)) {
return drush_set_error(dt('Function not found'));
}
$reflect = new ReflectionFunction($function_name);
}
else {
list($class, $method) = explode('::', $function_name);
if (!method_exists($class, $method)) {
return drush_set_error(dt('Method not found'));
}
$reflect = new ReflectionMethod($class, $method);
}
$function = _textmate_drush_print_function($type, $function_name, $reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
if (isset($write_files)) {
file_put_contents($dir . '/' . $function['name'] . '.' . $version . '.php', $function['code']);
} else {
drush_print_r($function);
}
}
}
/**
* Drush TextMate-format command callback.
*/
function drush_textmate_tm_format() {
$version = drush_drupal_major_version();
$textmate = drush_get_option('textmate');
$type = drush_get_option('type');
if (is_string(drush_get_option('write')) && $dir = realpath(drush_get_option('write'))) {
if (is_writable($dir)) {
$write_files = TRUE;
}
}
$existing_commands = array();
if (!empty($textmate)) {
$scan = array();
foreach(drush_scan_directory($textmate . '/Commands', '/\.tmCommand/', array('.', '..', 'CVS')) as $file) {
list($file_type) = explode('_', $file->basename);
if ($file_type == $type) {
$existing_commands[$file->name] = $file->name;
}
}
}
$functions = drush_textmate_find_functions($type, $version);
$output = array();
if (!empty($textmate)) {
$scan += drush_scan_directory($textmate . '/Support/commands/custom/hooks', '/\.php/', array('.', '..', 'CVS'));
$scan += drush_scan_directory($textmate . '/Support/commands/generated/hooks', '/\.php/', array('.', '..', 'CVS'));
$scan += drush_scan_directory($textmate . '/Support/commands/custom/theme', '/\.php/', array('.', '..', 'CVS'));
$scan += drush_scan_directory($textmate . '/Support/commands/generated/theme', '/\.php/', array('.', '..', 'CVS'));
}
$function = array();
foreach($scan as $file) {
list($file_function, $file_version) = explode('.', $file->basename);
list($file_type) = explode('_', $file->basename);
if ($file_type == $type) {
$function[$file_function] = $file_function;
}
}
// Delete some lowercase function.
// Example: delete hook_form_form_id_alter and preserve hook_form_FORM_ID_alter.
foreach($function as $name) {
$lower = strtolower($name);
if ($name != $lower && in_array($lower, $function)) {
$delete = array_search($lower, $function);
unset($function[$delete]);
}
}
switch (drush_get_option('filter')) {
case 'new':
$function = array_diff($function, $existing_commands);
break;
case 'existing':
$function = array_intersect($function, $existing_commands);
break;
}
if (drush_get_option('format') == 'plist') {
foreach($function as $name) {
$output[] = '{display = ' . "'" . $name . "'" . '; }';
}
$output = "(\n" . implode(",\n", $output) . "\n)";
} elseif (drush_get_option('format') == 'txt') {
$output = implode("\n", $function);
}
drush_print_r($output);
}
/**
* undocumented function
*
* @param string $type
* Function type. hook or theme
* @param int $version
* Drupal major version.
*/
function drush_textmate_find_functions($type, $version) {
$scan = array();
switch ($type) {
case 'hook':
switch ($version) {
case 7:
$scan = $scan + drush_scan_directory('./modules', '/\.api\.php$/', _textmate_drush_filter_files());
break;
case 6:
$scan = $scan + drush_scan_directory('./docs', '/\.php$/', _textmate_drush_filter_files());
break;
// case 5:
// $scan = $scan + drush_scan_directory('./d5-hooks', '/\.php$/', _textmate_drush_filter_files());
// break;
default:
print 'Invalid drupal version.';
exit;
break;
}
// $scan = $scan + drush_scan_directory('./modules', '/\.php$/', array('.', '..', 'CVS', '.tpl.php'));
$scan = $scan + drush_scan_directory('./modules', '/\.inc$/', _textmate_drush_filter_files());
$scan = $scan + drush_scan_directory('./modules', '/\.module$/', _textmate_drush_filter_files());
foreach($scan as $file) {
if (substr($file->basename, -7) != 'tpl.php') {
include_once $file->filename;
}
}
break;
case 'theme':
init_theme();
$theme_registry = theme_get_registry();
break;
default:
print 'Invalid type.';
exit;
}
$functions = get_defined_functions();
$functions = array_values(array_filter($functions['user'], '_textmate_drush_' . $type . '_filter'));
sort($functions);
return $functions;
}
/**
* Print the specified function
*/
function _textmate_drush_print_function($type, $function_name, $file, $start_line, $end_line) {
$line_num = 0;
$doxygen = NULL;
$fp = fopen($file, 'r');
$function = '';
while (!feof($fp) && ($line_num < ($start_line - 1))) {
$line = fgets($fp);
++$line_num;
if (substr($line,0,3) == '/**') {
$doxygen = $line;
}
elseif (isset($doxygen)) {
$doxygen .= $line;
if ($line_num + 1 == $start_line) {
// drush_print(rtrim($doxygen));
}
if (strstr($line, '*/') !== FALSE) {
$doxygen = NULL;
}
}
}
$line_function_start = $line_num;
$start_escape = $line_num + 1;
$end_escape = $end_line - 1;
$escape = FALSE;
while (!feof($fp) && ($line_num < $end_line)) {
$line = fgets($fp);
$done = FALSE;
if ($line_function_start == $line_num) {
$function_name_head = _textmate_drush_function_name($line);
$line = _textmate_drush_escape_line($line);
$line = str_replace('function ' . $type . '_', 'function <?php print $basename; ?>_', $line);
$done = TRUE;
} else {
if ($line_num == $start_escape && $start_escape == $end_escape) {
$line = ' $1' . "\n" . '}';
$done = TRUE;
} elseif ($line_num == $start_escape && $start_escape + 1 == $end_escape) {
$line = ' ${1:' . trim(_textmate_drush_escape_line($line)) . '}';
$done = TRUE;
} elseif ($line_num == $start_escape && $start_escape + 1 < $end_escape) {
$line = ' ${1:' . trim(_textmate_drush_escape_line($line));
$done = TRUE;
}
}
if ($done == FALSE) {
if ($line_num == $end_escape - 1) {
$line = _textmate_drush_escape_line(rtrim($line)) . '}';
} elseif ($line_num != $end_line - 1) {
$line = _textmate_drush_escape_line($line);
}
}
$body .= rtrim($line) . "\n";
$_line++;
$line_num++;
}
$head = '/**' . "\n";
$head .= ' * Implements ' . $function_name_head . '().' . "\n";
$head .= ' */' . "\n";
$function = $head . $body . "\n" . '$2';
return array('name' => $function_name_head, 'code' => rtrim($function));
}
/**
* Print the specified function
*/
function _textmate_drush_print_function_name($function_name, $file, $start_line, $end_line) {
$line_num = 0;
$doxygen = NULL;
$fp = fopen($file, 'r');
$function = '';
while (!feof($fp) && ($line_num < ($start_line - 1))) {
$line = fgets($fp);
++$line_num;
if (substr($line,0,3) == '/**') {
$doxygen = $line;
}
elseif (isset($doxygen)) {
$doxygen .= $line;
if ($line_num + 1 == $start_line) {
// drush_print(rtrim($doxygen));
}
if (strstr($line, '*/') !== FALSE) {
$doxygen = NULL;
}
}
}
$line_function_start = $line_num;
$start_escape = $line_num + 1;
$end_escape = $end_line - 1;
$escape = FALSE;
while (!feof($fp) && ($line_num < $end_line)) {
$line = fgets($fp);
$done = FALSE;
if ($line_function_start == $line_num) {
return _textmate_drush_function_name($line);
}
}
}
function _textmate_drush_escape_line($line) {
$line = str_replace('$', '\$', $line);
$line = str_replace('}', '\}', $line);
return $line;
}
/**
* Filter hook functions
**/
function _textmate_drush_hook_filter($function) {
return substr($function, 0, 5) == 'hook_';
}
/**
* Filter theme functions
**/
function _textmate_drush_theme_filter($function) {
static $theme;
if (empty($theme)) {
foreach (theme_get_registry() as $theme_function => $info) {
$theme[] = 'theme_' . $theme_function;
}
}
return in_array($function, $theme);
}
/**
* Get function name from line
**/
function _textmate_drush_function_name($line) {
$line = explode(' ', $line);
$function = explode('(', $line[1]);
return $function[0];
}
function _textmate_drush_filter_files() {
return array('.', '..', 'CVS', '.svn', '.git');
}