Skip to content

Commit 2e88118

Browse files
committed
fix(cli): improve --src path handling, error messaging, and documentation for new command\n\nReferences: #16
1 parent 77661a1 commit 2e88118

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ Run `wire-cli` followed by the desired command to execute various tasks. For exa
3434
wire-cli new myproject
3535
```
3636

37+
### Path Arguments (e.g., --src)
38+
- You can use both relative and absolute paths for arguments like `--src`.
39+
- Paths with spaces should be quoted: `--src="C:/path/with spaces/ProcessWire"`
40+
- Both forward `/` and backward `\` slashes are supported; they are normalized automatically.
41+
- If the path does not exist or is not accessible, a clear error message will be shown.
42+
3743
For a complete list of available commands and options, use the `help` command:
3844

3945
## Documentation

Tests/Commands/Common/NewCommandTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,58 @@ public function testDownload() {
5353
$this->assertDirectoryNotExists(Base::INSTALLATION_FOLDER . '/site');
5454
}
5555

56+
public function testDownloadWithRelativeSrc() {
57+
$this->checkInstallation();
58+
$relativePath = basename(Base::INSTALLATION_ARCHIVE); // e.g., 'archive.zip'
59+
if (!file_exists($relativePath)) {
60+
copy(Base::INSTALLATION_ARCHIVE, $relativePath);
61+
}
62+
// Ensure the ProcessWire directory exists for the test
63+
if (!is_dir(Base::INSTALLATION_FOLDER)) {
64+
mkdir(Base::INSTALLATION_FOLDER);
65+
}
66+
$options = array('--no-install' => true, '--src' => $relativePath);
67+
$this->tester->execute(array_merge($this->defaults, $options));
68+
$this->assertDirectoryExists(Base::INSTALLATION_FOLDER);
69+
if (file_exists($relativePath)) {
70+
unlink($relativePath);
71+
}
72+
}
73+
74+
public function testDownloadWithAbsoluteSrc() {
75+
$this->checkInstallation();
76+
$absolutePath = realpath(Base::INSTALLATION_ARCHIVE);
77+
// Ensure the ProcessWire directory exists for the test
78+
if (!is_dir(Base::INSTALLATION_FOLDER)) {
79+
mkdir(Base::INSTALLATION_FOLDER);
80+
}
81+
$options = array('--no-install' => true, '--src' => $absolutePath);
82+
$this->tester->execute(array_merge($this->defaults, $options));
83+
$this->assertDirectoryExists(Base::INSTALLATION_FOLDER);
84+
}
85+
86+
public function testDownloadWithNonExistentSrc() {
87+
$this->checkInstallation();
88+
$options = array('--no-install' => true, '--src' => 'nonexistent.zip');
89+
$this->expectException(\RuntimeException::class);
90+
$this->tester->execute(array_merge($this->defaults, $options));
91+
}
92+
93+
public function testDownloadWithMixedSlashesAndSpacesSrc() {
94+
$this->checkInstallation();
95+
$absolutePath = realpath(Base::INSTALLATION_ARCHIVE);
96+
// Simulate a path with mixed slashes and spaces
97+
$mixedPath = str_replace('/', '\\', $absolutePath);
98+
$mixedPath = ' ' . $mixedPath . ' ';
99+
// Ensure the ProcessWire directory exists for the test
100+
if (!is_dir(Base::INSTALLATION_FOLDER)) {
101+
mkdir(Base::INSTALLATION_FOLDER);
102+
}
103+
$options = array('--no-install' => true, '--src' => $mixedPath);
104+
$this->tester->execute(array_merge($this->defaults, $options));
105+
$this->assertDirectoryExists(Base::INSTALLATION_FOLDER);
106+
}
107+
56108
/**
57109
* @depends testDownload
58110
* @expectedException RuntimeException

docs/commands/common.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ $ wirecli new {directory}*
3131
--userpass : Admin password
3232
--useremail : Admin email address
3333
--profile : Default site profile: `path/to/profile.zip` OR one of `beginner, blank, classic, default, languages`
34-
--src : Path to pre-downloaded folder, zip or tgz: `path/to/src`
34+
--src : Path to pre-downloaded folder, zip or tgz: `path/to/src` (supports relative/absolute, spaces, and both / and \ slashes)
3535
--sha : Download specific commit
3636
--no-install : Disable installation
3737
--v : Increase the verbosity of messages
3838
```
3939

40+
**Note:**
41+
- Paths for `--src` can be relative or absolute.
42+
- Paths with spaces should be quoted: `--src="C:/path/with spaces/ProcessWire"`
43+
- Both `/` and `\` slashes are supported and normalized automatically.
44+
- If the path does not exist or is not accessible, a clear error message will be shown.
45+
4046
### Examples
4147

4248
Download and unzip ProcessWires master branch into current directory.

src/Commands/Common/NewCommand.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,31 @@ private function ask($name, $question, $default = null, $hidden = false, $autoc
266266
/**
267267
* Get absolute path
268268
*
269-
* @param $path
269+
* @param string $path
270270
* @return string
271271
*/
272272
private function getAbsolutePath($path) {
273-
return $this->fs->isAbsolutePath($path) ? $path : getcwd() . DIRECTORY_SEPARATOR . $path;
273+
// Normalize slashes for cross-platform compatibility
274+
$normalizedPath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
275+
// Remove trailing slashes
276+
$normalizedPath = rtrim($normalizedPath, DIRECTORY_SEPARATOR);
277+
// If path is quoted (e.g., with spaces), remove quotes
278+
$normalizedPath = trim($normalizedPath, "'\"");
279+
// If not absolute, prepend current working directory
280+
$absolutePath = $this->fs->isAbsolutePath($normalizedPath)
281+
? $normalizedPath
282+
: getcwd() . DIRECTORY_SEPARATOR . $normalizedPath;
283+
// Canonicalize path (resolve .., .)
284+
$realPath = realpath($absolutePath);
285+
if ($realPath === false) {
286+
throw new \RuntimeException(
287+
sprintf(
288+
"The provided --src path '%s' does not exist or is not accessible. Please check the path and try again.",
289+
$path
290+
)
291+
);
292+
}
293+
return $realPath;
274294
}
275295

276296
/**

0 commit comments

Comments
 (0)