Skip to content
Newer
Older
100755 229 lines (201 sloc) 5.68 KB
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 21, 2009
1 #!/usr/bin/ruby
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
2
5b3e097 @mxcl Suppress require 'FileUtils' warning
mxcl authored May 21, 2009
3 require 'find'
4 require 'pathname'
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
5 $:.unshift Pathname.new(__FILE__).dirname.parent.realpath+'Library'+'Homebrew'
6 require 'env'
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
7
35f2dd3 @mxcl brew rm command
mxcl authored May 22, 2009
8 def prune
985030a @mxcl Fix prune
mxcl authored May 22, 2009
9 n=0
10 dirs=Array.new
35f2dd3 @mxcl brew rm command
mxcl authored May 21, 2009
11 $root.find do |path|
12 if path.directory?
985030a @mxcl Fix prune
mxcl authored May 22, 2009
13 name=path.relative_path_from($root).to_s
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
14 if name == '.git' or name == 'Cellar' or name == 'Library/Homebrew' or name == 'Library/Formula'
985030a @mxcl Fix prune
mxcl authored May 22, 2009
15 Find.prune
16 else
17 dirs<<path
18 end
35f2dd3 @mxcl brew rm command
mxcl authored May 21, 2009
19 elsif path.symlink?
985030a @mxcl Fix prune
mxcl authored May 22, 2009
20 resolved_path=path.dirname+path.readlink
21 unless resolved_path.exist?
22 path.unlink
23 n+=1
24 end
35f2dd3 @mxcl brew rm command
mxcl authored May 21, 2009
25 end
26 end
985030a @mxcl Fix prune
mxcl authored May 22, 2009
27 # entries lists '.' and '..' so 2 is minimum basically
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
28 dirs.sort.reverse_each do |d|
29 if d.children.length == 0
30 d.rmdir
31 n+=1
32 end
33 end
985030a @mxcl Fix prune
mxcl authored May 22, 2009
34 return n
35f2dd3 @mxcl brew rm command
mxcl authored May 21, 2009
35 end
36
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
37 def shift_formulae
38 name=Pathname.new ARGV.shift
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
39
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
40 return name if name.directory? and name.parent.realpath == $cellar
41 return File.basename(name, '.rb') if name.file? and name.extname == '.rb' and name.parent.realpath == $formula
1b5d1c1 @mxcl Quote paths in shell commands
mxcl authored May 21, 2009
42
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
43 name=name.to_s
44 raise "#{name} is an invalid name for a formula" if name.include? '/'
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
45
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
46 return name if ($formula+(name+'.rb')).file?
47 return name if ($cellar+name).directory?
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
48
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
49 raise "No formula or keg for #{name} found"
50 end
51
52 def __class name
53 #remove invalid characters and camelcase
54 name.capitalize.gsub(/[-_\s]([a-zA-Z0-9])/) { $1.upcase }
55 end
56
57 def __rb name
58 $formula+(name+'.rb')
59 end
60
61 def __obj name
62 require "#{__rb name}"
63 o=eval(__class(name)).new
64 o.name=name
65 return o
66 end
67
68 def rm keg
69 #TODO if multiple versions don't rm all unless --force
70 path=$cellar+keg
71 path.rmtree
72 puts "#{path} removed (#{prune} files)"
73 end
74
75 def ln name
76 keg=$cellar+name
77 keg=keg.realpath
78
79 if keg.parent.parent == $root
80 # we are one dir too high
81 kids=keg.children
82 raise "#{keg} is empty :(" if kids.length == 0
83 raise "There are multiple versions of #{keg.basename} installed please specify one" if kids.length > 1
84 keg=keg.children.first
85 raise "#{keg} is not a directory" unless keg.directory?
86 elsif keg.parent.parent.parent != $root
87 raise '#{keg} is not a keg'
88 end
89
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
90 # yeah indeed, you have to force anything you need in the main tree into
91 # these directories :P
92 # NOTE that not everything needs to be in the main tree
93 # TODO consider using hardlinks
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
94 $n=0
95 lnd(keg, 'etc') {nil}
96 lnd(keg, 'include') {nil}
97
98 lnd(keg, 'lib') do |path|
99 :mkpath if ['pkgconfig','php'].include? path.to_s
100 end
101
102 lnd(keg, 'bin') do |path|
103 if path.extname.to_s == '.app'
104 # no need to put .app bundles in the path, just use spotlight, or the
105 # open command
106 :skip
107 else
108 :mkpath
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
109 end
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
110 end
111
112 lnd(keg, 'share') do |path|
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
113 includes=(1..9).collect {|x| "man/man#{x}"} <<'man'<<'doc'<<'locale'<<'info'
114 :mkpath if includes.include? path.to_s
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
115 end
116
117 return $n
118 end
119
120 def symlink_relative_to from, to
121 tod=to.dirname
122 tod.mkpath
123 Dir.chdir(tod) do
124 #TODO use ruby function so we get exceptions
125 `ln -sf "#{from.relative_path_from tod}"`
126 $n+=1
127 end
128 end
129
130 # symlinks a directory recursively into our FHS tree
131 def lnd keg, start
132 start=keg+start
133 return unless start.directory?
134
135 start.find do |from|
136 next if from == start
137
138 prune=false
139 relative_path=from.relative_path_from keg
140 to=$root+relative_path
141
142 if from.directory?
143 cmd=yield from.relative_path_from(start)
144
145 if :skip == cmd
146 Find.prune
147 elsif :mkpath == cmd
148 to.mkpath
149 $n+=1
150 else
151 symlink_relative_to from, to
152 Find.prune
153 end
154 elsif from.file?
155 symlink_relative_to from, to
2a5a754 @mxcl brew prefix command
mxcl authored May 23, 2009
156 end
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
157 end
158 end
159
160 def usage
161 name=File.basename $0
162 <<-EOS
163 Usage: #{name} [abv] [prune] [--prefix] [--cache]
164 Usage: #{name} [install] [ln] [rm] [info] [list] beverage
165 EOS
166 end
2a5a754 @mxcl brew prefix command
mxcl authored May 23, 2009
167
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
168 ######################################################################### impl
169 begin
170 #TODO proper options parsing so --options can go first if necessary
171
172 case ARGV.shift
173 when 'abv'
174 `find #{$cellar} -type f | wc -l`.strip+' files, '+`du -hd0 #{$cellar} | cut -d"\t" -f1`.strip
175 when 'prune'
176 puts "Pruned #{prune} files"
177 when '--prefix'
a1e3a98 @mxcl Correct --prefix behaviour
mxcl authored Jun 4, 2009
178 # we use the cwd because __FILE__ can be relative and expand_path
179 # resolves the symlink for the working directory if fed a relative path
180 # NOTE we don't use Dir.pwd because it resolves the symlink :P #rubysucks
181 cwd=Pathname.new `pwd`.strip
182 puts File.expand_path(cwd+__FILE__+'../../')
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
183 when '--cache'
184 puts File.expand_path('~/Library/Application Support/Homebrew')
185 when '-h', '--help', '--usage', '-?'
186 puts usage
187 when '-v', '--version'
188 puts HOMEBREW_VERSION
189 when 'list'
190 puts `find #{$cellar+shift_formulae}`
191 when 'install'
192 name=shift_formulae
193 beginning = Time.now
194 o=__obj(name)
195 raise "#{o.prefix} already exists!" if o.prefix.exist?
9c7229d @mxcl mkpath prefix before install in brew command
mxcl authored Jun 4, 2009
196 o.prefix.mkpath
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
197 o.brew { o.install }
e91aeaf @mxcl Created /Library moved brew tool to /bin
mxcl authored Jun 4, 2009
198 ohai 'Finishing up'
199 o.clean
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
200 ln name
201 puts "#{o.prefix}: "+`find #{o.prefix} -type f | wc -l`.strip+
202 ' files, '+
203 `du -hd0 #{o.prefix} | cut -d"\t" -f1`.strip+
204 ", built in #{Time.now - beginning} seconds"
205 when 'ln'
206 puts "Created #{ln shift_formulae} links"
207 when 'rm'
208 rm shift_formulae
209 when 'info'
210 o=__obj shift_formulae
211 puts "#{o.name} #{o.version}"
212 puts o.homepage
213 else
214 puts usage
215 end
2a5a754 @mxcl brew prefix command
mxcl authored May 23, 2009
216
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
217 rescue StandardError, Interrupt => e
218 if ARGV.include? '--verbose' or ENV['HOMEBREW_DEBUG']
219 raise
220 elsif e.kind_of? Interrupt
221 puts # seeimgly a newline is typical
222 exit 130
223 elsif e.kind_of? StandardError and not e.kind_of? NameError
224 puts "\033[1;31mError\033[0;0m: #{e}"
225 exit 1
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
226 else
d614113 @mxcl Refactor Formulae
mxcl authored Jun 2, 2009
227 raise
228 end
84c5c3e @mxcl The core tree, and some formulae
mxcl authored May 20, 2009
229 end
Something went wrong with that request. Please try again.