Skip to content

Instantly share code, notes, and snippets.

@erran-r7
Created June 27, 2013 07:46
Show Gist options
  • Save erran-r7/5874673 to your computer and use it in GitHub Desktop.
Save erran-r7/5874673 to your computer and use it in GitHub Desktop.
# NOTE: Grepping through ~/.vagrant.d/gems/specifications/ for installed versions isn't the way to do this
# TODO: Add Bundler/Berkshelf-like version management
# TODO: Try to use the Ruby equivalent (located in Vagrant's PluginCommand class) w/out OptParser
def plugin(name, version = nil, opts = {})
puts "Inside of the plugin method: name: #{name}, version: #{version}, opts: #{opts}"
@vagrant_home ||= opts[:home_path] || ENV['VAGRANT_HOME'] || "#{ENV['HOME']}/.vagrant.d"
plugins = JSON.parse(File.read("#@vagrant_home/plugins.json"))
if !plugins['installed'].include?(name) || (version && !version_matches(name, version))
cmd = "vagrant plugin install"
cmd << " --entry-point #{opts[:entry_point]}" if opts[:entry_point]
cmd << " --plugin-source #{opts[:source]}" if opts[:source]
cmd << " --plugin-version #{version}" if version
cmd << " #{name}"
result = %x(#{cmd})
puts result
result
end
end
def version_matches(name, version)
gems = Dir["#@vagrant_home/gems/specifications/*"].map { |spec| spec.split('/').last.sub(/\.gemspec$/,'').split(/-(?=[\d]+\.?){1,}/) }
gem_hash = {}
gems.each { |gem, v| gem_hash[gem] = v }
puts gems.inspect
puts gem_hash.inspect
puts "Version #{gem_hash[name]} of #{name} installed."
gem_hash[name] == version
end
plugin 'vagrant-berkshelf', '1.3.2'
plugin 'vagrant-omnibus'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment