Skip to content

Commit

Permalink
Improve the CommandHelper class (#649)
Browse files Browse the repository at this point in the history
* [src/Helpers]: Adds a new improved implementation of CommandHelper class with tests.

* [src/Console]: Use the new CommandHelper in all the artisan commands.

* [src/Http]: Remove the old CommandHelper class.

* [src]: Fix some minor issues.

* Fix code style

* [src/Helpers]: Change implementation of CommandHelper::ensureDirectoryExixst() to unlink from the Laravel method.
  • Loading branch information
dfsmania authored Jul 1, 2020
1 parent c625297 commit 5628daf
Show file tree
Hide file tree
Showing 6 changed files with 890 additions and 190 deletions.
36 changes: 29 additions & 7 deletions src/Console/AdminLteInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JeroenNoten\LaravelAdminLte\Console;

use Illuminate\Console\Command;
use JeroenNoten\LaravelAdminLte\Http\Helpers\CommandHelper;
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;

class AdminLteInstallCommand extends Command
{
Expand Down Expand Up @@ -184,7 +184,12 @@ protected function exportMainViews()
}
}

CommandHelper::directoryCopy($this->packagePath('resources/views'), base_path('resources/views/vendor/adminlte'), $this->option('force'), true);
CommandHelper::copyDirectory(
$this->packagePath('resources/views'),
base_path('resources/views/vendor/adminlte'),
$this->option('force'),
true
);

$this->comment('Main views installed successfully.');
}
Expand All @@ -201,7 +206,7 @@ protected function exportAuthViews()
return;
}
}
CommandHelper::ensureDirectoriesExist($this->getViewPath('auth/passwords'));
CommandHelper::ensureDirectoryExists($this->getViewPath('auth/passwords'));
foreach ($this->authViews as $file => $content) {
file_put_contents($this->getViewPath($file), $content);
}
Expand Down Expand Up @@ -276,7 +281,12 @@ protected function exportTranslations()
}
}

CommandHelper::directoryCopy($this->packagePath('resources/lang'), resource_path('lang/vendor/adminlte'), $this->option('force'), true);
CommandHelper::copyDirectory(
$this->packagePath('resources/lang'),
resource_path('lang/vendor/adminlte'),
$this->option('force'),
true
);

$this->comment('Translation files installed successfully.');
}
Expand Down Expand Up @@ -361,14 +371,26 @@ protected function copyAssets($asset_name, $force = false)
if (is_array($asset['package_path'])) {
foreach ($asset['package_path'] as $key => $asset_package_path) {
$asset_assets_path = $asset['assets_path'][$key];
CommandHelper::directoryCopy(base_path($this->assets_package_path).$asset_package_path, public_path($this->assets_path).$asset_assets_path, $force, ($asset['recursive'] ?? true), ($asset['ignore'] ?? []), ($asset['ignore_ending'] ?? null));
CommandHelper::copyDirectory(
base_path($this->assets_package_path).$asset_package_path,
public_path($this->assets_path).$asset_assets_path,
$force,
($asset['recursive'] ?? true),
($asset['ignore'] ?? [])
);
}
} else {
CommandHelper::directoryCopy(base_path($this->assets_package_path).$asset['package_path'], public_path($this->assets_path).$asset['assets_path'], $force, ($asset['recursive'] ?? true), ($asset['ignore'] ?? []), ($asset['ignore_ending'] ?? null));
CommandHelper::copyDirectory(
base_path($this->assets_package_path).$asset['package_path'],
public_path($this->assets_path).$asset['assets_path'],
$force,
($asset['recursive'] ?? true),
($asset['ignore'] ?? [])
);
}

if (isset($asset['images_path']) && isset($asset['images'])) {
CommandHelper::ensureDirectoriesExist(public_path($this->assets_path).$asset['images_path']);
CommandHelper::ensureDirectoryExists(public_path($this->assets_path).$asset['images_path']);
foreach ($asset['images'] as $image_package_path => $image_assets_path) {
if (file_exists(public_path($this->assets_path).$asset['images_path'].$image_assets_path) && ! $force) {
continue;
Expand Down
64 changes: 36 additions & 28 deletions src/Console/AdminLtePluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JeroenNoten\LaravelAdminLte\Console;

use Illuminate\Console\Command;
use JeroenNoten\LaravelAdminLte\Http\Helpers\CommandHelper;
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;

class AdminLtePluginCommand extends Command
{
Expand Down Expand Up @@ -114,7 +114,7 @@ class AdminLtePluginCommand extends Command
'name' => 'Filterizr',
'package_path' => 'filterizr',
'assets_path' => 'filterizr',
'ignore_ending' => '*.d.ts',
'ignore' => ['*.d.ts'],
'recursive' => false,
],
'flagIconCss' => [
Expand All @@ -131,9 +131,7 @@ class AdminLtePluginCommand extends Command
'name' => 'Fullcalendar',
'package_path' => 'fullcalendar',
'assets_path' => 'fullcalendar',
'ignore_ending' => [
'*.d.ts', '*.json', '*.md',
],
'ignore' => ['*.d.ts', '*.json', '*.md'],
],
'fullcalendarPlugins' => [
'name' => 'Fullcalendar Plugins',
Expand All @@ -149,17 +147,13 @@ class AdminLtePluginCommand extends Command
'fullcalendar-plugins/interaction',
'fullcalendar-plugins/timegrid',
],
'ignore_ending' => [
'*.d.ts', '*.json', '*.md',
],
'ignore' => ['*.d.ts', '*.json', '*.md'],
],
'icheckBootstrap' => [
'name' => 'iCheck Bootstrap',
'package_path' => 'icheck-bootstrap',
'assets_path' => 'icheck-bootstrap',
'ignore_ending' => [
'*.json', '*.md',
],
'ignore' => ['*.json', '*.md'],
],
'inputmask' => [
'name' => 'InputMask',
Expand All @@ -170,9 +164,7 @@ class AdminLtePluginCommand extends Command
'name' => 'ion RangeSlider',
'package_path' => 'ion-rangeslider',
'assets_path' => 'ion-rangeslider',
'ignore_ending' => [
'*.json', '*.md', '.editorconfig',
],
'ignore' => ['*.json', '*.md', '.editorconfig'],
],
'jqueryKnob' => [
'name' => 'jQuery Knob',
Expand All @@ -191,9 +183,7 @@ class AdminLtePluginCommand extends Command
'raphael',
'jquery-mousewheel',
],
'ignore_ending' => [
'*.json', '*.md', '.editorconfig',
],
'ignore' => ['*.json', '*.md', '.editorconfig'],
],
'jqueryUi' => [
'name' => 'jQuery UI',
Expand All @@ -206,9 +196,7 @@ class AdminLtePluginCommand extends Command
'jquery-ui/images',
],
'recursive' => false,
'ignore_ending' => [
'*.json', '*.md', '*.html', '.editorconfig',
],
'ignore' => ['*.json', '*.md', '*.html', '.editorconfig'],
],
'jqueryValidation' => [
'name' => 'jQuery Validation',
Expand Down Expand Up @@ -247,9 +235,7 @@ class AdminLtePluginCommand extends Command
'select2',
'select2-bootstrap4-theme',
],
'ignore_ending' => [
'*.json', '*.md',
],
'ignore' => ['*.json', '*.md'],
],
'sparklines' => [
'name' => 'Sparklines',
Expand Down Expand Up @@ -376,7 +362,6 @@ protected function checkPlugin($plugin_key)
$plugin_package_path = $this->plugins[$plugin_key]['package_path'];
$plugin_assets_path = $this->plugins[$plugin_key]['assets_path'];
$plugin_ignore = $this->plugins[$plugin_key]['ignore'] ?? [];
$plugin_ignore_ending = $this->plugins[$plugin_key]['ignore_ending'] ?? [];
$plugin_recursive = $this->plugins[$plugin_key]['recursive'] ?? true;

if (is_array($plugin_assets_path)) {
Expand All @@ -385,7 +370,12 @@ protected function checkPlugin($plugin_key)
$plugin_exist = false;
$plugin_child_exist = false;
} else {
$compare = CommandHelper::compareDirectories($plugin_base_path.$plugin_package_path[$key], $plugin_public_path.$assets_path, '', $plugin_ignore, $plugin_ignore_ending, $plugin_recursive);
$compare = CommandHelper::compareDirectories(
$plugin_base_path.$plugin_package_path[$key],
$plugin_public_path.$assets_path,
$plugin_recursive,
$plugin_ignore
);

if (! $plugin_child_missmatch && $compare) {
$plugin_child_missmatch = false;
Expand All @@ -398,7 +388,13 @@ protected function checkPlugin($plugin_key)
if (! file_exists($plugin_public_path.$plugin_assets_path)) {
$plugin_exist = false;
} else {
if (! $compare = CommandHelper::compareDirectories($plugin_base_path.$plugin_package_path, $plugin_public_path.$plugin_assets_path, '', $plugin_ignore, $plugin_ignore_ending, $plugin_recursive)) {
$compare = CommandHelper::compareDirectories(
$plugin_base_path.$plugin_package_path,
$plugin_public_path.$plugin_assets_path,
$plugin_recursive,
$plugin_ignore
);
if (! (bool) $compare) {
$plugin_missmatch = true;
}
}
Expand Down Expand Up @@ -453,10 +449,22 @@ protected function copyPlugins($force = null)
if (is_array($plugin['package_path'])) {
foreach ($plugin['package_path'] as $key => $plugin_package_path) {
$plugin_assets_path = $plugin['assets_path'][$key];
CommandHelper::directoryCopy(base_path($this->package_path).'plugins/'.$plugin_package_path, public_path($this->assets_path).$plugin_assets_path, $force, ($plugin['recursive'] ?? true), ($plugin['ignore'] ?? []), ($plugin['ignore_ending'] ?? null));
CommandHelper::copyDirectory(
base_path($this->package_path).'plugins/'.$plugin_package_path,
public_path($this->assets_path).$plugin_assets_path,
$force,
($plugin['recursive'] ?? true),
($plugin['ignore'] ?? [])
);
}
} else {
CommandHelper::directoryCopy(base_path($this->package_path).'plugins/'.$plugin['package_path'], public_path($this->assets_path).$plugin['assets_path'], $force, ($plugin['recursive'] ?? true), ($plugin['ignore'] ?? []), ($plugin['ignore_ending'] ?? null));
CommandHelper::copyDirectory(
base_path($this->package_path).'plugins/'.$plugin['package_path'],
public_path($this->assets_path).$plugin['assets_path'],
$force,
($plugin['recursive'] ?? true),
($plugin['ignore'] ?? [])
);
}

$bar->advance();
Expand Down
23 changes: 16 additions & 7 deletions src/Console/AdminLteStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JeroenNoten\LaravelAdminLte\Console;

use Illuminate\Console\Command;
use JeroenNoten\LaravelAdminLte\Http\Helpers\CommandHelper;
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;

class AdminLteStatusCommand extends Command
{
Expand Down Expand Up @@ -100,7 +100,7 @@ protected function checkAsset($asset_key, $include_images)

if (is_array($asset['package_path'])) {
foreach ($asset['package_path'] as $key => $value) {
$compare_multiple += $this->compareFolder($asset['package_path'][$key], $asset['assets_path'][$key], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
$compare_multiple += $this->compareFolder($asset['package_path'][$key], $asset['assets_path'][$key], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
}

$compare_multiple /= count($asset['package_path']);
Expand All @@ -112,7 +112,7 @@ protected function checkAsset($asset_key, $include_images)
$compare = 0;
}
} else {
$compare = $this->compareFolder($asset['package_path'], $asset['assets_path'], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
$compare = $this->compareFolder($asset['package_path'], $asset['assets_path'], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
}

return $compare;
Expand All @@ -127,12 +127,11 @@ protected function checkAsset($asset_key, $include_images)
* @param $destination_base_path
* @param $recursive
* @param $ignore
* @param $ignore_ending
* @param $images
* @param $images_path
* @return int
*/
public function compareFolder($source_path, $destination_path, $source_base_path = null, $destination_base_path = null, $recursive = true, $ignore = [], $ignore_ending = [], $images = null, $images_path = null, $ignore_base_folder = null)
public function compareFolder($source_path, $destination_path, $source_base_path = null, $destination_base_path = null, $recursive = true, $ignore = [], $images = null, $images_path = null, $ignore_base_folder = null)
{
$dest_exist = true;
$dest_missing = false;
Expand Down Expand Up @@ -160,7 +159,12 @@ public function compareFolder($source_path, $destination_path, $source_base_path
$dest_exist = false;
$dest_child_exist = false;
} else {
$compare = CommandHelper::compareDirectories($source_base_path.$source_path[$key], $destination_base_path.$destination_child_path, '', $ignore, $ignore_ending, $recursive);
$compare = CommandHelper::compareDirectories(
$source_base_path.$source_path[$key],
$destination_base_path.$destination_child_path,
$recursive,
$ignore
);

if (! $dest_child_missmatch && $compare) {
$dest_child_missmatch = false;
Expand All @@ -173,7 +177,12 @@ public function compareFolder($source_path, $destination_path, $source_base_path
if (! file_exists($destination_base_path.$destination_path)) {
$dest_exist = false;
} else {
$compare = CommandHelper::compareDirectories($source_base_path.$source_path, $destination_base_path.$destination_path, '', $ignore, $ignore_ending, $recursive, null, true);
$compare = CommandHelper::compareDirectories(
$source_base_path.$source_path,
$destination_base_path.$destination_path,
$recursive,
$ignore
);
if ($compare === false) {
$dest_missmatch = true;
} elseif ($compare === null) {
Expand Down
Loading

0 comments on commit 5628daf

Please sign in to comment.