Skip to content

Commit

Permalink
[console] Update profile path and fix its execution (#3596)
Browse files Browse the repository at this point in the history
* [console] Update profile path and fix its execution

* Change base-path to profile-path in profile generator

* Change validation path and add path on parameters to validate in the render files

* Delete path from parameters
  • Loading branch information
hjuarez20 authored and jmolivas committed Dec 5, 2017
1 parent 828ed9c commit c8f72d1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 79 deletions.
17 changes: 10 additions & 7 deletions src/Command/Generate/ModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Drupal\Console\Command\Generate;

use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -18,6 +17,7 @@
use Drupal\Console\Utils\Validator;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Utils\DrupalApi;
use Webmozart\PathUtil\Path;

class ModuleCommand extends Command
{
Expand Down Expand Up @@ -181,7 +181,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

$module = $this->validator->validateModuleName($input->getOption('module'));

$modulePath = $this->appRoot . $input->getOption('module-path');
// Get the profile path and define a profile path if it is null
// Check that it is an absolute path or otherwise create an absolute path using appRoot
$modulePath = $input->getOption('module-path');
$modulePath = $modulePath == null ? 'modules/custom' : $modulePath;
$modulePath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
$modulePath = $this->validator->validateModulePath($modulePath, true);

$machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
Expand Down Expand Up @@ -270,13 +274,12 @@ function ($machine_name) use ($validator) {

$modulePath = $input->getOption('module-path');
if (!$modulePath) {
$drupalRoot = $this->appRoot;
$modulePath = $io->ask(
$this->trans('commands.generate.module.questions.module-path'),
'/modules/custom',
function ($modulePath) use ($drupalRoot, $machineName) {
$modulePath = ($modulePath[0] != '/' ? '/' : '').$modulePath;
$fullPath = $drupalRoot.$modulePath.'/'.$machineName;
'modules/custom',
function ($modulePath) use ($machineName) {
$fullPath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
$fullPath = $fullPath.'/'.$machineName;
if (file_exists($fullPath)) {
throw new \InvalidArgumentException(
sprintf(
Expand Down
38 changes: 22 additions & 16 deletions src/Command/Generate/ProfileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Utils\Validator;
use Webmozart\PathUtil\Path;

/**
* Class ProfileCommand
Expand Down Expand Up @@ -94,10 +95,10 @@ protected function configure()
$this->trans('commands.generate.profile.options.machine-name')
)
->addOption(
'base-path',
'profile-path',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.profile.options.base-path')
$this->trans('commands.generate.profile.options.profile-path')
)
->addOption(
'description',
Expand Down Expand Up @@ -144,10 +145,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}


// Get the profile path and define a profile path if it is null
// Check that it is an absolute path or otherwise create an absolute path using appRoot
$profile_path = $input->getOption('profile-path');
$profile_path = $profile_path == null ? 'profiles' : $profile_path;
$profile_path = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
$profile_path = $this->validator->validateModulePath($profile_path, true);

$profile = $this->validator->validateModuleName($input->getOption('profile'));
$machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
$base_path = $this->appRoot . $input->getOption('base-path');
$base_path = $this->validator->validateModulePath($base_path, true);
$description = $input->getOption('description');
$core = $input->getOption('core');
$dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $io);
Expand All @@ -157,7 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->generator->generate(
$profile,
$machine_name,
$base_path,
$profile_path,
$description,
$core,
$dependencies,
Expand Down Expand Up @@ -216,15 +223,14 @@ function ($machine_name) use ($validators) {
$input->setOption('machine-name', $machine_name);
}

$base_path = $input->getOption('base-path');
if (!$base_path) {
$drupalRoot = $this->appRoot;
$base_path = $io->ask(
$this->trans('commands.generate.profile.questions.base-path'),
'/profiles',
function ($base_path) use ($drupalRoot, $machine_name) {
$base_path = ($base_path[0] != '/' ? '/' : '').$base_path;
$fullPath = $drupalRoot.$base_path.'/'.$machine_name;
$profile_path = $input->getOption('profile-path');
if (!$profile_path) {
$profile_path = $io->ask(
$this->trans('commands.generate.profile.questions.profile-path'),
'profiles',
function ($profile_path) use ($machine_name) {
$fullPath = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
$fullPath = $fullPath.'/'.$machine_name;
if (file_exists($fullPath)) {
throw new \InvalidArgumentException(
sprintf(
Expand All @@ -234,11 +240,11 @@ function ($base_path) use ($drupalRoot, $machine_name) {
);
}

return $base_path;
return $profile_path;
}
);
}
$input->setOption('base-path', $base_path);
$input->setOption('profile-path', $profile_path);

$description = $input->getOption('description');
if (!$description) {
Expand Down
20 changes: 12 additions & 8 deletions src/Command/Generate/ThemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Utils\Validator;
use Drupal\Core\Extension\ThemeHandler;
use Webmozart\PathUtil\Path;

/**
* Class ThemeCommand
Expand Down Expand Up @@ -185,7 +186,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$theme = $this->validator->validateModuleName($input->getOption('theme'));
$theme_path = $this->appRoot . $input->getOption('theme-path');
// Get the profile path and define a profile path if it is null
// Check that it is an absolute path or otherwise create an absolute path using appRoot
$theme_path = $input->getOption('theme-path');
$theme_path = $theme_path == null ? 'themes/custom' : $theme_path;
$theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
$theme_path = $this->validator->validateModulePath($theme_path, true);

$machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
Expand Down Expand Up @@ -263,18 +268,17 @@ function ($machine_name) use ($validators) {

$theme_path = $input->getOption('theme-path');
if (!$theme_path) {
$drupalRoot = $this->appRoot;
$theme_path = $io->ask(
$this->trans('commands.generate.theme.questions.theme-path'),
'/themes/custom',
function ($theme_path) use ($drupalRoot, $machine_name) {
$theme_path = ($theme_path[0] != '/' ? '/' : '') . $theme_path;
$full_path = $drupalRoot . $theme_path . '/' . $machine_name;
if (file_exists($full_path)) {
'themes/custom',
function ($theme_path) use ($machine_name) {
$fullPath = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
$fullPath = $fullPath.'/'.$machine_name;
if (file_exists($fullPath)) {
throw new \InvalidArgumentException(
sprintf(
$this->trans('commands.generate.theme.errors.directory-exists'),
$full_path
$fullPath
)
);
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/Generator/ModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generate(
$test,
$twigtemplate
) {
$dir .= '/'.$machineName;
$dir = ($dir == "/" ? '': $dir).'/'.$machineName;
if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(
Expand Down Expand Up @@ -74,15 +74,15 @@ public function generate(
}

$parameters = [
'module' => $module,
'machine_name' => $machineName,
'type' => 'module',
'core' => $core,
'description' => $description,
'package' => $package,
'dependencies' => $dependencies,
'test' => $test,
'twigtemplate' => $twigtemplate,
'module' => $module,
'machine_name' => $machineName,
'type' => 'module',
'core' => $core,
'description' => $description,
'package' => $package,
'dependencies' => $dependencies,
'test' => $test,
'twigtemplate' => $twigtemplate,
];

$this->renderFile(
Expand All @@ -96,7 +96,7 @@ public function generate(
'module/features.yml.twig',
$dir.'/'.$machineName.'.features.yml',
[
'bundle' => $featuresBundle,
'bundle' => $featuresBundle,
]
);
}
Expand Down
21 changes: 10 additions & 11 deletions src/Generator/ProfileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class ProfileGenerator extends Generator
public function generate(
$profile,
$machine_name,
$base_path,
$dir,
$description,
$core,
$dependencies,
$themes,
$distribution
) {
$dir = $base_path . '/' . $machine_name;

$dir = ($dir == "/" ? '': $dir).'/'.$machine_name;
if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(
Expand Down Expand Up @@ -52,14 +51,14 @@ public function generate(
}

$parameters = [
'profile' => $profile,
'machine_name' => $machine_name,
'type' => 'profile',
'core' => $core,
'description' => $description,
'dependencies' => $dependencies,
'themes' => $themes,
'distribution' => $distribution,
'profile' => $profile,
'machine_name' => $machine_name,
'type' => 'profile',
'core' => $core,
'description' => $description,
'dependencies' => $dependencies,
'themes' => $themes,
'distribution' => $distribution,
];

$this->renderFile(
Expand Down
24 changes: 12 additions & 12 deletions src/Generator/ThemeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generate(
$regions,
$breakpoints
) {
$dir .= '/' . $machine_name;
$dir = ($dir == "/" ? '': $dir).'/'.$machine_name;
if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(
Expand Down Expand Up @@ -74,17 +74,17 @@ public function generate(
}

$parameters = [
'theme' => $theme,
'machine_name' => $machine_name,
'type' => 'theme',
'core' => $core,
'description' => $description,
'package' => $package,
'base_theme' => $base_theme,
'global_library' => $global_library,
'libraries' => $libraries,
'regions' => $regions,
'breakpoints' => $breakpoints,
'theme' => $theme,
'machine_name' => $machine_name,
'type' => 'theme',
'core' => $core,
'description' => $description,
'package' => $package,
'base_theme' => $base_theme,
'global_library' => $global_library,
'libraries' => $libraries,
'regions' => $regions,
'breakpoints' => $breakpoints,
];

$this->renderFile(
Expand Down
34 changes: 20 additions & 14 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,33 @@ public function validateMachineName($machine_name)

public function validateModulePath($module_path, $create = false)
{
if (!is_dir($module_path)) {
if ($create && mkdir($module_path, 0755, true)) {
return $module_path;
}
if (strlen($module_path) > 1 && $module_path[strlen($module_path)-1] == "/") {
$module_path = substr($module_path, 0, -1);
}

throw new \InvalidArgumentException(
sprintf(
'Module path "%s" is invalid. You need to provide a valid path.',
$module_path
)
);
if (is_dir($module_path)) {
chmod($module_path, 0755);
return $module_path;
}
chmod($module_path, 0755);
return $module_path;


if ($create && mkdir($module_path, 0755, true)) {
return $module_path;
}

throw new \InvalidArgumentException(
sprintf(
'Path "%s" is invalid. You need to provide a valid path.',
$module_path
)
);
}

public function validateMachineNameList($list)
{
$list_checked = [
'success' => [],
'fail' => [],
'success' => [],
'fail' => [],
];

if (empty($list)) {
Expand Down

0 comments on commit c8f72d1

Please sign in to comment.