Slide 1

Slide 1 text

mruby A packaging story filled with freedom

Slide 2

Slide 2 text

Today is not Friday...

Slide 3

Slide 3 text

But I believe in hugs everyday

Slide 4

Slide 4 text

#rubykaraoke

Slide 5

Slide 5 text

Terence Lee @hone02

Slide 6

Slide 6 text

Austin, TX

Slide 7

Slide 7 text

23.10.2015

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

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 21

Slide 21 text

No great packaging story

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

sferik

Slide 24

Slide 24 text

I want to build things in Ruby

Slide 25

Slide 25 text

Making Packaging Possible

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

writing in ruby

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

performance is a feature

Slide 30

Slide 30 text

MRI # hello_world.rb puts 'Hello World'

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 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 34

Slide 34 text

No require

Slide 35

Slide 35 text

a single binary for every major platform

Slide 36

Slide 36 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 37

Slide 37 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 38

Slide 38 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 39

Slide 39 text

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

Slide 40

Slide 40 text

simple setup

Slide 41

Slide 41 text

Docker FROM hone/mruby-cli

Slide 42

Slide 42 text

Hello World Example $ mruby-cli --setup hello

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 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 47

Slide 47 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 bintest/ create bintest/hello.rb create test/ create test/test_hello.rb

Slide 48

Slide 48 text

MRuby

Slide 49

Slide 49 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 50

Slide 50 text

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

Slide 51

Slide 51 text

Contributors

Slide 52

Slide 52 text

Differences with MRI

Slide 53

Slide 53 text

no built in File Socket I/O

Slide 54

Slide 54 text

not threadsafe

Slide 55

Slide 55 text

no thread no fork

Slide 56

Slide 56 text

subset of Ruby 2.1

Slide 57

Slide 57 text

procs blocks DHH freedom patching meta programming literals

Slide 58

Slide 58 text

Executing mruby code

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 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 64

Slide 64 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 65

Slide 65 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 66

Slide 66 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 67

Slide 67 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 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 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 71

Slide 71 text

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

Slide 72

Slide 72 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' conf.build_mrbtest_lib_only gem_config(conf) end

Slide 73

Slide 73 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 74

Slide 74 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 75

Slide 75 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 76

Slide 76 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 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

mgem list

Slide 79

Slide 79 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 80

Slide 80 text

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

Slide 81

Slide 81 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 82

Slide 82 text

Testing

Slide 83

Slide 83 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 84

Slide 84 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 85

Slide 85 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 86

Slide 86 text

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

Slide 87

Slide 87 text

Gotchas

Slide 88

Slide 88 text

Can't leverage Rubygems

Slide 89

Slide 89 text

mrbgems that have native extensions need to support cross compiling

Slide 90

Slide 90 text

Roadmap

Slide 91

Slide 91 text

Releases $ docker-compose run release Creating releases/v0.0.1/hello-0.0.1-x86_64-pc-linux-gnu.tgz releases/v0.0.1/hello-0.0.1-i686-pc-linux-gnu.tgz releases/v0.0.1/hello-0.0.1-x86_64-apple-darwin14.tgz releases/v0.0.1/hello-0.0.1-i386-apple-darwin14.tgz releases/v0.0.1/hello-0.0.1-x86_64-w64-mingw32.tgz releases/v0.0.1/hello-0.0.1-i686-w64-mingw32.tgz releases/v0.0.1/hello-0.0.1.tgz

Slide 92

Slide 92 text

deb

Slide 93

Slide 93 text

rpm

Slide 94

Slide 94 text

dmg

Slide 95

Slide 95 text

windows installer

Slide 96

Slide 96 text

Upgrading $ docker-compose upgrade conflict Rakefile [r]eplace, [d]iff, [s]kip? d 61c64 < task :test => ['test:bintest', 'test:mtest'] --- > task :test => ["test:mtest", "test:bintest"] 66,102d68 < end < < desc "generate a release tarball" < task :release do < require 'tmpdir' < require 'fileutils' < require_relative 'mrblib/version'

Slide 97

Slide 97 text

Per Architecture Compiling $ docker-compose run compile x86_64- apple-darwin14

Slide 98

Slide 98 text

Curated Set of mrbgems

Slide 99

Slide 99 text

Upstream Cross Compile Toolchains MRuby::CrossBuild.new('64-bit OS X') do |conf| toolchain :x86_64_apple_darwin14 conf.build_mrbtest_lib_only gem_config(conf) end

Slide 100

Slide 100 text

Documentation Support

Slide 101

Slide 101 text

mruby-cli & you

Slide 102

Slide 102 text

Young Community

Slide 103

Slide 103 text

Building a CLI App ● download the mruby-cli binary ● install Docker/docker-compose ● mruby-cli --setup foo ● docker-compose run compile ● modify, recompile

Slide 104

Slide 104 text

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