Skip to content

Commit

Permalink
Merge pull request #9 from GreenMeteor/develop
Browse files Browse the repository at this point in the history
Fix: Grunt Assets Builder
  • Loading branch information
ArchBlood authored Apr 9, 2024
2 parents 84e6aeb + 24cc37f commit 55632db
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 82 deletions.
73 changes: 45 additions & 28 deletions controllers/GruntController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GruntController extends Controller
/**
* Displays the index page.
*
* @return string
* @return string the rendering result.
*/
public function actionIndex()
{
Expand All @@ -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]);
}

/**
Expand All @@ -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;
}
}
}
14 changes: 9 additions & 5 deletions views/grunt/build-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
?>

<div id="build-assets" class="panel panel-default">
Expand All @@ -19,15 +23,15 @@
<div class="panel-body" data-ui-widget="pjax-container">
<div id="output-container">
<?php if(isset($output)): ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?php endif; ?>
</div>

<?php $form = ActiveForm::begin(['id' => 'run-command-form']); ?>

<div class="form-group">
<?= Html::submitButton('Run Command', ['id' => 'run-command-btn', 'class' => 'btn btn-primary', 'data' => ['pjax' => 1, 'action' => '/composer/grunt/build-assets']]) ?>
<?= Html::submitButton('Run Command', ['id' => 'run-command-btn', 'class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
Expand All @@ -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) {
Expand All @@ -66,4 +70,4 @@
});
});
');
?>
?>
59 changes: 38 additions & 21 deletions views/grunt/build-search.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;

/* @var $this yii\web\View */
/* @var $output array */

$this->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]);
?>

<div id="build-search" class="panel panel-default">
Expand All @@ -17,40 +21,53 @@
</div>
</div>
<div class="panel-body" data-ui-widget="pjax-container">
<?php if (isset($output)): ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?php endif; ?>

<!-- Button to trigger the command -->
<?= Html::button('Run Command', [
'class' => 'btn btn-primary',
'data' => [
'pjax' => 1, // Enable PJAX for the button click
'action' => '/composer/grunt/build-search',
],
]) ?>
<div id="output-container">
<?php if(isset($output)): ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?php endif; ?>
</div>

<?php $form = ActiveForm::begin(['id' => 'run-command-form']); ?>

<div class="form-group">
<?= Html::submitButton('Run Command', ['id' => 'run-command-btn', 'class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
</div>
</div>

<?php
// Register JS to handle the PJAX button click
$this->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("<h2>Output</h2><pre>" + response.output.join("\n") + "</pre>");
} else {
console.error(response.error);
}
},
error: function(xhr, status, error) {
console.error(error);
},
complete: function() {
$btn.button("reset");
}
});
});
');
?>
?>
60 changes: 39 additions & 21 deletions views/grunt/build-theme.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;

/* @var $this yii\web\View */
/* @var $output array */

$this->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]);
?>

<div id="build-theme" class="panel panel-default">
Expand All @@ -16,40 +21,53 @@
</div>
</div>
<div class="panel-body" data-ui-widget="pjax-container">
<?php if(isset($output)): ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?php endif; ?>

<!-- Button to trigger the command -->
<?= Html::button('Run Command', [
'class' => 'btn btn-primary',
'data' => [
'pjax' => 1, // Enable PJAX for the button click
'action' => '/composer/grunt/build-theme',
],
]) ?>
<div id="output-container">
<?php if(isset($output)): ?>
<?= Html::tag('h2', 'Output'); ?>
<?= Html::tag('pre', implode("\n", $output)); ?>
<?php endif; ?>
</div>

<?php $form = ActiveForm::begin(['id' => 'run-command-form']); ?>

<div class="form-group">
<?= Html::submitButton('Run Command', ['id' => 'run-command-btn', 'class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
</div>
</div>

<?php
// Register JS to handle the PJAX button click
$this->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("<h2>Output</h2><pre>" + response.output.join("\n") + "</pre>");
} else {
console.error(response.error);
}
},
error: function(xhr, status, error) {
console.error(error);
},
complete: function() {
$btn.button("reset");
}
});
});
');
?>
?>
Loading

0 comments on commit 55632db

Please sign in to comment.