aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/cmd/brew-bundle-dir.rb
blob: c2c6f3a546e2af38e7723ccc717400917c04816f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby

# program name.
NAME = "bundle-dir"

# defaults.
PREFIX_DEFAULT = "~/.brews.d"

# effective prefix directory.
PREFIX = File.expand_path(ENV["BUNDLE_DIR"] || PREFIX_DEFAULT)

# validate prefix directory exists and is readable.
odie "'#{PREFIX}' does not exist, is not a directory, or is not readable." unless File.readable?(PREFIX) && File.directory?(PREFIX)

# list all available brewfiles in PREFIX.
def list
  Dir["#{PREFIX}/**/*.brewfile"].select do |brewfile|
    File.readable?(brewfile)
  end
end

# edit/open all available brewfiles in PREFIX.
def edit
  odie "environment variable EDITOR is not set." unless ENV["EDITOR"].length > 0
  system "$EDITOR #{list().join(' ')}"
end

# count number of `brew * install` lines across all found brewfiles.
def count
  system "echo #{list().join(' ')} | xargs cat | grep -vE '^[#]' | grep -vE '^$' | wc -l | awk '{ print $NR }'"
end

# print a usage message.
def usage
  <<-USAGE
Usage: brew #{NAME}

  Options:

   -c, --count  Count of formula found in all brewfiles found in PREFIX.
   -e, --edit   Edit/open all brewfiles found in PREFIX.
   -l, --list   List all brewfiles found in PREFIX.
   -h, --help   Display help information.
  USAGE
end

opoo <<-EOS.undent
  brew bundle-dir is unsupported and will be removed soon.
  Please feel free volunteer to support it in a tap.

EOS

# command.
command = ARGV.first

# help.
if ["--help", "-h"].include? command
  puts usage ; exit 0
end

# list.
if ["--list", "-l"].include? command
  puts list ; exit 0
end

# edit.
if ["--edit", "-e"].include? command
  edit ; exit 0
end

# count.
if ["--count", "-c"].include? command
  count ; exit 0
end

# unknown option.
if !command.nil? && command.length > 0
  puts "#{usage}"
  puts "\n"
  odie "Unknown option: #{command}"
end

# main.
list().each do |brewfile|
  system "brew bundle #{brewfile}"
end