Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename currentDirectory into workingDirectory #330

Merged
merged 1 commit into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -5,8 +5,8 @@
* Add a option `ignoreValidationErrors` on `AsTask` attribute to ignore
parameters & options validation errors
* Add a way to merge an application `box.json` config file used by `castor:repack`command
* Deprecate `Context::withPath()` in favor of `Context::withCurrentDirectory()`
* Deprecate `path` argument in `capture()`, `exit_code()`, `run()`, `with()` in favor of `currentDirectory`
* Deprecate `Context::withPath()` in favor of `Context::withWorkingDirectory()`
* Deprecate `path` argument in `capture()`, `exit_code()`, `run()`, `with()` in favor of `workingDirectory`

## 0.14.0 (2024-03-08)

Expand Down
6 changes: 3 additions & 3 deletions doc/getting-started/context.md
Expand Up @@ -28,9 +28,9 @@ function foo(): void
{
$context = context();

io()->writeln($context->currentDirectory); // will print the directory of the castor.php file
io()->writeln($context->workingDirectory); // will print the directory of the castor.php file

$context = $context->withCurrentDirectory('/tmp'); // will create a new context where the current directory is /tmp
$context = $context->withWorkingDirectory('/tmp'); // will create a new context where the current directory is /tmp
run('pwd', context: $context); // will print "/tmp"
}
```
Expand Down Expand Up @@ -124,7 +124,7 @@ use function Castor\run;
#[AsContext(default: true, name: 'my_context')]
function create_default_context(): Context
{
return new Context(['foo' => 'bar'], currentDirectory: '/tmp');
return new Context(['foo' => 'bar'], workingDirectory: '/tmp');
}

#[AsTask()]
Expand Down
6 changes: 3 additions & 3 deletions doc/getting-started/run.md
Expand Up @@ -66,7 +66,7 @@ function foo(): void
## Working directory

By default, Castor will execute the process in the same directory as
the `castor.php` file. You can change that by setting the `currentDirectory`
the `castor.php` file. You can change that by setting the `workingDirectory`
argument. It can be either a relative or an absolute path:

```php
Expand All @@ -77,8 +77,8 @@ use function Castor\run;
#[AsTask()]
function foo(): void
{
run('pwd', currentDirectory: '../'); // run the process in the parent directory of the castor.php file
run('pwd', currentDirectory: '/tmp'); // run the process in the /tmp directory
run('pwd', workingDirectory: '../'); // run the process in the parent directory of the castor.php file
run('pwd', workingDirectory: '/tmp'); // run the process in the /tmp directory
}
```

Expand Down
4 changes: 2 additions & 2 deletions doc/going-further/interacting-with-castor/advanced-context.md
Expand Up @@ -42,7 +42,7 @@ use function Castor\run;
#[AsContext(name: 'my_context')]
function create_my_context(): Context
{
return new Context(['foo' => 'bar'], currentDirectory: '/tmp');
return new Context(['foo' => 'bar'], workingDirectory: '/tmp');
}

#[AsTask()]
Expand Down Expand Up @@ -71,7 +71,7 @@ use function Castor\with;
#[AsContext(name: 'my_context')]
function create_my_context(): Context
{
return new Context(['foo' => 'bar'], currentDirectory: '/tmp');
return new Context(['foo' => 'bar'], workingDirectory: '/tmp');
}

#[AsTask()]
Expand Down
2 changes: 1 addition & 1 deletion examples/cd.php
Expand Up @@ -10,6 +10,6 @@
function directory(): void
{
run(['pwd']);
run(['pwd'], currentDirectory: 'src/Attribute');
run(['pwd'], workingDirectory: 'src/Attribute');
run(['pwd']);
}
2 changes: 1 addition & 1 deletion examples/failure.php
Expand Up @@ -9,7 +9,7 @@
#[AsTask(description: 'A failing task not authorized to fail')]
function failure(): void
{
run('i_do_not_exist', currentDirectory: '/tmp', pty: false);
run('i_do_not_exist', workingDirectory: '/tmp', pty: false);
}

#[AsTask(description: 'A failing task authorized to fail')]
Expand Down
34 changes: 17 additions & 17 deletions src/Context.php
Expand Up @@ -4,7 +4,7 @@

class Context implements \ArrayAccess
{
public readonly string $currentDirectory;
public readonly string $workingDirectory;

/**
* @phpstan-param ContextData $data The input parameter accepts an array or an Object
Expand All @@ -14,7 +14,7 @@ class Context implements \ArrayAccess
public function __construct(
public readonly array $data = [],
public readonly array $environment = [],
?string $currentDirectory = null,
?string $workingDirectory = null,
public readonly bool $tty = false,
public readonly bool $pty = true,
public readonly ?float $timeout = null,
Expand All @@ -25,7 +25,7 @@ public function __construct(
// Do not use this argument, it is only used internally by the application
public readonly string $name = '',
) {
$this->currentDirectory = $currentDirectory ?? PathHelper::getRoot();
$this->workingDirectory = $workingDirectory ?? PathHelper::getRoot();
}

public function __debugInfo()
Expand All @@ -34,7 +34,7 @@ public function __debugInfo()
'name' => $this->name,
'data' => $this->data,
'environment' => $this->environment,
'currentDirectory' => $this->currentDirectory,
'workingDirectory' => $this->workingDirectory,
'tty' => $this->tty,
'pty' => $this->pty,
'timeout' => $this->timeout,
Expand Down Expand Up @@ -67,7 +67,7 @@ public function withData(array $data, bool $keepExisting = true, bool $recursive
return new self(
$data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -85,7 +85,7 @@ public function withEnvironment(array $environment, bool $keepExisting = true):
return new self(
$this->data,
$keepExisting ? [...$this->environment, ...$environment] : $environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -101,15 +101,15 @@ public function withPath(string $path): self
{
trigger_deprecation('castor', '0.15', 'The method "%s()" is deprecated, use "%s::withCurrentDirectory()" instead.', __METHOD__, __CLASS__);

return $this->withCurrentDirectory($path);
return $this->withWorkingDirectory($path);
}

public function withCurrentDirectory(string $currentDirectory): self
public function withWorkingDirectory(string $workingDirectory): self
{
return new self(
$this->data,
$this->environment,
str_starts_with($currentDirectory, '/') ? $currentDirectory : PathHelper::realpath($this->currentDirectory . '/' . $currentDirectory),
str_starts_with($workingDirectory, '/') ? $workingDirectory : PathHelper::realpath($this->workingDirectory . '/' . $workingDirectory),
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -126,7 +126,7 @@ public function withTty(bool $tty = true): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$tty,
$this->pty,
$this->timeout,
Expand All @@ -143,7 +143,7 @@ public function withPty(bool $pty = true): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$pty,
$this->timeout,
Expand All @@ -160,7 +160,7 @@ public function withTimeout(?float $timeout): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$timeout,
Expand All @@ -177,7 +177,7 @@ public function withQuiet(bool $quiet = true): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -194,7 +194,7 @@ public function withAllowFailure(bool $allowFailure = true): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -211,7 +211,7 @@ public function withNotify(bool $notify = true): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -228,7 +228,7 @@ public function withVerbosityLevel(VerbosityLevel $verbosityLevel): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand All @@ -245,7 +245,7 @@ public function withName(string $name): self
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->workingDirectory,
$this->tty,
$this->pty,
$this->timeout,
Expand Down
52 changes: 26 additions & 26 deletions src/functions.php
Expand Up @@ -113,7 +113,7 @@ function parallel(callable ...$callbacks): array
function run(
string|array $command,
?array $environment = null,
?string $currentDirectory = null,
?string $workingDirectory = null,
?bool $tty = null,
?bool $pty = null,
?float $timeout = null,
Expand All @@ -130,16 +130,16 @@ function run(
$context = $context->withEnvironment($environment);
}

if ($currentDirectory) {
$context = $context->withCurrentDirectory($currentDirectory);
if ($workingDirectory) {
$context = $context->withWorkingDirectory($workingDirectory);
if ($path) {
throw new \LogicException('You cannot use both the "path" and "currentDirectory" arguments at the same time.');
throw new \LogicException('You cannot use both the "path" and "workingDirectory" arguments at the same time.');
}
}
if ($path) {
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "currentDirectory" instead.');
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "workingDirectory" instead.');

$context = $context->withCurrentDirectory($path);
$context = $context->withWorkingDirectory($path);
}

if (null !== $tty) {
Expand Down Expand Up @@ -167,9 +167,9 @@ function run(
}

if (\is_array($command)) {
$process = new Process($command, $context->currentDirectory, $context->environment, null, $context->timeout);
$process = new Process($command, $context->workingDirectory, $context->environment, null, $context->timeout);
} else {
$process = Process::fromShellCommandline($command, $context->currentDirectory, $context->environment, null, $context->timeout);
$process = Process::fromShellCommandline($command, $context->workingDirectory, $context->environment, null, $context->timeout);
}

// When quiet is set, it means we want to capture the output.
Expand Down Expand Up @@ -259,7 +259,7 @@ function run(
function capture(
string|array $command,
?array $environment = null,
?string $currentDirectory = null,
?string $workingDirectory = null,
?float $timeout = null,
?bool $allowFailure = null,
?string $onFailure = null,
Expand All @@ -274,19 +274,19 @@ function capture(
$allowFailure = true;
}

if ($currentDirectory && $path) {
throw new \LogicException('You cannot use both the "path" and "currentDirectory" arguments at the same time.');
if ($workingDirectory && $path) {
throw new \LogicException('You cannot use both the "path" and "workingDirectory" arguments at the same time.');
}
if ($path) {
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "currentDirectory" instead.');
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "workingDirectory" instead.');

$currentDirectory = $path;
$workingDirectory = $path;
}

$process = run(
command: $command,
environment: $environment,
currentDirectory: $currentDirectory,
workingDirectory: $workingDirectory,
timeout: $timeout,
allowFailure: $allowFailure,
context: $context,
Expand All @@ -307,25 +307,25 @@ function capture(
function exit_code(
string|array $command,
?array $environment = null,
?string $currentDirectory = null,
?string $workingDirectory = null,
?float $timeout = null,
?bool $quiet = null,
?Context $context = null,
?string $path = null,
): int {
if ($currentDirectory && $path) {
throw new \LogicException('You cannot use both the "path" and "currentDirectory" arguments at the same time.');
if ($workingDirectory && $path) {
throw new \LogicException('You cannot use both the "path" and "workingDirectory" arguments at the same time.');
}
if ($path) {
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "currentDirectory" instead.');
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "workingDirectory" instead.');

$currentDirectory = $path;
$workingDirectory = $path;
}

$process = run(
command: $command,
environment: $environment,
currentDirectory: $currentDirectory,
workingDirectory: $workingDirectory,
timeout: $timeout,
allowFailure: true,
context: $context,
Expand Down Expand Up @@ -831,7 +831,7 @@ function with(
callable $callback,
?array $data = null,
?array $environment = null,
?string $currentDirectory = null,
?string $workingDirectory = null,
?bool $tty = null,
?bool $pty = null,
?float $timeout = null,
Expand Down Expand Up @@ -861,16 +861,16 @@ function with(
$context = $context->withEnvironment($environment);
}

if ($currentDirectory) {
$context = $context->withCurrentDirectory($currentDirectory);
if ($workingDirectory) {
$context = $context->withWorkingDirectory($workingDirectory);
if ($path) {
throw new \LogicException('You cannot use both the "path" and "currentDirectory" arguments at the same time.');
throw new \LogicException('You cannot use both the "path" and "workingDirectory" arguments at the same time.');
}
}
if ($path) {
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "currentDirectory" instead.');
trigger_deprecation('castor', '0.15', 'The "path" argument is deprecated, use "workingDirectory" instead.');

$context = $context->withCurrentDirectory($path);
$context = $context->withWorkingDirectory($path);
}

if (null !== $tty) {
Expand Down