Last active
August 18, 2018 15:29
-
-
Save landsman/25a3cca9b1926afa821d3ba28f86ae2e to your computer and use it in GitHub Desktop.
OpenCart npm-based theme deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace BurdaPraha\composer; | |
use Composer\Script\Event; | |
use Composer\Util\ProcessExecutor; | |
use Symfony\Component\Filesystem\Filesystem; | |
use Symfony\Component\Finder\Finder; | |
use Symfony\Component\Console\Helper\Table; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
class ScriptHandler | |
{ | |
public static $parentTheme = 'burda_parent'; | |
/** | |
* @param $project_root | |
* @return string | |
*/ | |
protected static function getOpenCartRoot($project_root) | |
{ | |
return $project_root . '/upload/'; | |
} | |
/** | |
* @param Event $event | |
*/ | |
public static function clearCache(Event $event) | |
{ | |
$finder = new Finder(); | |
$fs = new Filesystem(); | |
$root = static::getOpenCartRoot(getcwd()); | |
$clear_temp_content = [ | |
'system/storage/cache', | |
'vqmod/vqcache', | |
]; | |
// delete temp content | |
$c = 0; | |
foreach($clear_temp_content as $folder) | |
{ | |
$folders = $finder | |
->files() | |
->in($root . $folder) | |
->depth('0'); | |
foreach ($folders as $key => $item) | |
{ | |
if (false == preg_match('/index.html$/', $item->getRelativePathname()) && | |
false == preg_match('/.gitignore$/', $item->getRelativePathname())) | |
{ | |
if($fs->remove($item)){ | |
++$c; | |
} | |
} | |
} | |
} | |
if($c > 0) { | |
$event->getIO()->write("Removed {$c} cache files"); | |
} | |
} | |
/** | |
* @param Event $event | |
*/ | |
public static function checkRequiredStructure(Event $event) | |
{ | |
$fs = new Filesystem(); | |
$root = static::getOpenCartRoot(getcwd()); | |
$fix_write_chmod = [ | |
'system/storage/cache', | |
'vqmod/vqcache', | |
'system/storage/logs', | |
'system/storage/download', | |
'system/storage/modification', | |
'system/storage/upload', | |
'UniModul/logs/UniModul.log', | |
'UniModul/sync.lock' | |
]; | |
// fix chmods | |
foreach($fix_write_chmod as $folder) | |
{ | |
if(file_exists($root . $folder)) | |
{ | |
$fs->chmod($root . $folder, 0777); | |
$event->getIO()->write("Chmod for '{$folder}' = 0777"); | |
} | |
} | |
} | |
/** | |
* Find all npm-based themes and order from "burda_parent" | |
* @return array | |
*/ | |
public static function findNpmThemes() | |
{ | |
$finder = new Finder(); | |
$root = static::getOpenCartRoot(getcwd()); | |
$folders = $finder | |
->files() | |
->in($root . "catalog/view/theme/") | |
->depth('1'); | |
$themes = []; | |
foreach ($folders as $key => $item) | |
{ | |
if (preg_match('/package.json$/', $item->getRelativePathname())) | |
{ | |
$themes[] = str_replace('/package.json', '', $item->getRelativePathname()); | |
} | |
} | |
// sort by parent theme | |
$themes = array_flip($themes); | |
unset($themes[self::$parentTheme]); | |
$themes = array_flip($themes); | |
array_unshift($themes, self::$parentTheme); | |
return $themes; | |
} | |
/** | |
* Install development dependencies for custom theme and run build tasker job | |
* @param \Composer\Script\Event $event | |
*/ | |
public static function deployThemes(Event $event) | |
{ | |
$themes = self::findNpmThemes(); | |
$root = static::getOpenCartRoot(getcwd()); | |
$output = new ConsoleOutput(); | |
$table = new Table($output); | |
$table->setHeaders(["Themes based on npm"]); | |
foreach($themes as $item) | |
{ | |
$table->addRow([$item]); | |
} | |
if(count($themes) > 0) $table->render(); | |
$executor = new ProcessExecutor($event->getIO()); | |
foreach($themes as $theme) | |
{ | |
$outputEvent = null; | |
$event->getIO()->write('>> Started install assets for theme: ' . $theme); | |
$executor->execute('npm install --silent', $outputEvent, $root . '/catalog/view/theme/' . $theme); | |
$event->getIO()->write($executor->getErrorOutput()); | |
$event->getIO()->write($outputEvent); | |
} | |
} | |
/** | |
* Clear huge folders before send to the production server | |
* @param Event $event | |
*/ | |
public static function clearThemeDeploymentTools(Event $event) | |
{ | |
$outputEvent = null; | |
$executor = new ProcessExecutor($event->getIO()); | |
$themes = self::findNpmThemes(); | |
foreach($themes as $theme) | |
{ | |
$finder = new Finder(); | |
$folders = $finder | |
->directories() | |
->in(static::getOpenCartRoot(getcwd()) . '/catalog/view/theme/' . $theme) | |
->depth('0'); | |
foreach($folders as $folder) | |
{ | |
if (preg_match('/node_modules/', $folder->getRelativePathname())) | |
{ | |
$event->getIO()->write('>> Removing deployment tools in: ' . $theme); | |
if($executor->execute('rm -rf ' . $folder, $outputEvent, $theme)) | |
{ | |
$event->getIO()->write('>> Removed: ' . $folder->getRelativePathname()); | |
} | |
} | |
} | |
} | |
$event->getIO()->write($outputEvent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment