outputDir = PathManager::getPublicPath() . '/assets/compiled/modules'; echo "๐ŸŽจ Modular CSS Build System - BitReport CRM\n"; echo "==========================================\n\n"; } public function build(): bool { echo "๐Ÿš€ Starting modular CSS compilation...\n\n"; // Initialize AssetManager to discover all module assets AssetManager::initialize(); $assetManager = AssetManager::getInstance(); // Get all registered assets $registeredAssets = $assetManager->getRegisteredAssets(); if (empty($registeredAssets)) { echo "โš ๏ธ No module assets found to compile.\n"; return true; } // Create output directory if (!is_dir($this->outputDir)) { mkdir($this->outputDir, 0755, true); } // Compile assets for each module foreach ($registeredAssets as $moduleName => $assets) { echo "๐Ÿ“ Processing module: {$moduleName}\n"; $this->compileModuleAssets($moduleName, $assets); } // Create symlinks for raw CSS/JS files $this->createAssetSymlinks(); // Generate build report $this->generateBuildReport(); if (empty($this->errors)) { echo "\nโœ… Modular CSS build completed successfully!\n"; echo "๐Ÿ“Š Compiled " . count($this->compiledFiles) . " files\n"; return true; } else { echo "\nโŒ Build completed with errors:\n"; foreach ($this->errors as $error) { echo " โ€ข {$error}\n"; } return false; } } private function compileModuleAssets(string $moduleName, array $assets): void { $moduleOutputDir = $this->outputDir . '/' . $moduleName; // Create module output directories if (!is_dir($moduleOutputDir . '/css')) { mkdir($moduleOutputDir . '/css', 0755, true); } if (!is_dir($moduleOutputDir . '/js')) { mkdir($moduleOutputDir . '/js', 0755, true); } // Compile SCSS files if (isset($assets['scss'])) { foreach ($assets['scss'] as $scss) { if (isset($scss['compile']) && $scss['compile']) { $this->compileScssFile($scss, $moduleName); } } } // Copy CSS files if (isset($assets['css'])) { foreach ($assets['css'] as $css) { $this->copyCssFile($css, $moduleName); } } // Copy JS files if (isset($assets['js'])) { foreach ($assets['js'] as $js) { $this->copyJsFile($js, $moduleName); } } } private function compileScssFile(array $scss, string $moduleName): void { $inputPath = $scss['path']; $filename = basename($inputPath, '.scss') . '.css'; $outputPath = $this->outputDir . '/' . $moduleName . '/css/' . $filename; if (!file_exists($inputPath)) { $this->errors[] = "SCSS file not found: {$inputPath}"; return; } echo " ๐Ÿ”จ Compiling: {$scss['name']}\n"; // Use sass command to compile $command = "npx sass --no-source-map \"{$inputPath}\" \"{$outputPath}\" 2>&1"; $result = shell_exec($command); if (!file_exists($outputPath)) { $this->errors[] = "SCSS compilation failed for {$inputPath}: {$result}"; return; } $this->compiledFiles[] = [ 'type' => 'scss', 'module' => $moduleName, 'input' => $inputPath, 'output' => $outputPath, 'size' => filesize($outputPath), ]; echo " โœ… Compiled to: {$filename}\n"; } private function copyCssFile(array $css, string $moduleName): void { if (!isset($css['path'])) { return; // Skip if no source path } $inputPath = $css['path']; $filename = $css['name']; $outputPath = $this->outputDir . '/' . $moduleName . '/css/' . $filename; if (!file_exists($inputPath)) { $this->errors[] = "CSS file not found: {$inputPath}"; return; } echo " ๐Ÿ“„ Copying: {$filename}\n"; if (copy($inputPath, $outputPath)) { $this->compiledFiles[] = [ 'type' => 'css', 'module' => $moduleName, 'input' => $inputPath, 'output' => $outputPath, 'size' => filesize($outputPath), ]; echo " โœ… Copied to: {$filename}\n"; } else { $this->errors[] = "Failed to copy CSS file: {$inputPath}"; } } private function copyJsFile(array $js, string $moduleName): void { if (!isset($js['path'])) { return; // Skip if no source path } $inputPath = $js['path']; $filename = $js['name']; $outputPath = $this->outputDir . '/' . $moduleName . '/js/' . $filename; if (!file_exists($inputPath)) { $this->errors[] = "JS file not found: {$inputPath}"; return; } echo " ๐Ÿ“œ Copying: {$filename}\n"; if (copy($inputPath, $outputPath)) { $this->compiledFiles[] = [ 'type' => 'js', 'module' => $moduleName, 'input' => $inputPath, 'output' => $outputPath, 'size' => filesize($outputPath), ]; echo " โœ… Copied to: {$filename}\n"; } else { $this->errors[] = "Failed to copy JS file: {$inputPath}"; } } private function createAssetSymlinks(): void { echo "\n๐Ÿ”— Creating asset symlinks...\n"; $modulesPath = PathManager::getModulesPath(); $publicAssetsPath = PathManager::getPublicPath() . '/assets/modules'; if (!is_dir($publicAssetsPath)) { mkdir($publicAssetsPath, 0755, true); } $modules = scandir($modulesPath); foreach ($modules as $module) { if ($module === '.' || $module === '..') { continue; } $moduleAssetsPath = $modulesPath . '/' . $module . '/assets'; $modulePublicPath = $publicAssetsPath . '/' . $module; if (is_dir($moduleAssetsPath) && !is_link($modulePublicPath)) { if (symlink($moduleAssetsPath, $modulePublicPath)) { echo " ๐Ÿ”— Created symlink: modules/{$module}\n"; } else { $this->errors[] = "Failed to create symlink for module: {$module}"; } } } } private function generateBuildReport(): void { $reportPath = PathManager::getStoragePath() . '/logs/modular-css-build-' . date('Y-m-d-H-i-s') . '.json'; $report = [ 'timestamp' => date('c'), 'compiled_files' => $this->compiledFiles, 'errors' => $this->errors, 'summary' => [ 'total_files' => count($this->compiledFiles), 'total_size' => array_sum(array_column($this->compiledFiles, 'size')), 'modules_processed' => count(array_unique(array_column($this->compiledFiles, 'module'))), 'errors_count' => count($this->errors), ], ]; if (!is_dir(dirname($reportPath))) { mkdir(dirname($reportPath), 0755, true); } file_put_contents($reportPath, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); echo "\n๐Ÿ“Š Build report saved: " . basename($reportPath) . "\n"; } public function clean(): void { echo "๐Ÿงน Cleaning compiled assets...\n"; if (is_dir($this->outputDir)) { $this->removeDirectory($this->outputDir); echo "โœ… Cleaned: {$this->outputDir}\n"; } $publicAssetsPath = PathManager::getPublicPath() . '/assets/modules'; if (is_dir($publicAssetsPath)) { $this->removeDirectory($publicAssetsPath); echo "โœ… Cleaned: {$publicAssetsPath}\n"; } } private function removeDirectory(string $dir): void { if (!is_dir($dir)) { return; } $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . '/' . $file; if (is_dir($path)) { $this->removeDirectory($path); } else { unlink($path); } } rmdir($dir); } } // Command line interface if (php_sapi_name() === 'cli') { $builder = new ModularCssBuildSystem(); $command = $argv[1] ?? 'build'; switch ($command) { case 'build': $success = $builder->build(); exit($success ? 0 : 1); case 'clean': $builder->clean(); exit(0); case 'rebuild': $builder->clean(); $success = $builder->build(); exit($success ? 0 : 1); default: echo "Usage: php build-modular-css.php [build|clean|rebuild]\n"; exit(1); } }