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 +} 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]); ?>
" + 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 +?> 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]); ?>
" + 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 +?> 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]); ?>
" + response.output.join("\n") + ""); + } else { + console.error(response.error); + } + }, + error: function(xhr, status, error) { + console.error(error); + }, + complete: function() { + $btn.button("reset"); + } + }); + }); +'); +?>