Skip to content

Commit 0ca76a5

Browse files
authored
Merge branch 'develop' into update/composer
2 parents 9f9ed70 + 129db44 commit 0ca76a5

File tree

3 files changed

+159
-42
lines changed

3 files changed

+159
-42
lines changed

php/EE/Completions.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace EE;
44

5+
use EE;
6+
use EE\Dispatcher\CommandFactory;
7+
use EE\Model\Site;
8+
59
class Completions {
610

711
private $words;
812
private $opts = array();
913

10-
public function __construct( $line ) {
14+
public function __construct( $line, $shell ) {
1115
// TODO: properly parse single and double quotes
1216
$this->words = explode( ' ', $line );
1317

@@ -21,7 +25,7 @@ public function __construct( $line ) {
2125
}
2226

2327
$is_alias = false;
24-
$is_help = false;
28+
$is_help = false;
2529
if ( ! empty( $this->words[0] ) && preg_match( '/^@/', $this->words[0] ) ) {
2630
array_shift( $this->words );
2731
// `ee @al` is false, but `ee @all ` is true.
@@ -48,21 +52,24 @@ public function __construct( $line ) {
4852
return;
4953
}
5054
}
51-
5255
if ( $command->can_have_subcommands() ) {
5356
// add completion when command is `ee` and alias isn't set.
54-
if ( 'ee' === $command->get_name() && false === $is_alias && false == $is_help ) {
57+
if ( 'ee' === $command->get_name() && false === $is_alias && false === $is_help ) {
5558
$aliases = \EE::get_configurator()->get_aliases();
5659
foreach ( $aliases as $name => $_ ) {
5760
$this->add( "$name " );
5861
}
5962
}
60-
foreach ( $command->get_subcommands() as $name => $_ ) {
61-
$this->add( "$name " );
63+
foreach ( $command->get_subcommands() as $name => $subcommand ) {
64+
if ( $shell === 'zsh') {
65+
$this->add( $name . ':' . $subcommand->get_shortdesc() );
66+
} else {
67+
$this->add( $name );
68+
}
6269
}
6370
} else {
6471
foreach ( $spec as $arg ) {
65-
if ( in_array( $arg['type'], array( 'flag', 'assoc' ) ) ) {
72+
if ( in_array( $arg['type'], array( 'flag', 'assoc' ), true ) ) {
6673
if ( isset( $assoc_args[ $arg['name'] ] ) ) {
6774
continue;
6875
}
@@ -78,24 +85,7 @@ public function __construct( $line ) {
7885
$this->add( $opt );
7986
}
8087
}
81-
82-
foreach ( $this->get_global_parameters() as $param => $runtime ) {
83-
if ( isset( $assoc_args[ $param ] ) ) {
84-
continue;
85-
}
86-
87-
$opt = "--{$param}";
88-
89-
if ( '' === $runtime || ! is_string( $runtime ) ) {
90-
$opt .= ' ';
91-
} else {
92-
$opt .= '=';
93-
}
94-
95-
$this->add( $opt );
96-
}
9788
}
98-
9989
}
10090

10191
private function get_command( $words ) {
@@ -109,8 +99,10 @@ private function get_command( $words ) {
10999
}
110100
}
111101

102+
$this->maybe_add_site_command( $words );
103+
112104
$r = \EE::get_runner()->find_command_to_run( $positional_args );
113-
if ( ! is_array( $r ) && array_pop( $positional_args ) == $this->cur_word ) {
105+
if ( ! is_array( $r ) && array_pop( $positional_args ) === $this->cur_word ) {
114106
$r = \EE::get_runner()->find_command_to_run( $positional_args );
115107
}
116108

@@ -123,29 +115,44 @@ private function get_command( $words ) {
123115
return array( $command, $args, $assoc_args );
124116
}
125117

126-
private function get_global_parameters() {
127-
$params = array();
128-
foreach ( \EE::get_configurator()->get_spec() as $key => $details ) {
129-
if ( false === $details['runtime'] ) {
130-
continue;
131-
}
118+
/**
119+
* Adds correct site-type to EE runner if autocompletion of site command is required
120+
*/
121+
private function maybe_add_site_command( array $words ) {
122+
if ( count( $words ) > 0 && 'site' === $words[0] ) {
123+
$type = $this->get_site_type( $words, 'html' );
124+
$site_types = \Site_Command::get_site_types();
125+
126+
$command = EE::get_root_command();
127+
$callback = $site_types[ $type ];
128+
$leaf_command = CommandFactory::create( 'site', $callback, $command );
129+
$command->add_subcommand( 'site', $leaf_command );
130+
}
131+
}
132132

133-
if ( isset( $details['deprecated'] ) ) {
134-
continue;
135-
}
133+
/**
134+
* Returns correct site-type for completion. Only for `site create`, type is specified in command.
135+
* For other commands, it is fetched from EE db.comp
136+
*/
137+
private function get_site_type( $words, $default_site_type ) {
138+
$type = $default_site_type;
136139

137-
if ( isset( $details['hidden'] ) ) {
138-
continue;
140+
foreach ( $words as $arg ) {
141+
if ( preg_match( '|^--type=(\S+)|', $arg, $matches ) ) {
142+
$type = $matches[1];
139143
}
140-
$params[ $key ] = $details['runtime'];
144+
}
145+
146+
if ( count( $words ) >= 3 && 'create' === $words[1] && ! preg_match( '|^--|', $words[2] ) ) {
147+
$sitename = str_replace( array( 'https://', 'http://' ), '', $words[2] );
148+
$sitetype = Site::find( $sitename, array( 'site_type' ) );
141149

142-
// Add additional option like `--[no-]color`.
143-
if ( true === $details['runtime'] ) {
144-
$params[ 'no-' . $key ] = '';
150+
if ( $sitetype ) {
151+
$type = $sitetype->site_type;
145152
}
146153
}
147154

148-
return $params;
155+
return $type;
149156
}
150157

151158
private function add( $opt ) {

php/commands/src/CLI_Command.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ public function self_uninstall( $args, $assoc_args ) {
536536
* --point=<point>
537537
* : The index to the current cursor position relative to the beginning of the command.
538538
*
539+
* [--shell=<shell>]
540+
* : Shell for completions output.
541+
* ---
542+
* default: bash
543+
* options:
544+
* - bash
545+
* - zsh
546+
* ---
547+
539548
* ## EXAMPLES
540549
*
541550
* # Generate tab completion strings.
@@ -545,7 +554,7 @@ public function self_uninstall( $args, $assoc_args ) {
545554
*/
546555
public function completions( $_, $assoc_args ) {
547556
$line = substr( $assoc_args['line'], 0, $assoc_args['point'] );
548-
$compl = new \EE\Completions( $line );
557+
$compl = new \EE\Completions( $line, $assoc_args['shell'] );
549558
$compl->render();
550559
}
551560

utils/ee-completion.zsh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#compdef ee
2+
3+
function _ee {
4+
5+
_ee_completion() {
6+
ee_completion=()
7+
ee_completion=$(sudo /usr/local/bin/ee cli completions --shell='zsh' --line="ee $current_command " --point="$current_position")
8+
9+
completion=()
10+
while read line; do
11+
completion+=("${line}")
12+
done < <(echo "$ee_completion")
13+
}
14+
15+
_ee_service_list() {
16+
ee_services=$(sudo docker-compose -f /opt/easyengine/services/docker-compose.yml ps --services | sed 's/global\-//g')
17+
completion=()
18+
while read line; do
19+
completion+=("${line}")
20+
done < <(echo "$ee_services")
21+
}
22+
23+
_ee_site_list() {
24+
ee_sites=$(sqlite3 /opt/easyengine/db/ee.sqlite "select site_url from sites;")
25+
completion=()
26+
while read line; do
27+
completion+=("${line}")
28+
done < <(echo "$ee_sites")
29+
}
30+
31+
if [[ "${words[2]}" == "cli" || "${words[2]}" == "config" || "${words[2]}" == "help" ]]; then
32+
_arguments '1: :-> sub_commands' '2: :->sub_command_param' '*: :->flags'
33+
34+
elif [[ "${words[2]}" == "service" ]]; then
35+
_arguments '1: :-> sub_commands' '2: :->sub_command_param' '3: :->get_service_name' '*: :->flags'
36+
37+
elif [[ "${words[2]}" == "shell" ]]; then
38+
_arguments '1: :-> sub_commands' '2: :->get_site_name' '*: :->flags'
39+
40+
elif [[ `pwd` == /opt/easyengine/sites*/* ]]; then
41+
_arguments '1: :-> sub_commands' '2: :->sub_command_param' '*: :->flags'
42+
43+
elif [[ "${words[2]}" == "site" && "${words[3]}" == "create" ]]; then
44+
_arguments '1: :-> sub_commands' '2: :->sub_command_param' '*: :->flags'
45+
46+
else
47+
_arguments '1: :-> sub_commands' '2: :->sub_command_param' '3: :->get_site_name' '*: :->flags'
48+
fi
49+
50+
current_command=""
51+
current_position=0
52+
53+
case $state in
54+
sub_commands)
55+
56+
current_command=${words[@]:1}
57+
current_position=$((CURRENT + 1))
58+
59+
_ee_completion
60+
_describe 'command' completion
61+
;;
62+
63+
sub_command_param)
64+
65+
chrlen="ee ${words[@]:1}"
66+
current_command=${words[@]:1}
67+
current_position=$((${#chrlen}))
68+
69+
_ee_completion
70+
71+
_describe 'command' completion
72+
;;
73+
74+
get_site_name)
75+
76+
_ee_site_list
77+
_describe 'command' completion
78+
;;
79+
80+
get_service_name)
81+
82+
_ee_service_list
83+
_describe 'command' completion
84+
;;
85+
86+
flags)
87+
88+
chrlen="ee ${words[@]:1}"
89+
current_command=${words[@]:1}
90+
current_position=$((${#chrlen}))
91+
92+
_ee_completion
93+
_describe 'command' completion
94+
95+
;;
96+
97+
*)
98+
echo "Error occured in auto completion"
99+
;;
100+
esac
101+
}

0 commit comments

Comments
 (0)