Skip to content

Commit c4cba7b

Browse files
committed
Clarify cd() behavior and document cwd: option for run()
1 parent 98fa6dd commit c4cba7b

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

docs/api.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,40 @@ cd(string $path): void
191191

192192
Change the current working directory.
193193

194+
Both `cd()` and the `cwd:` argument of `run()` change the working directory
195+
for executed commands. The difference: `cd()` changes it for the rest of the
196+
current task, while `cwd:` overrides it for a single `run()` call only.
197+
198+
```php
199+
set('deploy_path', '~/deployer.org');
200+
201+
task('task1', function () {
202+
cd('{{deploy_path}}');
203+
204+
run('pwd');
205+
// output: /home/deployer/deployer.org
206+
207+
run('pwd', cwd: '/usr'); // Override working dir for this run only.
208+
// output: /usr
209+
210+
run('pwd');
211+
// output: /home/deployer/deployer.org
212+
});
213+
```
214+
215+
Note that `cd()` only changes the working directory within a single task.
216+
The next task starts fresh.
217+
194218
```php
195-
cd('~/myapp');
196-
run('ls'); // Will run `ls` in ~/myapp.
219+
task('task2', function () {
220+
run('pwd'); // cd from previous task is not used.
221+
// output: /home/deployer
222+
});
223+
224+
task('all', [
225+
'task1',
226+
'task2',
227+
]);
197228
```
198229

199230

src/functions.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,40 @@ function option(string $name, $shortcut = null, ?int $mode = null, string $descr
308308
/**
309309
* Change the current working directory.
310310
*
311+
* Both `cd()` and the `cwd:` argument of `run()` change the working directory
312+
* for executed commands. The difference: `cd()` changes it for the rest of the
313+
* current task, while `cwd:` overrides it for a single `run()` call only.
314+
*
315+
* ```php
316+
* set('deploy_path', '~/deployer.org');
317+
*
318+
* task('task1', function () {
319+
* cd('{{deploy_path}}');
320+
*
321+
* run('pwd');
322+
* // output: /home/deployer/deployer.org
323+
*
324+
* run('pwd', cwd: '/usr'); // Override working dir for this run only.
325+
* // output: /usr
326+
*
327+
* run('pwd');
328+
* // output: /home/deployer/deployer.org
329+
* });
330+
* ```
331+
*
332+
* Note that `cd()` only changes the working directory within a single task.
333+
* The next task starts fresh.
334+
*
311335
* ```php
312-
* cd('~/myapp');
313-
* run('ls'); // Will run `ls` in ~/myapp.
336+
* task('task2', function () {
337+
* run('pwd'); // cd from previous task is not used.
338+
* // output: /home/deployer
339+
* });
340+
*
341+
* task('all', [
342+
* 'task1',
343+
* 'task2',
344+
* ]);
314345
* ```
315346
*/
316347
function cd(string $path): void

0 commit comments

Comments
 (0)