|
| 1 | +import { execSync } from 'node:child_process' |
| 2 | +import { existsSync } from 'node:fs' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { homedir, platform } from 'node:os' |
| 5 | +import { logUniqueMessage } from '../logging' |
| 6 | +import { install } from '../install' |
| 7 | + |
| 8 | +export interface DependencyPaths { |
| 9 | + includeDirs: string[] |
| 10 | + libDirs: string[] |
| 11 | + pkgConfigDirs: string[] |
| 12 | +} |
| 13 | + |
| 14 | +export class BuildDependencyManager { |
| 15 | + private launchpadDir: string |
| 16 | + private currentPlatform: string |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.currentPlatform = platform() |
| 20 | + this.launchpadDir = join(homedir(), '.local') |
| 21 | + } |
| 22 | + |
| 23 | + async installBuildDependencies(): Promise<void> { |
| 24 | + if (this.currentPlatform === 'darwin') { |
| 25 | + await this.installMacOSDependencies() |
| 26 | + } else if (this.currentPlatform === 'linux') { |
| 27 | + await this.installLinuxDependencies() |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private async installMacOSDependencies(): Promise<void> { |
| 32 | + logUniqueMessage('Installing macOS build dependencies via Launchpad...') |
| 33 | + |
| 34 | + const launchpadPackages = [ |
| 35 | + 'gnu.org/autoconf', |
| 36 | + 'gnu.org/automake', |
| 37 | + 'gnu.org/libtool', |
| 38 | + 'gnu.org/bison', |
| 39 | + 're2c.org', |
| 40 | + 'freedesktop.org/pkg-config', |
| 41 | + 'xmlsoft.org/libxml2', |
| 42 | + 'openssl.org', |
| 43 | + 'curl.se', |
| 44 | + 'libzip.org', |
| 45 | + 'zlib.net', |
| 46 | + 'github.com/kkos/oniguruma', |
| 47 | + 'sqlite.org', |
| 48 | + 'postgresql.org', |
| 49 | + 'mysql.com' |
| 50 | + ] |
| 51 | + |
| 52 | + try { |
| 53 | + // Install packages via Launchpad |
| 54 | + const installDir = this.launchpadDir |
| 55 | + |
| 56 | + for (const pkg of launchpadPackages) { |
| 57 | + logUniqueMessage(`Installing ${pkg}...`) |
| 58 | + await install([pkg], installDir) |
| 59 | + } |
| 60 | + |
| 61 | + logUniqueMessage('✅ macOS dependencies installed successfully') |
| 62 | + } catch (error) { |
| 63 | + throw new Error(`Failed to install macOS dependencies: ${error}`) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private async installLinuxDependencies(): Promise<void> { |
| 68 | + logUniqueMessage('Installing Linux build dependencies via Launchpad...') |
| 69 | + |
| 70 | + const launchpadPackages = [ |
| 71 | + 'gnu.org/gcc', |
| 72 | + 'gnu.org/autoconf', |
| 73 | + 'gnu.org/automake', |
| 74 | + 'gnu.org/libtool', |
| 75 | + 'gnu.org/bison', |
| 76 | + 're2c.org', |
| 77 | + 'freedesktop.org/pkg-config', |
| 78 | + 'xmlsoft.org/libxml2', |
| 79 | + 'openssl.org', |
| 80 | + 'curl.se', |
| 81 | + 'libzip.org', |
| 82 | + 'zlib.net', |
| 83 | + 'github.com/kkos/oniguruma', |
| 84 | + 'sqlite.org', |
| 85 | + 'postgresql.org', |
| 86 | + 'mysql.com' |
| 87 | + ] |
| 88 | + |
| 89 | + try { |
| 90 | + // Install packages via Launchpad |
| 91 | + const installDir = this.launchpadDir |
| 92 | + |
| 93 | + for (const pkg of launchpadPackages) { |
| 94 | + logUniqueMessage(`Installing ${pkg}...`) |
| 95 | + await install([pkg], installDir) |
| 96 | + } |
| 97 | + |
| 98 | + logUniqueMessage('✅ Linux dependencies installed successfully') |
| 99 | + } catch (error) { |
| 100 | + throw new Error(`Failed to install Linux dependencies: ${error}`) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + getDependencyPaths(): DependencyPaths { |
| 105 | + if (this.currentPlatform === 'darwin') { |
| 106 | + return this.findLaunchpadPaths() |
| 107 | + } else if (this.currentPlatform === 'linux') { |
| 108 | + return this.getLinuxDependencyPaths() |
| 109 | + } |
| 110 | + |
| 111 | + return { includeDirs: [], libDirs: [], pkgConfigDirs: [] } |
| 112 | + } |
| 113 | + |
| 114 | + private findLaunchpadPaths(): DependencyPaths { |
| 115 | + const launchpadDir = this.launchpadDir |
| 116 | + const includeDirs: string[] = [] |
| 117 | + const libDirs: string[] = [] |
| 118 | + const pkgConfigDirs: string[] = [] |
| 119 | + |
| 120 | + if (!existsSync(launchpadDir)) { |
| 121 | + logUniqueMessage(`⚠️ Launchpad directory not found: ${launchpadDir}`) |
| 122 | + return { includeDirs, libDirs, pkgConfigDirs } |
| 123 | + } |
| 124 | + |
| 125 | + try { |
| 126 | + // Find all package directories in Launchpad |
| 127 | + const entries = execSync(`find "${launchpadDir}" -maxdepth 3 -type d -name "v*"`, { encoding: 'utf8' }) |
| 128 | + .split('\n') |
| 129 | + .filter(Boolean) |
| 130 | + |
| 131 | + for (const entry of entries) { |
| 132 | + // Add include directories |
| 133 | + const includeDir = join(entry, 'include') |
| 134 | + if (existsSync(includeDir)) { |
| 135 | + includeDirs.push(includeDir) |
| 136 | + } |
| 137 | + |
| 138 | + // Add lib directories |
| 139 | + const libDir = join(entry, 'lib') |
| 140 | + if (existsSync(libDir)) { |
| 141 | + libDirs.push(libDir) |
| 142 | + } |
| 143 | + |
| 144 | + // Add pkgconfig directories |
| 145 | + const pkgConfigDir = join(entry, 'lib', 'pkgconfig') |
| 146 | + if (existsSync(pkgConfigDir)) { |
| 147 | + pkgConfigDirs.push(pkgConfigDir) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + logUniqueMessage(`Found ${includeDirs.length} include dirs, ${libDirs.length} lib dirs, ${pkgConfigDirs.length} pkgconfig dirs`) |
| 152 | + |
| 153 | + } catch (error) { |
| 154 | + logUniqueMessage(`⚠️ Failed to scan Launchpad directories: ${error}`) |
| 155 | + } |
| 156 | + |
| 157 | + return { includeDirs, libDirs, pkgConfigDirs } |
| 158 | + } |
| 159 | + |
| 160 | + private getLinuxDependencyPaths(): DependencyPaths { |
| 161 | + return { |
| 162 | + includeDirs: ['/usr/include', '/usr/local/include'], |
| 163 | + libDirs: ['/usr/lib', '/usr/local/lib', '/usr/lib/x86_64-linux-gnu'], |
| 164 | + pkgConfigDirs: ['/usr/lib/pkgconfig', '/usr/local/lib/pkgconfig', '/usr/lib/x86_64-linux-gnu/pkgconfig'] |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + findPostgresPrefix(): string | null { |
| 169 | + const possiblePaths = [ |
| 170 | + join(this.launchpadDir, 'postgresql.org'), |
| 171 | + '/usr/lib/postgresql/16', |
| 172 | + '/usr/lib/postgresql/15', |
| 173 | + '/usr/lib/postgresql/14', |
| 174 | + '/usr/lib/postgresql/13', |
| 175 | + '/usr/local/pgsql' |
| 176 | + ] |
| 177 | + |
| 178 | + for (const path of possiblePaths) { |
| 179 | + if (existsSync(path)) { |
| 180 | + // For Launchpad installations, find the latest version |
| 181 | + if (path.includes('postgresql.org')) { |
| 182 | + try { |
| 183 | + const versions = execSync(`find "${path}" -maxdepth 1 -type d -name "v*" | sort -V | tail -1`, { encoding: 'utf8' }).trim() |
| 184 | + if (versions && existsSync(versions)) { |
| 185 | + return versions |
| 186 | + } |
| 187 | + } catch (error) { |
| 188 | + continue |
| 189 | + } |
| 190 | + } |
| 191 | + return path |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return null |
| 196 | + } |
| 197 | + |
| 198 | + findLibraryPrefix(library: string): string | null { |
| 199 | + // Check Launchpad first |
| 200 | + const launchpadPath = join(this.launchpadDir, library) |
| 201 | + if (existsSync(launchpadPath)) { |
| 202 | + // Find the latest version directory |
| 203 | + try { |
| 204 | + const versions = execSync(`find "${launchpadPath}" -maxdepth 1 -type d -name "v*" | sort -V | tail -1`, { encoding: 'utf8' }).trim() |
| 205 | + if (versions && existsSync(versions)) { |
| 206 | + return versions |
| 207 | + } |
| 208 | + } catch (error) { |
| 209 | + // Continue to system paths |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Fallback to system paths |
| 214 | + const systemPaths = [ |
| 215 | + `/usr/local/opt/${library}`, |
| 216 | + `/usr/lib/${library}`, |
| 217 | + `/usr/local/lib/${library}`, |
| 218 | + `/opt/${library}` |
| 219 | + ] |
| 220 | + |
| 221 | + for (const path of systemPaths) { |
| 222 | + if (existsSync(path)) { |
| 223 | + return path |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return null |
| 228 | + } |
| 229 | +} |
0 commit comments