From 1b1911468b498a65fd4d01001bff8b150c861052 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:08:46 -0400 Subject: [PATCH 1/5] Update GruntController.php Signed-off-by: ArchBlood <35392110+ArchBlood@users.noreply.github.com> --- controllers/GruntController.php | 73 ++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/controllers/GruntController.php b/controllers/GruntController.php index f2736fe..f8931b8 100644 --- a/controllers/GruntController.php +++ b/controllers/GruntController.php @@ -13,7 +13,7 @@ class GruntController extends Controller /** * Displays the index page. * - * @return string + * @return string the rendering result. */ public function actionIndex() { @@ -23,63 +23,87 @@ public function actionIndex() /** * Builds assets using Grunt. * - * @return string + * @return string the rendering result. * @throws \yii\web\ForbiddenHttpException if the user is not allowed to perform this action. * @throws \yii\web\ServerErrorHttpException if the working directory is invalid. */ public function actionBuildAssets() { - $output = $this->executeGruntCommand('build-assets'); - return $this->renderPartial('build-assets', ['output' => $output]); + try { + $output = $this->executeGruntCommand('build-assets'); + return $this->renderPartial('build-assets', ['output' => $output]); + } catch (\Exception $e) { + Yii::error('Error executing Grunt command: ' . $e->getMessage()); + Yii::$app->session->setFlash('error', 'Error executing Grunt command: ' . $e->getMessage()); + return $this->redirect(['index']); + } } /** * Builds a theme using Grunt. * * @param string|null $themeName the name of the theme to build. - * @return string + * @return string the rendering result. * @throws \yii\web\ForbiddenHttpException if the user is not allowed to perform this action. * @throws \yii\web\ServerErrorHttpException if the working directory is invalid. */ public function actionBuildTheme($themeName = null) { - $gruntCommand = 'build-theme'; - if ($themeName !== null) { - $gruntCommand .= ' --name=' . escapeshellarg($themeName); + try { + $gruntCommand = 'build-theme'; + if ($themeName !== null) { + $gruntCommand .= ' --name=' . escapeshellarg($themeName); + } + $output = $this->executeGruntCommand($gruntCommand); + return $this->renderPartial('build-theme', ['output' => $output]); + } catch (\Exception $e) { + Yii::error('Error executing Grunt command: ' . $e->getMessage()); + Yii::$app->session->setFlash('error', 'Error executing Grunt command: ' . $e->getMessage()); + return $this->redirect(['index']); } - $output = $this->executeGruntCommand($gruntCommand); - return $this->renderPartial('build-theme', ['output' => $output]); } /** - * Rebuilds the search index using Grunt. + * Builds the search index using Grunt. * - * @return string + * @return string the rendering result. * @throws \yii\web\ForbiddenHttpException if the user is not allowed to perform this action. * @throws \yii\web\ServerErrorHttpException if the working directory is invalid. */ public function actionBuildSearch() { - $output = $this->executeGruntCommand('build-search'); - return $this->renderPartial('build-search', ['output' => $output]); + try { + $output = $this->executeGruntCommand('build-search'); + return $this->renderPartial('build-search', ['output' => $output]); + } catch (\Exception $e) { + Yii::error('Error executing Grunt command: ' . $e->getMessage()); + Yii::$app->session->setFlash('error', 'Error executing Grunt command: ' . $e->getMessage()); + return $this->redirect(['index']); + } } /** * Runs migrations using Grunt. * * @param string|null $module the name of the module to migrate. - * @return string + * @return string the rendering result. * @throws \yii\web\ForbiddenHttpException if the user is not allowed to perform this action. * @throws \yii\web\ServerErrorHttpException if the working directory is invalid. */ public function actionMigrateUp($module = null) { - $gruntCommand = 'migrate-up'; - if ($module !== null) { - $gruntCommand .= ' --module=' . escapeshellarg($module); + try { + $gruntCommand = 'migrate-up'; + if ($module !== null) { + $gruntCommand .= ' --module=' . escapeshellarg($module); + } + $output = $this->executeGruntCommand($gruntCommand); + return $this->renderPartial('migrate-up', ['output' => $output]); + } catch (\Exception $e) { + Yii::error('Error executing Grunt command: ' . $e->getMessage()); + Yii::$app->session->setFlash('error', 'Error executing Grunt command: ' . $e->getMessage()); + return $this->redirect(['index']); } - $output = $this->executeGruntCommand($gruntCommand); - return $this->renderPartial('migrate-up', ['output' => $output]); } /** @@ -94,37 +118,30 @@ private function executeGruntCommand($command) { $output = []; - // Check if the user has permission to run Grunt if (!Yii::$app->user->isAdmin()) { throw new \yii\web\ForbiddenHttpException('You are not allowed to perform this action.'); } - // Define the working directory where your Gruntfile is located $workingDirectory = $_SERVER['DOCUMENT_ROOT']; - // Validate the working directory if (!is_dir($workingDirectory)) { Yii::error('Invalid working directory: ' . $workingDirectory); throw new \yii\web\ServerErrorHttpException('Invalid working directory.'); } - // Log current working directory Yii::info('Current Working Directory: ' . $workingDirectory); - // Execute Grunt command using the Gruntfile.js from the root directory exec("cd $workingDirectory && grunt --gruntfile=$workingDirectory/Gruntfile.js $command 2>&1", $output, $returnCode); Yii::info('Grunt Command executed: grunt --gruntfile=' . $workingDirectory . '/Gruntfile.js ' . $command); Yii::info('Output: ' . print_r($output, true)); Yii::info('Return code: ' . $returnCode); - // Check if the command execution was successful if ($returnCode !== 0) { Yii::error('Failed to execute Grunt command: ' . $command . '. Return code: ' . $returnCode); Yii::$app->session->setFlash('error', 'Failed to execute Grunt command: ' . $command . '. Return code: ' . $returnCode); } - // Return the output of the Grunt command return $output; } -} \ No newline at end of file +} From 7c944970b8fb4e3bd112ad5366db31339b66a9bf Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:13:08 -0400 Subject: [PATCH 2/5] Update build-assets.php Signed-off-by: ArchBlood <35392110+ArchBlood@users.noreply.github.com> --- views/grunt/build-assets.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/views/grunt/build-assets.php b/views/grunt/build-assets.php index 5283878..ee17921 100644 --- a/views/grunt/build-assets.php +++ b/views/grunt/build-assets.php @@ -2,12 +2,16 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use yii\web\View; /* @var $this yii\web\View */ /* @var $output array */ $this->title = 'Build Assets'; $this->params['breadcrumbs'][] = $this->title; + +// Register PJAX library +$this->registerJsFile('@web/static/js/jquery.pjax.modified.js', ['position' => View::POS_HEAD]); ?>
@@ -19,15 +23,15 @@
- - + +
'run-command-form']); ?>
- 'run-command-btn', 'class' => 'btn btn-primary', 'data' => ['pjax' => 1, 'action' => '/composer/grunt/build-assets']]) ?> + 'run-command-btn', 'class' => 'btn btn-primary']) ?>
@@ -47,7 +51,7 @@ $.ajax({ type: "POST", - url: $form.attr("action"), + url: "' . Yii::$app->urlManager->createUrl(['/composer/grunt/build-assets']) . '/build-assets", data: formData, success: function(response) { if (response.success) { @@ -66,4 +70,4 @@ }); }); '); -?> \ No newline at end of file +?> From 9346eee05a6e82341aaf63b25c094e4bc07e0fa6 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:14:44 -0400 Subject: [PATCH 3/5] Update build-search.php Signed-off-by: ArchBlood <35392110+ArchBlood@users.noreply.github.com> --- views/grunt/build-search.php | 59 +++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/views/grunt/build-search.php b/views/grunt/build-search.php index 2c7c9dd..6d5f216 100644 --- a/views/grunt/build-search.php +++ b/views/grunt/build-search.php @@ -1,13 +1,17 @@ title = 'Build Search'; -$this->pageTitle = $this->title; $this->params['breadcrumbs'][] = $this->title; + +// Register PJAX library +$this->registerJsFile('@web/static/js/jquery.pjax.modified.js', ['position' => View::POS_HEAD]); ?>
- - - - - - - 'btn btn-primary', - 'data' => [ - 'pjax' => 1, // Enable PJAX for the button click - 'action' => '/composer/grunt/build-search', - ], - ]) ?> +
+ + + + +
+ + 'run-command-form']); ?> + +
+ 'run-command-btn', 'class' => 'btn btn-primary']) ?> +
+ +
registerJs(' - $(document).on("click", "[data-ui-widget=pjax-container] [data-pjax=1]", function(event) { + $(document).on("submit", "#run-command-form", function(event) { event.preventDefault(); - var $this = $(this); - var container = $this.closest("[data-ui-widget=pjax-container]"); + var $form = $(this); + var $btn = $form.find(":submit"); + var formData = $form.serialize(); + + $btn.button("loading"); + $.ajax({ type: "POST", - url: $this.data("action"), - data: "' . Yii::$app->request->csrfParam . '=' . Yii::$app->request->csrfToken . '", + url: "' . Yii::$app->urlManager->createUrl(['/composer/grunt/build-search']) . '/build-search", + data: formData, success: function(response) { - container.html(response); + if (response.success) { + // Update the container with the command output + $("#output-container").html("

Output

" + response.output.join("\n") + "
"); + } else { + console.error(response.error); + } }, error: function(xhr, status, error) { console.error(error); + }, + complete: function() { + $btn.button("reset"); } }); }); '); -?> \ No newline at end of file +?> From a6b83fa711e118113f803be0f4b33ac3351d0bc1 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:17:07 -0400 Subject: [PATCH 4/5] Update build-theme.php Signed-off-by: ArchBlood <35392110+ArchBlood@users.noreply.github.com> --- views/grunt/build-theme.php | 60 ++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/views/grunt/build-theme.php b/views/grunt/build-theme.php index 276838b..49af199 100644 --- a/views/grunt/build-theme.php +++ b/views/grunt/build-theme.php @@ -1,12 +1,17 @@ title = 'Build Theme'; -$this->pageTitle = $this->title; $this->params['breadcrumbs'][] = $this->title; + +// Register PJAX library +$this->registerJsFile('@web/static/js/jquery.pjax.modified.js', ['position' => View::POS_HEAD]); ?>
@@ -16,40 +21,53 @@
- - - - - - - 'btn btn-primary', - 'data' => [ - 'pjax' => 1, // Enable PJAX for the button click - 'action' => '/composer/grunt/build-theme', - ], - ]) ?> +
+ + + + +
+ + 'run-command-form']); ?> + +
+ 'run-command-btn', 'class' => 'btn btn-primary']) ?> +
+ +
registerJs(' - $(document).on("click", "[data-ui-widget=pjax-container] [data-pjax=1]", function(event) { + $(document).on("submit", "#run-command-form", function(event) { event.preventDefault(); - var $this = $(this); - var container = $this.closest("[data-ui-widget=pjax-container]"); + var $form = $(this); + var $btn = $form.find(":submit"); + var formData = $form.serialize(); + + $btn.button("loading"); + $.ajax({ type: "POST", - url: $this.data("action"), - data: "' . Yii::$app->request->csrfParam . '=' . Yii::$app->request->csrfToken . '", + url: "' . Yii::$app->urlManager->createUrl(['/composer/grunt/build-theme']) . '/build-theme", + data: formData, success: function(response) { - container.html(response); + if (response.success) { + // Update the container with the command output + $("#output-container").html("

Output

" + response.output.join("\n") + "
"); + } else { + console.error(response.error); + } }, error: function(xhr, status, error) { console.error(error); + }, + complete: function() { + $btn.button("reset"); } }); }); '); -?> \ No newline at end of file +?> From 24cc37ff95a1a18583c540c260cf5abfa8d2c119 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:18:20 -0400 Subject: [PATCH 5/5] Update migrate-up.php Signed-off-by: ArchBlood <35392110+ArchBlood@users.noreply.github.com> --- views/grunt/migrate-up.php | 62 +++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/views/grunt/migrate-up.php b/views/grunt/migrate-up.php index a278602..a563a41 100644 --- a/views/grunt/migrate-up.php +++ b/views/grunt/migrate-up.php @@ -1,13 +1,17 @@ title = 'Migrate Up'; -$this->pageTitle = $this->title; $this->params['breadcrumbs'][] = $this->title; + +// Register PJAX library +$this->registerJsFile('@web/static/js/jquery.pjax.modified.js', ['position' => View::POS_HEAD]); ?>
@@ -16,10 +20,54 @@ title) ?>
-
- - - - +
+
+ + + + +
+ + 'migrate-up-form']); ?> + +
+ 'run-command-btn', 'class' => 'btn btn-primary']) ?> +
+ +
-
\ No newline at end of file + + +registerJs(' + $(document).on("submit", "#migrate-up-form", function(event) { + event.preventDefault(); + var $form = $(this); + var $btn = $form.find(":submit"); + var formData = $form.serialize(); + + $btn.button("loading"); + + $.ajax({ + type: "POST", + url: $form.attr("action"), + data: formData, + success: function(response) { + if (response.success) { + // Update the container with the command output + $("#output-container").html("

Output

" + response.output.join("\n") + "
"); + } else { + console.error(response.error); + } + }, + error: function(xhr, status, error) { + console.error(error); + }, + complete: function() { + $btn.button("reset"); + } + }); + }); +'); +?>