forked from wp-cli/wp-cli.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhakefile.php
More file actions
150 lines (114 loc) · 3.92 KB
/
Phakefile.php
File metadata and controls
150 lines (114 loc) · 3.92 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
<?php
require __DIR__ . '/vendor/autoload.php';
function invoke_wp_cli( $cmd, $app ) {
ob_start();
system( "WP_CLI_CONFIG_PATH=/dev/null $cmd", $return_code );
$json = ob_get_clean();
if ( $return_code ) {
echo "WP-CLI returned error code: $return_code\n";
exit(1);
}
return json_decode( $json, true );
}
function render( $path, $binding ) {
$m = new Mustache_Engine;
$template = file_get_contents( __DIR__ . "/_templates/$path" );
return $m->render( $template, $binding );
}
desc( 'Generate a list of commands with all accepted arguments on STDOUT.' );
task( 'syn-list', function( $app ) {
function generate_synopsis( $command, $path = '' ) {
$full_path = $path . ' ' . $command['name'];
if ( !isset( $command['subcommands'] ) ) {
echo $full_path . ' ' . $command['synopsis'] . "\n";
} else {
foreach ( $command['subcommands'] as $subcommand ) {
generate_synopsis( $subcommand, $full_path );
}
}
}
generate_synopsis( invoke_wp_cli( 'wp cli cmd-dump', $app ) );
});
function gen_cmd_pages( $cmd, $parent = array() ) {
$parent[] = $cmd['name'];
$binding = $cmd;
$binding['synopsis'] = implode( ' ', $parent );
$binding['path'] = implode( '/', $parent );
$binding['has-subcommands'] = isset( $cmd['subcommands'] ) ? array(true) : false;
if ( $cmd['longdesc'] ) {
$docs = $cmd['longdesc'];
$docs = htmlspecialchars( $docs, ENT_COMPAT, 'UTF-8' );
// decrease header level
$docs = preg_replace( '/^## /m', '### ', $docs );
// escape `--` so that it doesn't get converted into `—`
$docs = preg_replace( '/^(\[?)--/m', '\1\--', $docs );
// hack to prevent double encoding in code blocks
$docs = preg_replace( '/ < /', ' < ', $docs );
$docs = preg_replace( '/ > /', ' > ', $docs );
$docs = preg_replace( '/ <</', ' <<', $docs );
$docs = preg_replace( '/"/', '"', $docs );
$binding['docs'] = $docs;
}
$path = __DIR__ . "/commands/" . $binding['path'];
if ( !is_dir( $path ) ) {
mkdir( $path );
}
file_put_contents( "$path/index.md", render( 'subcmd-list.mustache', $binding ) );
if ( !isset( $cmd['subcommands'] ) )
return;
foreach ( $cmd['subcommands'] as $subcmd ) {
gen_cmd_pages( $subcmd, $parent );
}
}
desc( 'Update the /commands/ page.' );
task( 'cmd-list', function( $app ) {
$wp = invoke_wp_cli( 'wp cli cmd-dump', $app );
// generate main page
file_put_contents( '_includes/cmd-list.html', render( 'cmd-list.mustache', $wp ) );
foreach ( $wp['subcommands'] as $cmd ) {
gen_cmd_pages( $cmd );
}
});
desc( 'Update the /config/ page.' );
task( 'param-list', function( $app ) {
$config_spec = invoke_wp_cli( 'wp cli param-dump', $app );
$out = '';
foreach ( $config_spec as $key => $details ) {
if ( isset( $details['hidden'] ) || isset( $details['deprecated'] ) )
continue;
if ( false !== $details['file'] ) {
$config = "$key: " . $details['file'];
} else {
$config = '';
}
if ( false !== $details['runtime'] ) {
$flag = ( true === $details['runtime'] )
? "--[no-]$key"
: "--$key" . $details['runtime'];
} else {
$flag = '';
}
$default = json_encode( $details['default'] );
$description = ( isset( $details['desc'] ) ) ? $details['desc'] : '';
$out .= render( 'config.mustache', compact( 'config', 'flag', 'default', 'description' ) );
}
file_put_contents( '_includes/param-list.html', $out );
});
desc( 'Generate pages for classes based on PHPdoc.' );
task( 'class-list', function( $app ) {
$wp = invoke_wp_cli( 'wp cli info --format=json', $app );
$class_dir_path = $wp['wp_cli_dir_path'] . '/php';
if ( ! is_dir( $class_dir_path ) ) {
echo "WP-CLI must be available as a non-Phar install.\n";
die(1);
}
ob_start();
@system( "./phpDocumentor.phar -d $class_dir_path -t 'phpdoc' --template='xml'" );
ob_get_clean();
if ( ! is_dir( './phpdoc' ) ) {
echo "Something broke generating the PHPdoc XML file.\n";
die(1);
}
});
desc( 'Build the site.' );
task( 'default', 'cmd-list', 'param-list', 'class-list' );