|
| 1 | +// |
| 2 | +// BuildController.swift |
| 3 | +// nnex |
| 4 | +// |
| 5 | +// Created by Nikolai Nobadi on 12/12/25. |
| 6 | +// |
| 7 | + |
| 8 | +import NnexKit |
| 9 | + |
| 10 | +struct BuildController { |
| 11 | + private let shell: any NnexShell |
| 12 | + private let picker: any NnexPicker |
| 13 | + private let fileSystem: any FileSystem |
| 14 | + private let buildService: any BuildService |
| 15 | + private let folderBrowser: any DirectoryBrowser |
| 16 | + |
| 17 | + init( |
| 18 | + shell: any NnexShell, |
| 19 | + picker: any NnexPicker, |
| 20 | + fileSystem: any FileSystem, |
| 21 | + buildService: any BuildService, |
| 22 | + folderBrowser: any DirectoryBrowser |
| 23 | + ) { |
| 24 | + self.shell = shell |
| 25 | + self.picker = picker |
| 26 | + self.fileSystem = fileSystem |
| 27 | + self.buildService = buildService |
| 28 | + self.folderBrowser = folderBrowser |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +// MARK: - Actions |
| 34 | +extension BuildController { |
| 35 | + func buildExecutable(path: String?, buildType: BuildType, clean: Bool, openInFinder: Bool) throws { |
| 36 | + let projectFolder = try fileSystem.getDirectoryAtPathOrCurrent(path: path) |
| 37 | + let executableName = try getExecutableName(for: projectFolder) |
| 38 | + let outputLocation = try selectOutputLocation(buildType: buildType) |
| 39 | + let config = BuildConfig(projectName: executableName, projectPath: projectFolder.path, buildType: buildType, extraBuildArgs: [], skipClean: !clean, testCommand: nil) |
| 40 | + let result = try buildService.buildExecutable(config: config, outputLocation: outputLocation) |
| 41 | + |
| 42 | + displayBuildResult(result, openInFinder: openInFinder) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +// MARK: - Private Methods |
| 48 | +private extension BuildController { |
| 49 | + func getExecutableName(for folder: any Directory) throws -> String { |
| 50 | + let names = try ExecutableNameResolver.getExecutableNames(from: folder) |
| 51 | + |
| 52 | + guard names.count > 1 else { |
| 53 | + return names.first! |
| 54 | + } |
| 55 | + |
| 56 | + do { |
| 57 | + return try picker.requiredSingleSelection("Which executable would you like to build?", items: names) |
| 58 | + } catch { |
| 59 | + throw BuildExecutionError.failedToSelectExecutable(reason: error.localizedDescription) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + func selectOutputLocation(buildType: BuildType) throws -> BuildOutputLocation { |
| 64 | + let options: [BuildOutputLocation] = [.currentDirectory(buildType), .desktop, .custom("")] |
| 65 | + let selection = try picker.requiredSingleSelection("Where would you like to place the built binary?", items: options) |
| 66 | + |
| 67 | + if case .custom = selection { |
| 68 | + return try handleCustomLocationInput() |
| 69 | + } |
| 70 | + |
| 71 | + return selection |
| 72 | + } |
| 73 | + |
| 74 | + func handleCustomLocationInput() throws -> BuildOutputLocation { |
| 75 | + let parentFolder = try folderBrowser.browseForDirectory(prompt: "Select the folder where you would like to save the build.") |
| 76 | + |
| 77 | + try picker.requiredPermission(prompt: "The binary will be placed at: \(parentFolder.path). Continue?") |
| 78 | + |
| 79 | + return .custom(parentFolder.path) |
| 80 | + } |
| 81 | + |
| 82 | + func displayBuildResult(_ result: BuildResult, openInFinder: Bool) { |
| 83 | + switch result.binaryOutput { |
| 84 | + case .single(let path): |
| 85 | + print("\(result.executableName.lightGreen) was built at \(path)") |
| 86 | + openFinder(path: openInFinder ? path : nil) |
| 87 | + case .multiple(let binaries): |
| 88 | + print("\(result.executableName.lightGreen) builds:".underline) |
| 89 | + for (arch, path) in binaries { |
| 90 | + print(" \(arch.name): \(path)") |
| 91 | + } |
| 92 | + |
| 93 | + openFinder(path: openInFinder ? binaries.values.first : nil) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func openFinder(path: String?) { |
| 98 | + if let path { |
| 99 | + try? shell.runAndPrint(bash: "open -R \(path)") |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +// MARK: - Dependencies |
| 106 | +protocol BuildService { |
| 107 | + func buildExecutable(config: BuildConfig, outputLocation: BuildOutputLocation) throws -> BuildResult |
| 108 | +} |
0 commit comments