Linking to a CodePlex project’s latest source code

CodePlex is a source code distribution platform that stacks up against the competition (like GitHub) very nicely. The one feature I found myself wishing I could find for my CodePlex projects is one that GitHub does very easily: permanent links to download the latest source code of a project. Even though this isn’t provided by CodePlex itself, it’s fairly easy to set up your own service to link to your latest code.

You may have noticed the “Downloads” sidebar widget that appears on some pages throughout this site. If you’ve read my post on WordPress custom fields you’ll have a pretty good idea of how it operates. The purpose of those links is to point to the most up-to-date version of code for each project, and with my GitHub projects that’s easy as the main download link for GitHub code always points to the most recent version.

CodePlex, however, does things a little differently. They dynamically create a new download link for each code commit, meaning that every time the code is updated a new link is created to point at the new code. This caused me some pain in my “Downloads” widget because I would have to go back through and change the links every time I made an update to the project.

Getting around it

I figured there must be some sort of way to dynamically find the latest code on a CodePlex project, and some poking around on the site yielded the discovery I needed. Each CodePlex project has a set of associated RSS feeds, one of which tracks new code commits and includes a link for each commit that contains the ID needed to construct a link to the source package.

The first step is simply connecting to the RSS feed and loading its data. I built my solution in PHP, but I imagine an ASP.NET version would be similarly simple to construct.

Note the CURLOPT_RETURNTRANSFER option. This is necessary to store the returned content to a variable instead of passing it straight to the browser. And of course be sure to replace both instances of “YOUR_PROJECT_NAME” in the URL string with the actual name of your project.

Once the content has been loaded, I used PHP’s DOMDocument system to move through the XML document to the first content item and get the URL from the appropriate node.

The link will have the required ID immediately following the final “/”, so by splitting the string into an array at each “/” we can take the last item and use that as our ID.

Then all that remains to do is construct the link to the latest code and redirect the user there. By using header redirects on the server you can even prevent the browser from attempting to load a new page, a .zip file should begin downloading without actually leaving the page from which the link was clicked.

Again, remember to change “YOUR_POJECT_NAME” to the actual name of your project, and you should be all set. This file will now always start the download of the most recent source code of its associated CodePlex Project.

Tagged with: , , ,
Posted in CodePlex, PHP, Server Side Code

Leave a Reply

Your email address will not be published. Required fields are marked *

*