Skip to content

Commit d4a9cc5

Browse files
hjuarez20enzolutions
authored andcommitted
Implement absolute and relative path on plugin and theme commands (#31)
1 parent 1f34839 commit d4a9cc5

File tree

8 files changed

+148
-28
lines changed

8 files changed

+148
-28
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"anolilab/wordpress-salt-generator": "^1.2",
4343
"twig/twig": "^1.31",
4444
"doctrine/annotations": "^1.4",
45-
"fzaninotto/faker": "^1.6"
45+
"fzaninotto/faker": "^1.6",
46+
"webmozart/path-util": "^2.3"
4647
},
4748
"bin": ["bin/wp-console"],
4849
"config": {

composer.lock

Lines changed: 97 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command/Generate/PluginCommand.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use WP\Console\Utils\Validator;
1919
use WP\Console\Core\Utils\StringConverter;
2020
use WP\Console\Utils\Site;
21+
use Webmozart\PathUtil\Path;
22+
2123

2224
class PluginCommand extends Command
2325
{
@@ -169,7 +171,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
169171

170172
$plugin = $this->validator->validatePluginName($input->getOption('plugin'));
171173

172-
$pluginPath = $this->appRoot . $input->getOption('plugin-path');
174+
// Get the plugin path and define it if it is null
175+
// Check that it is an absolute path or otherwise create an absolute path using appRoot
176+
$pluginPath = $input->getOption('plugin-path');
177+
$pluginPath = $pluginPath == null ? basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'plugins' : $pluginPath;
178+
$pluginPath = Path::isAbsolute($pluginPath) ? $pluginPath : Path::makeAbsolute($pluginPath, $this->appRoot);
173179
$pluginPath = $this->validator->validatePluginPath($pluginPath, true);
174180

175181
$machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
@@ -253,13 +259,12 @@ function ($machine_name) use ($validator) {
253259

254260
$pluginPath = $input->getOption('plugin-path');
255261
if (!$pluginPath) {
256-
$wordpressRoot = $this->appRoot;
257262
$pluginPath = $io->ask(
258263
$this->trans('commands.generate.plugin.questions.plugin-path'),
259-
basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $machineName,
260-
function ($pluginPath) use ($wordpressRoot, $machineName) {
261-
$pluginPath = ($pluginPath[0] != '/' ? '/' : '').$pluginPath;
262-
$fullPath = $wordpressRoot.$pluginPath.'/'.$machineName;
264+
basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'plugins',
265+
function ($pluginPath) use ($machineName) {
266+
$fullPath = Path::isAbsolute($pluginPath) ? $pluginPath : Path::makeAbsolute($pluginPath, $this->appRoot);
267+
$fullPath = $fullPath.'/'.$machineName;
263268
if (file_exists($fullPath)) {
264269
throw new \InvalidArgumentException(
265270
sprintf(

src/Command/Generate/ThemeCommand.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Console\Input\InputOption;
1313
use Symfony\Component\Console\Output\OutputInterface;
14+
use Webmozart\PathUtil\Path;
1415
use WP\Console\Command\Shared\ConfirmationTrait;
1516
use WP\Console\Core\Command\Command;
1617
use WP\Console\Generator\ThemeGenerator;
@@ -162,8 +163,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
162163
}
163164

164165
$theme = $this->validator->validatePluginName($input->getOption('theme'));
165-
166-
$themePath = $this->appRoot . $input->getOption('theme-path');
166+
167+
// Get the theme path and define it, if it is null
168+
// Check that it is an absolute path or otherwise create an absolute path using appRoot
169+
$themePath = $input->getOption('theme-path');
170+
$themePath = $themePath == null ? basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'themes' : $themePath;
171+
$themePath = Path::isAbsolute($themePath) ? $themePath : Path::makeAbsolute($themePath, $this->appRoot);
167172
$themePath = $this->validator->validatePluginPath($themePath, true);
168173

169174
$machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
@@ -248,13 +253,12 @@ function ($machine_name) use ($validator) {
248253
// --theme path
249254
$themePath = $input->getOption('theme-path');
250255
if (!$themePath) {
251-
$wordpressRoot = $this->appRoot;
252256
$themePath = $io->ask(
253257
$this->trans('commands.generate.theme.questions.theme-path'),
254-
basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $machineName,
255-
function ($themePath) use ($wordpressRoot, $machineName) {
256-
$themePath = ($themePath[0] != '/' ? '/' : '').$themePath;
257-
$fullPath = $wordpressRoot.$themePath.'/'.$machineName;
258+
basename(WP_CONTENT_DIR) . DIRECTORY_SEPARATOR . 'themes',
259+
function ($themePath) use ($machineName) {
260+
$fullPath = Path::isAbsolute($themePath) ? $themePath : Path::makeAbsolute($themePath, $this->appRoot);
261+
$fullPath = $fullPath.'/'.$machineName;
258262
if (file_exists($fullPath)) {
259263
throw new \InvalidArgumentException(
260264
sprintf(
@@ -263,7 +267,6 @@ function ($themePath) use ($wordpressRoot, $machineName) {
263267
)
264268
);
265269
}
266-
267270
return $themePath;
268271
}
269272
);

src/Core/Generator/Generator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ protected function renderFile(
7272
$flag = null
7373
) {
7474
if (!is_dir(dirname($target))) {
75-
mkdir(dirname($target), 0777, true);
75+
if(!mkdir(dirname($target), 0777, true)){
76+
throw new \InvalidArgumentException(
77+
sprintf(
78+
'Path "%s" is invalid. You need to provide a valid path.',
79+
dirname($target)
80+
)
81+
);
82+
}
7683
}
7784

7885
//Count the code lines if the file exist

src/Generator/PluginGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function generate(
4040
$deactivate,
4141
$uninstall
4242
) {
43+
$dir = ($dir == "/" ? '': $dir).'/'.$machineName;
4344
if (file_exists($dir)) {
4445
if (!is_dir($dir)) {
4546
throw new \RuntimeException(

src/Generator/ThemeGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function generate(
4242
$package,
4343
$test
4444
) {
45+
$dir = ($dir == "/" ? '': $dir).'/'.$machineName;
4546
if (file_exists($dir)) {
4647
if (!is_dir($dir)) {
4748
throw new \RuntimeException(

src/Utils/Validator.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,26 @@ public function validateMachineName($machine_name)
104104

105105
public function validatePluginPath($pluginPath, $create = false)
106106
{
107-
if (!is_dir($pluginPath)) {
108-
if ($create && mkdir($pluginPath, 0755, true)) {
109-
return $pluginPath;
110-
}
111107

112-
throw new \InvalidArgumentException(
113-
sprintf(
114-
'plugin path "%s" is invalid. You need to provide a valid path.',
115-
$pluginPath
116-
)
117-
);
108+
if (strlen($pluginPath) > 1 && $pluginPath[strlen($pluginPath)-1] == "/") {
109+
$pluginPath = substr($pluginPath, 0, -1);
110+
}
111+
112+
if (is_dir($pluginPath)) {
113+
return $pluginPath;
114+
}
115+
116+
if ($create && mkdir($pluginPath, 0755, true)) {
117+
chmod($pluginPath, 0755);
118+
return $pluginPath;
118119
}
119120

120-
return $pluginPath;
121+
throw new \InvalidArgumentException(
122+
sprintf(
123+
'Path "%s" is invalid. You need to provide a valid path.',
124+
$pluginPath
125+
)
126+
);
121127
}
122128

123129
/*public function validatePluginDependencies($dependencies)

0 commit comments

Comments
 (0)