Random posts about coding

Mostly blogging about dart.

First Steps to Dart Debugging


The debugger implementations are starting to land in bleeding_edge. This might all change by tomorrow, but for those of you who are tracking bleeding edge the following should get you running.



Choose a server side script you would like to debug, in this post will use chat-websocket-dart sample. Launch the dart vm with the –debug flag


The dart vm will now be waiting for incoming connections on 127.0.0.1:5858, the dart script state will be paused at the main entry point. ddbg.dart can be used to send debugging commands over the connection encoded as Json. Json has been selected as the format to be used for communications between the debugger client and dart vm debugger. From another shell, go into dart/tools and run ddbg.dart


Currently the shell is very raw, so standard input is used for interacting with ddbg.dart. Following commands are currently available, hit ‘h’ to get the list of commands.


Putting a break point and resume execution in the following way.


Connect with the web socket client and we should hit the break point.


Taking a few steps we can see the line of execution change.


While all this is still raw at the moment, the direction of the debugger is starting to come along. I feel the quality of the debugger really matters for adoption, Visual Studio for me has been the leading edge of debugger UI and functionality. The amazing part of the current state of the dart vm debugger, it should be trivial for anyone to implement an interface to the dart vm debugger with Json.

How to Publish Dart to Chrome Web Store for ChromeOS via Github

Document explains at a high level how to publish Dart apps to Chrome Web Store for Google ChromeOS via Github. This application targets ChromeOS, so it was compiled to javascript. Example code was taken directly from dart source tree for testing. Thanks to +Terry Lucas for the code!

1) Chrome Web Store needs the distribution site to be verified, this can be done by adding a site via webmaster tools. Using github’s ability to publish to a subdomain on github.com this is easy to accomplish.





2) We create a github project that will contain published gh-pages and a zipped manifest for hosted app distribution via Web Store.
$ git clone git@github.com:financeCoding/dart-calculator.git
$ cd dart-calculator

Export the calculator project
$ svn export –force http://dart.googlecode.com/svn/branches/bleeding_edge/dart/samples/calculator/ .

Modify the manifest uri to the github location

Build the templates and javascript code. Package the manifest and icon image into a zip folder so Chrome Web Store can publish it.

$ export PATH=$PATH:~/dart_bleeding/dart/out/Debug_ia32/
$ ~/dart_bleeding/dart/utils/template/template calcui.tmpl calcui.dart
$ ~/Documents/DartEditor/dart/dart-sdk/bin/frogc –out=calculator.js calculator.dart
$ mkdir calculator_app
$ cp manifest.json calc_128.png ./calculator_app/
$ zip -vr ./calculator_app.zip calculator_app -x “*.DS_Store”
$ git add .
$ git commit -a -m “adding calculator”

Create the gh-pages so Google Web Store can pull our applications data.
$ git symbolic-ref HEAD refs/heads/gh-pages
$ git add .
$ git commit -a -m “First pages commit”
$ git push origin gh-pages
$ git checkout master

3) Create a web app and fill out all the non optional values, publish to testers.



Comments and suggestions welcome. It was very impressive how quickly one could distribute apps via Chrome Web Store. I think Dart will really win in this respect.

Dart Run Swarm Locally With Live Google Reader Feeds

This post is for testing out the new blogger interface and integration with g+
How to run swarm locally with google reader feeds

This information is based on the following version of dart bleeding edge:
svn info .
Path: .
URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/samples/swarm
Repository Root: http://dart.googlecode.com/svn
Repository UUID: 260f80e4-7a28-3924-810f-c04153c831b5
Revision: 6310
Node Kind: directory
Schedule: normal
Last Changed Author: dgrove@google.com
Last Changed Rev: 6201
Last Changed Date: 2012-04-04 15:29:52 -0700 (Wed, 04 Apr 2012)

Most of the instructions from the dart/samples/swarm/README still apply with a few additional touches to get it going.
cd dart/
./tools/build.py –arch=ia32
cd samples/swarm/
python update.py
easy_install –upgrade google-api-python-client
cd samples/swarm/appengine
cp -r ../../outcode/* .
cp -r ../../lib .
Apply the following patches to app.yaml, main.py, top.html found here
https://gist.github.com/2333899
Create the appengine project
enable-app-engine-project .
Create app engine project at https://appengine.google.com

Create console api project at https://code.google.com/apis/console

Enable OAuth2 access

Create the client id for localhost:8080

Add secret and client id to main.py

Start dev_appengine from appengine directory
dev_appengine.py .


Point browser to http://localhost:8080 and enable access. Connect to your google reader, I’ve had issues with having too many feeds in google reader which would cause the swarm app to crash. Suggest using a dummy account that only as a few feeds.




Youll notice that appengine console is loading up the feeds, when that activity stops, youll be able to click on the following links and swarm will load.

At this point you should see




References
https://code.google.com/apis/console
https://appengine.google.com
http://dart.googlecode.com/svn/branches/bleeding_edge/dart/samples/swarm/

Notes for uploading to appengine (warning, this app might have bugs that cause free appengine usage limits to be hit.)

Had to diable cron in cron.yaml

Add new client_id and client_secret for dartswarms.appspot.com
appcfg update .

Dart Templates

| Comments

As mentioned by Seth Ladd, Dart now has a template system in the bleeding_edge branch. One way to learn what is offered, is to look at the uitest.dart unit test. Since the template code is in flux I’ve created some scripts to help init and build templates. A sample Dart Template Example is provided to follow along with this post. Please keep in mind this is currently subject to change by the Dart team, provide feedback to help the project along. exportTemplateUtils.sh script is used for exporting all the dependent code from the dartlang code base. The template build script depends on lib, css, and template folders from the utils directory. [code lang=”shell”] 22:54:59-adam@Adams-MacBook-Air:~/dart/DartTemplateExample $ ./exportTemplateUtils.sh A lib […] Exported revision 5635. A css […] Exported revision 5635. A template […] Exported revision 5635. [/code] The DivisionSales.tmpl was ripped from the unit test and adjusted for the project. The template keyword is used for defining the template name. Inside the template you can have css, html and scripting. Dart team is currently experimenting with the following. [code lang=”html”] template DivisionSales(var divisions) { css { .division-item { background-color: #bbb; border-top: 2px solid white; line-height: 20pt; padding-left: 5px; } .product-item { background-color: lightgray; margin-left: 10px; border-top: 2px solid white; line-height: 20pt; } .product-title { position: absolute; left: 45px; } .product-name { font-weight: bold; position: absolute; left: 100px; } .product-users { position: absolute; right: 150px; font-style: italic; color: gray; width: 110px; } .expand-collapse { margin-left: 5px; margin-right: 5px; vertical-align: top; cursor: pointer; } .expand { font-size: 9pt; } .collapse { font-size: 8pt; } .show-sales { display: inherit; } .hide-sales { display: none; } .sales-item { font-family: arial; background-color: lightgray; margin-left: 10px; border-top: 1px solid white; line-height: 18pt; padding-left: 5px; } .ytd-sales { position: absolute; left: 100px; } } <div> ${#each divisions} <div class="division-item"> <span>${name}</span> <span>&nbsp;-&nbsp;</span> <span>${id}</span> </div> <div> ${#each divisions.products} <div class="product-item"> <span var=productZippy class="expand-collapse expand">▼</span> <span class=’product-title’>Product</span> <span class="product-name">${name}</span> <span class="product-users">${users}&nbsp;</span> <div class="show-sales"> ${#each products.sales} <div class="sales-item"> <span>${country}</span> <span class="ytd-sales">${yearly}</span> </div> ${/each} </div> </div> ${/each} </div> ${/each} </div> } template Header(String company, Date date) { css { .header { background-color: slateGray; font-family: arial; color: lightgray; font-weight: bold; padding-top: 20px; } } <div class=’header’ align=center> <h2>${company}</h2> <div align=right>${date}</div> </div> } [/code] The scripting is very basic at the moment. Looking at the tokenkind.dart one can get an idea of what is supported. [code lang=”java”] […] // Synthesized tokens: static final int END_NO_SCOPE_TAG = 50; // /> static final int START_EXPRESSION = 51; // ${ static final int START_COMMAND = 52; // ${# static final int END_COMMAND = 53; // ${/ static final int EACH_COMMAND = 53; // ${#each list} static final int WITH_COMMAND = 54; // ${#with object} static final int IF_COMMAND = 55; // ${#if (expression)} static final int ELSE_COMMAND = 56; // ${#else} […] [/code] buildTemplates.sh script will loop over all *.tmpl files in the current working directory and generate *.dart files for each one. [code lang=”shell”] 22:57:41-adam@Adams-MacBook-Air:~/dart/DartTemplateExample $ ./buildTemplates.sh Building dart templates Parsed /Users/adam/dart/DartTemplateExample/DivisionSales.tmpl in 3134 msec. Codegen /Users/adam/dart/DartTemplateExample/DivisionSales.tmpl in 215 msec. Wrote file /Users/adam/dart/DartTemplateExample/DivisionSales.dart in 215 msec. Copy & Paste import statements #source(‘DivisionSales.dart’); [/code] After the dart code is generated the following source statements can be copied into the main/lib. [code lang=”javascript”] #import(‘dart:html’); #source(‘DivisionSales.dart’); class Sales { String country; String yearly; Sales(this.country, this.yearly); } class Products { List<Sales> sales; String name; String users; Products(this.name, this.users, this.sales); } class Divisions { List<Products> products; String id; String name; Divisions(this.id, this.name, this.products); } class DartTemplateExample { DartTemplateExample() { } void run() { document.body.elements.add(new Header("Dollar Shave Club", new Date.now().toString()).root); List<Sales> sales = [new Sales("USA", "3500"), new Sales("USA", "4500")]; List<Products> products = [new Products("Razor", "Sammy", sales)]; List<Divisions> divisions = [new Divisions("South West", "A-Team", products), new Divisions("North East", "B-Team", products),]; DivisionSales divisionSales = new DivisionSales(divisions); document.body.elements.add(divisionSales.root); } } void main() { new DartTemplateExample().run(); } [/code] This example is deployed on github. You can also experiment with the template system to see what code gets generated on a live page. At first glance the template system is simple enough to use, I would assume things would change in the future. For now the Dart team has provided enough for people to start banging away and provide feedback.

Dart Classify Markup Example

| Comments

I was looking for a simple way to create some markup dynamically of syntax highlighted dart code, in dart! A big thanks to Bob Nystrom from the Dart team for suggesting the simplest solution. Use classify from dartdoc. A live example of this can be found here along with the source code. Example of how to use dartdoc/classify.dart dartdoc is used for generating static HTML for the api.dartlang.org site. This example shows how importing dartdoc/classify.dart one could generate syntax highlighted dart code at runtime. Patching Currently you need to modify the import statement to reference the location of classify.dart. [code lang=”java”] #import(‘../../dart_bleeding/dart/utils/dartdoc/classify.dart’, prefix:"classify"); [/code] Building Build with minfrog for best results. At the moment dart editor does not seem happy with me on importing dartdoc/classify.dart. So compiling with minfrog worked best. [code lang=”bash”] minfrog –compile-only –out=ClassifyExample.dart.js ClassifyExample.dart [/code] Using classify This is the easiest part! Create a SourceFile and add pass it along to classifySource which will return a properly formatted string of span elements that has class attributes set. From that point you can add a css style to the span elements. [code lang=”java”] String code = "main() { print(‘hello world’); }"; classify.SourceFile sf = new classify.SourceFile("sf.dart", code); String c = classify.classifySource(sf); [/code] [code lang=”html”] <span class="i ">main</span><span class="p ">(</span><span class="p ">)</span><span> </span><span class="p ">{</span><span> </span><span class="i ">print</span> <span class="p ">(</span><span class="s si">’hello world’</span> <span class="p ">)</span><span class="p ">;</span><span> </span><span class="p ">}</span> [/code] Example css style [code lang=”css”] /* Syntax highlighting. */ /* Note: these correspond to the constants in classify.dart. */ .e { color: hsl( 0, 100%, 70%); } /* Error */ .c { color: hsl(220, 20%, 65%); } /* Comment */ .i { color: hsl(220, 20%, 20%); } /* Identifier */ .k { color: hsl(220, 100%, 50%); } /* Keyword */ .p { color: hsl(220, 20%, 50%); } /* Punctuation */ .o { color: hsl(220, 40%, 50%); } /* Operator */ .s { color: hsl( 40, 90%, 40%); } /* String */ .n { color: hsl( 30, 70%, 50%); } /* Number */ .t { color: hsl(180, 40%, 40%); } /* Type Name */ .r { color: hsl(200, 100%, 50%); } /* Special Identifier */ .a { color: hsl(220, 100%, 45%); } /* Arrow */ /* Interpolated expressions within strings. */ .si { background: hsl(40, 100%, 90%); } .s.si { background: hsl(40, 80%, 95%); } .i.si { color: hsl(40, 30%, 30%); } .p.si { color: hsl(40, 60%, 40%); } [/code]

Dart: Quick 0-60 With DartEditor and Dartium on MacOSX.

| Comments

Quick 0-60 with DartEditor and Dartium on MacOSX. The DartEditor now comes with the ability to launch Dartium directly. The key to getting that to work is to have Chromium folder located in the dart-sdk. Below is a simple script to bootstrap your MacOSX from the latest builds. Note, if you move the directory around you’ll need to adjust the runDartEditor.sh script. DartEditor expects an absolute path for the -debug flag. By the time Dartium hits the dart-sdk, his route will be obsolete. Now support for choosing frogc or dartc is available in the GUI options. [sourcecode lang=”bash”] git clone git://gist.github.com/1702746.git DartEditor cd DartEditor chmod +x get_and_unpack.sh ./get_and_unpack.sh && cd dart && ./runDartEditor.sh [/sourcecode] https://gist.github.com/1702746

Dart: Updated Dartium Builds With Breakpoint Support for Linux and Mac

| Comments

As Seth Ladd has commented on google plus, Dartium now has breakpoint support. Big win in such an early stage of the project. Providing Release builds for Mac and Linux below. While I was trying breakpoints out, one thing noticed was to getting scripts to show up properly in inspector you need to hit an extra refresh on the page. My public is can be found on pgp.mit.edu. [sourcecode lang=”bash”] wget http://dl.dropbox.com/u/33138127/dartium_macosx/Chromium.app.tar.gz wget http://dl.dropbox.com/u/33138127/dartium_macosx/Chromium.app.tar.gz.sig wget http://dl.dropbox.com/u/33138127/dartium_macosx/Chromium.app.tar.gz.md5 wget http://dl.dropbox.com/u/33138127/dartium_macosx/README.txt gpg –verify Chromium.app.tar.gz.sig Chromium.app.tar.gz md5sum -c Chromium.app.tar.gz.md5 [/sourcecode] [sourcecode lang=”bash”] wget http://gsdview.appspot.com/dart-editor-archive-continuous/latest/DartBuild-linux.gtk.x86.zip wget http://dl.dropbox.com/u/33138127/dartium/dartium-linux-32bit-Release.tar.gz wget http://dl.dropbox.com/u/33138127/dartium/dartium-linux-32bit-Release.tar.gz.sig wget http://dl.dropbox.com/u/33138127/dartium/dartium-linux-32bit-Release.tar.gz.md5 wget http://dl.dropbox.com/u/33138127/dartium/README.txt gpg –verify dartium-linux-32bit-Release.tar.gz.sig dartium-linux-32bit-Release.tar.gz md5sum -c dartium-linux-32bit-Release.tar.gz.md5 [/sourcecode]

Dart: Fillstyle on Cavnas

| Comments

To be able to draw on canvas in Dart you need two things. A canvas element defined and a canvas rendering context to draw on. In this example we use a CanvasRenderingContext2D to fillstyle a set of rectangles with rgb colors of size x and y. Modified for Dart, mozilla developer example can be done the following way. Check a live example here. [sourcecode lang=”html”] <html> <head> <title>CanvasFillstyleExample</title> </head> <body> <canvas id="canvas" width="600" height="600"></canvas> <script type="text/javascript" src="CanvasFillstyleExample.dart.app.js"></script> </body> </html> [/sourcecode] [sourcecode lang=”java”] draw() { var ctx = document.query(‘canvas’).getContext(‘2d’); for (var i=0;i<6;i++){ for (var j=0;j<6;j++){ ctx.fillStyle = ‘rgb(’ + (255-42.5*i).floor().toString() + ‘,’ + (255-42.5*j).floor().toString() + ‘,0)’; ctx.fillRect(j*25,i*25,25,25); } } } [/sourcecode]