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.


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

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


12
13
14
# File 'lib/vagrant/plugin/state_file.rb', line 12

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


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant/plugin/state_file.rb', line 36

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.


53
54
55
56
57
# File 'lib/vagrant/plugin/state_file.rb', line 53

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)

71
72
73
# File 'lib/vagrant/plugin/state_file.rb', line 71

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)

64
65
66
# File 'lib/vagrant/plugin/state_file.rb', line 64

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.


78
79
80
81
# File 'lib/vagrant/plugin/state_file.rb', line 78

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


86
87
88
89
90
# File 'lib/vagrant/plugin/state_file.rb', line 86

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

#save!Object

This saves the state back into the state file.


101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant/plugin/state_file.rb', line 101

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

96
97
98
# File 'lib/vagrant/plugin/state_file.rb', line 96

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