Jun 25, 2007

Automatically update to latest WebKit each night.

You like webkit. You want the latest WebKit installed fresh each night while you sleep. You've looked at NightShift and found that it lacked a certain simplicity. You came here.

Ok. We need a script to do the dirty work. Bash is fine:


#!/bin/bash
# coding: utf-8

# Update Webkit
# Alex Ross

function handle {
 echo "An error occured!"
 exit $?
}

trap handle ERR

APPLICATIONS="/Applications"
ORG='http://nightly.webkit.org'

LOCAL_REVISION=`cat /Applications/WebKit.app/Contents/Resources/VERSION`
LATEST_REVISION_PATH=`curl --silent $ORG | grep -o "/files/trunk/mac/WebKit-SVN-r[0-9]*.dmg" | head -n 1`
LATEST_REVISION=`echo \"$ORG+$LATEST_REVISION_PATH\" | grep -E '[0-9]{4,}' -io`
if [ $LOCAL_REVISION -eq $LATEST_REVISION ]; then
  echo "Local WebKit r$LOCAL_REVISION is latest build, exiting." > /dev/console  
  exit
else
  echo "Updating WebKit.app to r$LATEST_REVISION..." > /dev/console  
fi

curl -sL $ORG$LATEST_REVISION_PATH > /tmp/latest-webkit-svn.dmg
hdiutil attach /tmp/latest-webkit-svn.dmg -mountpoint /tmp/latest-webkit-svn -quiet

osascript <<-APPLESCRIPT
tell application "Finder"
activate
set answer to display dialog "Would you like to update WebKit now? Your open Webkit and Drosera windows will close.

Updating automatically in 30 seconds…" with title "WebKit Updater" buttons {"No", "Yes"} default button {"Yes"} giving up after 30


if button returned of answer is "Yes" or button returned of answer is not "No" then
 tell application "System Events"
  if (name of processes) contains "WebKit" then quit application "WebKit" saving no
  if (name of processes) contains "DroseraLauncher" then quit application "Drosera" saving no
 end tell
end if

end tell

delay 0.25

tell application "System Events"
 if (name of processes) does not contain "WebKit" and (name of processes) does not contain "DroseraLauncher" then
  return 1
 else
  return 0
 end if
end tell
APPLESCRIPT

rm -rf $APPLICATIONS/Drosera.app $APPLICATIONS/WebKit.app

cp -R /tmp/latest-webkit-svn/Drosera.app $APPLICATIONS/Drosera.app
cp -R /tmp/latest-webkit-svn/WebKit.app $APPLICATIONS/WebKit.app

hdiutil detach /tmp/latest-webkit-svn -quiet
rm /tmp/latest-webkit-svn.dmg

echo "Webkit is up-to-date." > /dev/console

Paste all this junk into a text file and save it to /Library/Scripts/update_webkit.sh.

We create a launch agent to run this script each morning at 4am. Open a blank text file and paste in this xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Label</key>
 <string>UpdateWebKit</string>
 <key>UserName</key>
 <string>root</string>
 <key>GroupName</key>
 <string>wheel</string>
 <key>ProgramArguments</key>
 <array>
  <string>/bin/sh</string>
  <string>/Library/Scripts/update_webkit.sh</string>
 </array>
 <key>StartCalendarInterval</key>
 <dict>
  <key>Hour</key>
  <integer>4</integer>
 </dict>
</dict>
</plist>

That's it! You're done. Mmmm. I love the smell of fresh nightlies in the morning.

2 comments:

Unknown said...

tried this on leopard


i got the following

./web.sh
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
./web.sh: line 16: http://nightly.webkit.org/files/mac/WebKit-SVN-r[0-9]*.dmg: No such file or directory
./web.sh: line 19: syntax error near unexpected token `newline'
./web.sh: line 19: ` echo "Local WebKit r$LOCAL_REVISION is latest build, exiting." > '

Alex said...

It should be fixed now.