Class: Vagrant::Plugin::StateFile

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/plugin/state_file.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ StateFile

Returns a new instance of StateFile.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant/plugin/state_file.rb', line 17

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

Instance Attribute Details

#pathPathname (readonly)

Returns path to file.

Returns:

  • (Pathname)

    path to file



15
16
17
# File 'lib/vagrant/plugin/state_file.rb', line 15

def path
  @path
end

Instance Method Details

#add_plugin(name, **opts) ⇒ Object

Add a plugin that is installed to the state file.

Parameters:

  • name (String)

    The name of the plugin



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant/plugin/state_file.rb', line 39

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] || [],
    "installed_gem_version" => opts[:installed_gem_version],
    "env_local"             => !!opts[:env_local]
  }

  save!
end

#add_source(url) ⇒ Object

Adds a RubyGems index source to look up gems.

Parameters:

  • url (String)

    URL of the source.



56
57
58
59
60
# File 'lib/vagrant/plugin/state_file.rb', line 56

def add_source(url)
  @data["sources"] ||= []
  @data["sources"] << url if !@data["sources"].include?(url)
  save!
end

#has_plugin?(name) ⇒ Boolean

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/vagrant/plugin/state_file.rb', line 74

def has_plugin?(name)
  @data["installed"].key?(name)
end

#installed_pluginsHash

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

Returns:

  • (Hash)


67
68
69
# File 'lib/vagrant/plugin/state_file.rb', line 67

def installed_plugins
  @data["installed"]
end

#remove_plugin(name) ⇒ Object

Remove a plugin that is installed from the state file.

Parameters:

  • name (String)

    The name of the plugin.



81
82
83
84
# File 'lib/vagrant/plugin/state_file.rb', line 81

def remove_plugin(name)
  @data["installed"].delete(name)
  save!
end

#remove_source(url) ⇒ Object

Remove a source for RubyGems.

Parameters:

  • url (String)

    URL of the source



89
90
91
92
93
# File 'lib/vagrant/plugin/state_file.rb', line 89

def remove_source(url)
  @data["sources"] ||= []
  @data["sources"].delete(url)
  save!
end

#save!Object

This saves the state back into the state file.



104
105
106
107
108
109
110
111
112
113
# File 'lib/vagrant/plugin/state_file.rb', line 104

def save!
  Tempfile.open(@path.basename.to_s, @path.dirname.to_s) do |f|
    f.binmode
    f.write(JSON.dump(@data))
    f.fsync
    f.chmod(0644)
    f.close
    FileUtils.mv(f.path, @path)
  end
end

#sourcesArray<String>

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

Returns:

  • (Array<String>)


99
100
101
# File 'lib/vagrant/plugin/state_file.rb', line 99

def sources
  @data["sources"] || []
end