VideoBar

This content is not yet available over encrypted connections.

Wednesday, January 18, 2012

Dart, Windows, Powershell, Oh My!

I’ve been working with the Dart SDK on Windows for just over a week now and I thought I would share a couple tips for working with Dart in the Windows environment.

I can’t guarantee any of this will work on your setup, Windows being what it is, but I hope you find it useful.

My Windows Setup
Windows 7 Ultimate 64-bit

Keeping Dart Up-To-Date
On my Ubuntu build, I was able to quickly whip up a script to download and update the Dart Editor and SDK for me.  I wanted that same functionality on Windows, which meant a foray into new territory for me:  Powershell.

After much Googlery, I was able to come up with a brute-force Powershell script that:
  1. Backs up the current SDK and Editor into corresponding “previous” directories.
  2. Downloads the Editor and SDK zip files from the continuous integration Dart repository.
  3. Unzips the Editor and SDK into the corresponding “latest” directories.
There are some pre-requisites:

7zip must reside somewhere in your PATH and must be named ‘sz.exe’ (I had trouble with Powershell executing it when it was named ‘7z.exe’, so I just made a copy of it called 'sz.exe')

In your “Dart” directory, you must have the following sub-directories created:
  • sdk_latest
  • sdk_previous
  • editor_latest
  • editor_previous
You can change the names of these directories in the script if you like. Here is the script:
# download and update dart environment

# vars
$dpath = 'd:\development\dart' # path to dart environment root
$sdk = 'sdk_latest'            # directory where the current dart sdk will reside
$sdkp = 'sdk_previous'         # directory where the previous dard sdk will reside
$editor = 'editor_latest'      # directory where the current dart editor will reside
$editorp = 'editor_previous'   # directory where the previous dart editor will reside

# Dart CI repository
$dartrepo = 'http://gsdview.appspot.com/dart-editor-archive-continuous/latest/'

# CI SDK and Editor file names
$sdkfile = 'dart-win32.zip'

# *** ALERT! *** Set for win32 editor if you aren't using 64-bit!
$editorfile = 'DartBuild-win32.win32.x86_64.zip'

# get a reference to a new web client
$wc = new-object System.Net.WebClient

write-host '*** Updating Dart Environment' -foregroundColor Yellow

function dobackup ([string]$s, [string]$d){
    
    # remove destination directory files
    rm $d\* -Recurse
    
    # copy source files to destination
    copy $s\* $d -Recurse;
    
    # remove source directory files
    rm $s\* -Recurse
}

function download ([string]$f, [string]$d) {
    $wc.DownloadFile($dartrepo+$f,$d);
}

write-host '... Backing up SDK'
dobackup $dpath\$sdk $dpath\$sdkp;

write-host '... Backing up Editor'
dobackup $dpath\$editor $dpath\$editorp;

write-host '... Downloading SDK'
download $sdkfile $dpath\$sdk\$sdkfile

write-host '... Downloading Editor'
download $editorfile $dpath\$editor\$editorfile

write-host '... Extracting SDK files'
set-location $dpath\$sdk | out-null
sz.exe x -y $sdkfile | out-null

write-host '... Extracting Dart Editor files'
set-location $dpath\$editor | out-null
sz.exe x -y $editorfile | out-null

set-location $dpath | out-null
write-host '*** Finished ***' -foregroundColor Yellow

(I called this one ‘updatedart.ps1’)

Compiling With Frog
If you’re developing a non-trivial application with Dart, chances are that you’re also compiling with frog instead of the dartc compiler that is currently used by Dart Editor.  Now, there is a way to get frog to work in the Editor via a configuration script, but I’ve yet to get it working on the windows version.  That leaves me with the requirement to compile form the command line.  I use a powershell script to make this a bit easier.  Again, I’m sure this script could be improved, but I’m a Powershell n00b:

# compile a dart project from anywhere

param([string]$dartfile = "")

if (!$dartfile)
{
    return "Missing dart file parameter";
}

$curdir = [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath

# Adjust this to wherever your dart sdk installation is
$dartpath = "d:\development\dart\sdk_latest\dart-sdk\bin"

#remove an existing js file
rm *.js

write-host 'Compiling Dart Application'
set-location $dartpath | out-null

$fullstring = "--out=" + $curdir + "\" + $dartfile + ".app.js " + $curdir + "\" + $dartfile

$file = $curdir + "\" + $dartfile
$output = $dartfile + ".js"
$renamed = $dartfile + ".app.js"

.\frogc.bat $file
set-location $curdir | out-null
ren $output $renamed 

I called this script “cdart.ps1” and made a powershell alias to it called “cdart”.  So compiling a dart app in Powershell looks like this:

cdart mydartapp.dart

I hope you find these two tips useful when working with Dart in the windows environment.

2 comments:

  1. Scripts worked like a charm. I did not have to rename 7z.exe, just used an absolute path to where I have it installed.

    write-host '... Extracting SDK files'
    set-location $dpath\$sdk | out-null
    C:\LiberKey\Apps\7Zip\App\7-Zip\x86\7z.exe x -y $sdkfile | out-null

    write-host '... Extracting Dart Editor files'
    set-location $dpath\$editor | out-null
    C:\LiberKey\Apps\7Zip\App\7-Zip\x86\7z.exe x -y $editorfile | out-null

    ReplyDelete
  2. Glad it worked nicely for you. :)

    ReplyDelete