commands/plugin: state file keeps track of the Vagrant version
This commit is contained in:
parent
b2844d420d
commit
39b2539ec7
|
@ -9,16 +9,23 @@ module VagrantPlugins
|
||||||
@path = path
|
@path = path
|
||||||
|
|
||||||
@data = {}
|
@data = {}
|
||||||
@data = JSON.parse(@path.read) if @path.exist?
|
if @path.exist?
|
||||||
@data["installed"] ||= []
|
@data = JSON.parse(@path.read)
|
||||||
|
upgrade_v0! if !@data["version"]
|
||||||
|
end
|
||||||
|
|
||||||
|
@data["version"] ||= "1"
|
||||||
|
@data["installed"] ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add a plugin that is installed to the state file.
|
# Add a plugin that is installed to the state file.
|
||||||
#
|
#
|
||||||
# @param [String] name The name of the plugin
|
# @param [String] name The name of the plugin
|
||||||
def add_plugin(name)
|
def add_plugin(name)
|
||||||
if !@data["installed"].include?(name)
|
if !@data["installed"].has_key?(name)
|
||||||
@data["installed"] << name
|
@data["installed"][name] = {
|
||||||
|
"vagrant_version" => Vagrant::VERSION,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
save!
|
save!
|
||||||
|
@ -43,15 +50,27 @@ module VagrantPlugins
|
||||||
|
|
||||||
# This saves the state back into the state file.
|
# This saves the state back into the state file.
|
||||||
def save!
|
def save!
|
||||||
# Scrub some fields
|
|
||||||
@data["installed"].sort!
|
|
||||||
@data["installed"].uniq!
|
|
||||||
|
|
||||||
# Save
|
|
||||||
@path.open("w+") do |f|
|
@path.open("w+") do |f|
|
||||||
f.write(JSON.dump(@data))
|
f.write(JSON.dump(@data))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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|
|
||||||
|
new_installed[plugin] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
@data["installed"] = new_installed
|
||||||
|
|
||||||
|
save!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,6 +18,7 @@ describe VagrantPlugins::CommandPlugin::StateFile do
|
||||||
|
|
||||||
subject { described_class.new(path) }
|
subject { described_class.new(path) }
|
||||||
|
|
||||||
|
context "new usage" do
|
||||||
it "should have no plugins without saving some" do
|
it "should have no plugins without saving some" do
|
||||||
expect(subject.installed_plugins).to be_empty
|
expect(subject.installed_plugins).to be_empty
|
||||||
end
|
end
|
||||||
|
@ -26,7 +27,9 @@ describe VagrantPlugins::CommandPlugin::StateFile do
|
||||||
subject.add_plugin("foo")
|
subject.add_plugin("foo")
|
||||||
|
|
||||||
instance = described_class.new(path)
|
instance = described_class.new(path)
|
||||||
expect(instance.installed_plugins).to eql(["foo"])
|
plugins = instance.installed_plugins
|
||||||
|
expect(plugins.length).to eql(1)
|
||||||
|
expect(plugins["foo"]).to eql({ "vagrant_version" => Vagrant::VERSION })
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should remove plugins" do
|
it "should remove plugins" do
|
||||||
|
@ -42,6 +45,23 @@ describe VagrantPlugins::CommandPlugin::StateFile do
|
||||||
subject.add_plugin("foo")
|
subject.add_plugin("foo")
|
||||||
|
|
||||||
instance = described_class.new(path)
|
instance = described_class.new(path)
|
||||||
expect(instance.installed_plugins).to eql(["foo"])
|
expect(instance.installed_plugins.keys).to eql(["foo"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with an old-style file" do
|
||||||
|
before do
|
||||||
|
data = {
|
||||||
|
"installed" => ["foo"],
|
||||||
|
}
|
||||||
|
|
||||||
|
path.open("w+") do |f|
|
||||||
|
f.write(JSON.dump(data))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have the right installed plugins" do
|
||||||
|
subject.installed_plugins.keys.should eql(["foo"])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue