Class: VagrantHosts::Cap::SyncHosts::Windows

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant-hosts/cap/sync_hosts/windows.rb

Overview

Provide a base class for syncing hosts entries on Windows systems.

Instance Method Summary collapse

Methods inherited from Base

#initialize, #sync!, sync_hosts

Constructor Details

This class inherits a constructor from VagrantHosts::Cap::SyncHosts::Base

Instance Method Details

#update_hostsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-hosts/cap/sync_hosts/windows.rb', line 4

def update_hosts
  host_entries = []
  all_hosts(@config).each do |(address, aliases)|
    aliases.each do |name|
      host_entries << "#{address} #{name}"
    end
  end

  script = []
  script << '$HostsLocation = "$env:windir\\System32\\drivers\\etc\\hosts";'

  host_entries.each do |entry|
    script << "\$HostEntry = \"#{entry}\""
    script << "if (!((gc \$HostsLocation) -contains $HostEntry)) { Add-Content -Path $HostsLocation -Value $HostEntry; }"
  end

  @machine.communicate.sudo(script.join("\r\n"))
end