aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorAdam Vandenberg2010-06-16 09:42:06 -0700
committerAdam Vandenberg2010-06-16 09:42:06 -0700
commita07e97618ddb2a23acdce3e6d6d3ff261f882118 (patch)
treea415aba27df04422020c9d0c4e00d63e4cd913e3 /Library
parente722309b288bcf37d2eccbc4375e9b78e649d769 (diff)
downloadhomebrew-a07e97618ddb2a23acdce3e6d6d3ff261f882118.tar.bz2
External command: brew switch formula version
This external command allows you to switch between installed versions of a formula. If you have multiple versions of a formula in your cellar, the standard "brew link" command will refuse to run. This new command "brew switch" tries to unlink all versions of the brew from the prefix, then links the specific requested version.
Diffstat (limited to 'Library')
-rwxr-xr-xLibrary/Contributions/examples/brew-switch.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/Library/Contributions/examples/brew-switch.rb b/Library/Contributions/examples/brew-switch.rb
new file mode 100755
index 000000000..e580feafa
--- /dev/null
+++ b/Library/Contributions/examples/brew-switch.rb
@@ -0,0 +1,40 @@
+require 'formula'
+require 'keg'
+
+if ARGV.named.length != 2
+ onoe "Two parameters are expected, the formula name and version to link."
+ exit 1
+end
+
+name = ARGV.shift
+version = ARGV.shift
+
+# Does this formula have any versions?
+f = Formula.factory(name.downcase)
+cellar = f.prefix.parent
+unless cellar.directory?
+ onoe "#{name} not found in the Cellar."
+ exit 2
+end
+
+# Does the target version exist?
+unless (cellar+version).directory?
+ onoe "#{name} does not have a version #{version} in the Cellar."
+
+ versions = cellar.children.select { |pn| pn.directory? }.collect { |pn| pn.basename.to_s }
+ puts "Versions available: #{versions.join(', ')}"
+
+ exit 3
+end
+
+# Unlink all existing versions
+cellar.children.select { |pn| pn.directory? }.each do |v|
+ keg = Keg.new(v)
+ puts "Cleaning #{keg}"
+ keg.unlink
+end
+
+# Link new version
+
+keg = Keg.new(cellar+version)
+puts "#{keg.link} links created for #{keg}" \ No newline at end of file