Automating Generation of xip.io URLs for Projects

posted


NOTE: This tip is for OSX-only and also requires that you have set up Anvil and/or Pow. If you install Anvil, it will prompt you to install pow if you haven’t set it up already.

Local Development == Winning

So, you’ve got Anvil (or just Pow) set up and are loving your .dev domains, I know I love them.

But, have you used xip.io to share those .dev domains with your coworkers?

Sharing Local Dev URLs

xip.io comes to you from the same people who built Pow, and it’s main purpose is to facilitate sharing links to Pow projects across your local network. I think with a static IP and some router magic you can share them outside of your local network, but I’m not entirely sure on that point.

If your Pow domain is example.dev your xip.io URL will look something like this http://example.10.10.0.8.xip.io/ where 10.10.0.8 is replaced with your local IP address.

It’s not terribly hard to come up with this URL on your own, either by memorizing your local IP or running an ifconfig every time you want to generate a link to your .dev site, but in the spirit of making things faster and easier, why not find a way to automate the process.

Enter xip.io.scpt

I created an AppleScript that will generate this URL for you, add it to your clipboard, and print it to the Terminal.

Now, we could stop there and just run that AppleScript when we need an xip.io URL, but let’s take it a step further with an alias.

When a Problem Comes Along You Must xipit, xipit Good

So, open your .bash_profile or .bashrc or wherever you like to store your aliases and add this line:

alias xipit='osascript xip.io.scpt'  

Now, you may have noticed that I didn’t specify a path to that file and wonder if that means you need to copy and paste it into every project you will be working on.

You could do it that way, or you could create a symlink to the file in each project, so we only ever have to edit one file if we want to update the AppleScript, but our xip.io url will always use the name of the directory your project is in, which is what pow uses to generate your .dev URL.

I have another alias for commands that I run when initializing a project folder, and one of the commands I added to that alias is ln -s ~/Sites/config/xip.io.scpt so that symlink gets created automatically.

It’s especially helpful to do the symlinking with an alias because I’ve set up Pow to serve PHP sites and there is a config.ru file, which tells Rack how to serve them, that I need to symlink in each project, as well.

The AppleScript

Here’s a gist of xip.io.scpt, and the full code of the AppleScript with some comments regarding the symlinking and bash alias.

<code data-language="applescript">
-- generates xip.io url -- I store this script in my ~/Sites directory and then use ln -s ~/Sites/xip.io.scpt in Terminal to create a symbolic link in the folder for my site -- add this alias to your .bash_profile or .bashrc for quickly using it via Terminal: alias xipit='osascript xip.io.scpt'

-- get IP address set tIP to do shell script "ifconfig en0|grep 'inet '|cut -d ' ' -f 2"

if(tIP is "") then set tIP to do shell script "ifconfig en1|grep 'inet '|cut -d ' ' -f 2" end if

-- get current directory set currentDir to do shell script "echo ${PWD##*/}"

-- set url set urlString to "http://" & currentDir & "." & tIP & ".xip.io"

-- add url to clipboard set the clipboard to urlString

do shell script "echo " & urlString

Enjoy local developing made easier.

For PHP

Below, I provide some brief information regarding getting pow and apache to cooperate.

I will be providing a more detailed post that outlines the process I went through, with a little more information for those who aren’t well versed in apache.

I’m not even average when it comes to dealing with server configuration and I got it working, so you shouldn’t have much trouble, but may require a little trial and error.

Bonus Points

If you’re a PHP developer, you may notice that pow and apache don’t really get along, so you’ll have to turn off pow every time you want to test a local PHP site.

You can follow the instructions for running pow and apache in harmony from 37signals (they created pow and xip.io) and you won’t have to worry about that problem anymore.

More Bonus Points

Why not set up your local PHP sites to run through pow, as well?

Follow the steps for setting up pow with PHP support and you can.

I had to remove the third to last line in the config.ru file to get my CSS and JavaScript loading correctly on WordPress sites, so you may need to try that if things aren’t loading correctly.

Here’s the offending line, for reference: use Rack::Legacy::Cgi, Dir.getwd

And here is a link to my gist of the config.ru file with that line commented out.

WordPress Path Issues

So, you may notice an issue when other people open your xip.io links where WordPress sites won’t load their assets. This can be solved with a plug-in: Root Relative URLs.

Just install it and activate it and you’re good to go.

NOTE: You could just stick to pow for serving up your PHP sites and forego the whole apache + pow setup, but if you like using PHPMyAdmin for local MySQL management it doesn’t play nice with pow, and this way you have the option of dealing with a regular apache setup whenever you want.

If you have any questions or improvements to the process, my AppleScript, or my shell modifications, please let me know via the comments or Twitter: @allenericr.

comments powered by Disqus