Wednesday, February 27, 2013

Dart in the Shell Video Tutorials Now Available

(Guest post by Dartisan Kevin Moore, author of BOT, Pop Pop Win, and Dart Widgets.)

Most of the discussion about Dart revolves around browser-based web app development. Dart is also a great platform for serving web content. As I've built out a number of projects, I've realized Dart is also a great language for scripting the shell.





While Dart is a first-class programming environment for building browser-based applications, the Dart Virtual Machine (VM) was designed from the beginning to run outside of the browser as well. The Dart SDK and accompanying packages includes libraries for accessing, inspecting, and modifying the file system along with features for creating and managing native processes.

It is possible to build an entire project in Dart - from manipulating HTML elements to serving HTTP requests to test automation and build scripts.

After success building a number of shell commands in Dart I wrote a blog post: Dart is great for shell programming, too.


Dart in the Shell video tutorials
Positive responses to this write-up along with some new features I wrote for the Dart Bag of Tricks (BOT) inspired a video series I've posted on YouTube: Dart in the Shell.

The video series focuses on the Unix shell, specifically Bash. Everything was done on a Mac, but most things should be applicable for folks on Linux or those running Z shell.





Part 1: Basics of creating and running a Dart command

  • PATH environment variable
  • Using a Shebang to run a Dart command directly

Part 2: Passing args to your command

  • Changing your PATH variable permanently
  • Using the Options class to query shell arguments

Part 3: Smarter arguments with ArgParser
  • Adding a library with Pub and the Dart Editor
  • Using ArgParser from the args package to easily parse shell arguments

Part 4: Advanced ArgParser and Logging
  • Using ArgParser to print usage
  • Using Logger from the logging package as an alternative to print
  • Using Pub to get the Dart Bag of Tricks (BOT) from GitHub
  • Using enableScriptLogListener from the bot_io library to print logs to a file.

Part 5: Implementing shell completion using the Bag of Tricks
  • Understanding the shell as a programming environment
  • Understanding command completion in Bash
  • Creating a custom completion script for a Dart command
  • Using bot_io and ArgParser to enable command completion

The source code for the final video on GitHub if you'd like to an easy way to get started. The full sample from BOT is here.

Also feel free to contact me if there are topics you'd like me to cover: either comment directly on a video or tweet me at @kevmoo.

I hope to make at least one more video for the series, getting into some of the more advanced command infrastructure in BOT. Stay tuned!

About the author

Kevin Moore is a freelance software engineer. He has an extensive background in .NET, Ruby on Rails, and standards-based web technologies: HTML, CSS, and Javascript. Kevin is an early adopter and advocate for Dart and is a Google Developer Expect for Dart. You can find him on Google+, Twitter, GitHub, and just a plain old web site.

Using Dart with Sublime Text

We know that developers have a wide range of preferences for their development tools, from simple text editors to full IDEs, and one size does not fit all. We think that all developers should have a productive and fun time working with Dart, so today we’re happy to announce an update to our Dart plugin for Sublime Text 2. The updated plugin supports syntax highlighting for modern Dart code, and includes several common Dart snippets to help you code faster. We also built dart2js into Sublime’s build system so it’s just a single keyboard shortcut away. Finally, the plugin automatically runs Pub whenever you create or update your pubspec files, so you can install and update your dependencies without ever leaving Sublime.

Check out this video where we walk through using the updated plugin:




If you’re looking for a more featured editor, you can try out our Dart Editor, the Dart plugin for Eclipse, or the Dart plugin for IntelliJ / Webstorm.

The Dart plugin for Sublime Text 2 is open source on GitHub, and we’d love to accept contributions. Install the plugin today, and if you have any questions, you can find us on our mailing list and Stack Overflow. Let us know what you think!




Monday, February 25, 2013

New Dart Editor Build with Pub Publish Available


Dart Editor has a new build. Daniel Rubel fills us in on the details:


A new Dart Editor build is available at www.dartlang.org/editorChanges include:
  • Pub publish command
  • More cleanups including:
      on.click.add => onClick.listen
      migrate Future.delayed()
      migrate Uri.isAbsolute/hasAuthority() to getter
      migrate Timer
  • Minor fixes to Samples
Breaking Change List:

dart:html
  • A number of Indexed DB APIs have been updated to return Futures, more will be updated.
  • Deprecated events syntax has been removed.
  • Element.dataAttributes has been removed, use Element.dataset instead.
libraries:
  • deprecated Collections-methods removed.
  • New StringBuffer API, implementing StringSink interface.
For more, see the latest news.

And as always, view the changelog for the full list of changes, and to get started with the Editor see our tutorial.

Thursday, February 21, 2013

I/O Library Now Uses Streams

We have done a full re-design of the dart:io API (file, network, directories, etc) to use the classes and concepts in the core library and in dart:async. This means that there is almost no callback registration in dart:io any more. All async operations now use streams and futures.

(These changes are available in bleeding_edge as of today, and should show up in a weekly build soon.)




We think this is a great step forward in aligning all of the “dart:” API's and for you to have fewer concepts to learn when using Dart for different tasks.

However this is a breaking change which requires rewriting code using dart:io. The good news is that most of these changes should be mechanical. The following sections describe these changes in more details and give some examples on how to migrate code to the new API. For more information take a look on the API documentation for dart:io.

Streams in dart:io
The classes InputStream and OutputStream are gone and have been replaced with classes implementing IOSink and Stream<List<int>>.

When reading from a Stream<List<int>> just use the listen method which have arguments matching the callbacks previously used. This shows how to migrate code using an InputStream to the streams-based API:

dart:io v1 code
InputStream stream = ...
stream.onData = () {
 var data = request.inputStream.read();
 /* Process data. */
};
stream.onClosed = () {
 /* All data received. */
};
stream.onError = (e) {
 /* Error on input. */
};

dart:io v2 code
Stream<List<int>> stream = ...
stream.listen(
 (data) { /* Process data. */ },
 onDone: () { /* All data received. */ },
 onError: (e) { /* Error on input. */ });

As the InputStream class is now gone so is the StringInputStream for turning a stream of bytes into strings and/or lines. Two new stream transformers StringDecoder and LineTransformer have been added to address this. The StringDecoder transforms a stream of List<int> to a stream of String and the LineTransformer transforms a stream of String to a new stream of String where each string is a line.

dart:io v1 code
InputStream stream = ...
StringInputStream stringStream = new StringInputStream(stream);
stringStream.onLine = () {
 String line = stringStream.readLine();
 /* Do something with line. */
};
stringStream.onClosed = () {
 /* No more lines */
};
stream.onError = (e) {
 /* Error on input. */
};

dart:io v2 code
Stream<<int>> stream = ...
stream
 .transform(new StringDecoder())
 .transform(new LineTransformer())
 .listen((String line) { /* Do something with line. */ },
         onDone: () { /* No more lines */ },
         onError: (e) { /* Error on input. */ });

The IOSink replaces OutputStream and this shows how to migrate code using OutputStream to use an IOSink:

dart:io v1 code
OutputStream stream = …
stream.write([72, 101, 108, 108, 111]);  // Hello
stream.writeString(", world!");
stream.close();

dart:io v2 code
IOSink sink = …
sink.add([72, 101, 108, 108, 111]);  // Hello
sink.addString(", world!");
sink.close();

The IOSink also allows you to pipe directly from a stream.

HTTP
The main changes to the HTTP server and client part of dart:io are the following:

* A new HttpServer listening for requests is created using the static method bind.
* An HttpServer is a stream of HttpRequests.
* The defaultRequestHandler setter and addRequestHandler method on HttpServer are gone.
* The HttpRequest and HttpClientResponse objects implement Stream<List<int>>.
* The HttpClientRequest and HttpResponse objects implement IOSink.

To create a new listening HTTP server use the static method bind which returns a future.

dart:io v1 code
HttpServer server = new HttpServer();
server.defaultRequestHandler = …
server.addRequestHandler(…);
server.listen(“127.0.0.1”, 8080);
// Now the HTTP server is bound and listening for requests.

dart:io v2 code
HttpServer.bind(“127.0.0.1”, 8080)
   .then((HttpServer server) {
     server.listen(
         (HttpRequest request) {
           // Handle the request.
         });
}

The request routing through defaultRequestHandler and addRequestHandler is gone and any routing to specific request handling methods should be added to the listen handling. The HttpResponse is available as the response property on the HttpRequest object.

For client side HTTP the HttpClient class still exists. The HttpClientConnection class is gone, and instead the request initiation methods get, post, open, etc. now returns a future for the HttpClientRequest object. The HttpClientRequest object has a response property which is a future for the HttpClientResponse. As a convenience the HttpClientRequest.close method also returns the future for the response. This shows how to migrate HTTP client code:

dart:io v1 code
HttpClient client = new HttpClient();
HttpClientConnection connection = client.get(...);
connection.onRequest = (HttpClientRequest request) {
 // Prepare the request.
 request.outputStream.close();
}
connection.onResponse = (HttpClientResponse response) {
 // Process the response.
}

dart:io v2 code
HttpClient client = new HttpClient();
client.get(...)
   .then((HttpClientRequest request) {
     // Prepare the request.
     return request.close();
   })
   .then((HttpClientResponse response) {
     // Process the response.
   });

Web Sockets
The web socket interface has been simplified and now uses the same class for web socket connections on both the client and the server. The WebSocket class is a stream of events.

On the server the web socket handling is implemented as a stream transformer. This transformer can transform a stream of HttpRequests into a stream of WebSockets.

dart:io v1 code
HttpServer server = new HttpServer();
server.listen(...);
WebSocketHandler handler = new WebSocketHandler();
handler.onOpen = (WebSocketConnection connection) {
 connection.onMessage = (Object message) {
   /* Handle message. */
 };
 connection.onClosed = (status, reason) {
   /* Handle closed. */
 };
};
server.defaultRequestHandler = handler.onRequest;

dart:io v2 code
HttpServer.bind(...).then((server) {
 server.transform(new WebSocketTransformer()).listen((WebSocket webSocket) {
   webSocket.listen((event) {
     if (event is MessageEvent) {
       /* Handle message. */
     } else if (event is CloseEvent) {
       /* Handle closed. */
     }
   });
 });

On the client connecting a web socket has become much simpler. Just use the WebSocket static method connect which returns a future for the web socket connection as shown here:

dart:io v1 code
HttpClient client = new HttpClient();
HttpClientConnection conn = client.openUrl(“https://127.0.0.1:8080”);
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onMessage = (message) {
 /* Handle message. */
}
wsconn.onClosed = (status, reason) {
 /* Handle closed. */
};

dart:io v2 code

WebSocket.connect("ws://echo.websocket.org")
   .then((WebSocket webSocket) {
     webSocket.listen((message) {
         /* Handle message. */
       },
onDone: () {
         /* Handle closed. */
       });
   });

Process
The Process class uses Stream<List<int>> for stdout and stderr and IOSink for stdin. The exit code for the process is now available through the exitCode future.

dart:io v1 code
Process process = ...
process.stdout.onData = ...
process.stdout.onDone = ...
process.onExit = (exitCode) { /* do something with exitCode. */ }

dart:io v2 code
Process process = ...
process.stdout.listen(...);
p.exitCode.then((exitCode) { /* do something with exitCode. */ });

Likewise the types of the top level properties stdin, stdout and stderr have been changed. stdio is a Stream<List<int>> and stdout and stderr are IOSinks.

File and directory
Reading and writing a file also uses Stream<List<int>> and IOSink. To read a file change the use of openInputStream to openRead which returns a stream of type Stream<List<int>>. Likewise change the use of openOutputStream to openWrite which returns an IOSink.

The Directory.list function now returns a stream of FileSystemEntity objects. The FileSystemEntity is a superclass of both File and Directory. The previous DirectoryLister where callbacks were set is now gone.

dart:io v1 code
Directory dir = ...
DirectoryLister lister = dir.list();
lister.onDir = (Directory directory) { /* Do something with directory. */ };
lister.onFile = (File file) { /* Do something with file. */ };
lister.onDone = (bool complete) { /* Listing ended.*/ };
lister.onError = (error) { /* Listing error.*/ };


dart:io v2 code
Directory dir = ...
dir.list().listen(
   (FileSystemEntity fse) {
     if (fse is Directory) {
       Directory directory = fse;
       /* Do something with directory. */
     } else if (fse is File) {
       File file = fse;
       /* Do something with file. */
     }
   },
   onDone: () { /* Listing ended.*/ },
   onError: (error) { /* Listing error.*/ });

Sockets
The classes Socket and SecureSocket both implement Stream<List<int>> and IOSink as they support bidirectional communication. This replaces all the reading and writing previously provided through a combination of callbacks, read and write methods, and InputStream and OutputStream objects backed by sockets.

Connecting a socket now uses a static method returning a future instead of a callback.

dart:io v1 code
Socket socket = new Socket(host, port);
socket.onConnect = () { /* Socket connected. */ }

dart:io v2 code
Socket.connect(host, port).then((Socket socket) {
 /* Socket connected. */
};

The classes ServerSocket and SecureServerSocket now uses a static method returning a future when binding to an interface. A listening server socket delivers the socket connections as a stream instead of through the onConnection callback.

dart:io v1 code
ServerSocket socket = new ServerSocket(host, port);
socket.onConnection = (Socket clientSocket) {
 /* Do something with the clientSocket. */
}

dart:io v2 code
ServerSocket.bind(host, port).then((ServerSocket socket) {
 socket.listen((Socket clientSocket) {
   /* Do something with the clientSocket. */
 })
});

Raw sockets
In order to provide a low level socket API we introduced raw sockets. Raw sockets give you access to low-level socket events without giving you data in your hand (think of this as the events you get from epoll on a socket file descriptor on Linux systems). Based on the low-level events you can read out data from the socket and decide when to write more data to the socket. The high-level Socket and ServerSocket classes are built on top of the RawSocket and RawServerSocket classes. Check out the API documentation on the Raw* classes.



(Photo Credit: Marooned cc)

New site for Dart HTML5 Game Developers

Great news game developers! John McCutchan has launched a new blog devoted to game development in Dart. I interviewed John about the site and game development with Dart. Keep reading to learn more about game development in Dart.



Shailen: Are game developers using Dart?
John: Yes, there is Pop Pop Win and a group of ex-flash developers are porting their actionscript games to Dart. An example of their work is Escape.

Shailen: Why do you think Dart is good for game development?
John: There are multiple reasons. First, Dart is a structured programming language with classes, libraries, an optional type system, lexically scoped this, and more. These features are taken for granted by most developers from other platforms, so they are happy to have them when programming for the web. Second, the Dart VM is already faster than V8 in some benchmarks* and work continues to make it faster. Game developers understand how important performance is. Finally, for ActionScript developers, the Dart syntax is very familiar.

Shailen: What types of games can you build with Dart?
John: Functionally, Dart exposes all of the HTML5 APIs available in the browser, including Canvas and WebGL, Web Audio API, Web sockets, and more. You can write 2D and 3D games, for both single-player and multi-players.

Shailen: There are many developers new to Dart. What libraries are out there to help them get their game running?
John: Even though Dart is new, there are already many libraries for game development:

dartflash - A porting library for flash developers.
game_loop - Main loop and input processing.

vector_math - 2D, 3D, and 4D linear algebra.
simple_audio - Simple audio playback designed for games.
box2d - A port of the popular 2D physics engine.
asset_pack - Asset loading made simple.
spectre - 3D graphics utility library including mesh importing, debug rendering, and cameras.

Shailen: Why did you start DartGameDevs?
John: Game developers love sharing and I wanted to create a place devoted to sharing game development stores in Dart.

Shailen: What can a developer find on DartGameDevs?
John: Tutorials, best practices, tips, and news about the latest in Dart game development libraries and tools.

Shailen: Who can write for DartGameDevs?
John: Anyone! If you have a useful tip, library, or more in depth article for Dart game developers, reach out to me.

There you have it game developers, try Dart for your next HTML5 game! Make sure you add www.dartgamedevs.org to your RSS feed and follow John McCutchan on Twitter, G+, and GitHub.

Wednesday, February 20, 2013

New Streams API with Dart Milestone 3

We are proud to announce the Milestone 3 release of the Dart SDK, which provides a new, cohesive API for asynchronous programming and a unified model for events. Some of the new or improved classes in this release include Iterable, Stream, and Future.

Dart's M3 release delivers on a common developer request for a more unified approach to events. New and updated in M3, the Iterable class and the Stream class now provide synchronous and asynchronous models, respectively, for a common theme: a sequence of items.

The Iterable class is now much more prominent in Dart and is the ancestor of common collections like List and Set. Many APIs now return Iterable instead of a specific collection. You can map, join, skip, take, and more on an Iterable. Here is an example:


var names = getAccounts().where((a) => a.isPlatinum)

.map((a) => a.fullName)
.toList();

The Stream class (and its dart:async library) is new, and it provides an asynchronous stream of events. An event can be any Dart object, which makes Streams very flexible. Consumers of a Stream can listen for events, and streams can be piped, transformed, filtered, and more. Streams are now the unified way to deliver asynchronous events, and we are working to apply them across HTML, I/O, isolates, and more. Here is an example of using streams with the HTML library, treating clicks as a stream of events:

query('#button').onClick.listen((e) => submitForm());

The Future class, which represents a single value available in the future, was cleaned up and is now much easier to use. In many cases this allows you to write asynchronous code very similar to its synchronous counterpart:



SynchronousAsynchronous
bool writeFile(String data, File file) {
try {
  var io = file.openSync(FileMode.WRITE);
  io.writeStringSync(data);
  io.closeSync();
  return true;
} catch (error) {
  return false;
 }

}
Future<bool> writeFile(String data, File file) {
return file.open(FileMode.WRITE)
  .then((io) => io.writeString(data))
  .then((io) => io.close())
  .then((io) => true)
  .catchError((error) => false);
}

Some of these changes are not backwards compatible. Luckily, you can use Dart Editor's Clean Up feature to help automatically port your code from the old API to the new API. Here is an example of Clean Up at work:






Over the following months we will continue to work on the libraries, applying the new asynchronous models introduced in M3. We always appreciate the feedback, please let us know what you think at dartbug.com and misc@dartlang.org.

To get started, you can download the Dart SDK from dartlang.org, learn about the language and libraries, and follow our tutorials. If you already have Dart Editor, it can auto-update to the latest release for you.


Posted by Florian Loitsch, Software Engineer and Corelib Hacker

Tuesday, February 19, 2013

Dart with Google Web Toolkit

In this episode of Dartisans, I'm going to show you a variety of ways to use Dart with Google Web Toolkit. I know that there are a lot of GWT developers out there who would like to give Dart a shot, but they aren't sure how because they already have a large, successful app that's written in GWT. I'm going to show you ways to integrate Dart into your existing GWT application without having to rewrite it from scratch.

To do this, I've built a sample application that uses both GWT and Dart. I'll show you how to setup a development environment so that you can work with both technologies. Then, I'll show you a variety of ways in which you can get GWT and Dart to interoperate, such as:
  • Using GWT and Dart to manage different parts of the same page
  • Using Dart to retrieve JSON from a Java servlet
  • Using window.postMessage and JSNI to pass messages between GWT and Dart
  • Using JavaScript, JSNI, and Dart's js package for synchronous interoperability between GWT and Dart
  • Using CustomEvent objects and Elemental to pass messages between GWT and Dart
Rather than show you a one-size-fits-all solution, I decided to show you a bunch of approaches so that you could pick the right tool for the job. Each of them has stengths and weaknesses, and I'll cover those along the way as well.

Aside from watching the video, you can also download the source code or view the video transcript.



As always, we invite you to join the discussion on the Dart mailing list, and ask us questions on Stack Overflow. Your feedback is important. Thanks for checking out Dart!

Monday, February 18, 2013

New Article on How to Use Future Based APIs in Dart


Read the latest article at dartlang.org and learn about using Future based APIs


Futures are crucial to how async works in Dart. The article covers Future basics, explains how to handle errors that are emitted asynchronously, and how to deal with multiple Future-returning functions. 

Your feedback really counts. Please join the conversation at the Dart mailing list, and ask questions at Stack Overflow.

Photo credit: http://www.flickr.com/photos/andrewcoulterenright/11333511/