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 Method Summary collapse

Constructor Details

#initialize(path) ⇒ StateFile

Returns a new instance of StateFile.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vagrant/plugin/state_file.rb', line 10

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 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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant/plugin/state_file.rb', line 32

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]
  }

  save!
end

#add_source(url) ⇒ Object

Adds a RubyGems index source to look up gems.

Parameters:

  • url (String)

    URL of the source.



48
49
50
51
52
# File 'lib/vagrant/plugin/state_file.rb', line 48

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)


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

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)


59
60
61
# File 'lib/vagrant/plugin/state_file.rb', line 59

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.



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

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



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

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

#save!Object

This saves the state back into the state file.



96
97
98
99
100
101
102
103
104
105
# File 'lib/vagrant/plugin/state_file.rb', line 96

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>)


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

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