Save 37% off PRO during our Black Friday Sale! »

Introduction to Rake

Introduction to Rake

How to write basic rake applications

58479f76374a3ba3c69b9804163f39f4?s=128

Eric Hodel

May 25, 2016
Tweet

Transcript

  1. Introduc)on to Rake Eric Hodel – @drbrain

  2. What is Rake? • Build automa)on tool • Dependency-oriented

  3. Jim Weirich

  4. Other “rake”s • make family • Java: ant, maven •

    Clojure: leiningen • Scala: sbt • Python: A-P-P
  5. Rake Basics

  6. Rake Task task "test" do # test actions # …

    end
  7. Dependencies task "test" => ["compile"] do # test actions #

    … end
  8. Dependencies task test: "compile" do # test actions # …

    end
  9. Special Tasks MRUBY_DOWNLOAD = "tmp/mruby-1.2.0.tgz" directory "tmp" file MRUBY_DOWNLOAD =>

    "tmp" do sh "curl", "-o", MRUBY_DOWNLOAD, … end
  10. Namespacing namespace "test" do task "unit" # test:unit task "functional"

    # test:functional end namespace "db" do task "create" # db:create end
  11. Namespacing namespace "test" do task "unit" task "functional" end task

    "test" => [ "test:unit", "test:functional" ]
  12. Documenta)on desc "Run the tests" task "test" do # …

    end
  13. Default Task task "default" do puts "this task runs by

    default" end
  14. Default Task $ rake this task runs by default $

  15. Programming with Rake

  16. task "default" => "test" task "test" => [MRUBY_EXE, "tmp"] file

    MRUBY_EXE => MRUBY_DOWNLOAD directory "tmp" file MRUBY_DOWNLOAD => "tmp"
  17. Unordered is OK task "default" => "test" task "test" =>

    MRUBY_EXE task "test" => MRUBY_EXE task "default" => "test" SAME
  18. Dependencies default test MRUBY_EXE MRUBY_ DOWNLOAD tmp

  19. Running rake $ rake # runs default task $ rake

    test # runs test task $ rake test default # runs both $ rake -t # traces execution $ rake -t test # traces test task $ rake -T # show descriptions
  20. Invoke & Execute Invoke checks dependencies Execute runs ac)ons (ac)ons

    run only once)
  21. $ rake -t ** Invoke default (first_time) ** Invoke test

    (first_time) ** Invoke tmp/mruby-1.2.0/bin/mruby (first_time) ** Invoke tmp/mruby-1.2.0.tgz (first_time) ** Invoke tmp (first_time) ** Execute tmp mkdir -p tmp ** Execute tmp/mruby-1.2.0.tgz ** Execute tmp/mruby-1.2.0/bin/mruby mkdir -p tmp/mruby-1.2.0/bin/mruby ** Invoke tmp (not_needed) ** Execute test ** Execute default default test MRUBY_EXE MRUBY_ DOWNLOAD tmp
  22. Rake Library

  23. Shell commands cd MRUBY_DIR do end sh "curl", "-o", MRUBY_DOWNLOAD,

    … ruby "my_script.rb" mv "a.txt", "b.txt" rm_rf "tmp"
  24. Execu)on directory task "current_directory" do puts "rake's current directory is

    #{Dir.pwd}" end
  25. Execu)on directory $ rake current_directory rake's current directory is ~/tmp/rake-example

    $ cd lib $ rake current_directory (in ~/tmp/rake-example) rake's current directory is ~/tmp/rake-example
  26. FileList markdown_files = FileList["*.md"] # => ["ch1.md, "ch2.md", …] html_files

    = markdown_files.ext "html" # => ["ch1.html, "ch2.html", …]
  27. import (require) import "tasks/build.rake" import "tasks/test.rake"

  28. Best Prac)ces

  29. One Thing per Task file MRUBY_EXE => MRUBY_DOWNLOAD do #

    1. unpack MRUBY_DOWNLOAD # 2. build mruby end
  30. One Thing per Task MRUBY_DIR = "tmp/mruby-1.2.0" directory MRUBY_DIR =>

    MRUBY_DOWNLOAD do # unpack MRUBY_DOWNLOAD end file MRUBY_EXE => MRUBY_DIR do # build mruby end
  31. Make Aliases MRUBY_EXE = "tmp/mruby-1.2.0/bin/mruby" task "test" => MRUBY_EXE file

    MRUBY_EXE => MRUBY_DIR
  32. Make Aliases MRUBY_EXE = "tmp/mruby-1.2.0/bin/mruby" task "test" => "mruby" task

    "mruby" => MRUBY_EXE file MRUBY_EXE => MRUBY_DIR
  33. Document desc "Run tests" task "default" => "test" desc "Run

    tests" task "test" => "mruby" desc "Build mruby" task "mruby" => MRUBY_EXE
  34. Document $ rake -T rake default # Run tests rake

    mruby # Build mruby rake test # Run tests
  35. Organize namespace related tasks make separate .rake files import those

    files
  36. Organize $ grep rake Rakefile rakefiles = FileList['tasks/*.rake'] import(*rakefiles) $

    find . -iname "*rake*" ./Rakefile ./tasks/test.rake
  37. Advanced Rake

  38. Re-declare tasks task "test" => "mruby" task "test" do #

    run tests end tests = FileList["test/*.rb"] task "test" => tests
  39. rake/clean require 'rake/clean' # remove temporary files CLEAN << "*.o"

    << "*.class" # rake clean # remove generated files CLOBBER << "*.so" << "*.jar" # rake clobber
  40. rule Generates output Turns file X into file Y

  41. rule rule ".html" => ".md" do |t| sh "md2html", t.source,

    t.name end task "default" => html_files # md2html ch1.md ch1.html # md2html ch2.md ch1.html BACKWARD
  42. Advanced import file "some.rake" => "do_this_first" import "some.rake"

  43. Task Arguments task "name", [:first, :last] do |t, args| args.with_defaults

    first: "Jamie", last: "Smith" puts "First name is #{args[:first]}" puts "Last name is #{args[:last]}" end
  44. Task Arguments $ rake name First name is Jamie Last

    name is Smith $ rake name[Eric,Hodel] First name is Eric Last name is Hodel
  45. Task Arguments $ rake name[Eric, Hodel] rake aborted! Don't know

    how to build task 'name[Eric,' (see --tasks)
  46. h`ps:/ /github.com/ruby/rake