In Progress
Unit 1, Lesson 21
In Progress

Distributable Desktop Apps – Jason Clark

Writing rich desktop applications has never been a strength of the Ruby ecosystem. In particular, the difficulty of packaging Ruby GUI applications for distribution has limited their appeal.

But don’t count out Ruby for rich client development quite yet! In today’s episode, guest chef Jason Clark shows you how new developments in the Shoes project have made distributing Ruby desktop applications a lot easier.

Video transcript & code

Let's be totally honest for a second: writing rich desktop applications has never been a strength of the Ruby ecosystem. While there were some early attempts at cross-platform GUI bindings for Ruby, their relative immaturity, and the difficulty of packaging applications for distribution limited their appeal.

But don't count out Ruby for rich client development quite yet! In today's episode, guest chef Jason Clark is going to show you how new developments in the Shoes project have made distributing Ruby desktop applications a lot easier.

Jason works as a principal engineer at New Relic. He likes being a maintainer of the Shoes project because it gives him an opportunity to build cool things with his kids. He has even spoken with his daughter at two Ruby conferences!

Here's Jason, with the lowdown on packaging client-side Ruby GUI applications using the Shoes framework. Enjoy!


Packaging GUI Applications with Shoes

The App

As an avid cyclist, I love tracking stats downloaded from my bike computer. I wrote some Ruby to upload the raw data to several cycling websites, but whenever I ran the script I'd end up just staring at various logs until it finished.

Seems like a good spot for a GUI instead. But how to do that with the language I love: Ruby?

Shoes?

Although Ruby is most often considered for web programming and scripting, there are some options for doing GUI development. One of them is Shoes, a GUI DSL for Ruby, written originally by the why the lucky stiff, a prolific and well known hacker from the early days of Ruby. Shoes is designed to be a fun, quick way to get a desktop app up and running.

Here's what Shoes code looks like. We won't be digging much into the details of, but you can see how simple Ruby methods creating and laying out element on the screen. Blocks are used to respond to user actions, like link and button clicks.


Shoes.app title: "Robot Likes Cycling" do
  style Shoes::Para, font: "Monospace 14px", margin_left: 10

  stack do
    flow width: "100%" do
      background lightgreen
      @bike = image "bike.png", left: 10
      @start_link = para link("Let's Go!") { start }, left: 450, top: 50
    end

    @log = para ""
  end

  @actions = Queue.new

  def start
    @directory = ask_open_folder
    return unless @directory

    @start_link.hide
    @actions << grab_data

    @animation = animate do
      move_bike
      check_next_action
    end
  end
end

With the app finished, though, I wondered if someone else could use from it, if only there was a way to share it.

The Problem

For many programmers—newcomers and veterans alike—building that cool desktop app skids to a halt the moment it needs to get distributed.

What if the user's computer doesn't have Ruby installed?

What if it doesn't have the right native libraries?

What if it's on another operating system altogether?

The Solution

And that's where Shoes is more than just a friendly DSL for GUI apps. Shoes' other major feature is packaging. Let's see how we can use that from the newly released 4.0 version of Shoes.

Developer Setup

To run Shoes as a developer, you'll need a recent copy of JRuby.. Shoes 4 gets lots of benefits from building on top of the JVM's excellent cross-platform UI support.


$ ruby --version
jruby 9.1.13.0 (2.3.3) 2017-09-06 8e1c115 Java HotSpot(TM) 64-Bit Server VM 25.121-b13 on 1.8.0_121-b13 [darwin-x86_64]

With that version of Ruby ready to go, install shoes gem. Having the gem will let us develop and package Shoes apps with ease.


$ gem install shoes --pre
# ...

Packaging It Up

Once the installation party is done, we can ask the Shoes CLI to package your application up.


shoes package --mac --windows --linux --jar robot-likes-cycling-app.rb

This command produces an archive file for each requested OS. Each archive contains your Shoes code all bundled up in a JVM JAR file, along with the bootstrapping code to look as much as possible like a native application on that given platform.


$ ls -la testing/bike/pkg
total 547880
drwxr-xr-x  6 jason  staff       204 Feb 20 21:12 .
drwxr-xr-x  4 jason  staff       136 Feb 20 21:11 ..
-rw-r--r--  1 jason  staff  69485697 Feb 20 21:12 bike-linux.tar.gz
-rw-r--r--  1 jason  staff  69662923 Feb 20 21:12 bike-mac.tar.gz
-rw-r--r--  1 jason  staff  71257807 Feb 20 21:12 bike-windows.zip
-rw-r--r--  1 jason  staff  70102285 Feb 20 21:12 bike.jar

The --jar option produces a self-executing JAR file that can be run directly on any system with a JVM already installed. This skips past the OS-specific niceities, but depending on your audience that may be just fine.


$ java -jar -XstartOnFirstThread testing/bike/pkg/bike.jar
... app starts

Shoes strives to make the running an app look just like any other program you run on your desktop. Today OS X has the smoothest experience, but Windows and Linux users aren't left out at all.

Shoes will do everything it can--up to an including downloading a JVM for you-- to let folks run your code hassle free.

Alternatively: Shoes Launcher

But wait, we went to all this trouble to make running apps seamless for our users... what about ourselves? What if we wanted to develop Shoes code without all updating Rubies and installing gems?

Shoes Launcher to the rescue!

The Shoes Launcher is a GUI application--written in Shoes of course!--that acts as a program launcher and packager for Shoes code. To use it, you can simply download the app from the Shoes GitHub releases page, then run it.

Pointing the launcher to package our Shoes code, and we get the same shareable archives out the other end.

This is create news for sharing GUI apps without the ceremony of multi-step programming tool installations.

Happy Hackety Hacking!

Responses