Skip to content

Commit

Permalink
Merge pull request #15 from zachberger/master
Browse files Browse the repository at this point in the history
Add support for custom brew installation path
  • Loading branch information
zkokaja authored Dec 7, 2020
2 parents 55fe713 + c7b8583 commit 09f75e4
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 25 deletions.
33 changes: 27 additions & 6 deletions Brewlet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
// Sync with brew
let data = String(decoding: data, as: UTF8.self)
let lines = data.split(separator: "\n")
for line in lines[1...] {
let parts = line.split(separator: " ", maxSplits: Int.max, omittingEmptySubsequences: true)
let package = parts[0]
let isStopped = parts[1] == "stopped"
services.append(Service(name: String(package), isStopped: isStopped))

// Check that there are services before parsing the output
if lines.count > 0 {
for line in lines[1...] {
let parts = line.split(separator: " ", maxSplits: Int.max, omittingEmptySubsequences: true)
let package = parts[0]
let isStopped = parts[1] == "stopped"
services.append(Service(name: String(package), isStopped: isStopped))
}
}

// Update UI
Expand Down Expand Up @@ -492,9 +496,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
func run_command(arguments: [String],
fileRedirect: FileHandle? = nil,
outputHandler: @escaping (Process,Data) -> Void) {
#if arch(arm64)
let brewPath = userDefaults.string(forKey: "brewPath") ?? PreferencesController.HomebrewPath.appleSilicon.rawValue
#elseif arch(x86_64)
let brewPath = userDefaults.string(forKey: "brewPath") ?? PreferencesController.HomebrewPath.intel.rawValue
#endif
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["/usr/local/Homebrew/bin/brew"] + arguments
task.arguments = [brewPath] + arguments

let pipe = Pipe()
var allData = Data() // What happens to the scope of this variable when used inside the closure??
Expand Down Expand Up @@ -670,6 +679,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
self.setupTimers()
}

/**
Handle a change of preferences for where the brew binary is.

- Parameter newState: The new state of the textfield.
*/
func brewPathChanged(newPath: String) {
// Update defaults and rerun update
userDefaults.set(newPath, forKey: "brewPath")
check_outdated()
update_info()
}

// MARK: - Termination functions

/**
Expand Down
63 changes: 61 additions & 2 deletions Brewlet/PreferencesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protocol PreferencesDelegate {
func updateIntervalChanged(newInterval: TimeInterval?) // if nil, then don't update
func includeDependenciesChanged(newState: NSControl.StateValue)
func shareAnalyticsChanged(newState: NSControl.StateValue)
func brewPathChanged(newPath: String)
}

class PreferencesController: NSWindowController {
Expand All @@ -22,9 +23,19 @@ class PreferencesController: NSWindowController {
@IBOutlet weak var shareAnalytics: NSButton!
@IBOutlet weak var autoUpgrade: NSButton!
@IBOutlet weak var dontNotifyAvailable: NSButton!

@IBOutlet weak var brewPath: NSTextField!
@IBOutlet weak var intel: NSButton!
@IBOutlet weak var appleSilicon: NSButton!
@IBOutlet weak var custom: NSButton!

var delegate: PreferencesDelegate?

enum HomebrewPath: String {
case appleSilicon = "/opt/homebrew/bin/brew"
case intel = "/usr/local/bin/brew"
case custom = ""
}

override func windowDidLoad() {
super.windowDidLoad()

Expand Down Expand Up @@ -57,6 +68,21 @@ class PreferencesController: NSWindowController {

autoUpgrade.state = defaults.bool(forKey: "autoUpgrade") ? .on : .off
dontNotifyAvailable.state = defaults.bool(forKey: "dontNotify") ? .on : .off

#if arch(arm64)
let currentBrewPath = defaults.string(forKey: "brewPath") ?? HomebrewPath.appleSilicon.rawValue
#elseif arch(x86_64)
let currentBrewPath = defaults.string(forKey: "brewPath") ?? HomebrewPath.intel.rawValue
#endif
switch currentBrewPath {
case HomebrewPath.intel.rawValue:
intelSelected(nil)
case HomebrewPath.appleSilicon.rawValue:
appleSiliconSelected(nil)
default:
custom.state = .on
brewPath.stringValue = currentBrewPath
}
}

@IBAction func includeDependenciesPressed(_ sender: NSButton) {
Expand Down Expand Up @@ -89,7 +115,40 @@ class PreferencesController: NSWindowController {
}

delegate?.updateIntervalChanged(newInterval: seconds)
}
}

@IBAction func appleSiliconSelected(_ sender: Any?) {
appleSilicon.state = .on
intel.state = .off
custom.state = .off
brewPath.isEnabled = false
let path = HomebrewPath.appleSilicon.rawValue
brewPath.stringValue = path
delegate?.brewPathChanged(newPath: path)
}

@IBAction func intelSelected(_ sender: Any?) {
intel.state = .on
appleSilicon.state = .off
custom.state = .off
brewPath.isEnabled = false
let path = HomebrewPath.intel.rawValue
brewPath.stringValue = path
delegate?.brewPathChanged(newPath: path)
}

@IBAction func customSelected(_ sender: Any) {
custom.state = .on
appleSilicon.state = .off
intel.state = .off
brewPath.isEnabled = true
delegate?.brewPathChanged(newPath: brewPath.stringValue)
}

@IBAction func brewPathChanged(_ sender: NSTextField) {
// TODO: Validate that the path is valid
delegate?.brewPathChanged(newPath: sender.stringValue)
}

override var windowNibName : String! {
return "PreferencesController"
Expand Down
Loading

0 comments on commit 09f75e4

Please sign in to comment.