class Vagrant::Plugin::StateFile

This is a helper to deal with the plugin state file that Vagrant uses to track what plugins are installed and activated and such.

Public Class Methods

new(path) click to toggle source
# File lib/vagrant/plugin/state_file.rb, line 8
def initialize(path)
  @path = path

  @data = {}
  if @path.exist?
    begin
      @data = JSON.parse(@path.read)
    rescue JSON::ParserError => e
      raise Vagrant::Errors::PluginStateFileParseError,
        :path => path, :message => e.message
    end

    upgrade_v0! if !@data["version"]
  end

  @data["version"] ||= "1"
  @data["installed"] ||= {}
end

Public Instance Methods

add_plugin(name, **opts) click to toggle source

Add a plugin that is installed to the state file.

@param [String] name The name of the plugin

# File lib/vagrant/plugin/state_file.rb, line 30
def add_plugin(name, **opts)
  @data["installed"][name] = {
    "ruby_version"    => RUBY_VERSION,
    "vagrant_version" => Vagrant::VERSION,
    "gem_version"     => opts[:version] || "",
    "require"         => opts[:require] || "",
    "sources"         => opts[:sources] || [],
  }

  save!
end
add_source(url) click to toggle source

Adds a RubyGems index source to look up gems.

@param [String] url URL of the source.

# File lib/vagrant/plugin/state_file.rb, line 45
def add_source(url)
  @data["sources"] ||= []
  @data["sources"] << url if !@data["sources"].include?(url)
  save!
end
has_plugin?(name) click to toggle source

Returns true/false if the plugin is present in this state file.

@return [Boolean]

# File lib/vagrant/plugin/state_file.rb, line 63
def has_plugin?(name)
  @data["installed"].has_key?(name)
end
installed_plugins() click to toggle source

This returns a hash of installed plugins according to the state file. Note that this may not directly match over to actually installed gems.

@return [Hash]

# File lib/vagrant/plugin/state_file.rb, line 56
def installed_plugins
  @data["installed"]
end
remove_plugin(name) click to toggle source

Remove a plugin that is installed from the state file.

@param [String] name The name of the plugin.

# File lib/vagrant/plugin/state_file.rb, line 70
def remove_plugin(name)
  @data["installed"].delete(name)
  save!
end
remove_source(url) click to toggle source

Remove a source for RubyGems.

@param [String] url URL of the source

# File lib/vagrant/plugin/state_file.rb, line 78
def remove_source(url)
  @data["sources"] ||= []
  @data["sources"].delete(url)
  save!
end
save!() click to toggle source

This saves the state back into the state file.

# File lib/vagrant/plugin/state_file.rb, line 93
def save!
  @path.open("w+") do |f|
    f.write(JSON.dump(@data))
  end
end
sources() click to toggle source

Returns the list of RubyGems sources that will be searched for plugins.

@return [Array<String>]

# File lib/vagrant/plugin/state_file.rb, line 88
def sources
  @data["sources"] || []
end

Protected Instance Methods

upgrade_v0!() click to toggle source

This upgrades the internal data representation from V0 (the initial version) to V1.

# File lib/vagrant/plugin/state_file.rb, line 103
def upgrade_v0!
  @data["version"] = "1"

  new_installed = {}
  (@data["installed"] || []).each do |plugin|
    new_installed[plugin] = {
      "ruby_version"    => "0",
      "vagrant_version" => "0",
    }
  end

  @data["installed"] = new_installed

  save!
end