2013-02-03 02:42:04 +00:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandPlugin
|
|
|
|
# This is a helper to deal with the plugin state file that Vagrant
|
|
|
|
# uses to track what plugins are installed and activated and such.
|
|
|
|
class StateFile
|
|
|
|
def initialize(path)
|
|
|
|
@path = path
|
|
|
|
|
|
|
|
@data = {}
|
2013-12-04 19:17:48 +00:00
|
|
|
if @path.exist?
|
2013-12-20 09:54:10 +00:00
|
|
|
begin
|
|
|
|
@data = JSON.parse(@path.read)
|
|
|
|
rescue JSON::ParserError => e
|
|
|
|
raise Vagrant::Errors::PluginStateFileParseError,
|
|
|
|
:path => path, :message => e.message
|
|
|
|
end
|
|
|
|
|
2013-12-04 19:17:48 +00:00
|
|
|
upgrade_v0! if !@data["version"]
|
|
|
|
end
|
|
|
|
|
|
|
|
@data["version"] ||= "1"
|
|
|
|
@data["installed"] ||= {}
|
2013-02-03 02:42:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add a plugin that is installed to the state file.
|
|
|
|
#
|
|
|
|
# @param [String] name The name of the plugin
|
|
|
|
def add_plugin(name)
|
2013-12-04 19:17:48 +00:00
|
|
|
if !@data["installed"].has_key?(name)
|
|
|
|
@data["installed"][name] = {
|
2013-12-04 19:43:39 +00:00
|
|
|
"ruby_version" => RUBY_VERSION,
|
2013-12-04 19:17:48 +00:00
|
|
|
"vagrant_version" => Vagrant::VERSION,
|
|
|
|
}
|
2013-02-03 18:34:43 +00:00
|
|
|
end
|
2013-02-03 18:35:59 +00:00
|
|
|
|
|
|
|
save!
|
2013-02-03 02:42:04 +00:00
|
|
|
end
|
|
|
|
|
2013-12-04 21:23:03 +00:00
|
|
|
# This returns a hash of installed plugins according to the state
|
2013-02-03 07:59:48 +00:00
|
|
|
# file. Note that this may _not_ directly match over to actually
|
|
|
|
# installed gems.
|
|
|
|
#
|
2013-12-04 21:23:03 +00:00
|
|
|
# @return [Hash]
|
2013-02-03 07:59:48 +00:00
|
|
|
def installed_plugins
|
|
|
|
@data["installed"]
|
|
|
|
end
|
|
|
|
|
2013-02-03 18:47:01 +00:00
|
|
|
# Remove a plugin that is installed from the state file.
|
|
|
|
#
|
|
|
|
# @param [String] name The name of the plugin.
|
|
|
|
def remove_plugin(name)
|
|
|
|
@data["installed"].delete(name)
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2013-02-03 02:42:04 +00:00
|
|
|
# This saves the state back into the state file.
|
|
|
|
def save!
|
|
|
|
@path.open("w+") do |f|
|
|
|
|
f.write(JSON.dump(@data))
|
|
|
|
end
|
|
|
|
end
|
2013-12-04 19:17:48 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# This upgrades the internal data representation from V0 (the initial
|
|
|
|
# version) to V1.
|
|
|
|
def upgrade_v0!
|
|
|
|
@data["version"] = "1"
|
|
|
|
|
|
|
|
new_installed = {}
|
|
|
|
(@data["installed"] || []).each do |plugin|
|
2013-12-04 19:29:17 +00:00
|
|
|
new_installed[plugin] = {
|
2013-12-04 19:43:39 +00:00
|
|
|
"ruby_version" => "0",
|
2013-12-04 19:29:17 +00:00
|
|
|
"vagrant_version" => "0",
|
|
|
|
}
|
2013-12-04 19:17:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@data["installed"] = new_installed
|
|
|
|
|
|
|
|
save!
|
|
|
|
end
|
2013-02-03 02:42:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|