Skip to content

Commit

Permalink
enough for now
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza72x committed Feb 21, 2023
1 parent 2e19d65 commit 9fe1de5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ brewc install ffmpeg git wget curl # for multiple formulae`,
}

func init() {
installCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 5, "number of threads to use for downloading the formulae")
installCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 10, "number of threads to use for downloading the formulae")
installCmd.Flags().BoolVarP(&_args.Verbose, "verbose", "v", false, "verbose output")

rootCmd.AddCommand(installCmd)
Expand Down
2 changes: 1 addition & 1 deletion cmd/resinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {

rootCmd.AddCommand(reinstallCmd)

reinstallCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 5, "number of threads to use for downloading the formulae")
reinstallCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 10, "number of threads to use for downloading the formulae")
reinstallCmd.Flags().BoolVarP(&_args.Verbose, "verbose", "v", false, "verbose output")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ brewc uninstall ffmpeg git wget curl # for multiple formulae`,
func init() {
rootCmd.AddCommand(uninstallCmd)

uninstallCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 5, "number of threads to use for downloading the formulae")
uninstallCmd.Flags().IntVarP(&_args.Threads, "threads", "t", 10, "number of threads to use for downloading the formulae")
uninstallCmd.Flags().BoolVarP(&_args.Verbose, "verbose", "v", false, "verbose output")
uninstallCmd.Flags().BoolVarP(&_args.DeleteUnusedDependencies, "delete-unused-dependencies", "d", false, "delete unused dependencies after uninstalling a formula")
uninstallCmd.Flags().BoolVarP(&_args.DeleteAllNestedDependencies, "delete-all-nested-dependencies", "D", false, "delete all sub-dependencies after uninstalling a formula (it will delete all nested unused dependencies)")
Expand Down
4 changes: 3 additions & 1 deletion pkg/brewc/brewc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func (b *BrewC) InstallFormula(name string) error {
fmt.Println("")

list.IterateChildFirst(b.threads, func(f *formula.Formula) {

fmt.Printf("%s Working On: %s\n", constant.GreenArrow, f.Name)

if err := b.brew.InstallFormula(f.Name, b.args.Verbose); err != nil {
fmt.Printf("%s Error installing formula: %s\n", constant.RedArrow, err.Error())
fmt.Printf("%s Error installing formula (%s): %s\n", constant.RedArrow, f.Name, err.Error())
}
})

Expand Down
2 changes: 2 additions & 0 deletions pkg/models/formula/formula_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type FormulaList struct {
httpClient *http.Client

threads int

iteratorChannelCount int
}

func newFormulaList(mainFormula *Formula, threads int) *FormulaList {
Expand Down
31 changes: 29 additions & 2 deletions pkg/models/formula/formula_list_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (list *FormulaList) AddChild(parent *FormulaNode, child *FormulaNode, keepU
// This means that the callback will be called only if there is no child of the given node.
// Otherwise, the callback will be called after all of the children have been processed.
func (list *FormulaList) IterateChildFirst(threads int, fn func(*Formula)) {
list.iteratorChannelCount = 0
list.childFirstIterator(list.root, fn)
}

Expand All @@ -166,10 +167,19 @@ func (list *FormulaList) childFirstIterator(node *FormulaNode, fn func(*Formula)
return
}

var threads = 5

if list.iteratorChannelCount >= list.threads {
threads = 1
}

list.iteratorChannelCount += threads

var wg sync.WaitGroup
var ch = make(chan int, list.threads)
var ch = make(chan int, threads)

fmt.Printf("🛠 Resolving dependencies for %s 🛠\n", node.formula.Name)
list.printActiveThreads()

// If there is a child, then we need to wait for all of the children to finish
for _, child := range node.children {
Expand All @@ -183,6 +193,7 @@ func (list *FormulaList) childFirstIterator(node *FormulaNode, fn func(*Formula)
}

wg.Wait()
list.iteratorChannelCount -= threads

// After all the children are done, we can call the callback
fmt.Printf("🎉 Completed all dependencies of %s 🎉\n", node.formula.Name)
Expand All @@ -191,14 +202,25 @@ func (list *FormulaList) childFirstIterator(node *FormulaNode, fn func(*Formula)

// IterateParentFirst iterates over the list in a parent-first manner.
func (list *FormulaList) IterateParentFirst(threads int, fn func(*Formula)) {
list.iteratorChannelCount = 0
list.parentFirstIterator(list.root, fn)
}

func (list *FormulaList) parentFirstIterator(node *FormulaNode, fn func(*Formula)) {
fn(node.formula)

var threads = 5

if list.iteratorChannelCount >= list.threads {
threads = 1
}

list.iteratorChannelCount += threads

var wg sync.WaitGroup
var ch = make(chan int, list.threads)
var ch = make(chan int, threads)

list.printActiveThreads()

for _, child := range node.children {
wg.Add(1)
Expand All @@ -211,6 +233,11 @@ func (list *FormulaList) parentFirstIterator(node *FormulaNode, fn func(*Formula
}

wg.Wait()
list.iteratorChannelCount -= threads
}

func (list *FormulaList) printActiveThreads() {
fmt.Printf("🧶 Active Threads: %d 🧶\n", list.iteratorChannelCount)
}

// DECIDE: should we use the github API to get the list of formulas?
Expand Down

0 comments on commit 9fe1de5

Please sign in to comment.