Slide 1

Slide 1 text

Building CLI Apps for Everyone

Slide 2

Slide 2 text

Terence Lee @hone02

Slide 3

Slide 3 text

Austin, TX

Slide 4

Slide 4 text

Austin, TX

Slide 5

Slide 5 text

#rubykaraoke

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

@aspleenic

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Packaging in Ruby

Slide 10

Slide 10 text

story of the heroku toolbelt

Slide 11

Slide 11 text

gem install heroku

Slide 12

Slide 12 text

Requires Ruby to be installed

Slide 13

Slide 13 text

Can't guarantee same version of ruby locally

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Packaging was a nightmare

Slide 16

Slide 16 text

hk

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

require in ruby is slow

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Traveling Ruby ● no need to rewrite your app ● packages your app as a directory ● runtime packaging is taken care of ● can leverage rubygems ecosystem ● native gems limited to precompiled exts

Slide 22

Slide 22 text

sferik

Slide 23

Slide 23 text

No great packaging story

Slide 24

Slide 24 text

I want to build things in Ruby

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Picking Ruby shouldn't be a technical limitation

Slide 28

Slide 28 text

Making Packaging Possible

Slide 29

Slide 29 text

mruby-cli http://github.com/hone/mruby-cli

Slide 30

Slide 30 text

writing in ruby

Slide 31

Slide 31 text

mrblib ├── [4.0K] mrblib │ ├── [4.0K] mruby-cli │ │ ├── [ 697] cli.rb │ │ ├── [ 424] help.rb │ │ ├── [ 403] option.rb │ │ ├── [1022] options.rb │ │ ├── [8.0K] setup.rb │ │ ├── [ 238] util.rb │ │ └── [ 206] version.rb │ └── [ 53] mruby-cli.rb

Slide 32

Slide 32 text

performance is a feature

Slide 33

Slide 33 text

MRI # hello_world.rb puts 'Hello World'

Slide 34

Slide 34 text

MRI # hello_world.rb puts 'Hello World' $ time ruby hello.rb Hello World real 0m0.041s user 0m0.028s sys 0m0.008s

Slide 35

Slide 35 text

mruby-cli # mrblib/hello.rb def __main__(argv) puts "Hello World" end

Slide 36

Slide 36 text

mruby-cli # mrblib/hello.rb def __main__(argv) puts "Hello World" end $ time mruby/build/host/bin/hello Hello World real 0m0.003s user 0m0.000s sys 0m0.000s

Slide 37

Slide 37 text

No require

Slide 38

Slide 38 text

a single binary for every major platform

Slide 39

Slide 39 text

$ docker-compose run compile Build summary: ================================================ Config Name: x86_64-pc-linux-gnu Output Directory: build/host Included Gems: mruby-print - standard print/puts/p mruby-sprintf - standard Kernel#sprintf method mruby-time - standard Time class mruby-io mruby-mtest hello - hello - Binaries: hello ================================================

Slide 40

Slide 40 text

================================================ Config Name: x86_64-apple-darwin14 Output Directory: build/x86_64-apple-darwin14 Included Gems: mruby-print - standard print/puts/p mruby-sprintf - standard Kernel#sprintf method mruby-time - standard Time class mruby-io mruby-mtest hello - hello - Binaries: hello ================================================

Slide 41

Slide 41 text

================================================ Config Name: x86_64-w64-mingw32 Output Directory: build/x86_64-w64-mingw32 Included Gems: mruby-print - standard print/puts/p mruby-sprintf - standard Kernel#sprintf method mruby-time - standard Time class mruby-io mruby-mtest hello - hello - Binaries: hello ================================================

Slide 42

Slide 42 text

$ ls -nlh mruby-cli | awk '{print $5}' 421K

Slide 43

Slide 43 text

simple setup

Slide 44

Slide 44 text

Docker FROM hone/mruby-cli

Slide 45

Slide 45 text

Hello World Example $ mruby-cli --setup hello

Slide 46

Slide 46 text

Hello World Example $ mruby-cli --setup hello $ cd hello

Slide 47

Slide 47 text

Hello World Example $ mruby-cli --setup hello $ cd hello $ docker-compose run compile

Slide 48

Slide 48 text

Hello World Example $ mruby-cli --setup hello $ cd hello $ docker-compose run compile $ docker-compose run shell # mruby/build/host/bin/hello

Slide 49

Slide 49 text

Hello World Example $ mruby-cli --setup hello $ cd hello $ docker-compose run compile $ docker-compose run shell # mruby/build/host/bin/hello Hello World

Slide 50

Slide 50 text

Hello World Example $ mruby/bin/mruby-cli --setup hello create .gitignore create mrbgem.rake create build_config.rb create Rakefile create Dockerfile create docker-compose.yml create tools/ create tools/hello/ create tools/hello/hello.c create mrblib/ create mrblib/hello.rb create mrblib/hello/ create mrblib/hello/version.rb create bintest/ create bintest/hello.rb create test/ create test/test_hello.rb

Slide 51

Slide 51 text

mruby

Slide 52

Slide 52 text

mruby is the lightweight implementation of the Ruby language complying with part of the ISO standard. mruby can be linked and embedded within your application.

Slide 53

Slide 53 text

used in the wild https://github.com/ximenzi/openwrt-mruby

Slide 54

Slide 54 text

Contributors

Slide 55

Slide 55 text

Differences with MRI

Slide 56

Slide 56 text

no built in File Socket I/O

Slide 57

Slide 57 text

not threadsafe

Slide 58

Slide 58 text

no thread no fork

Slide 59

Slide 59 text

subset of Ruby 1.9

Slide 60

Slide 60 text

procs blocks DHH freedom patching meta programming literals

Slide 61

Slide 61 text

Executing mruby code

Slide 62

Slide 62 text

Interpret (*.rb) $ mruby/bin/mruby -e "puts 'Hello World'" Hello World $ mruby/bin/mruby hello_world.rb Hello World

Slide 63

Slide 63 text

Interactive (mirb) $ mruby/bin/mirb mirb - Embeddable Interactive Ruby Shell > puts 'Hello World' Hello World => nil

Slide 64

Slide 64 text

bytecode (*.mrb) $ mruby/bin/mrbc hello_world.rb

Slide 65

Slide 65 text

bytecode (*.mrb) $ mruby/bin/mrbc hello_world.rb $ ls hello_world.* hello_world.rb hello_world.mrb

Slide 66

Slide 66 text

bytecode (*.mrb) $ mruby/bin/mrbc hello_world.rb $ ls hello_world.* hello_world.rb hello_world.mrb $ mruby/bin/mruby -b hello_world.mrb Hello World

Slide 67

Slide 67 text

Binary C Code (*.c) $ mruby/bin/mrbc -o hello_world.c - Bhello_world hello_world.rb $ cat hello_world.c /* dumped in little endian order. use `mrbc -E` option for big endian CPU. */ #include const uint8_t #if defined __GNUC__ __attribute__((aligned(4))) #elif defined _MSC_VER __declspec(align(4)) #endif hello_world[] = { 0x45,0x54,0x49,0x52,0x30,0x30,0x30,0x33,0xcb,0xa4,0x00, 0x00,0x00,0x65,0x4d,0x41,...}; This is an FYI No need to read this C code

Slide 68

Slide 68 text

int main(void) { mrb_state *mrb = mrb_open(); mrbc_context *c; mrb_value v; c = mrbc_context_new(mrb); mrb_load_irep_cxt(mrb, hello_world, c); mrbc_context_free(mrb, c); return 0; }

Slide 69

Slide 69 text

int main(void) { mrb_state *mrb = mrb_open(); mrbc_context *c; mrb_value v; c = mrbc_context_new(mrb); mrb_load_irep_cxt(mrb, hello_world, c); mrbc_context_free(mrb, c); return 0; }

Slide 70

Slide 70 text

tools/hello/hello.c #include #include /* Include the mruby header */ #include #include int main(int argc, char *argv[]) { mrb_state *mrb = mrb_open(); mrb_value ARGV = mrb_ary_new_capa(mrb, argc); int i; int return_value; for (i = 0; i < argc; i++) { mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i])); } mrb_define_global_const(mrb, "ARGV", ARGV); // call __main__(ARGV) mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV); return_value = EXIT_SUCCESS; if (mrb->exc) { mrb_print_error(mrb); return_value = EXIT_FAILURE; } mrb_close(mrb); return return_value; } This is an FYI No need to read this C code

Slide 71

Slide 71 text

# mrblib/hello.rb def __main__(argv) puts "Hello World" end

Slide 72

Slide 72 text

mrblib ├── [4.0K] mrblib │ ├── [4.0K] mruby-cli │ │ ├── [ 697] cli.rb │ │ ├── [ 424] help.rb │ │ ├── [ 403] option.rb │ │ ├── [1022] options.rb │ │ ├── [8.0K] setup.rb │ │ ├── [ 238] util.rb │ │ └── [ 206] version.rb │ └── [ 53] mruby-cli.rb

Slide 73

Slide 73 text

Rakefile $ rake -T rake all # build all targets... rake clean # clean all built.. rake compile # compile all the binaries rake release # generate release tarballs rake test # run all tests rake test:bintest # run integration tests rake test:mtest # run mruby & unit tests

Slide 74

Slide 74 text

build_config.rb MRuby::Build.new do |conf| toolchain :gcc conf.enable_bintest conf.enable_debug conf.enable_test gem_config(conf) end

Slide 75

Slide 75 text

build_config.rb MRuby::CrossBuild.new('x86_64-apple-darwin14') do |conf| toolchain :clang [conf.cc, conf.linker].each do |cc| cc.command = 'x86_64-apple-darwin14-clang' end conf.cxx.command = 'x86_64-apple-darwin14-clang++' conf.archiver.command = 'x86_64-apple-darwin14-ar' conf.build_target = 'x86_64-pc-linux-gnu' conf.host_target = 'x86_64-apple-darwin14' gem_config(conf) end

Slide 76

Slide 76 text

$ docker-compose run compile Build summary: ================================================ Config Name: host Output Directory: build/host Binaries: mrbc Included Gems: mruby-print - standard print/puts/p mruby-sprintf - standard Kernel#sprintf method mruby-time - standard Time class mruby-io mruby-mtest hello - hello - Binaries: hello mruby-compiler - mruby compiler library mruby-bin-mrbc - mruby compiler executable ================================================

Slide 77

Slide 77 text

mrbgem.rake MRuby::Gem::Specification.new('hello') do |spec| spec.license = 'MIT' spec.authors = ['Terence Lee'] spec.summary = 'hello world' spec.bins = ['hello'] spec.add_dependency 'mruby-print', :core => 'mruby- print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby- mtest' spec.add_dependency 'mruby-yaml', :github => 'hone/mruby-yaml' end

Slide 78

Slide 78 text

mrbgem.rake MRuby::Gem::Specification.new('hello') do |spec| spec.license = 'MIT' spec.authors = ['Terence Lee'] spec.summary = 'hello world' spec.bins = ['hello'] spec.add_dependency 'mruby-print', :core => 'mruby- print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby- mtest' spec.add_dependency 'mruby-yaml', :github => 'hone/mruby-yaml' end

Slide 79

Slide 79 text

core mrbgems ├── [4.0K] mruby-array-ext ├── [4.0K] mruby-bin-debugger ├── [4.0K] mruby-bin-mirb ├── [4.0K] mruby-bin-mrbc ├── [4.0K] mruby-bin-mruby ├── [4.0K] mruby-bin-mruby-config ├── [4.0K] mruby-bin-strip ├── [4.0K] mruby-compiler ├── [4.0K] mruby-enumerator

Slide 80

Slide 80 text

mrbgem.rake MRuby::Gem::Specification.new('hello') do |spec| spec.license = 'MIT' spec.authors = ['Terence Lee'] spec.summary = 'hello world' spec.bins = ['hello'] spec.add_dependency 'mruby-print', :core => 'mruby- print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby- mtest' spec.add_dependency 'mruby-yaml', :github => 'hone/mruby-yaml' end

Slide 81

Slide 81 text

mgem list

Slide 82

Slide 82 text

mrbgem.rake MRuby::Gem::Specification.new('hello') do |spec| spec.license = 'MIT' spec.authors = ['Terence Lee'] spec.summary = 'hello world' spec.bins = ['hello'] spec.add_dependency 'mruby-print', :core => 'mruby- print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby- mtest' spec.add_dependency 'mruby-yaml', :github => 'hone/mruby-yaml' end

Slide 83

Slide 83 text

build_config.rb MRuby::Build.new do |conf| toolchain :gcc conf.enable_bintest conf.enable_debug conf.enable_test gem_config(conf) end

Slide 84

Slide 84 text

build_config.rb def gem_config(conf) # be sure to include this gem conf.gem File.expand_path(File. dirname(__FILE__)) conf.gem :github => 'hone/mruby-yaml' end

Slide 85

Slide 85 text

Testing

Slide 86

Slide 86 text

mtest module MRubyCLI class TestUtil < MTest::Unit::TestCase def test_camelize assert_equal "GoodNightMoon", Util.camelize ("good_night_moon") assert_equal "FooBarBaz", Util.camelize("foo- bar-baz") end end end MTest::Unit.new.run

Slide 87

Slide 87 text

mtest (cont.) $ docker-compose run mtest >>> Test host <<< mrbtest - Embeddable Ruby Test .....S... # Running tests: .. Finished tests in 0.000305s, 6557.3770 tests/s, 26229.5082 assertions/s. 2 tests, 8 assertions, 0 failures, 0 errors, 0 skips

Slide 88

Slide 88 text

bintest require 'open3' require 'tmpdir' BIN_PATH = File.join(File.dirname(__FILE__), ".. /mruby/bin/hello") assert('setup') do output, status = Open3.capture2("#{BIN_PATH}") assert_true status.success?, "Process did not exit cleanly" assert_include output, "Hello World" end

Slide 89

Slide 89 text

bintest (cont.) $ docker-compose run bintest . Total: 1 OK: 1 KO: 0 Crash: 0 Time: 0.01 seconds

Slide 90

Slide 90 text

Gotchas

Slide 91

Slide 91 text

no stdlib

Slide 92

Slide 92 text

Can't leverage Rubygems

Slide 93

Slide 93 text

mrbgems that have native extensions need to support cross compiling

Slide 94

Slide 94 text

jruby-launcher

Slide 95

Slide 95 text

jruby/jruby -- bin/

Slide 96

Slide 96 text

jruby/jruby-launcher

Slide 97

Slide 97 text

jruby.cpp /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2008, 2010 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2

Slide 98

Slide 98 text

mjruby https://github.com/jkutner/mjruby

Slide 99

Slide 99 text

mjruby/test

Slide 100

Slide 100 text

mruby-cli & you

Slide 101

Slide 101 text

Young Community

Slide 102

Slide 102 text

mruby/mruby on github

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

Building a CLI App ● download the mruby-cli binary ● install Docker Toolbox ● mruby-cli --setup calculator ● add new functionality ○ modify to build a calculator or just start with addition ● docker-compose run compile ● modify, recompile ● tweet at me (@hone02)

Slide 105

Slide 105 text

Friday Hug!

Slide 106

Slide 106 text

#rubykaraoke

Slide 107

Slide 107 text

Let's build things in Ruby

Slide 108

Slide 108 text

Thank You @hone02 https://github.com/hone/mruby-cli