native macOS codings agent orchestrator
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(cli): extract open handler and path resolver into separate functions

- Extract makeOpenHandler factory from makeCLISocketServer to reduce
function body length (was 187 lines, limit 150)
- Extract resolveOpenPath into standalone static function to reduce
cyclomatic complexity (was 17, limit 15)
- swiftlint --strict clean

Friday 0d25f06d 80f44742

+106 -90
+106 -90
supacode/App/supacodeApp.swift
··· 254 254 bringMainWindowToFront() 255 255 } 256 256 ) 257 - let openHandler = OpenCommandHandler( 257 + let openHandler = Self.makeOpenHandler(appStore: appStore, terminalManager: terminalManager) 258 + let cliRouter = CLICommandRouter( 259 + openHandler: openHandler, 260 + listHandler: listHandler, 261 + focusHandler: focusHandler, 262 + sendHandler: sendHandler 263 + ) 264 + let cliServer = CLISocketServer(router: cliRouter) 265 + let logger = SupaLogger("CLIService") 266 + do { 267 + try cliServer.start() 268 + logger.info("CLI socket server started at \(ProwlSocket.defaultPath)") 269 + } catch { 270 + logger.warning("Failed to start CLI socket server: \(String(describing: error))") 271 + } 272 + return cliServer 273 + } 274 + 275 + // MARK: - Open handler factory 276 + 277 + private static func makeOpenHandler( 278 + appStore: StoreOf<AppFeature>, 279 + terminalManager: WorktreeTerminalManager 280 + ) -> OpenCommandHandler { 281 + OpenCommandHandler( 258 282 resolver: { path in 259 - guard let path else { 260 - return OpenResolverResult( 261 - resolution: .noArgument, worktreeID: nil, worktreeName: nil, 262 - worktreePath: nil, rootPath: nil, worktreeKind: nil, resolvedPath: nil 263 - ) 264 - } 265 - let normalized = URL(fileURLWithPath: path, isDirectory: true) 266 - .standardizedFileURL.path(percentEncoded: false) 267 - let repositories = appStore.state.repositories 268 - // Exact match: worktree working directory 269 - for repository in repositories.repositories { 270 - let kind = repository.kind.rawValue 271 - for worktree in repository.worktrees { 272 - let wtPath = worktree.workingDirectory 273 - .standardizedFileURL.path(percentEncoded: false) 274 - if wtPath == normalized { 275 - return OpenResolverResult( 276 - resolution: .exactRoot, worktreeID: worktree.id, 277 - worktreeName: worktree.name, worktreePath: wtPath, 278 - rootPath: repository.rootURL.standardizedFileURL.path(percentEncoded: false), 279 - worktreeKind: kind, resolvedPath: normalized 280 - ) 281 - } 282 - } 283 - // Exact match: repository root for non-worktree repos 284 - let repoRoot = repository.rootURL 285 - .standardizedFileURL.path(percentEncoded: false) 286 - if repoRoot == normalized, 287 - !repository.capabilities.supportsWorktrees, 288 - repository.capabilities.supportsRunnableFolderActions 289 - { 290 - return OpenResolverResult( 291 - resolution: .exactRoot, worktreeID: repository.id, 292 - worktreeName: repository.name, worktreePath: repoRoot, 293 - rootPath: repoRoot, worktreeKind: kind, resolvedPath: normalized 294 - ) 295 - } 296 - } 297 - // Inside-root: path is inside an existing worktree/repo root 298 - let normalizedSlash = normalized.hasSuffix("/") ? normalized : normalized + "/" 299 - for repository in repositories.repositories { 300 - let kind = repository.kind.rawValue 301 - for worktree in repository.worktrees { 302 - let wtPath = worktree.workingDirectory 303 - .standardizedFileURL.path(percentEncoded: false) 304 - let wtSlash = wtPath.hasSuffix("/") ? wtPath : wtPath + "/" 305 - if normalizedSlash.hasPrefix(wtSlash) { 306 - return OpenResolverResult( 307 - resolution: .insideRoot, worktreeID: worktree.id, 308 - worktreeName: worktree.name, worktreePath: wtPath, 309 - rootPath: repository.rootURL.standardizedFileURL.path(percentEncoded: false), 310 - worktreeKind: kind, resolvedPath: normalized 311 - ) 312 - } 313 - } 314 - if !repository.capabilities.supportsWorktrees, 315 - repository.capabilities.supportsRunnableFolderActions 316 - { 317 - let repoRoot = repository.rootURL 318 - .standardizedFileURL.path(percentEncoded: false) 319 - let repoSlash = repoRoot.hasSuffix("/") ? repoRoot : repoRoot + "/" 320 - if normalizedSlash.hasPrefix(repoSlash) { 321 - return OpenResolverResult( 322 - resolution: .insideRoot, worktreeID: repository.id, 323 - worktreeName: repository.name, worktreePath: repoRoot, 324 - rootPath: repoRoot, worktreeKind: kind, resolvedPath: normalized 325 - ) 326 - } 327 - } 328 - } 329 - // New root: unknown path 330 - return OpenResolverResult( 331 - resolution: .newRoot, worktreeID: nil, worktreeName: nil, 332 - worktreePath: nil, rootPath: nil, worktreeKind: nil, resolvedPath: normalized 333 - ) 283 + resolveOpenPath(path, repositories: appStore.state.repositories) 334 284 }, 335 285 selectWorktree: { worktreeID in 336 286 selectCLIWorktreeContext( ··· 373 323 ) 374 324 } 375 325 ) 376 - let cliRouter = CLICommandRouter( 377 - openHandler: openHandler, 378 - listHandler: listHandler, 379 - focusHandler: focusHandler, 380 - sendHandler: sendHandler 381 - ) 382 - let cliServer = CLISocketServer(router: cliRouter) 383 - let logger = SupaLogger("CLIService") 384 - do { 385 - try cliServer.start() 386 - logger.info("CLI socket server started at \(ProwlSocket.defaultPath)") 387 - } catch { 388 - logger.warning("Failed to start CLI socket server: \(String(describing: error))") 326 + } 327 + 328 + // MARK: - Open path resolution 329 + 330 + private static func resolveOpenPath( 331 + _ path: String?, 332 + repositories: RepositoriesFeature.State 333 + ) -> OpenResolverResult { 334 + guard let path else { 335 + return OpenResolverResult( 336 + resolution: .noArgument, worktreeID: nil, worktreeName: nil, 337 + worktreePath: nil, rootPath: nil, worktreeKind: nil, resolvedPath: nil 338 + ) 389 339 } 390 - return cliServer 340 + let normalized = URL(fileURLWithPath: path, isDirectory: true) 341 + .standardizedFileURL.path(percentEncoded: false) 342 + // Exact match: worktree working directory or repository root 343 + for repository in repositories.repositories { 344 + let kind = repository.kind.rawValue 345 + for worktree in repository.worktrees { 346 + let wtPath = worktree.workingDirectory 347 + .standardizedFileURL.path(percentEncoded: false) 348 + if wtPath == normalized { 349 + return OpenResolverResult( 350 + resolution: .exactRoot, worktreeID: worktree.id, 351 + worktreeName: worktree.name, worktreePath: wtPath, 352 + rootPath: repository.rootURL.standardizedFileURL.path(percentEncoded: false), 353 + worktreeKind: kind, resolvedPath: normalized 354 + ) 355 + } 356 + } 357 + let repoRoot = repository.rootURL 358 + .standardizedFileURL.path(percentEncoded: false) 359 + if repoRoot == normalized, 360 + !repository.capabilities.supportsWorktrees, 361 + repository.capabilities.supportsRunnableFolderActions 362 + { 363 + return OpenResolverResult( 364 + resolution: .exactRoot, worktreeID: repository.id, 365 + worktreeName: repository.name, worktreePath: repoRoot, 366 + rootPath: repoRoot, worktreeKind: kind, resolvedPath: normalized 367 + ) 368 + } 369 + } 370 + // Inside-root: path is inside an existing worktree/repo root 371 + let normalizedSlash = normalized.hasSuffix("/") ? normalized : normalized + "/" 372 + for repository in repositories.repositories { 373 + let kind = repository.kind.rawValue 374 + for worktree in repository.worktrees { 375 + let wtPath = worktree.workingDirectory 376 + .standardizedFileURL.path(percentEncoded: false) 377 + let wtSlash = wtPath.hasSuffix("/") ? wtPath : wtPath + "/" 378 + if normalizedSlash.hasPrefix(wtSlash) { 379 + return OpenResolverResult( 380 + resolution: .insideRoot, worktreeID: worktree.id, 381 + worktreeName: worktree.name, worktreePath: wtPath, 382 + rootPath: repository.rootURL.standardizedFileURL.path(percentEncoded: false), 383 + worktreeKind: kind, resolvedPath: normalized 384 + ) 385 + } 386 + } 387 + if !repository.capabilities.supportsWorktrees, 388 + repository.capabilities.supportsRunnableFolderActions 389 + { 390 + let repoRoot = repository.rootURL 391 + .standardizedFileURL.path(percentEncoded: false) 392 + let repoSlash = repoRoot.hasSuffix("/") ? repoRoot : repoRoot + "/" 393 + if normalizedSlash.hasPrefix(repoSlash) { 394 + return OpenResolverResult( 395 + resolution: .insideRoot, worktreeID: repository.id, 396 + worktreeName: repository.name, worktreePath: repoRoot, 397 + rootPath: repoRoot, worktreeKind: kind, resolvedPath: normalized 398 + ) 399 + } 400 + } 401 + } 402 + // New root: unknown path 403 + return OpenResolverResult( 404 + resolution: .newRoot, worktreeID: nil, worktreeName: nil, 405 + worktreePath: nil, rootPath: nil, worktreeKind: nil, resolvedPath: normalized 406 + ) 391 407 } 392 408 393 409 private static func selectCLIWorktreeContext(