Create workaround for brew script sudo timeout

Not sure why the clever little sudo keepalive hack isn't working,
perhaps because brew.sh is being called by another script (which also
isn't keeping sudo alive?). But because sudo seems to be timing out
anyway, move sudoed commands as close to the beginning of the script
as possible, and add a few keepalive commands between non-sudoed
dependencies.