home  |  articles  |  quotes   ::   website  |  profile

Ruby

RPoint – Why Ruby? What about XML?

I wanted to do a quick post on why I chose Ruby for RPoint. I talked about this a little in the first post but just looking at the examples I’ve given so far, one could make the argument that rather than creating these scripts in ruby, you could just create a simple XML document that defines your structure, templates, etc.

This is very true – and in fact I have written tools like this in the past. But here’s the problem I’ve encountered with XML. You end up trying to build a programming language out of XML. That’s basically where CAML has ended up. And I don’t know a single person who actually enjoys looking at, much less creating, CAML.

Here’s what I like about the ruby approach. It let’s you write something like this:

site_names = ["Site One", "Site Two", "Site Three"]
at "http://localhost" do
  site_names.each do |name|
    create_web name, TeamSite
  end
end

Just the simple addition of an array of site names is something that would be hard to do in XML. What if you wanted to pull your information from a database? Or a file system? Or an Excel spreadsheet? For any of those you would have to write more logic into your XML mini-language – constructs like looping, conditionals, some mechanism to handle plug-ins for additional logic, etc. By leveraging an actual programming language, we get all of that for free.

Want to create 50 test sites? Here you go.

at "http://localhost" do
  50.times do |i|
    create_web "Site #{i}", BlankSite
  end
end

’nuff said.

RPoint
Ruby
SharePoint

Comments (4)

Permalink

RPoint – Hopes and Dreams

Yesterday I posted about RPoint for the first time and I showed you a simple script to create a site structure. All of that code is working today. I wanted to keep the example simple and show you something that was actually functional.

Today I want to dream a little. Here are a few scripts I want RPoint to be able to support.

Creating a List Template

This script would create a list template.

class Milestones
  include ListTemplateMixin

  name "Milestones"
  description "A Simple Milestone List"

  column "Milestone", Text, :is_title => true, :required => true
  column "Description", RichText
  column "Date", Date, :required => true, :default => :today

  view "All Milestones" do
    show_columns "Milestone", "Date"
  end

  view "Overdue Milestones" do
    show_columns "Milestone", "Date"
    where :date => less_than(:today)
    order_by :date => :descending
  end
end

Once you had defined your template, you could use it in a few different ways. First, you could use it in a site creation script:

at "http://localhost" do
  create_list "My Milestones", Milestones
end

Second, and possibly cooler, you could generate a list definition:

>ir generate.rb list_definition --source MilestoneTemplate.rb

Migrations

Migrations in Rails is a great pre-defined structure for handling your database schema and changes to it over time. It may not be anything too ground-breaking on its own, but it enforces a discipline when it comes to managing your database. You are encouraged to think about situations where you want to change your database but data already exists. You can write scripts to migrate existing data to your new structure and it’s all managed within source control as an ordered list of steps to execute.

I want to be able to do something similar with RPoint.

# first migration - create my site collection
class CreateSiteMigration
  at :webapp => "http://localhost" do
    create_site "My Site", TeamSite
  end
end

# second migration - add a list and add a lookup to that list on an existing list
class AddIssuesMigration
  at "http://localhost/sites/mysite" do
    create_list "Issues", Issues
    update_list "Tasks" do
      add_column "Related Issue", Lookup("Issues", :field => :title)
    end
  end
end

Okay – that’s enough dreaming for now. Just incase anyone missed the intro THIS CODE DOES NOT WORK. It is completely imaginary. I am hoping that something along these lines will become reality. And I hope that you will agree that this would be a much nicer way to create SharePoint solutions than slogging through hundreds of lines of XML.

RPoint
Ruby
SharePoint

Comments (0)

Permalink

RPoint – a DSL for SharePoint

A few days back I talked about how the release of IronRuby would be great for SharePoint scripting. In my opinion, what makes Ruby such a powerful language is that it is great for creating domain specific languages (DSLs). I’m going to keep this post short so I won’t go into what DSLs are or examples of them. If you want to find out more info, look at some other ruby libraries such as rspec, rake, and datamapper.

I think that SharePoint is in dire need of better tools to facilitate solution development, deployment, and maintenance. It is simply too difficult to build a site in one environment and then deploy it in production. Sure you can do backup and restore, but what about when you want to make some upgrades?

Anyway, this weekend I started working on an IronRuby-based DSL for SharePoint scripting. I’m calling it RPoint for now (until I think of something sexier!). Now, this is very very very early in development and I am sure things aren’t set up today as nicely as they should be long term, but I’ve been really excited by the progress and the possibilities.

RPoint will allow you to write code like this to create SharePoint sites:

# create a new subweb beneath an existing site
at "http://localhost" do
  create_web "New Web", TeamSite
end

# create a new site collection and website structure
at :webapp => "http://localhost" do
  create_site "New Site Collection", BlankSite, "domain\\user" do
    inside root_web do

      # create our site structure
      create_web "Child Web", TeamSite
      create_web "Another Child", BlankSite do
        create_web "Private Site", TeamSite, :inherit_permissions => false
      end

      # create a few lists
      create_list "Documents", DocumentLibrary
      create_list "Announcements", Announcements

    end
  end
end

I think that’s pretty cool. Beyond simply creating sites, RPoint will allow you to create lists, views and probably upload resources like images and documents.

My goals for RPoint are to be able to do the following:

  • Easily write scripts to create site structures and lists
  • Rails-like “Migrations” for applying changes to existing sites
  • Custom site template and list definitions in ruby as opposed to XML
  • Ability to run template/list definitions against a live site or generate schema.xml/onet.xml files for production deployment
  • Support web-services automation as well as object model automation (this will be tough)

SharePoint is a huge product. The code-base is enormous and the aspirations for RPoint are lofty. I would greatly appreciate any assistance. The more hands there are to help, the sooner we will have a tool to make SharePoint development a little easier.

To get RPoint in its current form, you can download it from GitHub.

RPoint
Ruby
SharePoint

Comments (2)

Permalink

IronRuby – a SharePoint Developer’s Best Friend

I wrote a while back about SharePoint scripting with IronPython. Well today’s announcement of the first binary release of IronRuby got me pretty darn excited. I really like Python, but I LOVE Ruby. I’ve been waiting for IronRuby to catch up to IronPython for quite a while and it looks like they’re getting close.

Here’s a quick guide to getting up and running with IronRuby and SharePoint.

Installation:

  1. First, download the IronRuby release from Rubyforge.
  2. Next, copy the ironruby folder you extract to your Program Files (put it wherever you like – that’s where I put mine).
  3. Update your system’s PATH environment variable to contain the path to the ironruby\bin folder.  So if you placed it in your Program Files, you would add this to your PATH: c:\program files\ironruby\bin

Creating a Script:

Next you’ll want to create a new text file called print_sites.rb. I recommend the e text editor by the way – great tool.  Add the following lines to your file:

require 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL'
require 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL'

Uri = System::Uri
SPSite = Microsoft::SharePoint::SPSite
SPWebApplication = Microsoft::SharePoint::Administration::SPWebApplication

Okay – what does that do? Well the first two lines are like adding a reference to a standard Visual Studio project. They tell IronRuby that we need to reference (require) the System and Microsoft.SharePoint assemblies. The next three lines are for convenience. They set up aliases to the System.Uri class and two SharePoint classes. In our ruby code, now we can just use Uri, SPSite, etc instead of having to type out the fully qualified path.

Next, let’s add some ruby code to enumerate all sites in a web application.

webapp = SPWebApplication.Lookup(Uri.new("http://localhost"))
webapp.Sites.each do |site|
  puts site.Url
end

(hmm, looks like I don’t actually need the SPSite alias after all)

Now, save your file. You can execute it like this:

> ir print_sites.rb

If everything is set up right, you should see the URL for each site collection be printed to the screen.

There you go. Have fun.

Ruby
SharePoint

Comments (1)

Permalink

Region Folding in Textmate

As a long time Microsoft developer who is in the progress of branching out and learning new things, I sometimes find it hard to give up certain features I’ve grown accustom to.  When it comes to writing code, one feature of Visual Studio I have learned to rely on is folding sections of code using #region and #endregion.  I find regions to be a great way to organize code around different subjects, activities, etc.  For me it’s a way to reduce the visual clutter and immediately jump to the section of code I need to work on.

On the Mac, TextMate does a great job of code folding based on syntax.  I’m doing a fair amount of work with Ruby these days and TextMate is an excellent tool.  Unfortunately for me however all folding is based on the syntax of the code, and not extra non-code markers like #region.  Luckily TextMate’s bundle system is completely extensible and customizable.  Folding is based on regular expressions so with a fairly simple change I was able to get #region folding in TextMate.

The first step is to figure out the regular expression.  The core regex for #region folding would be this:

^\s*\#region
^\s*\#endregion

Next open up TextMate’s Bundle editor, open the Ruby language, and locate  the foldingStartMarker and foldingStopMarker sections.  Below is the entire contents of my foldingStartMarker and foldingStopMarker sections.

foldingStartMarker = '(?x)^
(\s*+
(module|class|def
|unless|if
|case
|begin
|for|while|until
|(  "(\\.|[^"])*+"          # eat a double quoted string
| ''(\\.|[^''])*+''        # eat a single quoted string
|   [^#"'']                # eat all but comments and strings
)*
(                 \s   (do|begin|case)
| [-+=&|*/~%^<>~] \s*+ (if|unless)
)
)\b
(?! [^;]*+ ; .*? \bend\b )
|(  "(\\.|[^"])*+"              # eat a double quoted string
| ''(\\.|[^''])*+''            # eat a single quoted string
|   [^#"'']                    # eat all but comments and strings
)*
( \{ (?!  [^}]*+ \} )
| \[ (?! [^\]]*+ \] )
)
).*$
|   [#] .*? \(fold\) \s*+ $         # Sune’s special marker
|    ^\s*\#region
';
foldingStopMarker = '(?x)
(   (^|;) \s*+ end   \s*+ ([#].*)? $
|   ^     \s*+ [}\]] \s*+ ([#].*)? $
|   [#] .*? \(end\) \s*+ $    # Sune’s special marker
|   ^\s*\#endregion
)';

You can see where my two regular expressions were added near the end of each section.

Once you do that, close the editor, reload your bundles, and you’re good to go.

Ruby
Software Development

Comments (2)

Permalink