Sunday, September 9, 2012

Getting started with using Pub on Windows – The Dart Package Manager

The Dart language is a thriving community that is growing and with it come the tools that assist with doing modern development.  One of the key tools that define modern development is a package manager and with Dart, the job of package management falls upon the Pub tool.  You can read an overview of the Pub tool on the Dartlang.org website.

A common question that I see in new groups is “How do I use pub on Windows?”.  In this article I’ll walk through using pub to help get the Dartsweeper application downloaded from GitHub and running on your machine. The basic steps that we are about to walk through are:

  • Install the Dart Editor on your machine
  • Install Git (distributed revision control and source code management (SCM) system)
  • Update some Windows Environment variables
  • Clone the Git source that we want
  • Run Pub to install dependencies
  • Run the application from within Dart Editor

OK, let’s get started

Get the Dart SDK

To get started, you need to have a few things in place on your machine.  The first thing is to ensure you have the Dart SDK.  The best way to get that is to download the Dart Editor which contains the Dart SDK files.  You can get this from the Dart Editor Download Page.

Once you have downloaded the .zip file, simply unzip the files to a location on your local machine.  The pub tool ships with the Dart SDK and you can find it at the following location: dart-sdk\util\pub

Configure Windows Environment Variables

You’ll need to configure a couple of Windows environment variables which make it easier to work with the SDK tools.  First, add an environment called DART_SDK and point it at the root SDK folder in your unzipped Dart download.  Next, update your Windows System Path variable and add a path which points to the bin folder of the SDK folder:

image

The reason for pointing the Path variable at the bin folder is because that’s where the main tools that you will need to use are located.  The DART_SDK variable however needs to point at the root SDK folder because it is used to access SDK resources other than just the SDK bin tools alone – e.g. Libraries, Packages, and other Utilities.

Test that you have configured your environment variables correctly by opening a Windows Command Prompt and type ‘pub help’ and you should see Help text for the Pub tool displayed:

image

Install Git

Many of the packages that you will want to include will require Git to pull them down.  Pub itself has support for working with Git via native protocol handlers which can be declared within the pubspec configuration files.  To get Git, download the latest stable build from the Git website.

Test that you have Git correctly by opening a Windows Command Prompt and type ‘git –version’ and you should see the version number for your Git installation displayed:

image

Grab Dartsweeper from Git

Now that we have all of our tools installed, it’s simply a matter of using Git commands to fetch the Dartsweeper files and then we’ll use Pub to update all of its dependencies.

Open a Windows Command Prompt and change to a directory where you want to work from and type the following commands:

> mkdir %USERPROFILE%\dart
> cd %USERPROFILE%\dart
> git clone
https://github.com/kevmoo/sweeper.dart.git
> cd sweeper.dart
> pub install

This will make a folder under your user profile called Dart and then clone the contents of Dartsweeper repo on GitHub into it.  Finally we run the pub install command from within the newly downloaded Dartsweeper folder.

Here you can see the result of running those commands in real time:

image

Running Dartsweeper

Now that we have Dartsweeper installed, it’s time to open it up in the Dart Editor and run it.  Open Dart Editor and open the sweeper.dart folder that we just grabbed from GitHub.

image

When you first open the application in Dart Editor, you will notice some red crosses against folders which indicate compiler errors:

image

The reason for this is that, currently in the Dart Editor, we need set the location for the packages on a per application basis.  In this case, we simply need to open Tools|Preferences and change the location that we want the compiler to look in for packages for this application.

image

After doing that, you can right-click on the test folder and exclude it by choosing Don’t Analyze to remove it from the compilation analysis process.  That should remove the remaining compiler error warning.

Now press Ctrl+R (or hit the Run button) to run the application in the Dartium build of Chrome:

image

4 comments:

  1. Big thx for this set-up tutorial. I am able to receive the data von github and also pub install is successfully. So i guess pub works, but when starting sweeper within Darteditor, there is no blue "playground" matrix. One can only see New Game button. Anyway, big thx!

    ReplyDelete
  2. When you run the application, what URL comes up in the browser? It should be /html/index.html

    ReplyDelete
  3. sweeper.dart/html/index.html ok this works now. The canvas based version reports an error:File not found: git/sweeper.dart/html/canvas (i think it states on the website that this project will not yet work with canvas+dartium)
    Btw, your tutorial rocks, but maybe you should be more specific with CTRL+R: it is a DARTIUM LAUNCH with HTML file: /sweeper.dart/html/index.html

    ReplyDelete
  4. sweeper.dart now requires authentication

    ReplyDelete