|
| 1 | +// |
| 2 | +// PackageManagerFactory.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Abe Malla on 2/3/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// Factory for creating the appropriate package manager based on installation method |
| 11 | +final class PackageManagerFactory { |
| 12 | + let installationDirectory: URL |
| 13 | + |
| 14 | + init(installationDirectory: URL) { |
| 15 | + self.installationDirectory = installationDirectory |
| 16 | + } |
| 17 | + |
| 18 | + /// Create the appropriate package manager for the given installation method |
| 19 | + func createPackageManager(for method: InstallationMethod) -> PackageManagerProtocol? { |
| 20 | + switch method.packageManagerType { |
| 21 | + case .npm: |
| 22 | + return NPMPackageManager(installationDirectory: installationDirectory) |
| 23 | + case .cargo: |
| 24 | + return CargoPackageManager(installationDirectory: installationDirectory) |
| 25 | + case .pip: |
| 26 | + return PipPackageManager(installationDirectory: installationDirectory) |
| 27 | + case .golang: |
| 28 | + return GolangPackageManager(installationDirectory: installationDirectory) |
| 29 | + case .nuget, .opam, .customBuild, .gem: |
| 30 | + // TODO: IMPLEMENT OTHER PACKAGE MANAGERS |
| 31 | + return nil |
| 32 | + case .github: |
| 33 | + return createPackageManagerFromGithub(for: method) |
| 34 | + case .none: |
| 35 | + return nil |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Parse a registry entry and create the appropriate installation method |
| 40 | + static func parseRegistryEntry(_ entry: [String: Any]) -> InstallationMethod? { |
| 41 | + guard let source = entry["source"] as? [String: Any], |
| 42 | + let sourceId = source["id"] as? String else { |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + let buildInstructions = source["build"] as? [[String: Any]] |
| 47 | + |
| 48 | + // Detect the build tool from the registry entry |
| 49 | + var buildTool: String? |
| 50 | + if let bin = entry["bin"] as? [String: String] { |
| 51 | + let binValues = Array(bin.values) |
| 52 | + if !binValues.isEmpty { |
| 53 | + let value = binValues[0] |
| 54 | + if value.hasPrefix("cargo:") { |
| 55 | + buildTool = "cargo" |
| 56 | + } else if value.hasPrefix("npm:") { |
| 57 | + buildTool = "npm" |
| 58 | + } else if value.hasPrefix("pypi:") { |
| 59 | + buildTool = "pip" |
| 60 | + } else if value.hasPrefix("gem:") { |
| 61 | + buildTool = "gem" |
| 62 | + } else if value.hasPrefix("golang:") { |
| 63 | + buildTool = "golang" |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + var method = PackageSourceParser.parse(sourceId, buildInstructions: buildInstructions) |
| 69 | + |
| 70 | + if let buildTool = buildTool { |
| 71 | + switch method { |
| 72 | + case .standardPackage(var source): |
| 73 | + var options = source.options |
| 74 | + options["buildTool"] = buildTool |
| 75 | + source = PackageSource( |
| 76 | + sourceId: source.sourceId, |
| 77 | + type: source.type, |
| 78 | + name: source.name, |
| 79 | + version: source.version, |
| 80 | + subpath: source.subpath, |
| 81 | + repositoryUrl: source.repositoryUrl, |
| 82 | + gitReference: source.gitReference, |
| 83 | + options: options |
| 84 | + ) |
| 85 | + method = .standardPackage(source: source) |
| 86 | + case .sourceBuild(var source, let instructions): |
| 87 | + var options = source.options |
| 88 | + options["buildTool"] = buildTool |
| 89 | + source = PackageSource( |
| 90 | + sourceId: source.sourceId, |
| 91 | + type: source.type, |
| 92 | + name: source.name, |
| 93 | + version: source.version, |
| 94 | + subpath: source.subpath, |
| 95 | + repositoryUrl: source.repositoryUrl, |
| 96 | + gitReference: source.gitReference, |
| 97 | + options: options |
| 98 | + ) |
| 99 | + method = .sourceBuild(source: source, buildInstructions: instructions) |
| 100 | + case .binaryDownload(var source, let url): |
| 101 | + var options = source.options |
| 102 | + options["buildTool"] = buildTool |
| 103 | + source = PackageSource( |
| 104 | + sourceId: source.sourceId, |
| 105 | + type: source.type, |
| 106 | + name: source.name, |
| 107 | + version: source.version, |
| 108 | + subpath: source.subpath, |
| 109 | + repositoryUrl: source.repositoryUrl, |
| 110 | + gitReference: source.gitReference, |
| 111 | + options: options |
| 112 | + ) |
| 113 | + method = .binaryDownload(source: source, url: url) |
| 114 | + case .unknown: |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + return method |
| 119 | + } |
| 120 | + |
| 121 | + /// Install a package from a registry entry |
| 122 | + func installFromRegistryEntry(_ entry: [String: Any]) async throws { |
| 123 | + guard let method = PackageManagerFactory.parseRegistryEntry(entry), |
| 124 | + let manager = createPackageManager(for: method) else { |
| 125 | + throw PackageManagerError.invalidConfiguration |
| 126 | + } |
| 127 | + try await manager.install(method: method) |
| 128 | + } |
| 129 | + |
| 130 | + /// Install a package from a source ID string |
| 131 | + func installFromSourceID(_ sourceID: String) async throws { |
| 132 | + let method = PackageSourceParser.parse(sourceID) |
| 133 | + guard let manager = createPackageManager(for: method) else { |
| 134 | + throw PackageManagerError.packageManagerNotInstalled |
| 135 | + } |
| 136 | + try await manager.install(method: method) |
| 137 | + } |
| 138 | + |
| 139 | + private func createPackageManagerFromGithub(for method: InstallationMethod) -> PackageManagerProtocol? { |
| 140 | + if case let .sourceBuild(source, instructions) = method { |
| 141 | + if let buildTool = source.options["buildTool"] { |
| 142 | + switch buildTool { |
| 143 | + case "cargo": return CargoPackageManager(installationDirectory: installationDirectory) |
| 144 | + case "npm": return NPMPackageManager(installationDirectory: installationDirectory) |
| 145 | + case "pip": return PipPackageManager(installationDirectory: installationDirectory) |
| 146 | + case "golang": return GolangPackageManager(installationDirectory: installationDirectory) |
| 147 | + default: break |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // If no buildTool option, try to determine from build instructions |
| 152 | + for instruction in instructions { |
| 153 | + for command in instruction.commands { |
| 154 | + if command.contains("cargo ") { |
| 155 | + return CargoPackageManager(installationDirectory: installationDirectory) |
| 156 | + } else if command.contains("npm ") { |
| 157 | + return NPMPackageManager(installationDirectory: installationDirectory) |
| 158 | + } else if command.contains("pip ") || command.contains("python ") { |
| 159 | + return PipPackageManager(installationDirectory: installationDirectory) |
| 160 | + } else if command.contains("go ") { |
| 161 | + return GolangPackageManager(installationDirectory: installationDirectory) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Check the binary path for clues if needed |
| 167 | + let binPath = instructions.first?.binaryPath ?? "" |
| 168 | + if binPath.contains("target/release") || binPath.hasSuffix(".rs") { |
| 169 | + return CargoPackageManager(installationDirectory: installationDirectory) |
| 170 | + } else if binPath.contains("node_modules") { |
| 171 | + return NPMPackageManager(installationDirectory: installationDirectory) |
| 172 | + } else if binPath.contains(".py") { |
| 173 | + return PipPackageManager(installationDirectory: installationDirectory) |
| 174 | + } else if binPath.hasSuffix(".go") || binPath.contains("/go/bin") { |
| 175 | + return GolangPackageManager(installationDirectory: installationDirectory) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Default to cargo |
| 180 | + return CargoPackageManager(installationDirectory: installationDirectory) |
| 181 | + } |
| 182 | +} |
0 commit comments