|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Build script for Component Migration Manager |
| 4 | + * |
| 5 | + * @package CCM |
| 6 | + * @author Reem |
| 7 | + * @copyright 2025 Reem |
| 8 | + * @license GNU General Public License version 2 or later |
| 9 | + */ |
| 10 | + |
| 11 | +class ComponentBuilder |
| 12 | +{ |
| 13 | + private $version = '1.0.0'; |
| 14 | + private $packageName = 'com_ccm'; |
| 15 | + private $buildDir = 'dist'; |
| 16 | + private $sourceDir = 'src'; |
| 17 | + |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + echo "Component Migration Manager Build Script\n"; |
| 21 | + echo "========================================\n"; |
| 22 | + } |
| 23 | + |
| 24 | + public function build() |
| 25 | + { |
| 26 | + // Create build directory |
| 27 | + $this->createBuildDir(); |
| 28 | + |
| 29 | + // Copy component files |
| 30 | + $this->copyComponentFiles(); |
| 31 | + |
| 32 | + // Process version replacement |
| 33 | + $this->replaceVersions(); |
| 34 | + |
| 35 | + // Create ZIP package |
| 36 | + $this->createPackage(); |
| 37 | + |
| 38 | + echo "\nBuild completed successfully!\n"; |
| 39 | + echo "Package: {$this->buildDir}/{$this->packageName}-{$this->version}.zip\n"; |
| 40 | + } |
| 41 | + |
| 42 | + private function createBuildDir() |
| 43 | + { |
| 44 | + $buildPath = "{$this->buildDir}/{$this->packageName}-{$this->version}"; |
| 45 | + |
| 46 | + if (is_dir($buildPath)) { |
| 47 | + $this->deleteDirectory($buildPath); |
| 48 | + } |
| 49 | + |
| 50 | + if (!is_dir($this->buildDir)) { |
| 51 | + mkdir($this->buildDir, 0755, true); |
| 52 | + } |
| 53 | + |
| 54 | + mkdir($buildPath, 0755, true); |
| 55 | + |
| 56 | + echo "Created build directory: $buildPath\n"; |
| 57 | + } |
| 58 | + |
| 59 | + private function copyComponentFiles() |
| 60 | + { |
| 61 | + $buildPath = "{$this->buildDir}/{$this->packageName}-{$this->version}"; |
| 62 | + |
| 63 | + // Copy component manifest file (rename to ccm.xml for component) |
| 64 | + if (file_exists('component_manifest.xml')) { |
| 65 | + copy('component_manifest.xml', "$buildPath/ccm.xml"); |
| 66 | + echo "Copied component manifest as ccm.xml\n"; |
| 67 | + } elseif (file_exists('manifest.xml')) { |
| 68 | + copy('manifest.xml', "$buildPath/manifest.xml"); |
| 69 | + echo "Copied manifest.xml\n"; |
| 70 | + } |
| 71 | + |
| 72 | + // Copy package script if it exists |
| 73 | + if (file_exists('script.php')) { |
| 74 | + copy('script.php', "$buildPath/script.php"); |
| 75 | + echo "Copied script.php\n"; |
| 76 | + } |
| 77 | + |
| 78 | + // Copy administrator component |
| 79 | + $adminSource = "{$this->sourceDir}/administrator/components/com_ccm"; |
| 80 | + $adminDest = "$buildPath/administrator/components/com_ccm"; |
| 81 | + |
| 82 | + if (is_dir($adminSource)) { |
| 83 | + $this->copyDirectory($adminSource, $adminDest); |
| 84 | + echo "Copied administrator component\n"; |
| 85 | + } |
| 86 | + |
| 87 | + // Copy site component if it exists |
| 88 | + $siteSource = "{$this->sourceDir}/components/com_ccm"; |
| 89 | + $siteDest = "$buildPath/components/com_ccm"; |
| 90 | + |
| 91 | + if (is_dir($siteSource)) { |
| 92 | + $this->copyDirectory($siteSource, $siteDest); |
| 93 | + echo "Copied site component\n"; |
| 94 | + } |
| 95 | + |
| 96 | + // Copy media files if they exist |
| 97 | + $mediaSource = "{$this->sourceDir}/media/com_ccm"; |
| 98 | + $mediaDest = "$buildPath/media/com_ccm"; |
| 99 | + |
| 100 | + if (is_dir($mediaSource)) { |
| 101 | + $this->copyDirectory($mediaSource, $mediaDest); |
| 102 | + echo "Copied media files\n"; |
| 103 | + } |
| 104 | + |
| 105 | + // Copy language files |
| 106 | + $this->copyLanguageFiles($buildPath); |
| 107 | + } |
| 108 | + |
| 109 | + private function copyLanguageFiles($buildPath) |
| 110 | + { |
| 111 | + // Administrator language files |
| 112 | + $adminLangSource = "{$this->sourceDir}/administrator/language"; |
| 113 | + $adminLangDest = "$buildPath/administrator/language"; |
| 114 | + |
| 115 | + if (is_dir($adminLangSource)) { |
| 116 | + $this->copyDirectory($adminLangSource, $adminLangDest); |
| 117 | + echo "Copied administrator language files\n"; |
| 118 | + } |
| 119 | + |
| 120 | + // Site language files |
| 121 | + $siteLangSource = "{$this->sourceDir}/language"; |
| 122 | + $siteLangDest = "$buildPath/language"; |
| 123 | + |
| 124 | + if (is_dir($siteLangSource)) { |
| 125 | + $this->copyDirectory($siteLangSource, $siteLangDest); |
| 126 | + echo "Copied site language files\n"; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private function replaceVersions() |
| 131 | + { |
| 132 | + $buildPath = "{$this->buildDir}/{$this->packageName}-{$this->version}"; |
| 133 | + |
| 134 | + // Find all PHP and XML files and replace version placeholders |
| 135 | + $iterator = new RecursiveIteratorIterator( |
| 136 | + new RecursiveDirectoryIterator($buildPath), |
| 137 | + RecursiveIteratorIterator::LEAVES_ONLY |
| 138 | + ); |
| 139 | + |
| 140 | + foreach ($iterator as $file) { |
| 141 | + if ($file->isFile() && preg_match('/\.(php|xml)$/', $file->getFilename())) { |
| 142 | + $content = file_get_contents($file->getPathname()); |
| 143 | + $content = str_replace('__DEPLOY_VERSION__', $this->version, $content); |
| 144 | + file_put_contents($file->getPathname(), $content); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + echo "Replaced version placeholders\n"; |
| 149 | + } |
| 150 | + |
| 151 | + private function createPackage() |
| 152 | + { |
| 153 | + $buildPath = "{$this->buildDir}/{$this->packageName}-{$this->version}"; |
| 154 | + $zipFile = "{$this->buildDir}/{$this->packageName}-{$this->version}.zip"; |
| 155 | + |
| 156 | + // Remove existing zip file if it exists |
| 157 | + if (file_exists($zipFile)) { |
| 158 | + unlink($zipFile); |
| 159 | + } |
| 160 | + |
| 161 | + // Use system zip command |
| 162 | + $command = "cd '$buildPath' && zip -r '../{$this->packageName}-{$this->version}.zip' . -x '*.DS_Store' '*.git*'"; |
| 163 | + $result = exec($command, $output, $returnCode); |
| 164 | + |
| 165 | + if ($returnCode !== 0) { |
| 166 | + throw new Exception("Failed to create ZIP package. Command: $command"); |
| 167 | + } |
| 168 | + |
| 169 | + echo "Created ZIP package: $zipFile\n"; |
| 170 | + } |
| 171 | + |
| 172 | + private function copyDirectory($source, $dest) |
| 173 | + { |
| 174 | + if (!is_dir($dest)) { |
| 175 | + mkdir($dest, 0755, true); |
| 176 | + } |
| 177 | + |
| 178 | + $iterator = new RecursiveIteratorIterator( |
| 179 | + new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
| 180 | + RecursiveIteratorIterator::SELF_FIRST |
| 181 | + ); |
| 182 | + |
| 183 | + foreach ($iterator as $item) { |
| 184 | + $relativePath = substr($item->getPathname(), strlen($source) + 1); |
| 185 | + $destPath = $dest . DIRECTORY_SEPARATOR . $relativePath; |
| 186 | + |
| 187 | + if ($item->isDir()) { |
| 188 | + if (!is_dir($destPath)) { |
| 189 | + mkdir($destPath, 0755, true); |
| 190 | + } |
| 191 | + } else { |
| 192 | + copy($item, $destPath); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private function deleteDirectory($dir) |
| 198 | + { |
| 199 | + if (!is_dir($dir)) { |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + $iterator = new RecursiveIteratorIterator( |
| 204 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
| 205 | + RecursiveIteratorIterator::CHILD_FIRST |
| 206 | + ); |
| 207 | + |
| 208 | + foreach ($iterator as $file) { |
| 209 | + if ($file->isDir()) { |
| 210 | + rmdir($file->getPathname()); |
| 211 | + } else { |
| 212 | + unlink($file->getPathname()); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + rmdir($dir); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +// Run the build |
| 221 | +try { |
| 222 | + $builder = new ComponentBuilder(); |
| 223 | + $builder->build(); |
| 224 | +} catch (Exception $e) { |
| 225 | + echo "Build failed: " . $e->getMessage() . "\n"; |
| 226 | + exit(1); |
| 227 | +} |
0 commit comments