Skip to content

Commit 9e10d77

Browse files
committed
Move newlines out of quoted header and footer. Match carriage return along with newline in hosts file pattern.
1 parent 8a24d73 commit 9e10d77

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/vagrant-hostmanager/hosts_file/updater.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def update_file(file, resolving_machine = nil, include_id = true, line_endings)
8888

8989
def update_content(file_content, resolving_machine, include_id, line_endings)
9090
id = include_id ? " id: #{read_or_create_id}" : ""
91-
header = "## vagrant-hostmanager-start#{id}\n"
92-
footer = "## vagrant-hostmanager-end\n"
91+
header = "## vagrant-hostmanager-start#{id}"
92+
footer = "## vagrant-hostmanager-end"
9393
body = get_machines
9494
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
9595
.join
@@ -146,12 +146,12 @@ def get_new_content(header, footer, body, old_content, line_endings)
146146
if body.empty?
147147
block = "\n"
148148
else
149-
block = "\n\n" + header + body + footer + "\n"
149+
block = "\n\n" + header + "\n" + body + footer + "\n\n"
150150
end
151151
# Pattern for finding existing block
152152
header_pattern = Regexp.quote(header)
153153
footer_pattern = Regexp.quote(footer)
154-
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
154+
pattern = Regexp.new("[\r\n]*#{header_pattern}.*?#{footer_pattern}[\r\n]*", Regexp::MULTILINE)
155155
# Replace existing block or append
156156
content = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
157157
if line_endings == "crlf"

0 commit comments

Comments
 (0)