|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require("child_process"); |
| 4 | +const { readFileSync, writeFileSync, existsSync } = require("fs"); |
| 5 | +const { join } = require("path"); |
| 6 | +const { createHash } = require("crypto"); |
| 7 | + |
| 8 | +/** |
| 9 | + * Update Homebrew formula with new version and checksums |
| 10 | + * @param {string} version - Version string (e.g., "0.1.0") |
| 11 | + */ |
| 12 | +async function updateHomebrew(version) { |
| 13 | + console.log(`🍺 Updating Homebrew formula to v${version}...`); |
| 14 | + |
| 15 | + // Validate version format |
| 16 | + if (!/^\d+\.\d+\.\d+$/.test(version)) { |
| 17 | + console.error("❌ Invalid version format. Expected: x.y.z (e.g., 1.0.0)"); |
| 18 | + process.exit(1); |
| 19 | + } |
| 20 | + |
| 21 | + const projectRoot = process.cwd(); |
| 22 | + const homebrewPath = join(projectRoot, "homebrew-tap"); |
| 23 | + const formulaPath = join(homebrewPath, "Formula", "occtx.rb"); |
| 24 | + |
| 25 | + // Check if homebrew-tap directory exists |
| 26 | + if (!existsSync(homebrewPath)) { |
| 27 | + console.error("❌ homebrew-tap directory not found. Please clone your homebrew-tap repository first."); |
| 28 | + console.error(" git clone https://github.com/hungthai1401/homebrew-tap.git"); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + // Check if formula file exists |
| 33 | + if (!existsSync(formulaPath)) { |
| 34 | + console.error("❌ Formula file not found:", formulaPath); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + // Define platforms and their binary names |
| 39 | + const platforms = [ |
| 40 | + { os: "macos", arch: "aarch64", name: "macOS ARM64 (Apple Silicon)" }, |
| 41 | + { os: "macos", arch: "x86_64", name: "macOS x86_64 (Intel)" }, |
| 42 | + { os: "linux", arch: "aarch64", name: "Linux ARM64" }, |
| 43 | + { os: "linux", arch: "x86_64", name: "Linux x86_64" }, |
| 44 | + ]; |
| 45 | + |
| 46 | + const checksums = {}; |
| 47 | + |
| 48 | + // Download and calculate checksums for all platforms |
| 49 | + for (const { os, arch, name } of platforms) { |
| 50 | + const binaryName = `occtx-${os}-${arch}`; |
| 51 | + const url = `https://github.com/hungthai1401/occtx/releases/download/v${version}/${binaryName}`; |
| 52 | + console.log(`📥 Calculating checksum for ${name}...`); |
| 53 | + |
| 54 | + try { |
| 55 | + // Download file temporarily |
| 56 | + const tempFile = `/tmp/${binaryName}`; |
| 57 | + execSync(`curl -L -s -o ${tempFile} "${url}"`, { stdio: 'pipe' }); |
| 58 | + |
| 59 | + // Check if download was successful |
| 60 | + if (!existsSync(tempFile)) { |
| 61 | + throw new Error(`Failed to download ${url}`); |
| 62 | + } |
| 63 | + |
| 64 | + // Calculate SHA256 |
| 65 | + const buffer = readFileSync(tempFile); |
| 66 | + const hash = createHash("sha256").update(buffer).digest("hex"); |
| 67 | + checksums[`${os}-${arch}`] = hash; |
| 68 | + |
| 69 | + // Clean up |
| 70 | + execSync(`rm -f ${tempFile}`); |
| 71 | + console.log(`✅ ${name}: ${hash}`); |
| 72 | + } catch (error) { |
| 73 | + console.error(`❌ Failed to download ${name}:`, error.message); |
| 74 | + console.error(` URL: ${url}`); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Read current formula |
| 80 | + console.log("📝 Reading current formula..."); |
| 81 | + const formula = readFileSync(formulaPath, "utf8"); |
| 82 | + |
| 83 | + // Update formula content |
| 84 | + console.log("🔄 Updating formula content..."); |
| 85 | + let updatedFormula = formula; |
| 86 | + |
| 87 | + // Update version |
| 88 | + updatedFormula = updatedFormula.replace( |
| 89 | + /version "[^"]+"/g, |
| 90 | + `version "${version}"` |
| 91 | + ); |
| 92 | + |
| 93 | + // Update URLs for each platform |
| 94 | + updatedFormula = updatedFormula.replace( |
| 95 | + /url "https:\/\/github\.com\/hungthai1401\/occtx\/releases\/download\/v[^\/]+\/occtx-macos-aarch64"/g, |
| 96 | + `url "https://github.com/hungthai1401/occtx/releases/download/v${version}/occtx-macos-aarch64"` |
| 97 | + ); |
| 98 | + |
| 99 | + updatedFormula = updatedFormula.replace( |
| 100 | + /url "https:\/\/github\.com\/hungthai1401\/occtx\/releases\/download\/v[^\/]+\/occtx-macos-x86_64"/g, |
| 101 | + `url "https://github.com/hungthai1401/occtx/releases/download/v${version}/occtx-macos-x86_64"` |
| 102 | + ); |
| 103 | + |
| 104 | + updatedFormula = updatedFormula.replace( |
| 105 | + /url "https:\/\/github\.com\/hungthai1401\/occtx\/releases\/download\/v[^\/]+\/occtx-linux-aarch64"/g, |
| 106 | + `url "https://github.com/hungthai1401/occtx/releases/download/v${version}/occtx-linux-aarch64"` |
| 107 | + ); |
| 108 | + |
| 109 | + updatedFormula = updatedFormula.replace( |
| 110 | + /url "https:\/\/github\.com\/hungthai1401\/occtx\/releases\/download\/v[^\/]+\/occtx-linux-x86_64"/g, |
| 111 | + `url "https://github.com/hungthai1401/occtx/releases/download/v${version}/occtx-linux-x86_64"` |
| 112 | + ); |
| 113 | + |
| 114 | + // Update SHA256 checksums using a more precise approach |
| 115 | + console.log("🔐 Updating SHA256 checksums..."); |
| 116 | + |
| 117 | + // Split content into lines for precise replacement |
| 118 | + const lines = updatedFormula.split('\n'); |
| 119 | + let inMacosSection = false; |
| 120 | + let inLinuxSection = false; |
| 121 | + let inArmBlock = false; |
| 122 | + |
| 123 | + for (let i = 0; i < lines.length; i++) { |
| 124 | + const line = lines[i]; |
| 125 | + |
| 126 | + // Track which section we're in |
| 127 | + if (line.includes('on_macos do')) { |
| 128 | + inMacosSection = true; |
| 129 | + inLinuxSection = false; |
| 130 | + } else if (line.includes('on_linux do')) { |
| 131 | + inLinuxSection = true; |
| 132 | + inMacosSection = false; |
| 133 | + } else if (line.trim() === 'end' && (inMacosSection || inLinuxSection)) { |
| 134 | + // Check if this is the end of the platform section (not just an if block) |
| 135 | + let foundPlatformBlock = false; |
| 136 | + for (let j = Math.max(0, i - 10); j < i; j++) { |
| 137 | + if (lines[j].includes('on_macos do') || lines[j].includes('on_linux do')) { |
| 138 | + foundPlatformBlock = true; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + if (foundPlatformBlock && !line.includes(' ')) { // Top-level end |
| 143 | + inMacosSection = false; |
| 144 | + inLinuxSection = false; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Track ARM vs x86 blocks |
| 149 | + if (line.includes('if Hardware::CPU.arm?')) { |
| 150 | + inArmBlock = true; |
| 151 | + } else if (line.includes('else') && (inMacosSection || inLinuxSection)) { |
| 152 | + inArmBlock = false; |
| 153 | + } |
| 154 | + |
| 155 | + // Update SHA256 hashes |
| 156 | + if (line.includes('sha256') && line.includes('"')) { |
| 157 | + if (inMacosSection && inArmBlock) { |
| 158 | + lines[i] = line.replace(/sha256 "[^"]*"/, `sha256 "${checksums['macos-aarch64']}"`); |
| 159 | + } else if (inMacosSection && !inArmBlock) { |
| 160 | + lines[i] = line.replace(/sha256 "[^"]*"/, `sha256 "${checksums['macos-x86_64']}"`); |
| 161 | + } else if (inLinuxSection && inArmBlock) { |
| 162 | + lines[i] = line.replace(/sha256 "[^"]*"/, `sha256 "${checksums['linux-aarch64']}"`); |
| 163 | + } else if (inLinuxSection && !inArmBlock) { |
| 164 | + lines[i] = line.replace(/sha256 "[^"]*"/, `sha256 "${checksums['linux-x86_64']}"`); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + updatedFormula = lines.join('\n'); |
| 170 | + |
| 171 | + // Write updated formula |
| 172 | + writeFileSync(formulaPath, updatedFormula); |
| 173 | + console.log("✅ Updated Formula/occtx.rb"); |
| 174 | + |
| 175 | + // Show the updated content |
| 176 | + console.log("\n📄 Updated formula content:"); |
| 177 | + console.log("=" .repeat(50)); |
| 178 | + console.log(updatedFormula); |
| 179 | + console.log("=" .repeat(50)); |
| 180 | + |
| 181 | + // Commit and push to homebrew-tap |
| 182 | + console.log("\n🚀 Committing and pushing changes..."); |
| 183 | + try { |
| 184 | + // Configure git if needed |
| 185 | + try { |
| 186 | + execSync('git config user.name', { cwd: homebrewPath, stdio: 'pipe' }); |
| 187 | + } catch { |
| 188 | + execSync('git config user.name "GitHub Actions"', { cwd: homebrewPath }); |
| 189 | + execSync('git config user.email "actions@github.com"', { cwd: homebrewPath }); |
| 190 | + } |
| 191 | + |
| 192 | + // Add changes |
| 193 | + execSync("git add .", { cwd: homebrewPath }); |
| 194 | + |
| 195 | + // Check if there are changes to commit |
| 196 | + try { |
| 197 | + execSync("git diff --cached --exit-code", { cwd: homebrewPath, stdio: 'pipe' }); |
| 198 | + console.log("ℹ️ No changes to commit"); |
| 199 | + return; |
| 200 | + } catch { |
| 201 | + // There are changes, continue with commit |
| 202 | + } |
| 203 | + |
| 204 | + // Commit with detailed message |
| 205 | + const commitMessage = `Update occtx to version ${version} |
| 206 | +
|
| 207 | +- Version: ${version} |
| 208 | +- macOS ARM64: ${checksums['macos-aarch64']} |
| 209 | +- macOS x86_64: ${checksums['macos-x86_64']} |
| 210 | +- Linux ARM64: ${checksums['linux-aarch64']} |
| 211 | +- Linux x86_64: ${checksums['linux-x86_64']} |
| 212 | +- Release: https://github.com/hungthai1401/occtx/releases/tag/v${version} |
| 213 | +
|
| 214 | +Auto-updated by GitHub Actions`; |
| 215 | + |
| 216 | + execSync(`git commit -m "${commitMessage}"`, { cwd: homebrewPath }); |
| 217 | + execSync("git push origin main", { cwd: homebrewPath }); |
| 218 | + console.log("✅ Pushed to homebrew-tap repository"); |
| 219 | + } catch (error) { |
| 220 | + console.error("❌ Failed to update homebrew-tap:", error.message); |
| 221 | + process.exit(1); |
| 222 | + } |
| 223 | + |
| 224 | + console.log(`\n🍺 Homebrew formula updated to v${version} successfully! 🎉`); |
| 225 | + |
| 226 | + // Summary |
| 227 | + console.log("\n📊 Summary:"); |
| 228 | + console.log(`Version: ${version}`); |
| 229 | + console.log(`macOS ARM64 SHA: ${checksums['macos-aarch64']}`); |
| 230 | + console.log(`macOS x86_64 SHA: ${checksums['macos-x86_64']}`); |
| 231 | + console.log(`Linux ARM64 SHA: ${checksums['linux-aarch64']}`); |
| 232 | + console.log(`Linux x86_64 SHA: ${checksums['linux-x86_64']}`); |
| 233 | +} |
| 234 | + |
| 235 | +// Export for use in other scripts |
| 236 | +module.exports = { updateHomebrew }; |
| 237 | + |
| 238 | +// Allow direct execution |
| 239 | +if (require.main === module) { |
| 240 | + const version = process.argv[2] || process.env.OCCTX_VERSION; |
| 241 | + if (!version) { |
| 242 | + console.error("❌ Version is required!"); |
| 243 | + console.error(""); |
| 244 | + console.error("Usage:"); |
| 245 | + console.error(" node .github/workflows/scripts/update-homebrew.js <version>"); |
| 246 | + console.error(" OCCTX_VERSION=1.0.0 node .github/workflows/scripts/update-homebrew.js"); |
| 247 | + console.error(""); |
| 248 | + console.error("Examples:"); |
| 249 | + console.error(" node .github/workflows/scripts/update-homebrew.js 1.0.0"); |
| 250 | + console.error(" OCCTX_VERSION=1.0.0 node .github/workflows/scripts/update-homebrew.js"); |
| 251 | + process.exit(1); |
| 252 | + } |
| 253 | + |
| 254 | + updateHomebrew(version).catch((error) => { |
| 255 | + console.error("❌ Script failed:", error.message); |
| 256 | + process.exit(1); |
| 257 | + }); |
| 258 | +} |
0 commit comments