Pro Yearly is on sale from $80 to $50! »

Bang.sh - Bash Framework

Bang.sh - Bash Framework

4a849f1aa47e5c99f1aa156337f6c951?s=128

Gustavo Dutra

May 18, 2013
Tweet

Transcript

  1. Bang.sh

  2. Gustavo Dutra twitter.com/gustavotkg github.com/bellthoven

  3. None
  4. None
  5. Agenda

  6. Introduction Setup Organization Options Arsenal Exceptions Unit Tests What’s next

  7. None
  8. BASH 4

  9. Easy setup

  10. mkdir project/ cd project/ git clone \ https://github.com/bellthoven/bang.sh

  11. #!/bin/bash source “bangsh/src/bang.sh” echo “Hello World”

  12. Hitting the target Organization

  13. Repositories

  14. No conventions

  15. source ./functions.sh

  16. source ./utils.sh

  17. SRC_DIR=$(dirname $0) source “$SRC_DIR/funcs.sh”

  18. Modules

  19. modules/<name>.sh

  20. b.module.prepend_lookup_dir <dir> b.module.append_lookup_dir <dir>

  21. (b.)<module>.<func_name>

  22. _<module>_<func_name>

  23. b.module.require <name> source ./modules/<name>.sh

  24. b.module.resolve_path <name> ./modules/<name>.sh

  25. Options

  26. b.module.require opt

  27. b.opt.add_opt \ --title “Sets the title”

  28. b.opt.add_flag \ --debug “Enables debug”

  29. b.opt.add_alias --title -t

  30. b.opts.required_args --title

  31. b.opt.init “$@”

  32. b.opt.check_required_args

  33. b.opt.has_flag? --debug

  34. b.opt.get_opt --title

  35. b.opt.show_usage

  36. Example!

  37. #!/bin/bash source “bang.sh” function load_options () { b.opt.add_flag --stderr "Prints

    to stderr" b.opt.add_opt --text "Specify text to be printed" b.opt.add_alias --text -t b.opt.required_args --text }
  38. #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { load_options b.opt.init "$@" if b.opt.check_required_args; then local text="$(b.opt.get_opt --text)" if b.opt.has_flag? --stderr; then echo "$text" 1>&2 else echo “$text” fi fi }
  39. #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { # ... } # Run! run “$@”
  40. $ ./samples/parsing_argv.sh \ --text ‘Hello World’ 2> /dev/null Hello World

    $ ./samples/parsing_argv.sh --stderr\ --text ‘Hello World’ > /dev/null Hello World
  41. Arsenal

  42. b.str.length “$foo” “${#foo}”

  43. b.str.part “$foo” 2 2 “${foo:2:2}” b.str.part “$foo” -2 “${foo:(-2)}”

  44. b.path.expand symlink/ # => /absolute/path/ (follow symlinks) b.path.expand ~/file.txt #

    => /absolute/path/file.txt
  45. if [ -f “$1” ] if b.path.file? “$1”

  46. if [ “$1” -ot “$2” ]; then

  47. if b.path.older? “$1” “$2”; then

  48. b.set ‘config.name’ ‘value’ b.get ‘config.name’

  49. b.unset ‘config’

  50. bangsh.com/api

  51. Exceptions

  52. b.try.do something b.catch ExceptionName action b.finnaly something b.try.end

  53. Example!

  54. #!/bin/bash source “bang.sh” function load_options () { b.opt.add_flag --stderr "Prints

    to stderr" b.opt.add_opt --text "Specify text to be printed" b.opt.add_alias --text -t b.opt.required_args --text }
  55. #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { load_options b.opt.init "$@" if b.opt.check_required_args; then local text="$(b.opt.get_opt --text)" if b.opt.has_flag? --stderr; then echo "$text" 1>&2 else echo “$text” fi fi }
  56. #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { # ... } # Run! b.try.do run “$@” b.catch RequiredOptionNotSet b.opt.show_usage b.try.end
  57. $ ./samples/parsing_argv.sh [--text] Showing usage: --stderr Prints to stderr --text|-t

    <value> (Required) Specify text to be printed
  58. b.module.require foobar The program was aborted due to an error:

    Uncautch exception NoModuleFound: Module foobar not found
  59. Hitting the target

  60. #!/bin/bash source “bang.sh” b.module.require unittest

  61. function b.test.trim () { b.unittest.assert_equal \ “ABC” “$(b.str.trim ‘ ABC

    ‘)” }
  62. function b.test.if_creates_dir () { mkdir /tmp/foo b.path.dir? /tmp/foo b.unittest.assert_success $?

    }
  63. function b.test.if_fails () { cat not_exists.txt &> /dev/null b.unittest.assert_error $?

    }
  64. function b.test.if_raises_error () { function foo () { b.raise InvalidArgumentsError

    } b.unittest.assert_raise \ foo InvalidArgumentsError }
  65. Mocking

  66. function net_up? () { ping -c1 8.8.8.8 &> /dev/null }

    function print_net_stats () { if net_up?; then echo "UP" else echo "DOWN" fi }
  67. function b.test.mocking_up () { function net_up () { return 0;

    } b.unittest.double.do net_up? net_up b.unittest.assert_equal \ "UP" "$(print_net_stats)" b.unittest.double.undo net_up? }
  68. b.module.require unittest function b.test.mocking_up () { ... } function b.test.mocking_down

    () { function net_down { return 1; } b.unittest.double.do \ net_up? net_down b.unittest.assert_equal \ "DOWN" "$(print_net_stats)" b.unittest.double.undo net_up? }
  69. Running Tests

  70. b.unittest.add_test_case \ <func> <description> b.unittest.run_tests

  71. b.unittest.autorun_tests

  72. Running test case ‘b.test.trim’ Running test case ‘b.test.if_creates_dir’ Running test

    case ‘b.test.if_fails’ Running test case ‘b.test.if_raises_error’ Running test case ‘b.test.doubling_calls’ 4 tests executed (Assertions: 5 passed / 0 failed)
  73. FYI

  74. github.com/sstephenson/bats

  75. #!/usr/bin/env bats create_version () { mkdir -p "${RBENV_ROOT}/versions/$1" } @test

    "set by RBENV_VERSION" { create_version "1.9.3" RBENV_VERSION=1.9.3 run rbenv-version assert_success "1.9.3 (set by RBENV_VERSION environment variable)" }
  76. We’re heading to...

  77. Color module

  78. Better test outputs

  79. Tasks

  80. Executables bang new <project> bang test

  81. Template module

  82. Repositories

  83. getopt

  84. Try module opt to parse function params print -msg “$hi”

    -to STDERR
  85. Thanks! twitter.com/gustavotkg github.com/bellthoven bangsh.com

  86. • http://www.flickr.com/photos/us_mission_canada/8513394054 • http://www.flickr.com/photos/linznicholson/6095327369 • http://www.flickr.com/photos/wfryer/3242289990 • http://www.flickr.com/photos/skyblue2/1459329859 • /http://commons.wikimedia.org/wiki/File:Saint-L

    %C3%B4_Railway_station_destroyed.jpg • http://commons.wikimedia.org/wiki/ File:Cowboy_Action_Shooting.JPG Images reference (in order)