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
# 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 << '$HostsPath = "$env:windir\\System32\\drivers\\etc\\hosts"'
  script << '$Hosts = gc $HostsPath'

  host_defs = "'" + host_entries.join('\', \'') + "'"
  script << "@(#{host_defs}) | % { if (\$Hosts -notcontains \$_) { Add-Content -Path \$HostsPath -Value \$_ }}"

  @machine.communicate.sudo(script.join("; "))
end