Skip to content

Commit 40d55ee

Browse files
committed
Refactor run and runLocally
1 parent 21abfca commit 40d55ee

File tree

19 files changed

+279
-219
lines changed

19 files changed

+279
-219
lines changed

docs/UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Upgrade from 7.x to 8.x
44

5+
- `run()` and `runLocally()` doesn't accept `options` parameter anymore. Use named arguments instead.
6+
- `no_throw` is now `nothrow`.
7+
- `real_time_output` is now `forceOutput`.
8+
- `idle_timeout` is now `idleTimeout`.
59

610
## Upgrade from 6.x to 7.x
711

docs/api.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## host()
88

99
```php
10-
host(string ...$hostname)
10+
host(string ...$hostname): Host
1111
```
1212

1313
Defines a host or hosts.
@@ -24,13 +24,18 @@ task('test', function () {
2424
```
2525

2626

27-
2827
## localhost()
2928

3029
```php
31-
localhost(string ...$hostnames)
30+
localhost(string ...$hostnames): Localhost
3231
```
3332

33+
Define a local host.
34+
Deployer will not connect to this host, but will execute commands locally instead.
35+
36+
```php
37+
localhost('ci'); // Alias and hostname will be "ci".
38+
```
3439

3540

3641
## currentHost()
@@ -85,7 +90,6 @@ import(__DIR__ . '/config/hosts.yaml');
8590
```
8691

8792

88-
8993
## desc()
9094

9195
```php
@@ -98,7 +102,7 @@ Set task description.
98102
## task()
99103

100104
```php
101-
task(string $name, $body = null): Task
105+
task(string $name, ?callable $body = null): Task
102106
```
103107

104108
Define a new task and save to tasks list.
@@ -110,12 +114,12 @@ Alternatively get a defined task.
110114
| Argument | Type | Comment |
111115
|---|---|---|
112116
| `$name` | `string` | Name of current task. |
113-
| `$body` | `callable():void` or `array` or `null` | Callable task, array of other tasks names or nothing to get a defined tasks |
117+
| `$body` | `?callable` | Callable task, array of other tasks names or nothing to get a defined tasks |
114118

115119
## before()
116120

117121
```php
118-
before(string $task, $do)
122+
before(string $task, string|callable $do): ?Task
119123
```
120124

121125
Call that task before specified task runs.
@@ -126,12 +130,12 @@ Call that task before specified task runs.
126130
| Argument | Type | Comment |
127131
|---|---|---|
128132
| `$task` | `string` | The task before $that should be run. |
129-
| `$do` | `string` or `callable():void` | The task to be run. |
133+
| `$do` | `string` or `callable` | The task to be run. |
130134

131135
## after()
132136

133137
```php
134-
after(string $task, $do)
138+
after(string $task, string|callable $do): ?Task
135139
```
136140

137141
Call that task after specified task runs.
@@ -142,12 +146,12 @@ Call that task after specified task runs.
142146
| Argument | Type | Comment |
143147
|---|---|---|
144148
| `$task` | `string` | The task after $that should be run. |
145-
| `$do` | `string` or `callable():void` | The task to be run. |
149+
| `$do` | `string` or `callable` | The task to be run. |
146150

147151
## fail()
148152

149153
```php
150-
fail(string $task, $do)
154+
fail(string $task, string|callable $do): ?Task
151155
```
152156

153157
Setup which task run on failure of $task.
@@ -159,7 +163,7 @@ When called multiple times for a task, previous fail() definitions will be overr
159163
| Argument | Type | Comment |
160164
|---|---|---|
161165
| `$task` | `string` | The task which need to fail so $that should be run. |
162-
| `$do` | `string` or `callable():void` | The task to be run. |
166+
| `$do` | `string` or `callable` | The task to be run. |
163167

164168
## option()
165169

@@ -187,6 +191,11 @@ cd(string $path): void
187191

188192
Change the current working directory.
189193

194+
```php
195+
cd('~/myapp');
196+
run('ls'); // Will run `ls` in ~/myapp.
197+
```
198+
190199

191200
## become()
192201

@@ -210,7 +219,7 @@ $restore(); // revert back to the previous user
210219
## within()
211220

212221
```php
213-
within(string $path, callable $callback)
222+
within(string $path, callable $callback): mixed
214223
```
215224

216225
Execute a callback within a specific directory and revert back to the initial working directory.
@@ -220,7 +229,15 @@ Execute a callback within a specific directory and revert back to the initial wo
220229
## run()
221230

222231
```php
223-
run(string $command, ?array $options = [], ?int $timeout = null, ?int $idle_timeout = null, ?string $secret = null, ?array $env = null, ?bool $real_time_output = false, ?bool $no_throw = false): string
232+
run(
233+
string $command,
234+
?int $timeout = null,
235+
?int $idle_timeout = null,
236+
?string $secret = null,
237+
?array $env = null,
238+
?bool $real_time_output = false,
239+
?bool $no_throw = false,
240+
): string
224241
```
225242

226243
Executes given command on remote host.
@@ -245,7 +262,6 @@ run("echo $path");
245262
| Argument | Type | Comment |
246263
|---|---|---|
247264
| `$command` | `string` | Command to run on remote host. |
248-
| `$options` | `array` or `null` | Array of options will override passed named arguments. |
249265
| `$timeout` | `int` or `null` | Sets the process timeout (max. runtime). The timeout in seconds (default: 300 sec; see {{default_timeout}}, `null` to disable). |
250266
| `$idle_timeout` | `int` or `null` | Sets the process idle timeout (max. time since last output) in seconds. |
251267
| `$secret` | `string` or `null` | Placeholder `%secret%` can be used in command. Placeholder will be replaced with this value and will not appear in any logs. |

recipe/deploy/update_code.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
start:
8989
// Clone the repository to a bare repo.
9090
run("[ -d $bare ] || mkdir -p $bare");
91-
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", ['env' => $env]);
91+
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", env: $env);
9292

9393
cd($bare);
9494

@@ -99,7 +99,7 @@
9999
goto start;
100100
}
101101

102-
run("$git remote update 2>&1", ['env' => $env]);
102+
run("$git remote update 2>&1", env: $env);
103103

104104

105105
// Copy to release_path.
@@ -108,7 +108,7 @@
108108
} elseif (get('update_code_strategy') === 'clone') {
109109
cd('{{release_path}}');
110110
run("$git clone -l $bare .");
111-
run("$git remote set-url origin $repository", ['env' => $env]);
111+
run("$git remote set-url origin $repository", env: $env);
112112
run("$git checkout --force $target");
113113
} else {
114114
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));

recipe/provision.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,22 @@
124124
set('remote_user', get('provision_user'));
125125

126126
// PHP
127-
run('apt-add-repository ppa:ondrej/php -y', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive']]);
127+
run('apt-add-repository ppa:ondrej/php -y', env: ['DEBIAN_FRONTEND' => 'noninteractive']);
128128

129129
// Caddy
130130
run("curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg");
131131
run("curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list");
132132

133133
// Update
134-
run('apt-get update', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive']]);
134+
run('apt-get update', env: ['DEBIAN_FRONTEND' => 'noninteractive']);
135135
})
136136
->oncePerNode()
137137
->verbose();
138138

139139
desc('Upgrades all packages');
140140
task('provision:upgrade', function () {
141141
set('remote_user', get('provision_user'));
142-
run('apt-get upgrade -y', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive'], 'timeout' => 900]);
142+
run('apt-get upgrade -y', env: ['DEBIAN_FRONTEND' => 'noninteractive'], timeout: 900);
143143
})
144144
->oncePerNode()
145145
->verbose();
@@ -174,7 +174,7 @@
174174
'uuid-runtime',
175175
'whois',
176176
];
177-
run('apt-get install -y ' . implode(' ', $packages), ['env' => ['DEBIAN_FRONTEND' => 'noninteractive'], 'timeout' => 900]);
177+
run('apt-get install -y ' . implode(' ', $packages), env: ['DEBIAN_FRONTEND' => 'noninteractive'], timeout: 900);
178178
})
179179
->verbose()
180180
->oncePerNode();

recipe/provision/databases.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838

3939
desc('Provision MySQL');
4040
task('provision:mysql', function () {
41-
run('apt-get install -y mysql-server', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive'], 'timeout' => 900]);
42-
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'0.0.0.0' IDENTIFIED BY '%secret%';\"", ['secret' => get('db_password')]);
43-
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'%' IDENTIFIED BY '%secret%';\"", ['secret' => get('db_password')]);
41+
run('apt-get install -y mysql-server', env: ['DEBIAN_FRONTEND' => 'noninteractive'], timeout: 900);
42+
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'0.0.0.0' IDENTIFIED BY '%secret%';\"", secret: get('db_password'));
43+
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'%' IDENTIFIED BY '%secret%';\"", secret: get('db_password'));
4444
run("mysql --user=\"root\" -e \"GRANT ALL PRIVILEGES ON *.* TO '{{db_user}}'@'0.0.0.0' WITH GRANT OPTION;\"");
4545
run("mysql --user=\"root\" -e \"GRANT ALL PRIVILEGES ON *.* TO '{{db_user}}'@'%' WITH GRANT OPTION;\"");
4646
run("mysql --user=\"root\" -e \"FLUSH PRIVILEGES;\"");
@@ -49,9 +49,9 @@
4949

5050
desc('Provision MariaDB');
5151
task('provision:mariadb', function () {
52-
run('apt-get install -y mariadb-server', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive'], 'timeout' => 900]);
53-
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'0.0.0.0' IDENTIFIED BY '%secret%';\"", ['secret' => get('db_password')]);
54-
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'%' IDENTIFIED BY '%secret%';\"", ['secret' => get('db_password')]);
52+
run('apt-get install -y mariadb-server', env: ['DEBIAN_FRONTEND' => 'noninteractive'], timeout: 900);
53+
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'0.0.0.0' IDENTIFIED BY '%secret%';\"", secret: get('db_password'));
54+
run("mysql --user=\"root\" -e \"CREATE USER IF NOT EXISTS '{{db_user}}'@'%' IDENTIFIED BY '%secret%';\"", secret: get('db_password'));
5555
run("mysql --user=\"root\" -e \"GRANT ALL PRIVILEGES ON *.* TO '{{db_user}}'@'0.0.0.0' WITH GRANT OPTION;\"");
5656
run("mysql --user=\"root\" -e \"GRANT ALL PRIVILEGES ON *.* TO '{{db_user}}'@'%' WITH GRANT OPTION;\"");
5757
run("mysql --user=\"root\" -e \"FLUSH PRIVILEGES;\"");
@@ -60,8 +60,8 @@
6060

6161
desc('Provision PostgreSQL');
6262
task('provision:postgresql', function () {
63-
run('apt-get install -y postgresql postgresql-contrib', ['env' => ['DEBIAN_FRONTEND' => 'noninteractive'], 'timeout' => 900]);
63+
run('apt-get install -y postgresql postgresql-contrib', env: ['DEBIAN_FRONTEND' => 'noninteractive'], timeout: 900);
6464
run("sudo -u postgres psql <<< $'CREATE DATABASE {{db_name}};'");
65-
run("sudo -u postgres psql <<< $'CREATE USER {{db_user}} WITH ENCRYPTED PASSWORD \'%secret%\';'", ['secret' => get('db_password')]);
65+
run("sudo -u postgres psql <<< $'CREATE USER {{db_user}} WITH ENCRYPTED PASSWORD \'%secret%\';'", secret: get('db_password'));
6666
run("sudo -u postgres psql <<< $'GRANT ALL PRIVILEGES ON DATABASE {{db_name}} TO {{db_user}};'");
6767
});

recipe/provision/php.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"php$version-xml",
3434
"php$version-zip",
3535
];
36-
run('apt-get install -y ' . implode(' ', $packages), ['env' => ['DEBIAN_FRONTEND' => 'noninteractive']]);
36+
run('apt-get install -y ' . implode(' ', $packages), env: ['DEBIAN_FRONTEND' => 'noninteractive']);
3737

3838
// Configure PHP-CLI
3939
run("sed -i 's/error_reporting = .*/error_reporting = E_ALL/' /etc/php/$version/cli/php.ini");

recipe/provision/user.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
// Make color prompt.
3333
run("sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/' /home/deployer/.bashrc");
3434

35-
$password = run("mkpasswd -m sha-512 '%secret%'", ['secret' => get('sudo_password')]);
36-
run("usermod --password '%secret%' deployer", ['secret' => $password]);
35+
$password = run("mkpasswd -m sha-512 '%secret%'", secret: get('sudo_password'));
36+
run("usermod --password '%secret%' deployer", secret: $password);
3737

3838
// Copy root public key to deployer user so user can login without password.
3939
run('cp /root/.ssh/authorized_keys /home/deployer/.ssh/authorized_keys');
@@ -82,9 +82,5 @@
8282
return;
8383
}
8484

85-
run('echo "$PUBLIC_KEY" >> /home/deployer/.ssh/authorized_keys', [
86-
'env' => [
87-
'PUBLIC_KEY' => $publicKeyContent,
88-
],
89-
]);
85+
run('echo "$PUBLIC_KEY" >> /home/deployer/.ssh/authorized_keys', env: ['PUBLIC_KEY' => $publicKeyContent]);
9086
});

recipe/provision/website.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Deployer;
66

7-
use function Deployer\Support\escape_shell_argument;
8-
97
set('domain', function () {
108
return ask(' Domain: ', get('hostname'));
119
});
@@ -40,7 +38,7 @@
4038

4139
if (test('[ -f Caddyfile ]')) {
4240
run("echo $'$caddyfile' > Caddyfile.new");
43-
$diff = run('diff -U5 --color=always Caddyfile Caddyfile.new', ['no_throw' => true]);
41+
$diff = run('diff -U5 --color=always Caddyfile Caddyfile.new', nothrow: true);
4442
if (empty($diff)) {
4543
run('rm Caddyfile.new');
4644
} else {

src/Command/RunCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ protected function execute(Input $input, Output $output): int
7272
cd($path);
7373
}
7474
}
75-
run($command, [
76-
'real_time_output' => true,
77-
'timeout' => intval($input->getOption('timeout')),
78-
]);
75+
run(
76+
$command,
77+
timeout: intval($input->getOption('timeout')),
78+
forceOutput: true,
79+
);
7980
});
8081

8182
foreach ($hosts as $host) {

src/Documentation/ApiGen.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function parse(string $source): void
2121
{
2222
$comment = '';
2323
$params = '';
24+
$signature = '';
2425

2526
$source = str_replace("\r\n", "\n", $source);
2627

@@ -34,7 +35,7 @@ public function parse(string $source): void
3435
}
3536
if (str_starts_with($line, 'function')) {
3637
$signature = preg_replace('/^function\s+/', '', $line);
37-
$funcName = preg_replace('/\(.+$/', '', $signature);
38+
$funcName = preg_replace('/\(.*$/', '', $signature);
3839
$this->fns[] = [
3940
'comment' => $comment,
4041
'params' => $params,
@@ -43,7 +44,12 @@ public function parse(string $source): void
4344
];
4445
$comment = '';
4546
$params = '';
46-
break;
47+
48+
if (str_ends_with($signature, '(')) {
49+
$state = 'params';
50+
} else {
51+
$signature = '';
52+
}
4753
}
4854
break;
4955

@@ -68,6 +74,16 @@ public function parse(string $source): void
6874
}
6975
$comment .= preg_replace('/^\s\*\s?/', '', $line) . "\n";
7076
break;
77+
78+
case 'params':
79+
if (preg_match('/^\).+\{$/', $line, $matches)) {
80+
$signature .= "\n" . preg_replace('/\{$/', '', $line);
81+
$this->fns[count($this->fns) - 1]['signature'] = $signature;
82+
$state = 'root';
83+
} else {
84+
$signature .= "\n" . $line;
85+
}
86+
break;
7187
}
7288
}
7389
}

0 commit comments

Comments
 (0)