|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Downloads the pre-built WebView helper binary for the current platform. |
| 6 | + * |
| 7 | + * Called automatically via composer post-install/post-update hooks, |
| 8 | + * or manually: php scripts/install-webview-helper.php |
| 9 | + * |
| 10 | + * Falls back to build-from-source instructions if download fails. |
| 11 | + */ |
| 12 | + |
| 13 | +const REPO = 'developersharif/php-gui'; |
| 14 | + |
| 15 | +function getOs(): string |
| 16 | +{ |
| 17 | + return match (PHP_OS_FAMILY) { |
| 18 | + 'Darwin' => 'darwin', |
| 19 | + 'Windows' => 'windows', |
| 20 | + default => 'linux', |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function getArch(): string |
| 25 | +{ |
| 26 | + $arch = php_uname('m'); |
| 27 | + if ($arch === 'AMD64') return 'x86_64'; |
| 28 | + $os = getOs(); |
| 29 | + if ($os === 'darwin' && $arch === 'aarch64') return 'arm64'; |
| 30 | + return $arch; |
| 31 | +} |
| 32 | + |
| 33 | +function getBinaryName(): string |
| 34 | +{ |
| 35 | + $os = getOs(); |
| 36 | + $arch = getArch(); |
| 37 | + $ext = $os === 'windows' ? '.exe' : ''; |
| 38 | + return "webview_helper_{$os}_{$arch}{$ext}"; |
| 39 | +} |
| 40 | + |
| 41 | +function getLibDir(): string |
| 42 | +{ |
| 43 | + // When run from project root (development or post-install) |
| 44 | + $dir = dirname(__DIR__) . '/src/lib/'; |
| 45 | + if (is_dir($dir)) { |
| 46 | + return $dir; |
| 47 | + } |
| 48 | + |
| 49 | + // When installed as a dependency (vendor/developersharif/php-gui/scripts/) |
| 50 | + $dir = dirname(__DIR__) . '/src/lib/'; |
| 51 | + if (!is_dir(dirname($dir))) { |
| 52 | + @mkdir(dirname($dir), 0755, true); |
| 53 | + } |
| 54 | + @mkdir($dir, 0755, true); |
| 55 | + return $dir; |
| 56 | +} |
| 57 | + |
| 58 | +function getLatestReleaseTag(): ?string |
| 59 | +{ |
| 60 | + $url = 'https://api.github.com/repos/' . REPO . '/releases/latest'; |
| 61 | + |
| 62 | + $context = stream_context_create([ |
| 63 | + 'http' => [ |
| 64 | + 'header' => "User-Agent: php-gui-installer\r\n", |
| 65 | + 'timeout' => 15, |
| 66 | + ], |
| 67 | + ]); |
| 68 | + |
| 69 | + $json = @file_get_contents($url, false, $context); |
| 70 | + if ($json === false) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + $data = json_decode($json, true); |
| 75 | + return $data['tag_name'] ?? null; |
| 76 | +} |
| 77 | + |
| 78 | +function downloadBinary(string $tag, string $binaryName, string $destPath): bool |
| 79 | +{ |
| 80 | + $url = sprintf( |
| 81 | + 'https://github.com/%s/releases/download/%s/%s', |
| 82 | + REPO, |
| 83 | + $tag, |
| 84 | + $binaryName |
| 85 | + ); |
| 86 | + |
| 87 | + echo " Downloading from: {$url}\n"; |
| 88 | + |
| 89 | + $context = stream_context_create([ |
| 90 | + 'http' => [ |
| 91 | + 'header' => "User-Agent: php-gui-installer\r\n", |
| 92 | + 'timeout' => 60, |
| 93 | + 'follow_location' => true, |
| 94 | + ], |
| 95 | + ]); |
| 96 | + |
| 97 | + $data = @file_get_contents($url, false, $context); |
| 98 | + if ($data === false) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if (file_put_contents($destPath, $data) === false) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + // Make executable on Unix |
| 107 | + if (PHP_OS_FAMILY !== 'Windows') { |
| 108 | + chmod($destPath, 0755); |
| 109 | + } |
| 110 | + |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +function buildFromSourceInstructions(): string |
| 115 | +{ |
| 116 | + $os = getOs(); |
| 117 | + $msg = " Build from source:\n"; |
| 118 | + $msg .= " cd src/lib/webview_helper && bash build.sh\n"; |
| 119 | + if ($os === 'linux') { |
| 120 | + $msg .= " Requires: sudo apt-get install -y cmake libgtk-3-dev libwebkit2gtk-4.1-dev\n"; |
| 121 | + } elseif ($os === 'darwin') { |
| 122 | + $msg .= " Requires: brew install cmake\n"; |
| 123 | + } else { |
| 124 | + $msg .= " Requires: cmake and Visual Studio Build Tools\n"; |
| 125 | + } |
| 126 | + return $msg; |
| 127 | +} |
| 128 | + |
| 129 | +// ── Main ────────────────────────────────────────────────────────────────── |
| 130 | + |
| 131 | +echo "php-gui: Installing WebView helper binary...\n"; |
| 132 | + |
| 133 | +$binaryName = getBinaryName(); |
| 134 | +$libDir = getLibDir(); |
| 135 | +$destPath = $libDir . $binaryName; |
| 136 | + |
| 137 | +// Skip if binary already exists |
| 138 | +if (file_exists($destPath)) { |
| 139 | + echo " Binary already exists: {$destPath}\n"; |
| 140 | + echo " To force re-download, delete it and run again.\n"; |
| 141 | + exit(0); |
| 142 | +} |
| 143 | + |
| 144 | +echo " Platform: " . getOs() . "/" . getArch() . "\n"; |
| 145 | +echo " Binary: {$binaryName}\n"; |
| 146 | + |
| 147 | +// Fetch latest release tag |
| 148 | +echo " Fetching latest release...\n"; |
| 149 | +$tag = getLatestReleaseTag(); |
| 150 | + |
| 151 | +if ($tag === null) { |
| 152 | + echo " WARNING: Could not fetch release info from GitHub.\n"; |
| 153 | + echo " This may be due to network issues or no releases published yet.\n"; |
| 154 | + echo "\n"; |
| 155 | + echo buildFromSourceInstructions(); |
| 156 | + exit(1); |
| 157 | +} |
| 158 | + |
| 159 | +echo " Release: {$tag}\n"; |
| 160 | + |
| 161 | +// Download |
| 162 | +if (downloadBinary($tag, $binaryName, $destPath)) { |
| 163 | + $size = round(filesize($destPath) / 1024 / 1024, 1); |
| 164 | + echo " Installed: {$destPath} ({$size} MB)\n"; |
| 165 | + exit(0); |
| 166 | +} |
| 167 | + |
| 168 | +// Download failed |
| 169 | +echo " WARNING: Download failed.\n"; |
| 170 | +echo "\n"; |
| 171 | +echo buildFromSourceInstructions(); |
| 172 | +exit(1); |
0 commit comments