Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Rake

Introduction to Rake

How to write basic rake applications

Eric Hodel

May 25, 2016
Tweet

More Decks by Eric Hodel

Other Decks in Programming

Transcript

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

    Clojure: leiningen • Scala: sbt • Python: A-P-P
  2. Namespacing namespace "test" do task "unit" # test:unit task "functional"

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

    "test" => [ "test:unit", "test:functional" ]
  4. task "default" => "test" task "test" => [MRUBY_EXE, "tmp"] file

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

    MRUBY_EXE task "test" => MRUBY_EXE task "default" => "test" SAME
  6. 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
  7. $ 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
  8. Shell commands cd MRUBY_DIR do end sh "curl", "-o", MRUBY_DOWNLOAD,

    … ruby "my_script.rb" mv "a.txt", "b.txt" rm_rf "tmp"
  9. 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
  10. FileList markdown_files = FileList["*.md"] # => ["ch1.md, "ch2.md", …] html_files

    = markdown_files.ext "html" # => ["ch1.html, "ch2.html", …]
  11. One Thing per Task file MRUBY_EXE => MRUBY_DOWNLOAD do #

    1. unpack MRUBY_DOWNLOAD # 2. build mruby end
  12. 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
  13. Document desc "Run tests" task "default" => "test" desc "Run

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

    mruby # Build mruby rake test # Run tests
  15. Re-declare tasks task "test" => "mruby" task "test" do #

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

    << "*.class" # rake clean # remove generated files CLOBBER << "*.so" << "*.jar" # rake clobber
  17. 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
  18. 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
  19. Task Arguments $ rake name First name is Jamie Last

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

    how to build task 'name[Eric,' (see --tasks)